簡體   English   中英

在 Safari 上使用 JavaScript 獲取 IPv4 地址

[英]Get IPv4 Address using JavaScript on Safari

我正在創建一個 web 應用程序,為了讓它運行,您必須“注冊”您的設備。 注冊過程涉及將您希望使用的設備的 IPv4 地址輸入到更新數據庫的字段中。

訪問應用程序時,它會通過window.webkitRTCPeerConnectionwindow.mozRTCPeerConnection檢查以獲取 IP 地址。 這適用於最新版本的 Chrome 和 Firefox,但不適用於 IE。 因此,如果 IP 不是從 RTCPeerConnection 檢查中獲得的,我就必須使用 ActiveX。

我的問題是我希望 Safari 能夠運行該應用程序,但我似乎無法找到有關如何為運行 Safari 的設備獲取 IPv4 地址的任何信息。

需要澄清的是,這個 web 應用程序不對一般公眾開放使用,我們將完全控制運行該應用程序的設備上安裝的內容。 (這是我使用 ActiveX 的理由)。

任何關於我如何做這樣的事情的建議和/或參考將不勝感激。

該API可以獲取您的IP地址。

https://l2.io/

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

根據此信息,該網站還支持Safari: http : //dev.maxmind.com/geoip/geoip2/javascript/#Browser_Support

我認為最后一個是最好的。 我在我的一個項目中使用了那個。

編輯:在野生動物園上尚不支持WebRTC連接。 因此,不可能在野生動物園上獲得本地IP地址。

請嘗試這種方式。

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

網站https://api.ipify.org知道您的 ip 地址。 你唯一要做的就是刮掉它。 您可以使用 JavaScript 做到這一點。

var xhttp = new XMLHttpRequest();
xhttp.open("GET", "https://api.ipify.org", false);
xhttp.send();
var ip = xhttp.responseText;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM