JS 越过代理获取客户端IP
火狐、谷歌、360等浏览器解决方法(IE浏览器除外)
//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 using an iframe if (!RTCPeerConnection) { //NOTE: you need to have an iframe in the page right above the script tag // //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe> //<script>...getIPs called in here... // var win = iframe.contentWindow; RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection; useWebKit = !!win.webkitRTCPeerConnection; } //minimal requirements for data connection var mediaConstraints = { optional: [{ RtpDataChannels: true }] }; var 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}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/ 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); } //将获取的ip保存到cookie中 getIPs(function (ip) { var CookieIP = { getCookie: function (cookieName) { if (document.cookie.length > 0) { var c_start = document.cookie.indexOf(cookieName + "="); if (c_start !== -1) { c_start = c_start + cookieName.length + 1; var c_end = document.cookie.indexOf(";", c_start); if (c_end === -1) c_end = document.cookie.length; return decodeURIComponent(document.cookie.substring(c_start, c_end)); } } return ""; }, setCookie: function (cookieName, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); var expire_cookie = cookieName + "=" + decodeURI(value) + ";expires=" + exdate.toUTCString(); document.cookie = expire_cookie; } }; CookieIP.setCookie('localip', ip, 90); });
IE浏览器JS脚本获取客户端IP
注:因为上面方法在IE浏览器使用时,因为IE浏览器不支持这个js获取ip的WebRTC请求,所以需要单独写一个js,并且IE获取ip需要将Internet中安全选项中的ActiveX权限启用才可以获取到。
<html> <head> <meta charset="UTF-8"> <title>兰光数据驱动平台 - 登录</title> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" /> <object id="locator" style="position:absolute" classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></object> <object id="foo" style="position:absolute" classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></object> <script language="javascript" type="text/javascript"> try { var service = locator.ConnectServer(); var IPAddr; service.Security_.ImpersonationLevel = 3; service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration'); } catch (err) { } </script> <script language="javascript" type="text/javascript" event="OnObjectReady(objObject,objAsyncContext)" for="foo"> if (objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true) { if (objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined") IPAddr = objObject.IPAddress(0); if(objObject.IPAddress(0) == null) IPAddr = "ActiveX权限未启用"; } </script> <script language="javascript" type="text/javascript" event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for="foo"> var CookieIP = { getCookie: function (cookieName) { if (document.cookie.length > 0) { var c_start = document.cookie.indexOf(cookieName + "="); if (c_start !== -1) { c_start = c_start + cookieName.length + 1; var c_end = document.cookie.indexOf(";", c_start); if (c_end === -1) c_end = document.cookie.length; return decodeURIComponent(document.cookie.substring(c_start, c_end)); } } return ""; }, setCookie: function (cookieName, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); var expire_cookie = cookieName + "=" + decodeURI(value) + ";expires=" + exdate.toUTCString(); document.cookie = expire_cookie; } }; CookieIP.setCookie('localip', IPAddr, 90); </script> </head> <body> </body> </html>