2

I use laptop for both work and personal use. So it is sometimes attached to home network and sometimes to one of many possible working places networks'

I've been thinking about customizing my browser's home page and/or MDM theme (linux mint logon page, html5 capable), both consist on customizing an html page, maybe using javascript, but i should know where the laptop is.

Think about me going to office, i want info about server's statuses and links to office's servers (A, B, C); going to a customer place, i will need connection to office's external IPs (A*, B*), and customer servers (X, Y, Z); back home, i will like to have wheater info, news, my soccer's team results, twitter, and so. I can setup all this through javascript (for example, using a table or dictionary: places = {ip1: [A, B, C], ip2: [X, Y, Z]} or something like that.

I know there are lots of places talking about retrieving my public IP and so... but using them requires an active and open internet connection. Some of the places I will be using this have no internet access or it is required to use a proxy with login and password. Thus any solution involving a reference to external sites won't work (but customers use local intranet services i need to access).

I also try to keep it simple: the idea is building some fast index page. So I'd prefer NOT loading java in the pages (and I'm not sure if MDM html5 themes would support it)

So the question is: Is there any way for retrieving LOCAL IP (not public IP) using pure javascript or html5, fully client-side, avoiding php, asp, ssi, calls to public networks, or third-party plugins (java/flash)?

I think response is not, as i haven't found any js function like "get_ip()", but i hope to be wrong.

The only solutions I can think about are:

  • installing a local web server, a little nginx or something that can be used for retrieving client request for ip
  • using java (please, no!)
  • using html5 filesystem api and look for data in a folder where in boot time some script has wrote the ip
  • change the previous for that script simply rewriting the HTML files i will be using the ip with that ip hardcoded
  • using javascript to ping a list of known servers on each possible network

Any other ideas? thanks

  • 1
    No, **for security reasons**, this is not possible for browsers - do you know about the [OSI model](https://en.wikipedia.org/wiki/OSI_model)? What have you tried so far? Btw `php` does not work on client side. There are either too many possible answers, or good answers would be too long for this format. – mate64 Sep 05 '14 at 19:00
  • 2
    @cept0 [Actually finding out the IP is possible](https://dl.dropboxusercontent.com/u/1878671/enumhosts.html), but the result is maybe not the one the questioner does expect. – Ulrich Thomas Gabor Sep 05 '14 at 19:09
  • @GhostGambler Of course, but only the **public IP** ... – mate64 Sep 05 '14 at 19:12
  • @cept0 This javascript example gathers the **local** IP. Not the public IP. – Ulrich Thomas Gabor Sep 05 '14 at 19:14
  • Actually I was wrong with this. The questioner explicitly asks for the local IP, so this example page is exactly what he searches. – Ulrich Thomas Gabor Sep 05 '14 at 19:16
  • Maybe I'm missing the point, but you *could* query a pre-written script using pure javascript (AJAX, JSON, JSONP)? http://stackoverflow.com/a/810461/1330505 – Jongosi Sep 05 '14 at 19:17
  • @Jongosi: that answer uses a server-side script, which OP wants to avoid. Plus the server will only see your public IP address. – Frank van Puffelen Sep 05 '14 at 19:52
  • @GhostGambler That's a perfect example of the functionality I was looking for. I thought it could not be possible (as cept0 stated) but had some hopes. My coding skills maybe aren't good enough for finishing what i have in mind, but your example shows it is not impossible. I'll accept the only response "as is" posted below, as is based on same concept. (if I get to know hoy to do it, it's my first time on stackoverflow... I'm supposed to post an answer? uf) – Thomas Shaddack Sep 15 '14 at 17:28

1 Answers1

5

stripping out the complications of the linked example:

function getLocalIP(cb) {

    var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection || window.RTCPeerConnection,
    rtc = new RTCPeerConnection({
        iceServers: []
    });
    if (window.mozRTCPeerConnection) rtc.createDataChannel('', {reliable: false});
    rtc.onicecandidate = function(e) {
        if (cb && e.candidate) {
            cb(e.candidate.candidate.split(" ")[4]);
            cb = null;
        }
    }; 

    rtc.createOffer(function(resp) {
            var moz=resp.sdp && 
                   resp.sdp.indexOf("c=IN IP4") !==0 && 
                   resp.sdp.split("c=IN IP4")[1].trim().split(/\s/)[0];
            if(moz && moz!=="0.0.0.0"){
                 cb( moz );
            }else{
                 rtc.setLocalDescription(resp);
            }
    }, Boolean);
} /* end getIP() */

getLocalIP(alert)

gives the same result in Chrome and Firefox, didn't work in my copy of IE.

dandavis
  • 16,370
  • 5
  • 40
  • 36
  • According to HTML5ROCKS WebRTC only works in Firefox and Chrome (or Opera which uses webkit like Chrome) -- You have to install/enable Chrome Frame for Internet Explorer to add that capability. Info here: http://www.html5rocks.com/en/tutorials/webrtc/basics/ but it is not natively supported yet as of IE11 without that Chrome-based plugin. – Jeff Clayton Sep 10 '14 at 01:46
  • Google Chrome dropped support for Chrome Frame for Internet Explorer in 2013 anyway it turns out. – Jeff Clayton Sep 10 '14 at 01:52
  • Thanks. Now I could just define a js function "PrepareLinks" accepting one parameter, and callgetLocalIP(PrepareLinks). Then, inside PrepareLinks I could build the links depending on the IP. Exactly what i was looking for. – Thomas Shaddack Sep 15 '14 at 17:32
  • follow up: this stopped working in chrome since i posted in september, probably for the better. – dandavis Mar 10 '15 at 04:14