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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • DDoS攻击模拟与Metasploitable渗透

    DDoS攻击模拟与Metasploitable渗透DDoS攻击模拟与Metasploitable渗透Part.1DDoS介绍和发展史1.DDoS:DistributedDenialofService,分布式拒绝服务攻击。2.带宽消耗型攻击,所谓带宽消耗型攻击就是攻击者将提供网络服务的正常的带宽消耗殆尽,带宽消耗殆尽后就会造成网络堵塞,从而当正常的用户与服务器进行通信时无法提供正常服务。举vps的例子,vps服务器一般都有固定的带宽,…

    2022年10月21日
    0
  • 拉姆达语法_map的常用方法

    拉姆达语法_map的常用方法importorg.apache.commons.collections4.MapUtils;@OverridepublicList<Map<String,Object>>getGuidelineSceneStatistical(StringdeptCode){List<Map<String,Object>>mapList=newArrayList<>();List<

    2022年9月2日
    2
  • Java代码实现文件上传「建议收藏」

    Java代码实现文件上传「建议收藏」Java代码实现文件上传在文件上传过程中,文件是以流的形式从浏览器提交到服务端的。一般情况下采用Apache公司的开源文件上传组件common-fileupioad来进行文件的上传。由于common-fileupioad依赖于common-io,所以还要下载common-io这个包。准备工作:下载jar包,建立项目,把包导入lib目录,将lib添加到项目输出目录,配置好Tomcat。我们只…

    2022年5月14日
    49
  • 贴片电阻识别_贴片电阻型号识别方法

    贴片电阻识别_贴片电阻型号识别方法2019独角兽企业重金招聘Python工程师标准>>>…

    2022年8月21日
    3
  • 几款.Net加密/加壳工具的比较

    几款.Net加密/加壳工具的比较前言  使用过.NET的程序员都知道,.NET是一个巨大的跨时代进步,它开发效率高、功能强、界面观、耐用、新的语言C#已经提交为行业规范、CLR共公运行库资源丰富,这所有的特点标志着它成为主流编程语言是必然的。     可是它也有一个缺点,那就是编译好的程序集可以完全反编译成源代码,这给一些不法份子提供了很好的机会,试想想,您辛苦的劳动成果就这样给了别人;所以如何保护我们的知识产

    2022年6月27日
    37
  • Ajax中responseText解析json格式数据

    Ajax中responseText解析json格式数据解决ajax处理服务器端返回结果responseText中是JSON的数据。第一,json格式的文件内容如下:{"city":"ShangHai",&q

    2022年7月3日
    22

发表回复

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

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