The browser to get the current ip

function findIP(callback) {
    var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
    //console.log(myPeerConnection )

    var restartConfig = { iceServers: [] };
    // var pc = new myPeerConnection({ iceServers: [] }),
    var pc = new myPeerConnection({ iceServers: [] }),
        noop = function() {},
        localIPs = {},
        ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
        key;

    function ipIterate(ip) {
        if (!localIPs[ip]) callback(ip);
        localIPs[ip] = true;
    }
    pc.setConfiguration(restartConfig);
    pc.createDataChannel("");
    pc.createOffer().then(function(sdp) {
        sdp.sdp.split('\n').forEach(function(line) {
            if (line.indexOf('candidate') < 0) return;
            line.match(ipRegex).forEach(ipIterate);
        });
        pc.setLocalDescription(sdp, noop, noop);
    });
    pc.onicecandidate = function(ice) {
        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
        ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
    };
}

findIP(function(ip) {
    console.log('got ip: ', ip);
});

 

 

If some browsers can not get out to try clearing the cache in the implementation of the above code ctrl + shift + delete

 

Guess you like

Origin www.cnblogs.com/myloveblogs/p/11817097.html