Skip to content

Instantly share code, notes, and snippets.

@vthibault
Created February 24, 2015 16:29
Show Gist options
  • Save vthibault/6d3676537ca8f0b0908d to your computer and use it in GitHub Desktop.
Save vthibault/6d3676537ca8f0b0908d to your computer and use it in GitHub Desktop.
Get IP behind proxy (if available)
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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment