无法从 RTCPeerConnection 获取 IP V4 地址 - chrome



我需要从 Web 应用程序中获取客户端本地 IP 地址。

为此,我正在使用标准的RTCPeerConnection实现来获取。但是返回的 ice 候选者不携带 IP V4 地址,而是一个看起来像 guid 的地址:asdf-xxxx-saass-xxxx.local

但令人惊讶的是,这个chrome扩展程序能够在同一台机器和浏览器上获取相同的内容。

注意:我在Web应用程序中使用的代码与扩展名相同

这是相同的 html 代码:

<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript">
function logit(msg) {
var dt = new Date(); var time = dt.getHours() + ":" + dt.getMinutes() + ":"
+ dt.getSeconds();
console.log(time + " " + msg);
};
function getChromeVersion() {
try {
var raw = navigator.userAgent.match(/Chrom(e|ium)/([0-9]+)./);
return raw ? parseInt(raw[2], 10) : false;
} catch (e) {
return null;
}
}
function getChromeManifest() {
return chrome.runtime && typeof chrome.runtime === "function" ? chrome.runtime.getManifest() : {}
}
function getUserIP(callback) {
logit(" getting user local ip ")
getLocalIPs(function (ips) {
logit(" got user local ip : " + ips)
if (ips && ips.length) return callback(ips[0]);
logit(" getting user local ip with stun ")
getLocalIPs(function (ips) {
logit(" got user local ip with stun : " + ips)
if (ips && ips.length) return callback(ips[0])
logit(" cannot get user local ip, returning null ")
callback(null)
}, true, 2000)
})
}
function getLocalIPs(callback, withStun, timeout) {
var ips = [];
var RTCPeerConnection = window.RTCPeerConnection ||
window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
var pc = new RTCPeerConnection({
// Don't specify any stun/turn servers, otherwise you will
// also find your public IP addresses.
// iceServers: [],
iceServers: withStun ? [{ urls: "stun:stun.services.mozilla.com" }] : []
});
var closeAndCallback = function () {
clearTimeout(waitTimeout)
try {
if (pc && pc.close) {
pc.close();
}
} catch (e) { console.log("exception while closing pc, err: %s", err) }
callback(ips);
}
var waitTimeout = timeout ? setTimeout(closeAndCallback, timeout) : null;
// Add a media line, this is needed to activate candidate gathering.
pc.createDataChannel('');
// onicecandidate is triggered whenever a candidate has been found.
pc.onicecandidate = function (e) {
console.log(e)
if (!e.candidate) { // Candidate gathering completed.
pc.close();
closeAndCallback();
return;
}
var ip = /^candidate:.+ (S+) d+ typ/.exec(e.candidate.candidate)[1];
if (ips.indexOf(ip) == -1) // avoid duplicate entries (tcp/udp)
ips.push(ip);
};
pc.createOffer(function (sdp) {
pc.setLocalDescription(sdp);
}, function onerror() { });
};
function callThirdParty(server, name) {
var api = server;
logit("Connecting " + server + " ...");
$.ajax({
type: "GET",
url: api,
success: function (data) {
if (data && data['ip']) {
logit("Public IP: " + data['ip']);
}
}, error:
function (request, status, error) {
logit('Response: ' + request.responseText);
logit(' Error: ' + error);
logit(' Status: ' + status);
},
complete: function (data) {
logit(' API Finished: ' + name + " Server!");
}
});
}
document.addEventListener('DOMContentLoaded', function () {
getUserIP(function (ip) { //
ipaddress = ip;
$('#ip2').html(ipaddress);
var manifest = getChromeManifest();
logit(manifest.name);
logit("Version: " + manifest.version);
logit("Chrome Version: " + getChromeVersion());
callThirdParty("https://api.ipify.org?format=json", "ipify.org");
}, 100);
}, false);
</script>
</head>
<p>Public IPs</p>
<div id="ip"></div>
<p>Local IP</p>
<div id="ip2"></div>
<p>Logs</p>
<div id="log"></div>
<div id="log1"></div>
<div id="log2"></div>
</html>

TL;博士

看起来本地地址是/将使用 mDNS 匿名化的,并且该标志的默认设置将逐渐设置为对所有 Chrome 用户Enabled

对于本地发展,请查看此处(设置为Disable): chrome://flags/#enable-webrtc-hide-local-ips-with-mdns

除非有人发现一些聪明的黑客,否则您可能无法为Web应用程序的用户还原更改。


该 guid 实际上是 mDNS 地址。快速搜索Chromium https://bugs.chromium.org/p/chromium/issues/list?can=2&q=component%3ABlink%3EWebRTC+中最新的WebRTC错误 揭示了一些有趣的条目,并且很少有关于匿名不起作用的StackOverflow问题(比如这个:Google Chrome M74对WebRTC的mDNS支持)。

现在,我在少数装有Windows 75的计算机上看到了Chrome 10的效果-一些以前能够完美检测本地IP的站点(http://net.ipcalf.com,https://ipleak.net,https://browserleaks.com/webrtc)现在不显示它或显示mDNS url。

作为旁注:启用 mDNS 标志后,您链接的扩展无法检测到我的确切本地 IP。相反,它显示了来自/24 地址组的几个候选人。即便如此,扩展程序也可以以某种方式获得特权,因此它不会受到mDNS匿名化的影响。

>EDIT(2020 年 3 月):看起来 Firefox 也可以匿名化本地 IP。

截至 2020 年 3 月,about:config页面中有两个设置:

  • media.peerconnection.ice.obfuscate_host_addresses- 设置为 true 时,它将本地 IP 更改为 {uuid}.local
  • media.peerconnection.ice.obfuscate_host_addresses.whitelist- 带有 URL 的字符串,即使启用了混淆,也能够检索真实 IP

我已经检查了Firefox 73和开发人员版74(没有任何可能更改设置的扩展名),首先将obfuscate_host_addresses设置为false,而开发版则启用了它。

>EDIT(2020 年 10 月):自 Chrome 86 起,mDNS 设置已启用,无法再通过 chrome://flags 禁用(没有此类选项可用)。

最新更新