获取本机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 ...
随机推荐
- 洛谷 P1807 最长路_NOI导刊2010提高(07) 题解
P1807 最长路_NOI导刊2010提高(07) 题目描述 设G为有n个顶点的有向无环图,G中各顶点的编号为1到n,且当为G中的一条边时有i < j.设w(i,j)为边的长度,请设计算法,计算 ...
- 洛谷P1040 加分二叉树题解
dp即可 \(f[i][j]\)表示i到j的加分 相当于区间dp了 #include<cstdio> using namespace std; int v[50]; int f[55][5 ...
- AtCoder Grand Contest 011题解
传送门 \(A\) 直接按时间排序之后贪心就可以了 const int N=1e5+5; int a[N],q[N],c,k,h,t,n,res; inline int min(R int x,R i ...
- ubuntu之路——day10.2单一数字评估指标与满足和优化的评估指标
单一数字评估指标: 我们在平时常用到的模型评估指标是精度(accuracy)和错误率(error rate),错误率是:分类错误的样本数站样本总数的比例,即E=n/m(如果在m个样本中有n个样本分类错 ...
- python常用规范
Python代码规范和命名规范 前言 Python 学习之旅,先来看看 Python 的代码规范,让自己先有个意识,而且在往后的学习中慢慢养成习惯 目录 一.简明概述 1.编码 如无特殊情况, 文件一 ...
- JAVA日志工具类
package com.ming.util; import java.io.File; import java.io.FileWriter; import java.io.IOException; i ...
- PostgreSQL 增量备份详解以及相关示例
PostgreSQL 没有类似MySQL 的二进制日志, 但是有和MySQL 类似的REDO LOG,并且有MySQL 没有的REDO 归档功能.当然REDO 的归档已经MariaDB 和Percon ...
- if ( ! defined('BASEPATH')) exit('No direct script access allowed')的作用
在看源代码时,发现codeigniter框架的控制器中,总是加上这样一段话: if(!defined('BASEPATH'))exit('No direct script access allowed ...
- 项目启动报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.wuhongyu.mapper.OrdersMapper.selectByExample
在用maven配置mybatis环境时出现此BindingExceptiony异常,发现在classes文件下没有mapper配置文件,应该是maven项目没有扫描到mapper包下的xml文件, 在 ...
- php异步处理
<?php namespace Index\Controller; use Core\Controller; class test extends Controller { public fun ...