我正在创建一个Web应用程序,为了使其运行,您必须让您的设备“注册”。注册过程涉及将您希望使用的设备的IPv4地址输入到更新数据库的字段中。
当访问应用程序时,它会通过window.webkitRTCPeerConnection
或window.mozRTCPeerConnection
检查来获取IP地址。这适用于最新版本的Chrome和Firefox,但不适用于IE。所以,如果IP不是从RTCPeerConnection检查中获得的,我必须使用ActiveX。
我的问题是,我希望Safari能够运行应用程序,但我似乎无法找到任何有关如何获得运行Safari的设备的IPv4地址。
为了澄清,这个Web应用程序并未公开供公众使用,我们将完全控制运行该应用程序的设备上安装的内容。 (这是我使用ActiveX的理由)。
任何建议和/或参考如何我可以做这样的事情,将不胜感激。在Safari上使用JavaScript获取IPv4地址
0
A
回答
0
此API可以获得您的IP地址。
更先进的解决方案: http://dev.maxmind.com/geoip/geoip2/javascript/
这其中也支持Safari浏览器,根据此:http://dev.maxmind.com/geoip/geoip2/javascript/#Browser_Support
最后一个是我认为最好的一个。我在我的一个项目中使用了那个。
编辑: 在Safari浏览器上还没有支持WebRTC连接。所以在safari上无法获得本地IP地址。
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
相关问题
- 1. 在bat-file中只获取没有“IPv4地址......”的IPv4地址
- 2. Java获取IPv4地址
- 3. 在Android上从IPv4地址合成IPv4映射的IPv6地址?
- 4. libvirt:从客户获取ipv4地址
- 5. Yesod获取客户端Ipv4/Ipv6地址
- 6. C#获取活动NIC IPv4地址
- 7. IP邻居获取IPV4地址
- 8. 试图让我的IPv4地址获取的VirtualBox的IPv4
- 9. 使用WMI获取portalinformation和ipv4地址。与PS和Python
- 10. 用于在Windows中单独获取IPv4地址的CMD命令
- 11. printf ipv6地址使用struct in6_addr和ipv4地址使用u_int32_t
- 12. 多个IPV4地址
- 13. 编码IPv4地址
- 14. IPv4地址替换
- 15. 如何获取本地机器的IPv4和IPv6地址?
- 16. 如何使用VB.net显示IPv4地址
- 17. 使用IPv4地址对NSTableView列排序
- 18. safari扩展从地址栏获取url
- 19. 如何从IPv4映射的IPv6地址解析IPv4地址?
- 20. Patricia/radix树和ipv4地址
- 21. 力InetAddress.getHostAddress()返回IPv4地址
- 22. 净ip地址的IPv4
- 23. IPv4地址自动更改
- 24. remote_addr不返回IPv4地址
- 25. Tarantool 1.6不听ipv4地址
- 26. VB.Net返回IPv4地址
- 27. 如何匹配IPv4地址
- 28. inet_aton IPv4地址规范化
- 29. 如何获取客户端的IPv4地址?
- 30. 获取服务器的IPv4地址,而不是机器
这看起来确实不错,但它得到的公网IP地址,而不是我要找的是设备(192.168.x.x)的本地地址。 – Nathangrad
答复已更新。 – Red
我已经尝试过使用RTCPeerConnection,它可以在Chrome和Firefox中正常工作。但遗憾的是,它在Safari中无法使用,所以我无法使用这种方法。 – Nathangrad