浏览器输入 about:config
搜索配置 media.peerconnection.enabled 改为false ( 刷新程序,IP正常显示 )
浏览器输入:chrome://flags/#enable-webrtc-hide-local-ips-with-mdns
把 Anonymize local IPs exposed by WebRTC 设置为 disabled ( 刷新程序,IP正常显示 )
浏览器输入: edge://flags/#enable-webrtc-hide-local-ips-with-mdns
把 Anonymize local IPs exposed by WebRTC 设置为 disabled ( 刷新程序,IP正常显示 )
1.设置完成后要重启浏览器
2.若没有查到相对应的设置,请检查浏览器版本更新
// 获取ip地址getUserIP(onNewIP) { console.log('获取ip')let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnectionlet pc = new MyPeerConnection({iceServers: []})let noop = function () {}let localIPs = {}let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/gfunction iterateIP (ip) {if (!localIPs[ip]) onNewIP(ip)localIPs[ip] = true}pc.createDataChannel('')pc.createOffer().then(function (sdp) {sdp.sdp.split('\n').forEach(function (line) {if (line.indexOf('candidate') < 0) returnline.match(ipRegex).forEach(iterateIP)})pc.setLocalDescription(sdp, noop, noop)}).catch(function (reason) {// An error occurred, so handle the failure to connect})// seen for candidate eventspc.onicecandidate = function (ice) {if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) returnice.candidate.candidate.match(ipRegex).forEach(iterateIP)}},
var _that = thisthis.getUserIP(function(ip){console.log("得到的本地IP :" + ip) // 得到的本地IP :192.168.21.23console.log(_that.loginForm) _that.loginForm.userIP = ip //这里不能用this 因为this指向的是这个回调函数的this})