如何用 JavaScript 获取 IP 地址
直接获取
- WebRTC API
async function getIP() { const configuration = { iceServers: [ { urls: [\'stun:stun.l.google.com:19302\'] } ] }; const peerConnection = new RTCPeerConnection(configuration); const iceCandidate = await new Promise((resolve) => { peerConnection.onicecandidate = (e) => { if (e.candidate && e.candidate.type === \'srflx\') { resolve(e.candidate.address); } }; }); peerConnection.close(); return iceCandidate; }