• Register

Is it possible to detect Lan IP address by Browser using JavaScript?

0 like 0 dislike
11,071 views
I knew that Public IP Address can be detected by Server Side programming Language, but, I am not sure if browser (Client-Side JavaScript) or any Server-Side programming can detect Lan IP Address (Private IP Address)?

Please provide an example of working code if possible.
asked Mar 7, 2016 by anonymous

1 Answer

0 like 0 dislike
 
Best answer

YES! Its possible.

Using Peer-to-peer communications with WebRTC (WebRTC is a free, open project that provides browsers and mobile applications with Real-Time Communications (RTC) capabilities via simple APIs. The WebRTC components have been optimized to best serve this purpose. https://webrtc.org/) - Its possible to detect all IP Address of your computer including (Lan IP Address, WiFi IP Address, VPN IP Address, Public IP Address, or any IP that you are using).

There is no server-side programming to detect your local IP address (LAN and WLAN), because the traffic is changed by NAT (Network address translation NAT is a methodology of remapping one IP address space into another by modifying network address information in Internet Protocol (IP) datagram packet headers while they are in transit across a traffic routing device. https://en.wikipedia.org/wiki/Network_address_translation)

Then, the remote server will not receive any information about your private IP address (IP Address that assigned to your local network). But, sometimes, its possible to get Public IP Address from ISP and not using any router, in this case, the Local IP Address will be equal to Public IP Address.

As I mentioned, by Using WebRTC API in JavaScript you can detect user's IP Address, a working example can be found here:

http://ip.learnipaddress.com/what-is-my-ip-address

You can see the source code and learn how it works in detail. Here is also sample code:

(This example explains how JavaScript can detect Public and Local (private) IP address using WebRTC API)

//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);
}

  
answered Mar 7, 2016 by anonymous
edited Mar 20, 2016 by learnipaddress
Please check our IP Tools - This Project sponsored by STANDING Tech Company
...