STUN IP地址Javascript

时间:2018-01-03 00:57:56

标签: javascript webrtc

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

  

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

我按照以下引文的建议做了。

  

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

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

错误是:

  

未捕获的TypeError:无法读取null的属性“1”       在handleCandidate(:38:47)       在RTCPeerConnection.pc.onicecandidate(:52:13)

此处发生错误:

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

更多数据

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

更正了更新

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

第二次冰事件失败,因为返回了IPv6地址而不是客户端的公共IP地址:

2299073356 1 udp 2113932031 2001 :: 9d38:953c:1c28:17c0:xxx:xxx 52281 typ host generation 0 ufrag NQtJ network-cost 50

问题:

这种方法对于检测客户端的公共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);});

1 个答案:

答案 0 :(得分:1)

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

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

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