Created
January 1, 2017 05:02
-
-
Save tabjy/ba0534774da227cbc1f1093bc24ad965 to your computer and use it in GitHub Desktop.
Get real IP behind proxy using WebRTC
This file contains 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 characters
function getPeerConnection() { | |
var RTCPeerConnection = window.RTCPeerConnection || | |
window.mozRTCPeerConnection || | |
window.webkitRTCPeerConnection; | |
//bypass naive webrtc blocking | |
if (!RTCPeerConnection) { | |
var iframe = document.createElement('iframe'); | |
iframe.style.display = 'none'; | |
document.body.appendChild(iframe); | |
var win = iframe.contentWindow; | |
window.RTCPeerConnection = win.RTCPeerConnection; | |
window.mozRTCPeerConnection = win.mozRTCPeerConnection; | |
window.webkitRTCPeerConnection = win.webkitRTCPeerConnection; | |
RTCPeerConnection = window.RTCPeerConnection || | |
window.mozRTCPeerConnection || | |
window.webkitRTCPeerConnection; | |
} | |
return RTCPeerConnection; | |
} | |
function getLeakingIP(callback) { | |
// Based on work by https://github.com/diafygi/webrtc-ips | |
var ip_dups = {}; | |
var RTCPeerConnection = getPeerConnection(); | |
var mediaConstraints = { | |
optional: [{ | |
RtpDataChannels: true | |
}] | |
}; | |
var servers = { | |
iceServers: [{ | |
urls: "stun:stun.dnsleak.net" | |
}] | |
}; | |
var pc = new RTCPeerConnection(servers, mediaConstraints); | |
pc.onicecandidate = function(ice) { | |
//skip non-candidate events | |
if (ice.candidate) { | |
var parts = ice.candidate.candidate.split(" "); | |
var ip_addr = parts[4]; | |
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/; | |
if (ip_regex.test(ip_addr)) { | |
//remove duplicates | |
if (ip_dups[ip_addr] === undefined) | |
//callback(ip_addr); | |
callback(ip_addr); | |
ip_dups[ip_addr] = true; | |
} | |
} | |
}; | |
pc.createDataChannel(""); | |
pc.createOffer(function(result) { | |
//trigger the stun server request | |
pc.setLocalDescription(result, function() {}, function() {}); | |
}, function() {}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment