When trying to find out my public IP address using a public STUN server, it works for IPv4 but not for IPv6, regardless of the STUN server I use.
I only get stuff like "59aeb370-1d93-44ee-a526-27d639256cf4.local" under candidate.address. Is there any way to get the public IPv6 via javascript using STUN?
The code I use is:
(don't mind the IPv4 regex; the entire 'candidate' is console logged anyway)
var ip_dups = {};
function getRTCPeerConnection () {
var iframe, content_window, rtc_peer_con = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.msRTCPeerConnection;
return rtc_peer_con || (iframe = document.createElement("iframe"), iframe.style.display = "none", document.body.appendChild(iframe), content_window = iframe.contentWindow, rtc_peer_con = content_window.RTCPeerConnection || content_window.mozRTCPeerConnection || content_window.webkitRTCPeerConnection || content_window.msRTCPeerConnection), rtc_peer_con
};
var n = getRTCPeerConnection();
servers = {
iceServers: [{
urls: "stun:stun.l.google.com:19302"
}]
};
mediaConstraints = {
optional: [{
RTCPChannel: true
}]
};
pc = new n(servers, mediaConstraints);
function handleCandidate(candidate){
console.log(candidate);
//match just the IP address
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
var ip_addr = ip_regex.exec(candidate)[1];
if(ip_dups[ip_addr] === undefined) {
console.log(ip_addr);
ip_dups[ip_addr] = true;
}
pc.close()
}
pc.onicecandidate = function(ice){
//skip non-candidate events
console.log(ice)
if(ice.candidate) {
handleCandidate(ice.candidate.candidate);
}
};
pc.createDataChannel("");
void pc.createOffer(function(e) {
console.log(e)
console.log(e.sdp)
pc.setLocalDescription(e, function() {}, function() {})
}, function() {})
EDIT:
seems like in some cases I do get my IPv6 in the response when running the code inside the consoles of some web pages.
But then when accessing the same page from chrome incognito for example, it doesn't work, so it's not because of the domain or anything like that.
No permissions are involved too.
So what the hell is going on? What's the logic behind it? Is there anything can do so my webpage will always cause its visitors to receive their IPv6 addresses?