获取Chrome扩展中设备的本地IP [英] Get Local IP of a device in chrome extension

查看:1673
本文介绍了获取Chrome扩展中设备的本地IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个Chrome扩展,它应该可以发现并与本地网络中的其他设备进行通信。为了发现它们,需要找出自己的IP地址来找出网络的IP范围来检查其他设备。我坚持如何找到本地机器的IP地址(我不是在谈论本地主机,也不是在谈论暴露于互联网的地址,而是本地网络上的地址)。 基本上,我最喜欢的是获取将在 ifconfig 中输出的内容终端在我的 background.js 中。



Chrome Apps API提供了 chrome.socket 为了能够做到这一点,然而,它不适用于扩展程序。阅读扩展的API 我还没有找到任何似乎使我能够找到的东西本地ip。

我是否错过了某些东西,或者出于某种原因这是不可能的?有没有其他方法可以发现网络上的其他设备,这也可以做得很好(因为它们会处于相同的IP范围),但2012年12月有传言说可能有扩展的发现API没有任何东西似乎还没有存在。



有人有什么想法吗?

解决方案


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



示例:

  //示例(使用下面的函数).getLocalIPs(function(ips){// <! -  ips是一个本地IP地址数组document.body.textContent ='本地IP地址:\ n'+ ips.join('\\\
');}); function getLocalIPs(callback){var ips = []; var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection; var pc = new RTCPeerConnection({//不要指定任何stun / turn服务器,否则你//也会找到你的公共IP地址。iceServers:[]}); //添加媒体线,这是激活候选人聚会所需要的。 pc.createDataChannel(); //找到候选人时触发onicecandidate。 pc.onicecandidate = function(e){if(!e.candidate){//候选收集已完成。 pc.close();回调(IPS);返回; } var ip = /^candidate:.+(\S +)\ d + typ / .exec(e.candidate.candidate)[1]; if(ips.indexOf(ip)== -1)//避免重复条目(tcp / udp)ips.push(ip); };函数onerror(){});}

 < body style =white-space:pre> IP地址将打印在这里...< / body>  

I am working on a chrome extension which is supposed to discover and then communicate with other devices in a local network. To discover them it needs to find out its own IP-address to find out the IP-range of the network to check for other devices. I am stuck on how to find the IP-address of the local machine (I am not talking about the localhost nor am I talking about the address that is exposed to the internet but the address on the local network). Basically, what I would love is to get what would be the ifconfig output in the terminal inside my background.js.

The Chrome Apps API offers chrome.socket which seems to be able to do this, however, it is not available for extensions. Reading through the API for extensions I have not found anything that seems to enable me to find the local ip.

Am I missing something or is this impossible for some reason? Is there any other way to discover other devices on the network, that would also do just fine (as they would be on the same IP-range) but while there are some rumors from December of 2012 that there could be a discovery API for extensions nothing seems to exist yet.

Does anybody have any ideas?

解决方案

You can get a list of your local IP addresses (more precisely: The IP addresses of your local network interfaces) via the WebRTC API. This API can be used by any web application (not just Chrome extensions).

Example:

// 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() {});
}

<body style="white-space:pre"> IP addresses will be printed here... </body>

这篇关于获取Chrome扩展中设备的本地IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆