js html 发布内网,如何在不联网情况下在前端使用js拿到用户内网IP。。。

问题描述

如何在不联网情况下,使用js拿到客户端内网IP并且支持ie浏览器!

问题出现的环境背景及自己尝试过哪些方法

从网上找了很多方法已经试过了。

1.在Chrome已经使用过webrtc-ips方法,但是不知道是什么原因,还是拿不到,并且该方法不支持ie。

2.解决webrtc-ips拿不到ip的问题时,也使用了webrtc-ips插件,作者本人的demo就不能拿到。。。

3.使用过搜狐,腾讯的ip支持,但是必须联网才能获取到。

相关代码

// 网上唯一的webrtc-ips方法,这个也拿不到ip?

Demo for:

https://github.com/diafygi/webrtc-ips

This demo secretly makes requests to STUN servers that can log your

request. These requests do not show up in developer consoles and

cannot be blocked by browser plugins (AdBlock, Ghostery, etc.).

Your local IP addresses:
Your public IP addresses:

//get the IP addresses associated with an account

function getIPs(callback){

var ip_dups = {};

//compatibility for firefox and chrome

var RTCPeerConnection = window.RTCPeerConnection

|| window.mozRTCPeerConnection

|| window.webkitRTCPeerConnection;

var useWebKit = !!window.webkitRTCPeerConnection;

//bypass naive webrtc blocking

if(!RTCPeerConnection){

//create an iframe node

var iframe = document.createElement('iframe');

iframe.style.display = 'none';

//invalidate content script

iframe.sandbox = 'allow-same-origin';

//insert a listener to cutoff any attempts to

//disable webrtc when inserting to the DOM

iframe.addEventListener("DOMNodeInserted", function(e){

e.stopPropagation();

}, false);

iframe.addEventListener("DOMNodeInsertedIntoDocument", function(e){

e.stopPropagation();

}, false);

//insert into the DOM and get that iframe's webrtc

document.body.appendChild(iframe);

var win = iframe.contentWindow;

RTCPeerConnection = win.RTCPeerConnection

|| win.mozRTCPeerConnection

|| win.webkitRTCPeerConnection;

useWebKit = !!win.webkitRTCPeerConnection;

}

//minimal requirements for data connection

var mediaConstraints = {

optional: [{RtpDataChannels: true}]

};

//firefox already has a default stun server in about:config

// media.peerconnection.default_iceservers =

// [{"url": "stun:stun.services.mozilla.com"}]

var servers = undefined;

//add same stun server for chrome

if(useWebKit)

servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};

//construct a new RTCPeerConnection

var pc = new RTCPeerConnection(servers, mediaConstraints);

function handleCandidate(candidate){

//match just the IP address

var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/

var ip_addr = ip_regex.exec(candidate)[1];

//remove duplicates

if(ip_dups[ip_addr] === undefined)

callback(ip_addr);

ip_dups[ip_addr] = true;

}

//listen for candidate events

pc.onicecandidate = function(ice){

//skip non-candidate events

if(ice.candidate)

handleCandidate(ice.candidate.candidate);

};

//create a bogus data channel

pc.createDataChannel("");

//create an offer sdp

pc.createOffer(function(result){

//trigger the stun server request

pc.setLocalDescription(result, function(){}, function(){});

}, function(){});

//wait for a while to let everything done

setTimeout(function(){

//read candidate info from local description

var lines = pc.localDescription.sdp.split('\n');

lines.forEach(function(line){

if(line.indexOf('a=candidate:') === 0)

handleCandidate(line);

});

}, 1000);

}

//insert IP addresses into the page

getIPs(function(ip){

var li = document.createElement("li");

li.textContent = ip;

//local IPs

if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/))

document.getElementsByTagName("ul")[0].appendChild(li);

//assume the rest are public IPs

else

document.getElementsByTagName("ul")[1].appendChild(li);

});

你期待的结果是什么?实际看到的错误信息又是什么?

上述代码会报错:pc.localDescription.sdp undefined,打印出来pc.localDescription为null。

麻烦各位帮忙看一下。

最后: 如何在不联网情况下在前端使用js拿到用户内网IP,并且支持ie!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值