javascript
通过JS如何获取IP地址
通過JS獲取你真實的外網(wǎng)IP和內網(wǎng)IP,就算開代理也沒有用,想想真是太可怕了,還能不能愉快的裝逼了!
代碼:
//get the IP addresses associated with an account
function getIPs(callback){
??? var ip_dups = {};
?
??? //compatibility for firefox and chrome
??? var RTCPeerConnection = window.RTCPeerConnection
??????? || window.mozRTCPeerConnection
??????? || window.webkitRTCPeerConnection;
?
??? //bypass naive webrtc blocking
??? if (!RTCPeerConnection) {
??????? var iframe = document.createElement('iframe');
??????? //invalidate content script
??????? iframe.sandbox = 'allow-same-origin';
??????? 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;
??? }
?
??? //minimal requirements for data connection
??? var mediaConstraints = {
??????? optional: [{RtpDataChannels: true}]
??? };
?
??? //firefox already has a default stun server in about:config
??? //??? media.peerconnection.default_iceservers =
??? //??? [{"url": "stun:stun.services.mozilla.com"}]
??? var servers = undefined;
?
??? //add same stun server for chrome
??? if(window.webkitRTCPeerConnection)
??????? servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
?
??? //construct a new RTCPeerConnection
??? var pc = new RTCPeerConnection(servers, mediaConstraints);
?
??? //listen for candidate events
??? pc.onicecandidate = function(ice){
?
??????? //skip non-candidate events
??????? if(ice.candidate){
?
??????????? //match just the IP address
??????????? var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
??????????? var ip_addr = ip_regex.exec(ice.candidate.candidate)[1];
?
??????????? //remove duplicates
??????????? if(ip_dups[ip_addr] === undefined)
??????????????? callback(ip_addr);
?
??????????? ip_dups[ip_addr] = true;
??????? }
??? };
?
??? //create a bogus data channel
??? pc.createDataChannel("");
?
??? //create an offer sdp
??? pc.createOffer(function(result){
?
??????? //trigger the stun server request
??????? pc.setLocalDescription(result, function(){}, function(){});
?
??? }, function(){});
}
?
//Test: Print the IP addresses into the console
getIPs(function(ip){console.log(ip);});
?
因為Firefox和Chrome支持WebRTC,可以向STUN服務器請求,返回內外網(wǎng)IP,不同于XMLHttpRequest請求,STUN請求開發(fā)者工具當中看不到網(wǎng)絡請求的。
?
解決辦法
那我們有沒有辦法繼續(xù)裝逼呢?答案是肯定的。我們可以關閉WebRTC。
?
Chrome
安裝插件 https://chrome.google.com/webstore/detail/webrtc-block/nphkkbaidamjmhfanlpblblcadhfbkdm?hl=en
無法下載的一定是你上網(wǎng)姿勢不科學。
?
Firefox
用 media.peerconnection.enabled 禁用。
?
轉載于:https://www.cnblogs.com/ranyonsue/p/9174137.html
總結
以上是生活随笔為你收集整理的通过JS如何获取IP地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库学习(MySQL):JDBC的简单
- 下一篇: Springboot——HelloWor