Skip to content

Instantly share code, notes, and snippets.

@liketaurus
Last active May 12, 2018 06:55
Show Gist options
  • Save liketaurus/644dbf40cda8a966d27d8a6b061d4d3c to your computer and use it in GitHub Desktop.
Save liketaurus/644dbf40cda8a966d27d8a6b061d4d3c to your computer and use it in GitHub Desktop.
How to get local IP address via JavaScript
// found at https://stackoverflow.com/a/32841043
// many thanks to author!
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
// Firefox & Chrome only! This will not work in Edge!
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) {
if (ice && ice.candidate && ice.candidate.candidate) {
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];
$('#showIP').append('Current IP : '+ myIP);
pc.onicecandidate = noop;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment