Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local/internal IP with JavaScript

How can I get the local client IP using WebRTC.

I don't need the REMOTE_ADDR of the client but his local network IP. I've seen this before on websites like sharedrop.com, it recognizes computer within the same network using WebRTC.

In PHP I do this to get the clients remote IP:

<?php
  echo $_SERVER["REMOTE_ADDR"]; // which would return 72.72.72.175
?>

I looked through stackoverflow but every question is answered using the remote addr.

How can I get my local IP (192.168.1.24 for example) with JavaScript instead of the remote addr.

like image 218
Pascal Raszyk Avatar asked Sep 29 '15 06:09

Pascal Raszyk


People also ask

Can I get IP address from JavaScript?

JavaScript works with third-party applications to fetch the IP addresses. These are the application services that fetch the user's IP address and simply return it in three formats, ie., plain text, JSON, and JSONP format. There are loads of them available on the internet.

How do I find my localhost IP?

First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.

How do I get an IP address from HTML?

var ip = HttpContext. Current. Request. ServerVariables["REMOTE_ADDR"]; var ip = Request.

Can you get MAC address using JavaScript?

No you cannot get the MAC address in JavaScript, mainly because the MAC address uniquely identifies the running computer so it would be a security vulnerability. Now if all you need is a unique identifier, I suggest you create one yourself using some cryptographic algorithm and store it in a cookie.


2 Answers

You can use this version on modern browser (with Promises and async / await)

// minified onliner, 219b
const ip = await new Promise((s,f,c=new RTCPeerConnection(),k='candidate')=>(c.createDataChannel(''),c.createOffer(o=>c.setLocalDescription(o),f),c.onicecandidate=i=>i&&i[k]&&i[k][k]&&c.close(s(i[k][k].split(' ')[4]))))
// cleaned, 363b
const ip = await new Promise((resolve, reject) => {
  const conn = new RTCPeerConnection()
  conn.createDataChannel('')
  conn.createOffer(offer => conn.setLocalDescription(offer), reject)
  conn.onicecandidate = ice => {
    if (ice && ice.candidate && ice.candidate.candidate) {
      resolve(i.candidate.candidate.split(' ')[4])
      conn.close()
    }
  }
})
like image 200
kigiri Avatar answered Sep 20 '22 13:09

kigiri


UPDATE

Unfortunately the below answer no longer works as browsers have changed this behavior due to security concerns. See this StackOverflow Q&A for more details.


where I took code from --> Source

You can find a demo at --> Demo

I have modified the source code, reduced the lines, not making any stun requests since you only want Local IP not the Public IP, the below code works in latest Firefox and Chrome:

    window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;   //compatibility for firefox and chrome
    var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};      
    pc.createDataChannel("");    //create a bogus data channel
    pc.createOffer(pc.setLocalDescription.bind(pc), noop);    // create offer and set local description
    pc.onicecandidate = function(ice){  //listen for candidate events
        if(!ice || !ice.candidate || !ice.candidate.candidate)  return;
        var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
        console.log('my IP: ', myIP);   
        pc.onicecandidate = noop;
    };
    

what is happening here is, we are creating a dummy peer connection, and for the remote peer to contact us, we generally exchange ice candidates with each other. And reading the ice candiates we can tell the ip of the user.

like image 30
mido Avatar answered Sep 22 '22 13:09

mido