Get Local IP of a device in chrome extension

2019-02-01 11:26发布

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?

4条回答
小情绪 Triste *
2楼-- · 2019-02-01 11:55

see http://developer.chrome.com/extensions/webRequest.html for detail, my code example:

// 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: []
  },
  []
);
查看更多
疯言疯语
3楼-- · 2019-02-01 12:03
> chrome.system.network.getNetworkInterfaces(function(interfaces){
>       console.log(interfaces);    }); 

manifest-permissions:

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

Works for me too and it replies:

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

0 : {address: "xxxx", name: "en0", prefixLength: 64}

1 : {address: "192.168.86.100", name: "en0", prefixLength: 24}

2 : {address: "xxxx", name: "awdl0", prefixLength: 64}

3 : {address: "xxxx", name: "utun0", prefixLength: 64}

length : 4

查看更多
何必那么认真
4楼-- · 2019-02-01 12:04

After some searching I've found that similar question was answered before. This API is inaccessible from extension, but available for chrome apps:

use chrome.system.network.getNetworkInterfaces.

This will return an array of all interfaces with their IP address.

This is my sample code:

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

manifest-permissions:

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

查看更多
Viruses.
5楼-- · 2019-02-01 12:07

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>

查看更多
登录 后发表回答