2

It seems impossible to get your IP address with pure javascript without recurring to external services or server side code.

My question is: why didn't the language implement this feature natively? After all, the browser should know "its" IP address.

4
  • The browser knows it's IP address, sure. Why do you need it? You can't get it from the browser for security reasons. Commented Sep 3, 2015 at 19:14
  • 1
    Actually, you can get the user's IP address with JavaScript. WebRTC has a security flaw (read: feature) that can get both your external and internal IPs. Have a gander at ipleak.net, it uses code based off github.com/diafygi/webrtc-ips Commented Sep 3, 2015 at 19:15
  • 1
    My question was simpler: why doesn't native Javascript have a "getMyIP()" function? Commented Sep 4, 2015 at 5:47
  • I'd say it's just for security/privacy. Though, with the code from github.com/diafygi/webrtc-ips, JavaScript basically does have a getMyIP() function. Commented Sep 4, 2015 at 13:10

1 Answer 1

7

You actually can the user's IP address using vanilla JavaScript. WebRTC has a security flaw (read: feature) that lets get both the user's external and even internal (LAN) IP addresses.

Have a look at this GitHub project: https://github.com/diafygi/webrtc-ips

//get the IP addresses associated with an account
function getIPs(callback){
    var ip_dups = {};

    //compatibility for firefox and chrome
    var RTCPeerConnection = window.RTCPeerConnection
        || window.mozRTCPeerConnection
        || window.webkitRTCPeerConnection;
    var useWebKit = !!window.webkitRTCPeerConnection;

    //bypass naive webrtc blocking using an iframe
    if(!RTCPeerConnection){
        //NOTE: you need to have an iframe in the page right above the script tag
        //
        //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
        //<script>...getIPs called in here...
        //
        var win = iframe.contentWindow;
        RTCPeerConnection = win.RTCPeerConnection
            || win.mozRTCPeerConnection
            || win.webkitRTCPeerConnection;
        useWebKit = !!win.webkitRTCPeerConnection;
    }

    //minimal requirements for data connection
    var mediaConstraints = {
        optional: [{RtpDataChannels: true}]
    };

    var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};

    //construct a new RTCPeerConnection
    var pc = new RTCPeerConnection(servers, mediaConstraints);

    function handleCandidate(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];

        //remove duplicates
        if(ip_dups[ip_addr] === undefined)
            callback(ip_addr);

        ip_dups[ip_addr] = true;
    }

    //listen for candidate events
    pc.onicecandidate = function(ice){

        //skip non-candidate events
        if(ice.candidate)
            handleCandidate(ice.candidate.candidate);
    };

    //create a bogus data channel
    pc.createDataChannel("");

    //create an offer sdp
    pc.createOffer(function(result){

        //trigger the stun server request
        pc.setLocalDescription(result, function(){}, function(){});

    }, function(){});

    //wait for a while to let everything done
    setTimeout(function(){
        //read candidate info from local description
        var lines = pc.localDescription.sdp.split('\n');

        lines.forEach(function(line){
            if(line.indexOf('a=candidate:') === 0)
                handleCandidate(line);
        });
    }, 1000);
}

//Test: Print the IP addresses into the console
getIPs(function(ip){console.log(ip);});

Check out the demo: https://diafygi.github.io/webrtc-ips/

Sign up to request clarification or add additional context in comments.

4 Comments

That's oddly terrifying.
@ScottKaye: What's worse is (according to a Mozilla bug: bugzilla.mozilla.org/show_bug.cgi?id=959893), it's by design. See: tools.ietf.org/html/…
It's not giving public ip.
As of 2022, this doesn't work in any browser I've tried (Chrome, Firefox, Edge).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.