zoukankan      html  css  js  c++  java
  • 根据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浏览器的极速模式可以获取到;

  • 相关阅读:
    Centos系统修改时间临时和永久生效
    Oracle数据泵恢复用户数据实例
    Oracle修改用户密码错误次数及解锁用户
    MongoDB的启动与停止
    pip常用命令
    mysql执行拉链表操作
    Python实现人脸识别
    Mysql触发器学习
    WordCount程序
    Java学习--多态
  • 原文地址:https://www.cnblogs.com/fyjz/p/11905877.html
Copyright © 2011-2022 走看看