Skip to content

Instantly share code, notes, and snippets.

@frydaykg
Created April 22, 2019 18:23
Show Gist options
  • Select an option

  • Save frydaykg/7535cd3fd2c9d539b4dfe476efb58966 to your computer and use it in GitHub Desktop.

Select an option

Save frydaykg/7535cd3fd2c9d539b4dfe476efb58966 to your computer and use it in GitHub Desktop.
Local IP
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<script type="text/javascript">
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];
alert('my IP: '+ myIP);
pc.onicecandidate = noop;
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment