Skip to content

Instantly share code, notes, and snippets.

@hectorguo
Last active March 18, 2024 06:21
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save hectorguo/672844c319547498dcb569df583f959d to your computer and use it in GitHub Desktop.
Save hectorguo/672844c319547498dcb569df583f959d to your computer and use it in GitHub Desktop.
Get local IP address through Javascript
/**
* Get Local IP Address
*
* @returns Promise Object
*
* getLocalIP().then((ipAddr) => {
* console.log(ipAddr); // 192.168.0.122
* });
*/
function getLocalIP() {
return new Promise(function(resolve, reject) {
// NOTE: window.RTCPeerConnection is "not a constructor" in FF22/23
var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (!RTCPeerConnection) {
reject('Your browser does not support this API');
}
var rtc = new RTCPeerConnection({iceServers:[]});
var addrs = {};
addrs["0.0.0.0"] = false;
function grepSDP(sdp) {
var hosts = [];
var finalIP = '';
sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13
var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1
addr = parts[4],
type = parts[7];
if (type === 'host') {
finalIP = addr;
}
} else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7
var parts = line.split(' '),
addr = parts[2];
finalIP = addr;
}
});
return finalIP;
}
if (1 || window.mozRTCPeerConnection) { // FF [and now Chrome!] needs a channel/stream to proceed
rtc.createDataChannel('', {reliable:false});
};
rtc.onicecandidate = function (evt) {
// convert the candidate to SDP so we can run it through our general parser
// see https://twitter.com/lancestout/status/525796175425720320 for details
if (evt.candidate) {
var addr = grepSDP("a="+evt.candidate.candidate);
resolve(addr);
}
};
rtc.createOffer(function (offerDesc) {
rtc.setLocalDescription(offerDesc);
}, function (e) { console.warn("offer failed", e); });
});
}
@Natarajan8344
Copy link

Hi Experts,

I am getting some random value as IP. Example:"04e6e16d-5ca8-4643-bccd-f870c84d4aed.local".
Can you please guide me to solve this.

@SASFMONTOYA
Copy link

The function don't work, see:

image

@DamianoP
Copy link

same, it doesn't work anymore in chrome, any update ?

@SASFMONTOYA
Copy link

SASFMONTOYA commented Aug 13, 2019

Chrome, now block by Default the option to capture de local IP of the user, I used the function of this page, but it only works if, in Google Chrome, the user have disabled the option chrome://flags/#enable-webrtc-hide-local-ips-with-mdns, else, the function won't works.

I prefer this option because it return me and string, y prefer this than nothing (the function that i shared with your don't return any value if the option is enabled)

@haydenm315
Copy link

Examples like this are why I'm left trying to explain why we can't resolve mdns addresses across vlan segments...

@addyevdox
Copy link

Hi, this function is not working now...

Screen Shot 2020-01-06 at 1 23 39 PM

@mpalavrov
Copy link

:( Yes, this function is not working anymore, maybe it is something from the Chrome version? If someone has similar function to use for getting the Local IP would be much appriciated

@kartikdutta28
Copy link

Did anyone found an alternative workaround to fetch client ip.?

@CristianoFreitas
Copy link

In Chrome it is necessary to disable webrtc.

chrome://flags/#enable-webrtc-hide-local-ips-with-mdns

I haven't tested it on other browser.

@yingwei1025
Copy link

On the latest version of Chrome version 86 cannot find this chrome://flags/#enable-webrtc-hide-local-ips-with-mdns. Any alternative solution?

@ravindragullapalli
Copy link

I am getting ipv6 address. Not ipv4. How can I get ipv4 address?

@krisnadwia
Copy link

image
Hi man, it works! Thanks!

@indhunathan
Copy link

@krisnadwia can you please share you are code. im not getting result like you.

@krisnadwia
Copy link

@krisnadwia can you please share you are code. im not getting result like you.

Hi @indhunathan, I will explain it.

First of all, you have to turn off/disable webrtc in Chrome. Copy this link and paste it in your Chrome address -> "chrome://flags/#enable-webrtc-hide-local-ips-with-mdns", then select "disabled".
enable-webrtc-hide-local-ips-with-mdns

Then, you just need to follow the code above and give it a try. I tried it in inspect.
get-local-ip

It can also be implemented into a project like this.
get Local IP

I hope you understand with my explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment