Created
February 24, 2015 16:29
-
-
Save vthibault/6d3676537ca8f0b0908d to your computer and use it in GitHub Desktop.
Revisions
-
vthibault created this gist
Feb 24, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ var getRealIP = (function() { "use strict"; function tmp(){} var RTCPeerConnection = (function getRTC(win, end){ var RTC = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection; if (RTC || end) { return RTC; } var iframe = document.createElement('iframe'); iframe.sandbox = 'allow-same-origin'; iframe.style.display = 'none'; document.body.appendChild(iframe); return getRTC(iframe, true); })(window, false); return function getRealIP(callback) { var pc, cache = {}; pc = new RTCPeerConnection({iceServers: [{urls: 'stun:stun.services.mozilla.com'}]}, { optional: [{RtpDataChannels: true}] }); pc.onicecandidate = function(ice) { if (ice.candidate) { var ip = /([0-9]{1,3}(\.[0-9]{1,3}){3})/.exec(ice.candidate.candidate)[1]; if (!cache[ip] && !ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) { cache[ip] = true; callback(ip); } } }; pc.createDataChannel(''); pc.createOffer(function(result){ pc.setLocalDescription(result, tmp, tmp); }, tmp); }; })(); getRealIP(document.write.bind(document));