Can You Get a Users Local Lan Ip Address via JavaScript

Can You Get A Users Local LAN IP Address Via JavaScript?

As it turns out, the recent WebRTC extension of HTML5 allows javascript to query the local client IP address. A proof of concept is available here: http://net.ipcalf.com

This feature is apparently by design, and is not a bug. However, given its controversial nature, I would be cautious about relying on this behaviour. Nevertheless, I think it perfectly and appropriately addresses your intended purpose (revealing to the user what their browser is leaking).

How to get local IP address in javascript html5

I dont think that there is a notion of hosts or ip-addresses in the javascript standard library. So you'll have to access some external service to look up hostnames for you.

Unless you might want to send a request to the server which returns you the host IP address!!

EDIT

In JSP you you can use getRemoteHost() method from HttpServletRequest

to get the IP address of the user.

So you can write something like this -

var ip = '<%=request.getRemoteHost();%>'; 

^^ the above line is JSP code, this should be part of the JSP file that you return from java servlet container like a tomcat. This does not work in static HTML pages.

How to get local/internal IP with JavaScript

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.



Related Topics



Leave a reply



Submit