Created
April 18, 2019 09:22
-
-
Save geekykant/5420f87cbe50735a9081d2e85409875b to your computer and use it in GitHub Desktop.
Get local IP address on a network using JS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
{ | |
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]; | |
pc.onicecandidate = alert(`IP Address: ${myIP}`); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment