【问题标题】:Get Local IP of a device in chrome extension在 chrome 扩展中获取设备的本地 IP
【发布时间】:2013-09-02 11:37:36
【问题描述】:

我正在开发一个 chrome 扩展程序,它应该能够发现并与本地网络中的其他设备进行通信。要发现它们,它需要找出自己的 IP 地址以找出网络的 IP 范围以检查其他设备。我被困在如何找到本地机器的 IP 地址上(我不是在谈论本地主机,也不是在谈论暴露在互联网上的地址,而是在本地网络上的地址)。 基本上,我希望在我的background.js 内的终端中获得ifconfig 输出。

Chrome 应用程序 API 提供 chrome.socket 似乎可以做到这一点,however, it is not available for extensions。通读the API for extensions 我没有发现任何似乎可以让我找到本地ip 的东西。

是我遗漏了什么还是出于某种原因这是不可能的?有没有其他方法可以发现网络上的其他设备,这也很好(因为它们会在同一个 IP 范围内),但是从 2012 年 12 月开始有传言说可能会有一个用于扩展的发现 API似乎什么都不存在。

有人有什么想法吗?

【问题讨论】:

  • 如果有多个地址,因为机器有多个本地网络接口怎么办?
  • 我想拥有所有的地址,前提是可以得到任何地址。
  • 你明白了吗?
  • 指向“扩展 API”的链接已过时且不再有效。考虑将其更新为:developer.chrome.com/docs/extensions/reference。指向“chrome.socket”和“background.js”的链接也很烂(不确定更新的等价物)。 “ifconfig”链接是德语的,但存在等效的英语。 en.wikipedia.org/wiki/Ifconfig

标签: google-chrome networking google-chrome-extension


【解决方案1】:

您可以通过 WebRTC API 获取本地 IP 地址列表(更准确地说:本地网络接口的 IP 地址)。此 API 可用于任何网络应用程序(不仅仅是 Chrome 扩展程序)。

例子:

// Example (using the function below).
getLocalIPs(function(ips) { // <!-- ips is an array of local IP addresses.
    document.body.textContent = 'Local IP addresses:\n ' + ips.join('\n ');
});

function getLocalIPs(callback) {
    var ips = [];

    var RTCPeerConnection = window.RTCPeerConnection ||
        window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

    var pc = new RTCPeerConnection({
        // Don't specify any stun/turn servers, otherwise you will
        // also find your public IP addresses.
        iceServers: []
    });
    // Add a media line, this is needed to activate candidate gathering.
    pc.createDataChannel('');
    
    // onicecandidate is triggered whenever a candidate has been found.
    pc.onicecandidate = function(e) {
        if (!e.candidate) { // Candidate gathering completed.
            pc.close();
            callback(ips);
            return;
        }
        var ip = /^candidate:.+ (\S+) \d+ typ/.exec(e.candidate.candidate)[1];
        if (ips.indexOf(ip) == -1) // avoid duplicate entries (tcp/udp)
            ips.push(ip);
    };
    pc.createOffer(function(sdp) {
        pc.setLocalDescription(sdp);
    }, function onerror() {});
}
&lt;body style="white-space:pre"&gt; IP addresses will be printed here... &lt;/body&gt;

【讨论】:

  • 这应该是公认的答案!!像魅力一样工作!我整天都在寻找这个,但每个人都说向 www 服务器发送请求,但感谢@Rob W,他解决了它:-)
  • 我看到您使用的是 ip 数组。它是否可能返回多个 IP,为什么?只是为了额外的IPV6地址吗?如何只过滤掉一个有用的 IPV4 地址?它只是数组的第一个元素还是我必须全部解析?
  • @Zorgatone 它可能超过 1,例如如果您通过电缆、无线、VPN 等连接。活动的网络接口将显示在列表中。所有 IP 地址都可能有用。请注意,我的方法只显示本地地址(例如 192.168.1.1),如果您要查找公共 IP 地址,则必须在 iceServers 列表中指定 STUN 服务器。
  • 不,我不需要公开的,谢谢。好吧,我正在开发一个移动应用程序。因此,在移动设备上,您通常只有一个网络。如果没有 IPV6 地址而只有 IPV4,我会选择第一个。谢谢! :)
  • 由于某种原因,当我将它放入我的代码中时,回调永远不会受到影响。我可以在 getLocalIPs 之前中断,但是当我恢复时它只是跳过回调函数。
【解决方案2】:

经过一番搜索,我发现了类似的问题was answered before。 此 API 无法从扩展程序中访问,但可用于 chrome 应用程序:

使用chrome.system.network.getNetworkInterfaces

这将返回一个包含所有接口及其 IP 地址的数组。

这是我的示例代码:

chrome.system.network.getNetworkInterfaces(function(interfaces){ console.log(interfaces); });

清单权限:

"permissions": [ "system.network" ], ...

【讨论】:

  • 其实可以查询你本地网络接口的地址。我在stackoverflow.com/a/29514292/938089 发布了一个示例。
  • 我已经使用适当的 API 更新了答案。您的解决方案看起来不错,但这不是 WebRTC 实现中的错误吗?在 Firefox 中,NoScript 将其检测为 XSS 尝试阻止了 sn-p 的执行。
  • 不;能够获取 IP 地址是 WebRTC 的一项功能。为了与其他对等体进行通信,两个对等体都必须知道彼此的地址。如果两者都在同一个 Intranet 上,则知道本地地址就足够了。否则需要 STUN 来获取公共 IP 地址。
  • 这不适用于扩展,它会给出以下错误:'system.network' is only allowed for packaged apps, but this is a extension.
【解决方案3】:
> chrome.system.network.getNetworkInterfaces(function(interfaces){
>       console.log(interfaces);    }); 

清单权限:

“权限”: [“system.network”],...

对我也有用,它会回复:

(4) [{…}, {…}, {…}, {…}]

0 : {地址:“xxxx”,名称:“en0”,前缀长度:64}

1 : {地址:“192.168.86.100”,名称:“en0”,前缀长度:24}

2 : {地址:“xxxx”,名称:“awdl0”,前缀长度:64}

3 : {地址:“xxxx”,名称:“utun0”,前缀长度:64}

长度 : 4

【讨论】:

  • 这给了我'system.network' is only allowed for packaged apps, but this is a extension.
【解决方案4】:

详情请参阅http://developer.chrome.com/extensions/webRequest.html, 我的代码示例:

// get IP using webRequest
var currentIPList = {};
chrome.webRequest.onCompleted.addListener(
  function(info) {
    currentIPList[info.url] = info.ip;
    currentIPList[info.tabId] = currentIPList[info.tabId] || [];
    currentIPList[info.tabId].push(info);
    return;
  },
  {
    urls: [],
    types: []
  },
  []
);

【讨论】:

  • 据我所知,这将获取远程服务器的 IP,而不是客户端自己的 IP 地址。看起来@LukasRuge 正在尝试获取客户端的 IP。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 2013-06-29
相关资源
最近更新 更多