I am trying to find some code example to query a STUN server to get my public IP and Port, using JavaScript. Perhaps using the server at
While the STUN specification is explained here .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. Something like
function getMyIPAndPort() {
//query stun server for it
}
Thank you
I am trying to find some code example to query a STUN server to get my public IP and Port, using JavaScript. Perhaps using the server at
http://www.stunserver
While the STUN specification is explained here http://www.ietf/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. Something like
function getMyIPAndPort() {
//query stun server for it
}
Thank you
Share Improve this question edited Jun 13, 2013 at 11:32 iAteABug_And_iLiked_it asked Jun 13, 2013 at 10:32 iAteABug_And_iLiked_itiAteABug_And_iLiked_it 3,79510 gold badges39 silver badges77 bronze badges 3- 12 views and no answers..... Chuck Norris please help!!! :( – iAteABug_And_iLiked_it Commented Jun 23, 2013 at 15:57
- Are you talking about server-side nodejs application or javascript running in a web browser? – lontivero Commented May 16, 2014 at 14:32
- Oh e on. There should be an Answer. – GunJack Commented Feb 16, 2015 at 4:50
3 Answers
Reset to default 3you can get both local and external IP
function determineIPs() {
const pc = new RTCPeerConnection({ iceServers: [ {urls: 'stun:stun.l.google.: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();
e.g. https://jsbin./zosayiyidu/1/edit?js,console
the STUN server is for external IP. make sure you are not behind any proxy that prevents access to stun:stun.l.google.:19302 or use a different STUN server
Very late answer. Have a look at this mini project: https://diafygi.github.io/webrtc-ips/
It does give you the IP, but doesn't seem to give the 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];
It seems that they remove the port. I am not sure, but you can play with it to find out.
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. 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's a public IPv4. This is not always true. You can (unfortunately) have an IPv4 address of other range in your LAN and NAT it. This ideas is to obtain the IP address return by the STUN Server, not assume it.
Regards,