2016-04-29 19 views
0

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

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

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

为了澄清,这个Web应用程序并未公开供公众使用,我们将完全控制运行该应用程序的设备上安装的内容。 (这是我使用ActiveX的理由)。

任何建议和/或参考如何我可以做这样的事情,将不胜感激。在Safari上使用JavaScript获取IPv4地址

回答

0

此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地址。

+0

这看起来确实不错,但它得到的公网IP地址,而不是我要找的是设备(192.168.x.x)的本地地址。 – Nathangrad

+0

答复已更新。 – Red

+0

我已经尝试过使用RTCPeerConnection,它可以在Chrome和Firefox中正常工作。但遗憾的是,它在Safari中无法使用,所以我无法使用这种方法。 – Nathangrad

0

请尝试这种方式。

<!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>

+0

这可悲的是没有在Safari浏览器上工作,我已经尝试使用RTCPeerConnection,但它不像Safari浏览器上的Chrome和Firefox那样工作。 – Nathangrad