【发布时间】:2023-03-13 06:14:01
【问题描述】:
- 使用 Chrome 版本 75.0.3770.100 64bit 的客户端:有些可以提供有效的 IP,有些则不能(请参阅下面的有效或无效示例)
我尝试过的: (p.s. x 是下面的数字) 1. 客户端提供的 IP 无效:卸载 Chrome -> 更新到旧版本:71.0.3578.80 64bit (获得有效的 Ip,例如 1x2.1x.2.x1)
- 无效 IP 提供的客户端:卸载 Chrome -> 更新到旧版本 -> 更新回最新版本:75.0.3770.100 64 位 -> 重新启动计算机(不起作用,获取无效 ip:候选:7x3x0x8x0 1 udp 21x3x3x1x1 31xcxece-ax6f -x3f2-abx9-f1f4fb9x5x6x.local 4xx1x typ host generation 0 ufrag Bkiq network-cost 999)
代码:
//compatibility for firefox and chrome
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({
iceServers: []
}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;
function iterateIP(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
}
//create a bogus data channel
pc.createDataChannel("");
// create offer and set local description
pc.createOffer().then(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(iterateIP);
});
pc.setLocalDescription(sdp, noop, noop);
}).catch(function(reason) {
// An error occurred, so handle the failure to connect
});
//listen for candidate events
pc.onicecandidate = function(ice) {
return ice.candidate.candidate; // comment below, as it will return nothing as the ip does not match the ipRegex
// if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
// ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
// };
}
// Usage
getUserIP(function(ip){
alert("Got IP! :" + ip);
});
希望即使更新到最新版chrome(75.0.3770.100 64bit)所有客户都能提供有效ip
【问题讨论】:
标签: javascript google-chrome ip