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


相关推荐

  • 目标检测 | OHEM

    目标检测 | OHEM 这里主要说下该论文的hardmining过程: 先上图,如Figure2所示: 从图中可以看出,本文的亮点在于在每次迭代中,较少训练样本下,如何hardnegativemining,来提升效果。 即针对Fast-RCNN框架,在每次minibatch(1张或者2张)训练时加入在线筛选hardregion的策略,达到新的SoA。需要注意的是,这个OHEM适合于b…

    2022年5月28日
    36
  • databus教程_搭建区观察记录表

    databus教程_搭建区观察记录表最近公司因需要同步oracle数据到mysql,调研了Datax对于大数据量的同步代价有些大。开源的databus需要对源码做二次开发,才可以使用,前期我们搭建后,用自带的person表做了测试。确认可行后研发更改了源码。准备工作:1.配制gradle和java2.ojdbc6-11.2.0.2.0.jar放到如下目录:databus-master/sandbox-repo/com/oracle/ojdbc6/11.2.0.2.0/更改defaultEnvironment.gradl

    2022年8月31日
    5
  • jdk中的哪些源码是必看的_jdk源码阅读顺序

    jdk中的哪些源码是必看的_jdk源码阅读顺序一点一点看JDK源码(〇)liuyuhang原创,未经允许进制转载写在前面:几乎所有的大神都会强调看源码,也强调源码的重要性;但是如何看源码,源码看什么?看了什么用?看了怎么用?困扰很多人,

    2022年8月6日
    9
  • 分布式文件存储——简介

    分布式文件存储——简介1、分布式文件系统种类简介:常见的分布式文件系统有:GlusterFS、GoogleFS、FastDFS、TFS等,各自适用的领域不同,它们都不是系统级的分布式文件系统,而是应用级的分布式文件存储服务。现在比较火的并且好用的应该就是OSS了,阿里云对象存储服务(ObjectStorageService,简称OSS),是阿里云提供的海量、安全、低成本、高可靠的云存储服务。OSS具有与…

    2022年5月1日
    58
  • navicat生成激活码错误(注册激活)[通俗易懂]

    (navicat生成激活码错误)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月28日
    587
  • 解决ajax跨域问题【5种解决方案】「建议收藏」

    解决ajax跨域问题【5种解决方案】「建议收藏」什么是跨域问题?跨域问题来源于JavaScript的”同源策略”,即只有协议+主机名+端口号(如存在)相同,则允许相互访问。也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源。跨域问题是针对JS和ajax的,html本身没有跨域问题。查看浏览器开发者工具Console报错:Failedtoloadhttp://a.a.com:8080/A/…

    2022年8月24日
    10

发表回复

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

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