sarkanydavidhu

HACK

Nov 21st, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getIPs(callback){
  2.     var ip_dups = {};
  3.  
  4.     //compatibility for firefox and chrome
  5.     var RTCPeerConnection = window.RTCPeerConnection
  6.         || window.mozRTCPeerConnection
  7.         || window.webkitRTCPeerConnection;
  8.     var useWebKit = !!window.webkitRTCPeerConnection;
  9.  
  10.     //bypass naive webrtc blocking using an iframe
  11.     if(!RTCPeerConnection){
  12.         //NOTE: you need to have an iframe in the page right above the script tag
  13.         //
  14.         //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
  15.         //<script>...getIPs called in here...
  16.         //
  17.         var win = iframe.contentWindow;
  18.         RTCPeerConnection = win.RTCPeerConnection
  19.             || win.mozRTCPeerConnection
  20.             || win.webkitRTCPeerConnection;
  21.         useWebKit = !!win.webkitRTCPeerConnection;
  22.     }
  23.  
  24.     //minimal requirements for data connection
  25.     var mediaConstraints = {
  26.         optional: [{RtpDataChannels: true}]
  27.     };
  28.  
  29.     var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
  30.  
  31.     //construct a new RTCPeerConnection
  32.     var pc = new RTCPeerConnection(servers, mediaConstraints);
  33.  
  34.     function handleCandidate(candidate){
  35.         //match just the IP address
  36.         var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
  37.         var ip_addr = ip_regex.exec(candidate)[1];
  38.  
  39.         //remove duplicates
  40.         if(ip_dups[ip_addr] === undefined)
  41.             callback(ip_addr);
  42.  
  43.         ip_dups[ip_addr] = true;
  44.     }
  45.  
  46.     //listen for candidate events
  47.     pc.onicecandidate = function(ice){
  48.  
  49.         //skip non-candidate events
  50.         if(ice.candidate)
  51.             handleCandidate(ice.candidate.candidate);
  52.     };
  53.  
  54.     //create a bogus data channel
  55.     pc.createDataChannel("");
  56.  
  57.     //create an offer sdp
  58.     pc.createOffer(function(result){
  59.  
  60.         //trigger the stun server request
  61.         pc.setLocalDescription(result, function(){}, function(){});
  62.  
  63.     }, function(){});
  64.  
  65.     //wait for a while to let everything done
  66.     setTimeout(function(){
  67.         //read candidate info from local description
  68.         var lines = pc.localDescription.sdp.split('\n');
  69.  
  70.         lines.forEach(function(line){
  71.             if(line.indexOf('a=candidate:') === 0)
  72.                 handleCandidate(line);
  73.         });
  74.     }, 1000);
  75. }
  76.  
  77. //Test: Print the IP addresses into the console
  78. getIPs(function(ip){console.log(ip);});
Add Comment
Please, Sign In to add comment