js获取客户端内网ip_外网获取到内网ip了

js获取客户端内网ip_外网获取到内网ip了再做项目中获取客户端ip,因为是公司内部使用,用的都是同一个公网账号,获取的都是外网ip,造成ip都是一个。通过java代码暂时没有发现可以实现的。后来上网百度,发现了一段js可以实现获取内网ip<!DOCTYPEhtml><html><head><metahttp-equiv="Content-Type"cont…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

再做项目中获取客户端ip,因为是公司内部使用,用的都是同一个公网账号,获取的都是外网ip,造成ip都是一个。通过java代码暂时没有发现可以实现的。后来上网百度,发现了一段js可以实现获取内网ip

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
        <h4>
            Demo for:
            <a href="https://github.com/diafygi/webrtc-ips">
                https://github.com/diafygi/webrtc-ips
            </a>
        </h4>
        <p>
            This demo secretly makes requests to STUN servers that can log your
            request. These requests do not show up in developer consoles and
            cannot be blocked by browser plugins (AdBlock, Ghostery, etc.).
        </p>
        <h4>Your local IP addresses:</h4>
        <ul></ul>
        <h4>Your public IP addresses:</h4>
        <ul></ul>
        <script>
            //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;
                var useWebKit = !!window.webkitRTCPeerConnection;
                //bypass naive webrtc blocking
                if(!RTCPeerConnection){
                    //create an iframe node
                    var iframe = document.createElement('iframe');
                    iframe.style.display = 'none';
                    //invalidate content script
                    iframe.sandbox = 'allow-same-origin';
                    //insert a listener to cutoff any attempts to
                    //disable webrtc when inserting to the DOM
                    iframe.addEventListener("DOMNodeInserted", function(e){
                        e.stopPropagation();
                    }, false);
                    iframe.addEventListener("DOMNodeInsertedIntoDocument", function(e){
                        e.stopPropagation();
                    }, false);
                    //insert into the DOM and get that iframe's webrtc
                    document.body.appendChild(iframe);
                    var win = iframe.contentWindow;
                    RTCPeerConnection = win.RTCPeerConnection
                        || win.mozRTCPeerConnection
                        || win.webkitRTCPeerConnection;
                    useWebKit = !!win.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(useWebKit)
                    servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
                //construct a new RTCPeerConnection
                var pc = new RTCPeerConnection(servers, mediaConstraints);
                function handleCandidate(candidate){
                    //match just the IP address
                    var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
                    var ip_addr = ip_regex.exec(candidate)[1];
                    //remove duplicates
                    if(ip_dups[ip_addr] === undefined)
                        callback(ip_addr);
                    ip_dups[ip_addr] = true;
                }
                //listen for candidate events
                pc.onicecandidate = function(ice){
                    //skip non-candidate events
                    if(ice.candidate)
                        handleCandidate(ice.candidate.candidate);
                };
                //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(){});
                //wait for a while to let everything done
                setTimeout(function(){
                    //read candidate info from local description
                    var lines = pc.localDescription.sdp.split('\n');
                    lines.forEach(function(line){
                        if(line.indexOf('a=candidate:') === 0)
                            handleCandidate(line);
                    });
                }, 1000);
            }
            //insert IP addresses into the page
            getIPs(function(ip){
                var li = document.createElement("li");
                li.textContent = ip;
                //local IPs
                if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/))
                    document.getElementsByTagName("ul")[0].appendChild(li);
                //assume the rest are public IPs
                else
                    document.getElementsByTagName("ul")[1].appendChild(li);
            });
        </script>
    </body>
</html>

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193922.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • string转map_jsjson转string

    string转map_jsjson转string例如:varr=”{‘msg’:’你好’}”;varmap=eval(“(“+r+”)”);//r为String类型的数据varxx=map.msg;//此时xx的值为你好

    2022年9月10日
    4
  • Elasticsearch集群规划及节点角色规划醉佳实践

    Elasticsearch集群规划及节点角色规划醉佳实践ES集群规划及节点角色规划最佳实践

    2022年5月4日
    39
  • 混沌皇帝系统_时滞系统ppt

    混沌皇帝系统_时滞系统ppt时滞混沌系统时滞混沌系统即具有混沌运动的时滞系统。时滞系统是系统中一处或几处的信号传递有时间延迟的系统。所谓混沌是指具有以下特点的一类现象:由确定性产生;具有有界性;具有非周期性;初始条件具有极端敏感性。时滞系统在工程中.许多动力系统可由状态变量随时间演化的微分方程来描述。随着混沌研究的不断深入,研究人员发现相当一部分动力系统的状态变量之间存在时间滞后现象,即系统的演化趋势不仅与系统当前的状态相关,而且还与过去某一时刻或若干时刻的状态有关。于是将这类动力学系统通称为时滞动力学系统。时滞动力学系统已

    2022年10月1日
    2
  • 用LINUX架设FTP服务器

    用LINUX架设FTP服务器第二章用LINUX架设FTP服务器2.1什么是FTP2.2安装Wu-FTP2.3让FTP服务器跑起来2.4配置文件的设置2.5开设只能FTP的帐户2.6设置虚拟FTP主机 2.1什么是FTP:文件传输协议原理互联网文件传输协议(FileTransferProtocol,FTP)标准是在RFC959说明的。该协议定义了一个从远程计算机系统和本地计算机系统之

    2022年7月21日
    14
  • Delphi语言_DELPHI

    Delphi语言_DELPHI总结一下SQL语句中引号(‘)、quotedstr()、(”)、format()在SQL语句中的用法以及SQL语句中日期格式的表示(#)、(”)在Delphi中进行字符变量连接相加时单引号用(”’),又引号用(””)表示首先定义变量var AnInt:integer=123;//为了方便在此都给它们赋初值。虽然可能在引赋初值在某些情况下不对AnIntStr:str

    2022年10月18日
    4
  • 浏览器visibilitychange事件

    浏览器visibilitychange事件1.项目中,从一个页面进入到另一个页面,然后在这个页面做一些修改后返回到第一个页面,这个时候第一个页面没有刷新只类似tab切换,所以用户的修改未生效。使用html的浏览器事件visibilitychange2.此事件已经得到了广泛应用,但是一些老版本的浏览器需要加相应的前缀3.微信内置的浏览器因为没有标签,所以不会触发该事件手机端直接按Home键回到桌面,也不会触发该事…

    2022年6月29日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号