Advertisement
rgruber

webrtc local ip addr

Dec 19th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;   //compatibility for firefox and chrome
  2.     var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};      
  3.     pc.createDataChannel("");    //create a bogus data channel
  4.     pc.createOffer(pc.setLocalDescription.bind(pc), noop);    // create offer and set local description
  5.     pc.onicecandidate = function(ice){  //listen for candidate events
  6.         if(!ice || !ice.candidate || !ice.candidate.candidate)  return;
  7.         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];
  8.         console.log('my IP: ', myIP);  
  9.         pc.onicecandidate = noop;
  10.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement