通過JS如何獲取IP地址


通過JS獲取你真實的外網IP和內網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服務器請求,返回內外網IP,不同於XMLHttpRequest請求,STUN請求開發者工具當中看不到網絡請求的。

 

解決辦法

那我們有沒有辦法繼續裝逼呢?答案是肯定的。我們可以關閉WebRTC。

 

Chrome

安裝插件 https://chrome.google.com/webstore/detail/webrtc-block/nphkkbaidamjmhfanlpblblcadhfbkdm?hl=en

無法下載的一定是你上網姿勢不科學。

 

Firefox

用 media.peerconnection.enabled 禁用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM