1. 获取内网ip,即路由器分配的地址,局域网内部具有唯一性
http://net.ipcalf.com/ 显示本机内网ip地址
此网站可以获取用户的内网ip,但是必须设置浏览器的配置,现在浏览器都默认隐藏ip地址
不设置时显示和设置后显示
谷歌浏览器设置
chrome://flags/#enable-webrtc-hide-local-ips-with-mdns
但是也有人说
360浏览器设置
se://flags/#enable-webrtc-hide-local-ips-with-mdns
火狐浏览器
在地址栏中输入:absout:config
搜索配置media.peerconnection.ice.obfuscate_host_addresses 改为false
- 网址代码提取
var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;if(RTCPeerConnection)(function(){var rtc =newRTCPeerConnection({iceServers:[]});if(1|| window.mozRTCPeerConnection){
rtc.createDataChannel('',{reliable:false});};
rtc.onicecandidate=function(evt){if(evt.candidate)grepSDP("a="+ evt.candidate.candidate);};
rtc.createOffer(function(offerDesc){grepSDP(offerDesc.sdp);
rtc.setLocalDescription(offerDesc);},function(e){ console.warn("offer failed", e);});var addrs = Object.create(null);
addrs["0.0.0.0"]=false;functionupdateDisplay(newAddr){if(newAddr in addrs)return;else addrs[newAddr]=true;var displayAddrs = Object.keys(addrs).filter(function(k){return addrs[k];});
console.log(displayAddrs.join(" or perhaps ")||"n/a",'ip地址');}functiongrepSDP(sdp){var hosts =[];
sdp.split('\r\n').forEach(function(line){if(~line.indexOf("a=candidate")){var parts = line.split(' '),
addr = parts[4],
type = parts[7];if(type ==='host')updateDisplay(addr);}elseif(~line.indexOf("c=")){var parts = line.split(' '),
addr = parts[2];updateDisplay(addr);}});}})();
2. 获取外网ip,即公网ip,具有世界范围的唯一性
http://api.ipify.org 此接口可以直接获取公网ip地址
axios.get('http://api.ipify.org').then(res=>{if(res.data){
console.log(res.data,'公网ip')}}).catch((e)=>{})
https://ipv4.icanhazip.com/ 也可以
3. 参考链接
https://blog.csdn.net/qq_21901233/article/details/107369852
https://www.cnblogs.com/Denny_Yang/p/16827797.html
https://blog.csdn.net/hjue/article/details/90442859
4. 有很多方法引入了搜狐的、腾讯的javascript链接,我试过了都不管用,搜狐一直403 forbidden
版权归原作者 weixin_42255542 所有, 如有侵权,请联系我们删除。