How to get the IP or team name with JavaScript

1

Hello good afternoon I am trying to get the local IP of the team through javascript and I managed to work in mozila and chrome but I need to be able to obtain it in internet explorer since the company where the system is implemented the majority of the staff uses internet explorer .... any idea or plugin or how to get the Internet IP explorer with javascript ???

    
asked by Edwing 14.01.2017 в 19:41
source

1 answer

4

This code works in Mozilla Firefox and Google Chrome , in case someone requires it. Internet explorer does not work because its API has not implemented it yet, hopefully it does so soon, at least in Edge.

    //obtiene la direccion IP:
    function getIPs(callback){
        var ip_dups = {};
    
        //compatibilidad exclusiva de firefox y chrome, el usuario @guzgarcia compartio este enlace muy util: http://iswebrtcreadyyet.com/
        var RTCPeerConnection = window.RTCPeerConnection
            || window.mozRTCPeerConnection
            || window.webkitRTCPeerConnection;
        var useWebKit = !!window.webkitRTCPeerConnection;
    
        //bypass naive webrtc blocking using an iframe
        if(!RTCPeerConnection){
            //NOTE: necesitas tener un iframe in la pagina, exactamente arriba de la etiqueta script
            //
            //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
            //<script>... se llama a la funcion getIPs aqui...
            //
            var win = iframe.contentWindow;
            RTCPeerConnection = win.RTCPeerConnection
                || win.mozRTCPeerConnection
                || win.webkitRTCPeerConnection;
            useWebKit = !!win.webkitRTCPeerConnection;
        }
    
        //requisitos minimos para conexion de datos
        var mediaConstraints = {
            optional: [{RtpDataChannels: true}]
        };
    
        var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
    
        //construccion de una nueva RTCPeerConnection
        var pc = new RTCPeerConnection(servers, mediaConstraints);
    
        function handleCandidate(candidate){
            // coincidimos con la direccion IP
            var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
            var ip_addr = ip_regex.exec(candidate)[1];
    
            //eliminamos duplicados
            if(ip_dups[ip_addr] === undefined)
                callback(ip_addr);
    
            ip_dups[ip_addr] = true;
        }
    
        //escuchamos eventos candidatos
        pc.onicecandidate = function(ice){
    
            //dejamos de lado a los eventos que no son candidatos
            if(ice.candidate)
                handleCandidate(ice.candidate.candidate);
        };
    
        //creamos el canal de datos
        pc.createDataChannel("");
    
        //creamos un offer sdp
        pc.createOffer(function(result){
    
            //disparamos la peticion (request) al stun server (para entender mejor debemos ver la documentacion de WebRTC.
            pc.setLocalDescription(result, function(){}, function(){});
    
        }, function(){});
    
        //esperamos un rato para dejar que todo se complete:
        setTimeout(function(){
            //leemos la informacion del candidato desde la descripcion local
            var lines = pc.localDescription.sdp.split('\n');
    
            lines.forEach(function(line){
                if(line.indexOf('a=candidate:') === 0)
                    handleCandidate(line);
            });
        }, 1000);
    }
    
    //Llego la hora de la verdad! vamos a probar: con esto veremos nuestra IP Local:
    getIPs(function(ip){console.log(ip); console.log("saludos hermandad :D !");});
    
answered by 27.01.2017 в 22:11