gpt4 book ai didi

javascript - STUN IP 地址 JavaScript

转载 作者:行者123 更新时间:2023-12-03 02:46:34 27 4
gpt4 key购买 nike

以下是来自此 GitHub 项目的引用 STUN IP Address requests for WebRTC .

These request results are available to JavaScript, so you can now obtain a users local and public IP addresses in JavaScript.

我按照以下引用中的建议进行了操作。

Here is the annotated demo function that makes the STUN request. You can copy and paste this into the Firefox or Chrome developer console to run the test.

结果是脚本错误,输出结果为undefined和192.168.x.x。它确实正确检测到我的一台笔记本电脑的内部家庭 IP 地址。

错误是:

Uncaught TypeError: Cannot read property '1' of null at handleCandidate (:38:47) at RTCPeerConnection.pc.onicecandidate (:52:13)

错误发生在这里:

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

更多数据

对于有效的内部网络 IP 案例,候选值为:candidate:1178812653 1 udp 2113937151 192.168.x.x 52663 Typ Host Generation 0 ufrag syTM network-cost 50。

更正更新

我在handleCandidate之后有console.log,这就是为什么我没有看到第二个结果。我已使用 console.log 条目更新了代码。

第二个ice事件失败,因为返回的是IPv6地址而不是客户端的公共(public)IP地址:

2299073356 1 udp 2113932031 2001::9d38:953c:1c28:17c0:xxx:xxx 52281典型主机代0 ufrag NQtJ网络成本50

<小时/>

问题:

这种方法对于检测客户端的公共(public) IP 地址仍然可行吗?如果是,你知道 GitHub 代码出了什么问题吗?

此处包含代码:

         //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 using an iframe
if(!RTCPeerConnection){
//NOTE: you need to have an iframe in the page right above the script tag
//
//<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
//<script>...getIPs called in here...
//
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}

//minimal requirements for data connection
var mediaConstraints = {
optional: [{RtpDataChannels: true}]
};

var 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}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
console.log("candidate in handler" + candidate);
var ip_addr = ip_regex.exec(candidate)[1];

//remove duplicates
if(ip_dups[ip_addr] === undefined)
callback(ip_addr);

ip_dups[ip_addr] = true;
}
var count = 1;
//listen for candidate events
pc.onicecandidate = function(ice){
console.log("ice event " + count + ": " + ice)
//skip non-candidate events
var propertyCount = 1
if(ice.candidate){
console.log("ice candidate " + count + ": " + ice.candidate.candidate);
handleCandidate(ice.candidate.candidate);
}
count++;
};

//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);
}

//Test: Print the IP addresses into the console
getIPs(function(ip){console.log(ip);});

最佳答案

是的,使用 WebRTC 发出 STUN 请求来确定客户端的 IP 地址仍然是一种可行的方法。要确定您发布的特定代码有什么问题,请尝试在 handleCandidate() 中转储 candidate 以查看 ip_regex 正则表达式令人窒息的原因:

function handleCandidate(candidate){
console.log(candidate);
...
}

编辑:看起来问题出在所使用的 STUN 服务器上。我已将 stun.services.mozilla.com 替换为 stun.l.google.com:19302,并且我在控制台中获取了我的公共(public) IP 地址以及私有(private)的。

关于javascript - STUN IP 地址 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48069862/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com