JS获取浏览器客户端IP及解除获取本机IP安全限制

前端开发   发布日期:2023年05月31日   浏览次数:464

如果电脑没获取到,基本上是因为浏览器限制了,解除方法如下:解决方案:

火狐(FireFox) 删除隐藏IP

浏览器输入 about:config

搜索配置 media.peerconnection.enabled 改为false ( 刷新程序,IP正常显示 )

谷歌(Chrome) 删除隐藏IP

浏览器输入:chrome://flags/#enable-webrtc-hide-local-ips-with-mdns

把 Anonymize local IPs exposed by WebRTC 设置为 disabled ( 刷新程序,IP正常显示 )

eage浏览器删除隐藏ip

浏览器输入: edge://flags/#enable-webrtc-hide-local-ips-with-mdns

把 Anonymize local IPs exposed by WebRTC 设置为 disabled ( 刷新程序,IP正常显示 )

JS获取浏览器客户端IP:

  1. <script>
  2. function getIP(callback) {
  3. let recode = {};
  4. let RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
  5. if (!RTCPeerConnection) {
  6. let win = iframe.contentWindow;
  7. RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection;
  8. }
  9. //创建实例,生成连接
  10. let pc = new RTCPeerConnection();
  11. // 匹配字符串中符合ip地址的字段
  12. function handleCandidate(candidate) {
  13. let ip_regexp = /([0-9]{1,3}(\.[0-9]{1,3}){3}|([a-f0-9]{1,4}((:[a-f0-9]{1,4}){7}|:+[a-f0-9]{1,4}){6}))/;
  14. let ip_isMatch = candidate.match(ip_regexp)[1];
  15. if (!recode[ip_isMatch]) {
  16. callback(ip_isMatch);
  17. recode[ip_isMatch] = true;
  18. }
  19. }
  20. //监听icecandidate事件
  21. pc.onicecandidate = (ice) => {
  22. if (ice.candidate) {
  23. handleCandidate(ice.candidate.candidate);
  24. }
  25. };
  26. //建立一个伪数据的通道
  27. pc.createDataChannel('');
  28. pc.createOffer((res) => {
  29. pc.setLocalDescription(res);
  30. }, () => {});
  31. //延迟,让一切都能完成
  32. setTimeout(() => {
  33. let lines = pc.localDescription.sdp.split('\n');
  34. lines.forEach(item => {
  35. if (item.indexOf('a=candidate:') === 0) {
  36. handleCandidate(item);
  37. }
  38. })
  39. }, 1000);
  40. }
  41. getIP(function(ip){
  42. console.log("得到的本地IP :" + ip); // 192.168.1.80
  43. });
  44. </script>

以上就是JS获取浏览器客户端IP及解除获取本机IP安全限制的详细内容,更多关于JS获取浏览器客户端IP及解除获取本机IP安全限制的资料请关注九品源码其它相关文章!