Skip to content

Instantly share code, notes, and snippets.

@vthibault
Created February 24, 2015 16:29
Show Gist options
  • Select an option

  • Save vthibault/6d3676537ca8f0b0908d to your computer and use it in GitHub Desktop.

Select an option

Save vthibault/6d3676537ca8f0b0908d to your computer and use it in GitHub Desktop.

Revisions

  1. vthibault created this gist Feb 24, 2015.
    45 changes: 45 additions & 0 deletions gethiddenip.js
    Original 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));