首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >STUN IP地址Javascript

STUN IP地址Javascript
EN

Stack Overflow用户
提问于 2018-01-03 00:57:55
回答 1查看 2.6K关注 0票数 0

下面是这个GitHub项目WebRTC的STUN地址请求的引文。

这些请求结果可供JavaScript使用,因此您现在可以在JavaScript中获得用户本地和公共IP地址。

我照下面的话说的做了。

下面是发出STUN请求的带注释的演示函数。您可以将其复制并粘贴到Firefox或控制台以运行测试。

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

错误是:

Uncaught :无法在handleCandidate (:38:47)的RTCPeerConnection.pc.onicecandidate (:52:13)处读取null的属性'1‘

错误发生在这里:

代码语言:javascript
复制
var ip_addr = ip_regex.exec(candidate)[1];

更多数据

对于有效的内部网络IP情况,候选值为:1178812653 1 udp 2113937151 192.168.x.x 52663 typ主机生成的0 ufrag syTM网络-成本50。

修正的更新

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

第二个ice事件失败,因为返回了一个IPv6地址,而不是客户机的公共IP地址:

2299073356 1 udp 2113932031 2001::9d38:953 C:1c28:17c0:xxx:xxx 52281类型主机生成0 ufrag NQtJ网络-费用50

问题:

这种方法是否仍然适用于检测客户端的公共IP地址?如果是的话,您知道GitHub代码有什么问题吗?

其中包括:

代码语言:javascript
复制
         //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);});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-03 09:57:02

是的,使用WebRTC发出眩晕请求以确定客户端的IP地址仍然是一种可行的方法。要确定您发布的特定代码有什么问题,请尝试将candidate转储到handleCandidate()中,以了解为什么ip_regex regexp会阻塞:

代码语言:javascript
复制
function handleCandidate(candidate){
    console.log(candidate);
    ...
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48069862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档