Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过WebRTC捕获IP的JavaScript(异步/等待问题)_Javascript_Security_Webrtc - Fatal编程技术网

通过WebRTC捕获IP的JavaScript(异步/等待问题)

通过WebRTC捕获IP的JavaScript(异步/等待问题),javascript,security,webrtc,Javascript,Security,Webrtc,我提出了通过WebRTC捕获IP的代码。 但我遇到了这个问题,我不能将IPs作为变量返回,我只能在控制台中打印IPs,在警报中显示它们,或者在HTML页面上显示它们。但我需要从函数返回IPs作为变量 我认为问题在于异步/等待。我不是JavaScript开发人员,要花上一整天的时间才能找到放置wait的地方 请帮我解决这个问题。 多谢各位 function findIPsWithWebRTC() { var myPeerConnection = window.RTCPeerConnecti

我提出了通过WebRTC捕获IP的代码。 但我遇到了这个问题,我不能将IPs作为变量返回,我只能在控制台中打印IPs,在警报中显示它们,或者在HTML页面上显示它们。但我需要从函数返回IPs作为变量

我认为问题在于异步/等待。我不是JavaScript开发人员,要花上一整天的时间才能找到放置
wait
的地方

请帮我解决这个问题。 多谢各位

function findIPsWithWebRTC() {
    var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
    var pc = new myPeerConnection({iceServers: [{urls: "stun:stun.l.google.com:19302"}]}),
    noop = function() {},
    IPs = {},
    ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
    key;

    function ipIterate(ip) {
        if (!IPs[ip]) console.log('got ip: ', ip);
        IPs[ip] = true;
    }

    pc.createDataChannel("");

    pc.createOffer(function(sdp) {
        sdp.sdp.split('\n').forEach(function(line) {
            if (line.indexOf('candidate') < 0) return;
            line.match(ipRegex).forEach(ipIterate);
        });
        pc.setLocalDescription(sdp, noop, noop);
    }, noop);

    pc.onicecandidate = function(ice) {
        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
        ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
    };

    console.log("ips: " + JSON.stringify(IPs));
    return {
        "source": "WebRTC",
        "name": "IPs",
        "value": JSON.stringify(IPs)
    }
}

alert(findIPsWithWebRTC().value);
函数findipswithwebtc(){
var myPeerConnection=window.rtpeerconnection | | window.mozrtpeerconnection | | window.webkirtpeerconnection;
var pc=newmypeerconnection({iceServers:[{url:stun:stun.l.google.com:19302“}]}),
noop=函数(){},
IPs={},
ipRegex=/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
钥匙
函数ipIterate(ip){
if(!IPs[ip])console.log('got ip:',ip);
IPs[ip]=真;
}
pc.createDataChannel(“”);
pc.createOffer(功能(sdp){
sdp.sdp.split('\n').forEach(函数(行){
if(line.indexOf('candidate')<0)返回;
line.match(ipRegex).forEach(ipIterate);
});
pc.setLocalDescription(sdp、noop、noop);
},noop);
pc.onicecandidate=函数(ice){
如果(!ice | |!ice.candidate | |!ice.candidate.candidate | |!ice.candidate.candidate.match(ipRegex))返回;
ice.candidate.candidate.match(ipRegex.forEach)(ipIterate);
};
log(“ips:+JSON.stringify(ips));
返回{
“来源”:“WebRTC”,
“名称”:“IPs”,
“值”:JSON.stringify(IPs)
}
}
警报(findipswithwebtc().value);
我可以在控制台中输出IPs,但不能将IPs作为函数的值返回


findipswithwebtc
无法返回IP,因为IP是异步提供给代码的。同步函数不能返回异步进程的结果

相反,您的
findipswithwebtc
应该返回一个承诺,该承诺将通过您当前尝试返回的对象来实现。看起来您在
onicecandidate
(或者可能是对
createOffer
?)的回调中获得了IP,因此(请参见
***
):

…或在非
异步
函数中,使用
然后
捕获

findIPsWithWebRTC()
.then(ipInfo => {
    // ...use `ipInfo`...
})
.catch(error => {
    // Handle/report error
});

在这两种情况下,如果使用结果的函数不是顶级使用者,则通常将错误处理留给它。在
async
示例中,您可以通过不使用
try
/
catch
(拒绝会自动传播到调用方)来实现。在非
异步
示例中,您可以通过返回调用
然后
的结果,并且没有
捕获

findipswithwebtc
无法返回IP,因为IP是异步提供给代码的。同步函数不能返回异步进程的结果

相反,您的
findipswithwebtc
应该返回一个承诺,该承诺将通过您当前尝试返回的对象来实现。看起来您在
onicecandidate
(或者可能是对
createOffer
?)的回调中获得了IP,因此(请参见
***
):

…或在非
异步
函数中,使用
然后
捕获

findIPsWithWebRTC()
.then(ipInfo => {
    // ...use `ipInfo`...
})
.catch(error => {
    // Handle/report error
});
在这两种情况下,如果使用结果的函数不是顶级使用者,则通常将错误处理留给它。在
async
示例中,您可以通过不使用
try
/
catch
(拒绝会自动传播到调用方)来实现。在非
异步
示例中,您可以通过返回调用
的结果,然后
并且没有
catch

来执行此操作
.createOffer()
调用是异步的。它立即返回;您传递的回调函数稍后会在建立连接时调用。
.createOffer()
调用是异步的。它立即返回;稍后在建立连接时调用您传递的回调函数。
findIPsWithWebRTC()
.then(ipInfo => {
    // ...use `ipInfo`...
})
.catch(error => {
    // Handle/report error
});