我尝试了很多不同的代码,但它们都只返回用户的本地IP地址。 (例如 192.168.1.2)。
如何使用WebRTC获取用户的公共IPv4地址? (例如 104.200.21.83)
我知道这是可能的,因为即使我使用 VPN,网站也可以显示我泄露的公共 IPv4 地址。
这是一个流行的概念验证:
https://github.com/diafygi/webrtc-ips
对我来说,它只是发回我的私人IP。看来你可以通过更改服务器 IP 来解决这个问题:
https://github.com/diafygi/webrtc-ips/issues/33
您链接到的网站使用服务器地址
stun:stun.l.google.com:19302
,它按预期返回了我的公共地址。
这里有一个 Powershell 小示例,可以满足您的需求:
# get public IP via WebRTC-Protocol in Chrome-browser (even if VPN has another IP)
# [email protected]
$tmpFile = New-TemporaryFile
Start-Process -FilePath 'C:\Program Files\Google\Chrome\Application\chrome.exe' "--no-first-run --headless --disable-gpu --dump-dom https://www.expressvpn.com/webrtc-leak-test" -RedirectStandardOutput $tmpFile -wait
$result = Get-Content $tmpFile
Remove-Item $tmpFile -Force -ea 0
$ip = [regex]::Match($result, '(?ms)(?<=<th class="ip">).*?(?=</th>)').value
cls
write-host "Your public IP: $ip" -f y
不要误会我的意思,但客户端(运行浏览器的地方)可能不知道他的公共IP。我会以某种方式在互联网上的服务器上托管一个小型 php,打印 $_SERVER['REMOTE_ADDR'],从 JS 调用它,将地址收集回 JS var...听起来很脏!
编辑:错误的答案,不知何故让提问的用户错了。