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

时间:2019-06-11 12:37:04

标签: google-chrome webrtc rtcpeerconnection

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

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

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

注意:我在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>

1 个答案:

答案 0 :(得分:1)

TL; DR

看起来/本地地址将使用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问题也很少(像这个mDNS Support for WebRTC at Google Chrome M74)。

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

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