简体   繁体   English

如何使用 JavaScript 查询 STUN 服务器以获取公共 IP 和端口?

[英]How to query a STUN server with JavaScript to get public IP and Port?

I am trying to find some code example to query a STUN server to get my public IP and Port, using JavaScript.我正在尝试使用 JavaScript 查找一些代码示例来查询 STUN 服务器以获取我的公共 IP 和端口。 Perhaps using the server at也许使用服务器在

http://www.stunserver.org http://www.stunserver.org

While the STUN specification is explained here http://www.ietf.org/rfc/rfc3489.txt ( this is a long document, obviously I don't expect you to read it), I have been unable to find any code example at all, which would make my life easier.虽然这里解释了 STUN 规范http://www.ietf.org/rfc/rfc3489.txt (这是一个很长的文档,显然我不希望你阅读它),但我一直找不到任何代码示例总之,这会让我的生活更轻松。 Something like就像是

function getMyIPAndPort() {
//query stun server for it
}

Thank you谢谢

Very late answer.很晚的答案。 Have a look at this mini project: https://diafygi.github.io/webrtc-ips/看看这个小项目: https : //diafygi.github.io/webrtc-ips/

It does give you the IP, but doesn't seem to give the port.它确实为您提供了 IP,但似乎没有提供端口。 However, if you look at these lines: //match just the IP address var ip_regex = /([0-9]{1,3}(\\.[0-9]{1,3}){3})/ var ip_addr = ip_regex.exec(candidate)[1];但是,如果您查看这些行: //match just the IP address var ip_regex = /([0-9]{1,3}(\\.[0-9]{1,3}){3})/ var ip_addr = ip_regex.exec(candidate)[1];

It seems that they remove the port.似乎他们删除了端口。 I am not sure, but you can play with it to find out.我不确定,但你可以用它来找出答案。

you can get both local and external IP您可以获得本地和外部IP

 function  determineIPs() {
    const pc = new RTCPeerConnection({ iceServers: [ {urls: 'stun:stun.l.google.com:19302'} ] });
    pc.createDataChannel('');
    pc.createOffer().then(offer => pc.setLocalDescription(offer))
    pc.onicecandidate = (ice) => {
        if (!ice || !ice.candidate || !ice.candidate.candidate) {
          console.log("all done.");
          pc.close();   
          return;
        }
        let split = ice.candidate.candidate.split(" ");
        if (split[7] === "host") {
          console.log(`Local IP : ${split[4]}`);
        } else {
          console.log(`External IP : ${split[4]}`);
         }
    };
  }
determineIPs();

eg https://jsbin.com/zosayiyidu/1/edit?js,console例如https://jsbin.com/zosayiyidu/1/edit?js,console

the STUN server is for external IP. STUN 服务器用于外部 IP。 make sure you are not behind any proxy that prevents access to stun:stun.l.google.com:19302 or use a different STUN server确保您没有使用任何阻止访问 stun:stun.l.google.com:19302 或使用不同 STUN 服务器的代理

I've also check the project ( https://diafygi.github.io/webrtc-ips ), even when it's a fantastic script I did not like too much.我还检查了该项目( https://diafygi.github.io/webrtc-ips ),即使它是一个很棒的脚本,我也不太喜欢。 I mean, the reason is that it assumes whichs IPs are public and private.我的意思是,原因是它假设哪些 IP 是公共和私有的。 If the IP is not 10/8, 172.16/12 or 192.168/16 it's a public IPv4.如果 IP 不是 10/8、172.16/12 或 192.168/16,则它是公共 IPv4。 This is not always true.这并非总是如此。 You can (unfortunately) have an IPv4 address of other range in your LAN and NAT it.您可以(不幸的是)在 LAN 中拥有其他范围的 IPv4 地址并对其进行 NAT。 This ideas is to obtain the IP address return by the STUN Server, not assume it.这个思路是获取STUN Server返回的IP地址,不是假设。

Regards,问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM