Skip to content

Instantly share code, notes, and snippets.

@Steven24K
Created December 20, 2018 13:03
Show Gist options
  • Save Steven24K/8fe44c8b319ae203c6bdbbdf9363a54f to your computer and use it in GitHub Desktop.
Save Steven24K/8fe44c8b319ae203c6bdbbdf9363a54f to your computer and use it in GitHub Desktop.
A Javascript function that returns the users local IP adress
function getLocalIP()
{
var ip = [];
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection || false;
if (window.RTCPeerConnection)
{
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel('');
pc.createOffer(pc.setLocalDescription.bind(pc), noop);
pc.onicecandidate = function(event)
{
if (event && event.candidate && event.candidate.candidate)
{
var s = event.candidate.candidate.split('\n');
ip.push(s[0].split(' ')[4]);
}
}
}
return ip;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment