Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get my IP address in a chrome app without using an external service?

I am building a chrome app and created a UDP socket via the chrome socket api.

Is there a way to retrieve your own IP address without using an external service? What I mean by "own IP address": Both client and server are on the same network. The chrome app needs to answer to a UDP broadcast with it's own local IP address.

There is actually a chrome socket API for exactly that use-case. But unfortunately I only receive the following callback object: Object {paused: false, persistent: false, socketId: 17}, which misses the localAddress attribute. It is an (optional) attribute in the SocketInfo object according to the documentation. This is in essence the code I am running:

chrome.sockets.udp.create({}, function(socketInfo){
    chrome.sockets.udp.getInfo(socketInfo.socketId, function (detailedInfo){
        ipAddress = detailedInfo.localAddress;
        console.debug(detailedInfo); // containts no `localAddress`
    });
});

I also do not think that I am missing any manifest-permissions as there are no special permissions described in the API documentation. This is what I use:

"sockets": {
  "udp": {
    "send": "*",
    "bind": "*"
  }
}

When I use Python I can achieve that goal as follows:

import socket
ip_address = socket.gethostbyname(socket.gethostname())

Any help would be great!


1 Answers

turns out that I have been using the wrong API. For my use case I needed to 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"
],

Considering the comments a perfect solution is not easy to provide, as one has to find the correct interface to the UDP-port.

At this point the only way to perfectly match the interface one has to bind all addresses for all interfaces given by getNetworkInterfaces. Then you know exactly which interface was used to receive the call and you can respond with the correct IP address.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!