一,首先一個獲取當前客戶端的IP地址的網(wǎng)址:http://net.ipcalf.com/ 顯示的是一段機器碼
Your network IP is:
fff83651-0b2a-4213-9d4a-18a9e88572c4.local
Make the locals proud.
二,Chrome和Firefox瀏覽器會默認隱藏內(nèi)網(wǎng)的IP地址,所以需要設置一些額外的東西才可以將IP地址顯示出來
Chrome:在Chrome瀏覽器地址欄中輸入:chrome://flags/
搜索#enable-webrtc-hide-local-ips-with-mdns 該配置 并將屬性改為 disabled
之后按照chrome的指示重啟一下IP地址就正常了。
但是chrome更新到86版本之后就找不到#enable-webrtc-hide-local-ips-with-mdns 這個配置項了,
三,替代的解決方案是安裝一個WebRTC Network Limiter插件,然后選擇第二項,這樣的話本地ip地址就又能正常的顯示出來了

image.png
刷新頁面

image.png
js腳本
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaScript獲取客戶端IP[利用新浪接口]</title>
</head>
<body>
<script>
//創(chuàng)建RTCPeerConnection接口
let conn = new RTCPeerConnection({
iceServers: []
})
let noop = function(){}
conn.onicecandidate = function(ice){
if (ice.candidate){
//使用正則獲取ip
let ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
let ip_addr = ip_regex.exec(ice.candidate.candidate)[1];
console.log(ip_addr);
conn.onicecandidate = noop
}
}
//隨便創(chuàng)建一個叫狗的通道(channel)
conn.createDataChannel('dog')
//創(chuàng)建一個SDP協(xié)議請求
conn.createOffer(conn.setLocalDescription.bind(conn),noop)
</script>
</body>
</html>