How to request a STUN server with JavaScript to get a public IP and port?

I am trying to find sample code to query a STUN server to get my public IP and port using JavaScript. Perhaps using a server

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 do not expect you to read it), I could not find any code at all, which would make my life easier. Sort of

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

thanks

+4
source share
2 answers

Very late reply. Take a look at this mini-project: https://diafygi.imtqy.com/webrtc-ips/

This gives you an IP address but does not seem to give a port. 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];

They seem to be removing the port. I'm not sure, but you can play with him to find out.

+1
source

I also check the project ( https://diafygi.imtqy.com/webrtc-ips ), even when this is a fantastic script I didn't like too much. I mean, the reason is that it assumes that the IP addresses are public and private. If the IP is not 10/8, 172.16 / 12, or 192.168 / 16, then this is public IPv4. This is not always true. You can (unfortunately) have an IPv4 address of a different range on your local network and NAT. These ideas are to get the IP address returned on the STUN server, rather than guessing it.

Hello,

+1
source

Source: https://habr.com/ru/post/1485974/


All Articles