WebRTC get IP inconsistencies

My friend recently finished a project that I’ve been hearing about for some time, and I wanted to test it out. The source code can be found here. I first tried including the code in a <script> tag in my head, but he said that jsdelivr hasn’t updated their database yet.
However, he told me that the /dist folder is the code, so I copied production.min.js and pasted it into a JSFiddle to test.

Now, I am getting really inconsistent results. For example, in the fiddle, it doesn’t return anything, but when I look in the console, I see this image:

Console showing IP addresses.

It’s done an incredible job of exposing my 3 IPs, my local IPv6, temporary IPv6, and my IPv4, just duplicated. However, it’s in a set, and also, I get an error: No IP addresses found which (when looking at the source) appears to get thrown when the returned array for either Private or Public empty.

Now, I contacted my friend again, and he showed me a working fiddle located here. However, when I test it here, it doesn’t work:

function getPrivateIPs(timer) {
    return new Promise((resolve, reject)=>{
        var ipArray = [];
        var emptyConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
        var pc = new emptyConnection();
        var emptyFunc = function(){};
        var ipList = {};
        var simpleIPRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;

        function start(){
            pc.createDataChannel("");
                pc.onicecandidate = handleCandidates;
            createConnection();
            startTimer();
        };

        function push(ip) {
            if (!ipList[ip]) ipArray.push(ip);
            ipList[ip] = true;
        };
        
        function createConnection(){
            pc.createOffer(function(data) {
                data.sdp.split('\n').forEach(function(line) {
                    if (line.indexOf('candidate') === -1) return;
                    line.match(simpleIPRegex).forEach(push);
                });
    
                pc.setLocalDescription(data);
            }, emptyFunc);
        };

        function handleCandidates(ice) {
            if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(simpleIPRegex)) return;
            ice.candidate.candidate.match(simpleIPRegex).forEach(push);
        };

        function startTimer(){
            setTimeout(()=>{
                if(ipArray && ipArray.length){
                    resolve(ipArray);
                    stop();
                } else{
                    reject('No IPs found.');
                    stop();
                };
            }, timer || 500);
        }

        function stop(){
            if (pc) {
                try {
                    pc.close();
                } finally {
                    pc.onicecandidate = () => {};
                    pc = null;
                };
            };
        };
        
        return start();
    })
}

getPrivateIPs(200).then(console.log)

Also, pasting the snippet into various website’s developer consoles seem to give various results. For example, a blank Google tab 100% of the time for me returned my IPs, while StackOverflow returns the error as do Github among other sites.

What is causing these inconsistencies? It seems like some sites are obfuscating the IPs while others do not (I get IPs like a15b7927-5ef6-4f58-9748-1e40aec155b4.local, mDNS IP). Is this common? Thanks!

92 thoughts on “WebRTC get IP inconsistencies”

Leave a Comment