web项目获取访问者内网Ip
来源:互联网 发布:西安软件新城楼盘 编辑:程序博客网 时间:2024/06/08 15:27
最近做一个项目,需要收集操作系统的用户的内网ip地址。(由于需要知道哪个员工操作的系统,有的多个员工公用一个账号,所以很多时候不知道到底谁操作的),所以需要获取内网ip。但是通过常规的request.getHeader(“x-forwarded-for”) 获取到的通常是外网ip。这样整个公司访问系统的ip都一样了。后来想了个办法,通过前端获取用户的内网ip,然后传到后台。
百度了半天,依旧没有找到合适的解决办法,最后还是Google给力。
通过WebRTC可以获取到用户的内网ip和外网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>
有更好方法的大神请多指教
0 0
- web项目获取访问者内网Ip
- Web端获取访问者IP
- python获取访问者ip
- PHP获取访问者IP
- php获取访问者ip
- PHP获取访问者IP
- PHP获取访问者IP
- 获取访问者IP
- PHP获取访问者IP
- Java_util_获取访问者 IP
- 获取访问者IP
- java 反向代理的项目怎么获取访问者IP
- 获取服务器IP和获取访问者IP
- WebService中获取访问者IP
- Jsp 获取访问者IP地址
- 获取访问者ip的方法
- java获取访问者的IP
- Jsp 获取访问者IP地址
- NameValueCollection类读取配置信息
- Android操作HTTP实现与服务器通信
- 安卓使用URL显示网络图片
- codeforces 554B Ohana Cleans Up
- 课程实训--银行储蓄系统
- web项目获取访问者内网Ip
- C语言中的struct用法
- localtime死锁——多线程下fork子进程
- 在 CakePHP 中使用 SimpleExcel 遇到的一个问题小记
- 头文件相互包含导致的错误 解决
- java web 服务器推送技术--comet4j
- 单行函数和多行函数
- mac 安装 openfire
- hadoop总结