获取本机IP地址[JavaScript / Node.js]
--web客户端JavaScript
<body onload="checkCookie()"></body>
function getYourIP(){
const RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) (function(){
const rtc = new RTCPeerConnection({iceServers: []});
if (1 || window.mozRTCPeerConnection){
rtc.createDataChannel('', {reliable: false});
}
rtc.onicecandidate = function(evt){
if (evt.candidate) grepSDP(`a=${evt.candidate.candidate}`);
};
rtc.createOffer(function(offerDesc){
grepSDP(offerDesc.sdp);
rtc.setLocalDescription(offerDesc);
}, function(e) {
console.warn('offer failed', e);
});
const addrs = Object.create(null);
addrs['0.0.0.0'] = false;
function updateDisplay(newAddr){
if (newAddr in addrs) return;
addrs[newAddr] = true;
const displayAddrs = Object.keys(addrs).filter(function(k) {return addrs[k];});
for (let i = 0; i < displayAddrs.length; i++){
if (displayAddrs[i].length > 16){
displayAddrs.splice(i, 1);
i--;
}
}
console.info('您的IP: ', displayAddrs[0]);
}
function grepSDP(sdp){
sdp.split('\r\n').forEach(function(line, index, arr){
if (~line.indexOf('a=candidate')){
const parts = line.split(' '),
addr = parts[4],
type = parts[7];
if (type === 'host') updateDisplay(addr);
} else if (~line.indexOf('c=')){
const parts = line.split(' '),
addr = parts[2];
updateDisplay(addr);
}
});
}
})();
else {
console.warn('请使用主流浏览器:chrome,firefox,opera,safari');
}
}
--web服务端Node.js
const os = require('os');
module.exports ={
getLocalIP : function(){
const eth0 = os.networkInterfaces().eth0;
let localhost = null;
for (let i = 0; i < eth0.length; i++){
if (eth0[i].family == 'IPv4'){
localhost = eth0[i].address;
}
}
return localhost;
}
};
备注
引用作者的文章
原址
获取本机IP地址[JavaScript / Node.js]的更多相关文章
- 获取本机IP地址
这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...
- 关于是用dotnet获取本机IP地址+计算机名的方法
印象中在maxscript帮助文档里找到过方法,但是当时没记下来.只能通过dotnet实现了. 如果电脑有无线网卡和本地连接,可能会出现乱码,也问了写dotnet的朋友,提供了一些思路,不过最终还是使 ...
- Windows下获取本机IP地址方法介绍
Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...
- java获取本机IP地址
转载自:http://blog.csdn.net/thunder09/article/details/5360251 在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一 ...
- C# 获取本机IP地址以及转换字符串
/// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址 ...
- QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址
获取主机名称 /* * 名称:get_localmachine_name * 功能:获取本机机器名称 * 参数:no * 返回:QString */ QString CafesClient::get_ ...
- 详谈再论JAVA获取本机IP地址
首先,你如果搜索“JAVA获取本机IP地址”,基本上搜到的资料全是无用的.比如这篇:http://www.cnblogs.com/zrui-xyu/p/5039551.html实际上的代码在复杂环境下 ...
- Linux下获取本机IP地址的代码
Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...
- shell中获取本机ip地址
shell中获取本机ip地址 方法一: /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr ...
随机推荐
- 鼠标经过提高层级案例(margin,相对定位,z-index)
<body> <ul> <li class="box1">1</li> <li class="box2"& ...
- Ancient Go(简单DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 AC代码: #include<iostream> #include<cstdi ...
- javascript生成表格增删改查 JavaScript动态改变表格单元格内容 动态生成表格 JS获取表格任意单元格 javascript如何动态删除表格某一行
jsp页面表格布局Html代码 <body onload="show()"> <center> <input type="text" ...
- shell编程题(四)
编译当前目录下的所有.c文件 #!/bin/bash ] ;] 输入参数个数 echo "Please follow up file.c!" echo "eg: ./ma ...
- Linux shell head 命令
输出前25行 head -n25 input.txt head -n input.txt head - input.txt 输出前5个字符 head -c5 test.cpp head -c test ...
- vue + .net core 项目,源码在GitHub 希望对大家有所帮助
一. github UI库 : iview 前端部分 vue .net core + DI + EF(dbfirst) 后端API 部分 .Net Core 二. 往期相关博客 SqlServer 获 ...
- 2018-2019 20165226 Exp9 Web安全基础
2018-2019 20165226 Exp9 Web安全基础 目录 一.实验内容说明及基础问题回答 二.实验过程 Webgoat准备 XSS攻击 ① Phishing with XSS 跨站脚本钓鱼 ...
- #C++初学记录(贪心算法#二分查找)
D - Aggressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 < ...
- WEB传参调用EXE
WEB传参调用EXE 让浏览器运行本地的EXE程序.例如:点击浏览器的一个下载链接,就会打开本地的迅雷. 1)注册表注册 Windows Registry Editor Version 5.00 [H ...
- NPAPI绘图和事件处理
https://developer.mozilla.org/en-US/docs/Plugins/Guide/Drawing_and_Event_Handling 本章介绍如何确定插件实例是窗口化还是 ...