如何使用 JavaScript 获取本地/内部 IP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32837471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:48:44  来源:igfitidea点击:

How to get local/internal IP with JavaScript

javascriptnetwork-programmingipwebrtc

提问by Pascal Raszyk

How can I get the local client IP using WebRTC.

如何使用WebRTC获取本地客户端 IP 。

I don't need the REMOTE_ADDRof the client but his local network IP. I've seen this before on websites like sharedrop.com, it recognizes computer within the same network using WebRTC.

我不需要REMOTE_ADDR客户端的,但需要他的本地网络 IP。我以前在网站上看到过这个sharedrop.com,它使用 WebRTC 识别同一网络中的计算机。

In PHP I do this to get the clients remote IP:

在 PHP 中,我这样做是为了获取客户端的远程 IP:

<?php
  echo $_SERVER["REMOTE_ADDR"]; // which would return 72.72.72.175
?>

I looked through stackoverflow but every question is answered using the remote addr.

我查看了stackoverflow,但使用远程地址回答了每个问题。

How can I get my local IP (192.168.1.24 for example) with JavaScript instead of the remote addr.

如何使用 JavaScript 而不是远程地址获取我的本地 IP(例如 192.168.1.24)。

回答by mido

where I took code from --> Source

我从哪里获取代码 -->源代码

You can find a demo at --> Demo

您可以在 --> Demo找到演示

I have modified the source code, reduced the lines, not making any stun requests since you only want Local IP not the Public IP, the below code works in latest Firefox and Chrome:

我修改了源代码,减少了行数,没有提出任何令人震惊的请求,因为您只想要本地 IP 而不是公共 IP,以下代码适用于最新的 Firefox 和 Chrome:

    window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;   //compatibility for firefox and chrome
    var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};      
    pc.createDataChannel("");    //create a bogus data channel
    pc.createOffer(pc.setLocalDescription.bind(pc), noop);    // create offer and set local description
    pc.onicecandidate = function(ice){  //listen for candidate events
        if(!ice || !ice.candidate || !ice.candidate.candidate)  return;
        var myIP = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
        console.log('my IP: ', myIP);   
        pc.onicecandidate = noop;
    };

what is happening here is, we are creating a dummy peer connection, and for the remote peer to contact us, we generally exchange ice candidates with each other. And reading the ice candiates we can tell the ip of the user.

这里发生的事情是,我们正在创建一个虚拟的对等连接,并且为了让远程对等与我们联系,我们通常会相互交换冰候选。阅读冰候选者,我们可以知道用户的 ip。

回答by kigiri

You can use this version on modern browser (with Promisesand async / await)

您可以在现代浏览器上使用此版本(带有Promisesasync / await

// minified onliner, 219b
const ip = await new Promise((s,f,c=new RTCPeerConnection(),k='candidate')=>(c.createDataChannel(''),c.createOffer(o=>c.setLocalDescription(o),f),c.onicecandidate=i=>i&&i[k]&&i[k][k]&&c.close(s(i[k][k].split(' ')[4]))))
// cleaned, 363b
const ip = await new Promise((resolve, reject) => {
  const conn = new RTCPeerConnection()
  conn.createDataChannel('')
  conn.createOffer(offer => conn.setLocalDescription(offer), reject)
  conn.onicecandidate = ice => {
    if (ice && ice.candidate && ice.candidate.candidate) {
      resolve(i.candidate.candidate.split(' ')[4])
      conn.close()
    }
  }
})