文章正文
通过WEBRTC结合stun服务器实现获取真实主机IP
测试:
真实IP:
挂代理之后的ip
保存以下代码到本地:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h3>你的代理IP是:<div id=1></div></h3>
<h3>你的真实IP是:<div id=2></div></h3>
<script>
// turn 配置
const config = {
iceServers: [{
urls: "stun:stun.l.google.com:19302" // stun.voippro.com stun.voipraider.com 这里使用谷歌,线上部署直接替换
}]
};
// 构建
let pc = new RTCPeerConnection(config);
pc.onicecandidate = function(event) {
if(event.candidate)
handleCandidate(event.candidate.candidate);
}
function handleCandidate(candidate) {
if (candidate.indexOf("srflx") != -1) {
console.log(candidate)
var regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
var ip_addr = regex.exec(candidate)[0];
//alert("Your public network ip: "+ ip_addr)
document.getElementById('2').innerHTML = ip_addr;
}
}
pc.createDataChannel("");
pc.createOffer(function(result){
pc.setLocalDescription(result);
}, function(){});
</script>
<script src='http://pv.sohu.com/cityjson?ie=utf-8'></script>
<script>
var ip=returnCitySN["cip"];
//var city=returnCitySN["cname"];
document.getElementById('1').innerHTML = ip;
</script>
</body>
</html>
在挂代理的条件下,访问,即可获得 代理后面的真实IP:
具体WEBRTC、STUN的原理,感兴趣的自行google
好文推荐