我试图使用 WebRTC 查找客户端 IP 地址,但在 Firefox 中我收到此错误:
ICE 失败,您的 TURN 服务器似乎已损坏,请参阅 about:webrtc 了解更多详细信息
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({ iceServers: [
{ url: "turn:numb.viagenie.ca:3478", username: "[email protected]", "credential": "xxxxxx" },
{ urls: "stun:stun.l.google.com:19302" }
] }),
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 ipIterate(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[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);
};
登录关于:webrtc:
ICE 中继日志
0.009 rtp主机3350409123 udp e6e7f092-e632-4986-97b2-90b20c3b15cd.local 59923 126 | 30| 255 0.062 rtp srflx 842163049 udp IP 59923 100 | 30| 255 0.313 rtp 中继 453802058 udp IP 57652 2 | 30| 255 0.313 完成 0.315
这可能是由某些“隐私”插件引起的(例如,ublock 的“防止 WebRTC 泄露本地 IP 地址” 设置可能会导致此问题)。因此,请确保
media.peerconnection.ice.proxy_only
中 about:config
的值未设置为 true
转到 Firefox 中的
about:webrtc
,然后点击清除历史记录和清除日志。就我而言,这对我有用