Skip to content

Instantly share code, notes, and snippets.

@tadeubdev
Created April 26, 2019 23:08
Show Gist options
  • Select an option

  • Save tadeubdev/3965677b551dd90554d490a4c6b03654 to your computer and use it in GitHub Desktop.

Select an option

Save tadeubdev/3965677b551dd90554d490a4c6b03654 to your computer and use it in GitHub Desktop.
Get the actual IP Adress in JavaScript
<script>
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];
alert('My IP is: ' + myIP);
pc.onicecandidate = noop;
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment