无法从RTCPeerConnection获取IP V4地址-chrome [英] Unable to fetch IP V4 address from RTCPeerConnection - chrome

查看:235
本文介绍了无法从RTCPeerConnection获取IP V4地址-chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to fetch the client local IP address from a web application.

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

For which I am using a standard RTCPeerConnection implementation to fetch. But the ice candidate that is returned does not carry the IP V4 address, but an address that look like a guid: asdf-xxxx-saass-xxxx.local

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

But surprisingly this chrome extension is able to fetch the same on same machine and browser.

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

Note: code that I used in web application is same as of the extension

这是相同的html代码:

This is the html code for same:

<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; DR

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

It looks like local addresses are/will be anonymized using mDNS and default setting for the flag would be gradually set to Enabled for all Chrome users.

对于本地开发,请看这里(设置为Disable):chrome://flags/#enable-webrtc-hide-local-ips-with-mdns

For local development take a look here (set to Disable): chrome://flags/#enable-webrtc-hide-local-ips-with-mdns

除非有人发现了一些聪明的技巧,否则您可能将无法为您的Web应用程序的用户还原更改.

Unless someone finds out some clever hack for it, you probably won't be able to revert the change for users of your webapp.

该Guid实际上是mDNS地址.快速搜索Chromium中的最新WebRTC错误 https://bugs.chromium.org/p/chromium/issues/list?can=2&q=component%3ABlink%3EWebRTC+ 揭示了一些有趣的条目,并且关于匿名化正常工作的StackOverflow问题也很少(像这样的问题:

That guid is actually mDNS address. Quick search in newest WebRTC bugs in Chromium https://bugs.chromium.org/p/chromium/issues/list?can=2&q=component%3ABlink%3EWebRTC+ revealed few interesting entries, and there are few StackOverflow questions regarding anonymization not working (like this one: mDNS Support for WebRTC at Google Chrome M74).

现在,我在几台装有Windows 10的计算机上看到了Chrome 75的效果-某些以前能够完美检测本地IP的站点( https://ipleak.net ,<现在,a href ="https://browserleaks.com/webrtc" rel ="nofollow noreferrer"> https://browserleaks.com/webrtc )不再显示它或显示mDNS网址.

Right now I see the effect in Chrome 75 on few computers with Windows 10 - some sites which previously were able to detect local IP flawlessly (http://net.ipcalf.com, https://ipleak.net, https://browserleaks.com/webrtc) now don't display it or show mDNS url instead.

作为旁注:启用mDNS标志后,您链接的扩展名无法检测到我的确切本地IP.相反,它没有显示来自/24地址组的候选人.即使这样,该扩展名也可以以某种方式获得特权,因此它不会受到mDNS匿名性的太大影响.

As a sidenote: after enabling the mDNS flag, the extension you've linked wasn't able to detect my exact local IP. Instead, it showed few candidates from /24 address group. Even then, the extension could be privileged in some way, so it wouldn't be affected so much by mDNS anonymization.

编辑(2020年3月)::Firefox似乎也可以匿名化本地IP.

EDIT (Mar 2020): it looks like Firefox could be anonymizing local IPs as well.

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

As of March 2020, there are two settings in about:config page:

  • media.peerconnection.ice.obfuscate_host_addresses-设置为true时,它将本地IP更改为{uuid} .local
  • media.peerconnection.ice.obfuscate_host_addresses.whitelist-带有URL的字符串,即使启用了混淆功能,也可以检索真实IP
  • media.peerconnection.ice.obfuscate_host_addresses - when set to true, it changes local IP to {uuid}.local
  • media.peerconnection.ice.obfuscate_host_addresses.whitelist - string with URLs, which are able to retrieve real IP, even with obfuscation enabled

我已经检查了Firefox 73和Developer Edition 74(没有任何可以更改设置的扩展名),首先将obfuscate_host_addresses设置为false,而开发版已将其启用.

I've checked Firefox 73 and Developer Edition 74 (without any extension that could have changed the setting), first had obfuscate_host_addresses set to false, while dev edition had it enabled.

编辑(2020年10月):由于Chrome 86启用了mDNS设置,因此无法再通过chrome://flags禁用(没有此类选项).

EDIT (Oct 2020): since Chrome 86 the mDNS setting is enabled and cannot be disabled via chrome://flags anymore (there is no such option available).

这篇关于无法从RTCPeerConnection获取IP V4地址-chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆