有什么办法可以将.local IP地址在rtc ice候选对象中隐藏为IPv4?

时间:2019-08-01 06:38:43

标签: javascript webrtc

我正在尝试通过JavaScript从浏览器获取客户端的私有IP。我正在使用rtc对等连接。问题是在某些情况下,对于Chrome,问题是ip以some_hex_string.local的形式返回(例如7c392ca5-d635-45be-841c-99dbfe3a0bfd.local)。如果禁用了chrome标志,它将返回Firefox和chrome的专用IP:(chrome:// flags /#enable-webrtc-hide-local-ips-with-mdns)。我需要IP的IPv4版本,以便将我的应用程序中的特定客户端IP列入白名单。

根据https://bloggeek.me/psa-mdns-and-local-ice-candidates-are-coming/,rtc客户端引入了将本地ip屏蔽为上述的mdns字符串,最后带有.local的字符串。我什至找不到任何可能将mdns字符串屏蔽为IPv4地址的东西。

这是我正在尝试的代码:

window.RTCPeerConnection =
    window.RTCPeerConnection ||
    window.mozRTCPeerConnection ||
    window.webkitRTCPeerConnection; //compatibility for Firefox and chrome

  var pc = new RTCPeerConnection({ iceServers: [] }),
    noop = function() {};

  pc.createDataChannel(""); //create a bogus data channel

 // create offer and set local description
  pc.createOffer(pc.setLocalDescription.bind(pc), noop);     
  pc.onicecandidate = function(ice) {
    if (ice && ice.candidate && ice.candidate.candidate) {

      let myIp = ice.candidate.address;
      alert(myIp);
      pc.onicecandidate = noop;
    }
  };

我什至不确定这种方法是否是获取IP地址的正确方法。到目前为止,我已经找到了这种rtc方式,并向一些第三方api发送信息以获取IP(返回我的公共IP,而不是私有IP)。我想知道是否有人可以向我指出获取客户端专用IP的方法。 IP过滤的任何其他方式也将受到赞赏。

1 个答案:

答案 0 :(得分:0)

WebRTC并非旨在获取用户IP。 “取消遮罩”的唯一方法是询问用户摄像机的权限。您的用例是什么?