【问题标题】:Get IPv4 Address using JavaScript on Safari在 Safari 上使用 JavaScript 获取 IPv4 地址
【发布时间】:2016-08-24 09:03:04
【问题描述】:

我正在创建一个 Web 应用程序,为了运行它,您必须“注册”您的设备。注册过程涉及将您希望使用的设备的 IPv4 地址输入到更新数据库的字段中。

访问应用程序时,它会通过 window.webkitRTCPeerConnectionwindow.mozRTCPeerConnection 检查以获取 IP地址。这适用于最新版本的 Chrome 和 Firefox,但不适用于 IE。因此,如果没有从 RTCPeerConnection 检查获得 IP,我必须使用 ActiveX。

我的问题是我希望 Safari 能够运行该应用程序,但我似乎找不到任何关于如何获取运行 Safari 的设备的 IPv4 地址。

澄清一下,此 Web 应用程序不向公众开放,我们将完全控制运行该应用程序的设备上安装的内容。 (这是我使用 ActiveX 的理由)。

任何关于我如何做这件事的建议和/或参考都将不胜感激。

【问题讨论】:

    标签: javascript safari ip ipv4


    【解决方案1】:

    这个API可以获取你的IP地址。

    https://l2.io/

    更高级的解决方案: http://dev.maxmind.com/geoip/geoip2/javascript/

    这个也支持Safari,据此:http://dev.maxmind.com/geoip/geoip2/javascript/#Browser_Support

    我认为最后一个是最好的。我在我的一个项目中使用了那个。

    编辑: 在 safari 上尚不支持 WebRTC 连接。所以在safari上是无法获取本地IP地址的。

    【讨论】:

    • 这看起来确实不错,但它获取的是公共 IP 地址,而不是我要查找的设备的本地地址(192.168.x.x)。
    • 我已经尝试过使用在 Chrome 和 Firefox 中运行良好的 RTCPeerConnection。但遗憾的是它在 Safari 中不起作用,所以我不能使用这种方法。
    • 是的,我刚刚看到 safari 还没有实现 WebRTC 连接。所以我认为这将很难做到。我会看看我是否能以某种方式在 Safari 上工作。如果我愿意,我会尽快更新我的答案:)
    • 已更新。使用 safari 是不可能的。我猜公共 IP 是你唯一的解决方案 :(
    【解决方案2】:

    请尝试这种方式。

    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        </head>
        <body>
            <h4>
                Demo for:
                <a href="https://github.com/diafygi/webrtc-ips">
                    https://github.com/diafygi/webrtc-ips
                </a>
            </h4>
            <p>
                This demo secretly makes requests to STUN servers that can log your
                request. These requests do not show up in developer consoles and
                cannot be blocked by browser plugins (AdBlock, Ghostery, etc.).
            </p>
            <h4>Your local IP addresses:</h4>
            <ul></ul>
            <h4>Your public IP addresses:</h4>
            <ul></ul>
            <h4>Your IPv6 addresses:</h4>
            <ul></ul>
            <iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
            <script>
                //get the IP addresses associated with an account
                function getIPs(callback){
                    var ip_dups = {};
                    //compatibility for firefox and chrome
                    var RTCPeerConnection = window.RTCPeerConnection
                        || window.mozRTCPeerConnection
                        || window.webkitRTCPeerConnection;
                    var useWebKit = !!window.webkitRTCPeerConnection;
                    //bypass naive webrtc blocking using an iframe
                    if(!RTCPeerConnection){
                        //NOTE: you need to have an iframe in the page right above the script tag
                        //
                        //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
                        //<script>...getIPs called in here...
                        //
                        var win = iframe.contentWindow;
                        RTCPeerConnection = win.RTCPeerConnection
                            || win.mozRTCPeerConnection
                            || win.webkitRTCPeerConnection;
                        useWebKit = !!win.webkitRTCPeerConnection;
                    }
                    //minimal requirements for data connection
                    var mediaConstraints = {
                        optional: [{RtpDataChannels: true}]
                    };
                    var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
                    //construct a new RTCPeerConnection
                    var pc = new RTCPeerConnection(servers, mediaConstraints);
                    function handleCandidate(candidate){
                        //match just the IP address
                        var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
                        var ip_addr = ip_regex.exec(candidate)[1];
                        //remove duplicates
                        if(ip_dups[ip_addr] === undefined)
                            callback(ip_addr);
                        ip_dups[ip_addr] = true;
                    }
                    //listen for candidate events
                    pc.onicecandidate = function(ice){
                        //skip non-candidate events
                        if(ice.candidate)
                            handleCandidate(ice.candidate.candidate);
                    };
                    //create a bogus data channel
                    pc.createDataChannel("");
                    //create an offer sdp
                    pc.createOffer(function(result){
                        //trigger the stun server request
                        pc.setLocalDescription(result, function(){}, function(){});
                    }, function(){});
                    //wait for a while to let everything done
                    setTimeout(function(){
                        //read candidate info from local description
                        var lines = pc.localDescription.sdp.split('\n');
                        lines.forEach(function(line){
                            if(line.indexOf('a=candidate:') === 0)
                                handleCandidate(line);
                        });
                    }, 1000);
                }
                //insert IP addresses into the page
                getIPs(function(ip){
                    var li = document.createElement("li");
                    li.textContent = ip;
                    //local IPs
                    if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/))
                        document.getElementsByTagName("ul")[0].appendChild(li);
                    //IPv6 addresses
                    else if (ip.match(/^[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}$/))
                        document.getElementsByTagName("ul")[2].appendChild(li);
                    //assume the rest are public IPs
                    else
                        document.getElementsByTagName("ul")[1].appendChild(li);
                });
            </script>
        </body>
    </html>

    【讨论】:

    • 遗憾的是,这在 Safari 上不起作用。我已经尝试使用 RTCPeerConnection,但它不像在 Safari 上的 Chrome 和 Firefox 上那样工作。
    • 获取不到公网ip的原因是什么?我正在测试演示,我得到了本地和 IPv6,但没有得到 public ip。谢谢
    猜你喜欢
    • 2017-04-16
    • 2017-01-11
    • 1970-01-01
    • 2010-11-06
    • 2022-06-28
    • 2020-12-21
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多