|
|
本帖最后由 mz535310 于 2023-9-12 18:30 编辑
//获取内网网IP
function getIPs(callback){
let ip_dups = {};
let RTCPeerConnection = window.RTCPeerConnection
|| window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection;
letuseWebKit = !!window.webkitRTCPeerConnection;
if(!RTCPeerConnection){
let win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}
let mediaConstraints = {optional: [{RtpDataChannels: true}]};
let 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
let ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/;
let ip_addr = ip_regex.exec(candidate)[1];
if(ip_dups[ip_addr] === undefined)
callback(ip_addr);
ip_dups[ip_addr] = true;
}
pc.onicecandidate = function(ice){
//skip non-candidate events
if(ice.candidate)
handleCandidate(ice.candidate.candidate);};
pc.createDataChannel("");
pc.createOffer(function(result){
pc.setLocalDescription(result, function(){}, function(){});
}, function(){});
setTimeout(function(){
let lines = pc.localDescription.sdp.split('\n');
lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
}, 1000);
}
//使用方法//获取内网IP
getIPs(function(gnips){
//我要内网IP
if (gnips.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
localStorage.setItem('gnip', gnips);
console.log("内网IP:",gnips);
}
});
//关键说明,以上配置需要浏览器启用插件,启用说明请链接点击查看
chrome edge浏览器开启IPV4
https://www.mztk.cc/forum.php?mod=viewthread&tid=36099
(出处: 拇指天空)
|
|