首页 文章

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

提问于
浏览
4

我试图找到一些代码示例来查询STUN服务器以使用JavaScript获取我的公共IP和端口 . 也许在使用服务器

http://www.stunserver.org

虽然这里解释了STUN规范http://www.ietf.org/rfc/rfc3489.txt(这是一篇很长的文档,显然我不希望你阅读它),但我根本找不到任何代码示例,这会让我的生活变得更轻松 . 就像是

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

谢谢

2 回答

  • 1

    我也检查了项目(https://diafygi.github.io/webrtc-ips),即使它是一个公共IPv4的's a fantastic script I did not like too much. I mean, the reason is that it assumes whichs IPs are public and private. If the IP is not 10/8, 172.16/12 or 192.168/16 it' . 这并非总是如此 . 您可以(不幸的是)在您的LAN中具有其他范围的IPv4地址并对其进行NAT . 这个想法是通过STUN服务器获取IP地址返回,而不是假设它 .

    问候,

  • 1

    答案很晚 . 看看这个迷你项目:https://diafygi.github.io/webrtc-ips/

    它确实给你IP,但似乎没有给出端口 . 但是,如果你看一下这些行: //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];

    似乎他们删除了端口 . 我不确定,但你可以用它来发现 .

相关问题