根据webGl获取内网IP

function getIpFn(postData){
    var noop = function() {},iceflag=false;
    postData.IP ="";
    //compatibility for firefox and chrome
    var RTCPeerConnection = window.RTCPeerConnection
        || window.mozRTCPeerConnection
        || window.webkitRTCPeerConnection;
    var useWebKit = !!window.webkitRTCPeerConnection;
    //bypass naive webrtc blocking
    if(RTCPeerConnection){
      var ipreg =/([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;	 
      var rtc = new RTCPeerConnection({iceServers:[]});
      //listen for candidate events
      rtc.onicecandidate = function(ice){
          //skip non-candidate events
          if (ice && ice.candidate && ice.candidate.candidate && ice.candidate.candidate.match(ipreg)) {
            hasIp = true;
            ice.candidate.candidate.match(ipreg).forEach(ipIterate);
          }else{
            if(!iceflag){
               renderHtml(postData,"ice");
            };
            iceflag = true;
            return;
            
          };   
      };
      //create a bogus data channel
      rtc.createDataChannel("");
      //create an offer sdp
      rtc.createOffer().then(function (offerDesc) {
          grepSDP(offerDesc);
          rtc.setLocalDescription(offerDesc,function(){},function(){});
      }).then(noop);
      function grepSDP(sdp) {
           hasIp=false;
            var hosts = [];
            sdp.sdp.split('
').forEach(function (line, index, arr) { 
              if (~line.indexOf("a=candidate")) {  
                  line.match(ipreg).forEach(ipIterate);
                } else if (~line.indexOf("c=")) { 
                  line.match(ipreg).forEach(ipIterate);
                };
            }); 
      };
      function ipIterate(ip) {
        if(ip !== "0.0.0.0"){
          console.log(ip)
        };
      };
      
    }else{
      console.log("不是主浏览器");
    };
  };

 直接调用   getIpFn();

  这个方法是参考网上的方法,目前测试

  获取不到IP的浏览器:chrome78及以上的版本;safari;ie ; 360 以及 QQ浏览器中的兼容模式获取不到;

可获取ip的浏览器:火狐,oction getIpFn(postData){

    var noop = function() {},iceflag=false;

    postData.IP ="";

    //compatibility for firefox and chrome

    var RTCPeerConnection = window.RTCPeerConnection

        || window.mozRTCPeerConnection

        || window.webkitRTCPeerConnection;

    var useWebKit = !!window.webkitRTCPeerConnection;

    //bypass naive webrtc blocking

    if(RTCPeerConnection){

      var ipreg =/([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;  

      var rtc = new RTCPeerConnection({iceServers:[]});

      //listen for candidate events

      rtc.onicecandidate = function(ice){

          //skip non-candidate events

          if (ice && ice.candidate && ice.candidate.candidate && ice.candidate.candidate.match(ipreg)) {

            hasIp = true;

            ice.candidate.candidate.match(ipreg).forEach(ipIterate);

          }else{

            if(!iceflag){

               renderHtml(postData,"ice");

            };

            iceflag = true;

            return;

            

          };   

      };

      //create a bogus data channel

      rtc.createDataChannel("");

      //create an offer sdp

      rtc.createOffer().then(function (offerDesc) {

          grepSDP(offerDesc);

          rtc.setLocalDescription(offerDesc,function(){},function(){});

      }).then(noop);

      function grepSDP(sdp) {

           hasIp=false;

            var hosts = [];

            sdp.sdp.split(' ').forEach(function (line, index, arr) { 

              if (~line.indexOf("a=candidate")) {  

                  line.match(ipreg).forEach(ipIterate);

                } else if (~line.indexOf("c=")) { 

                  line.match(ipreg).forEach(ipIterate);

                };

            }); 

      };

      function ipIterate(ip) {

        if(ip !== "0.0.0.0"){

          console.log(ip)

        };

      };

      

    }else{

      console.log("不是主浏览器");

    };

  };

 直接调用 getIpFn();

  这个方法是参考网上的方法,目前测试

  获取不到IP的浏览器:chrome78及以上的版本;safari;ie ; 360 以及 QQ浏览器中的兼容模式获取不到;

  可获取ip的浏览器:火狐,opera,360和QQ浏览器的极速模式可以获取到;

原文地址:https://www.cnblogs.com/fyjz/p/11905877.html