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

时光总嘲笑我的痴心妄想 提交于 2019-12-20 03:42:06

问题


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:


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.




回答2:


An unconnected socket does not have a local address yet, unless it is specifically bound to an address . It only gets a local and remote address once the socket is connected (TCP) or you send data through the socket (UDP). And it only gets the IP address of the local machine, whereas an external service sees the internet-facing external address of the router if you are behind some NAT router (i.e. most home users).




回答3:


The callback you provide to udp.create() is called when the socket has been initially created, not when the socket receives packets. At creation time, the socket is not associated with any localAddress or localPort yet. You have to call udp.bind() to establish the specific localPort (and optionally a specific localAddress) that the socket will listen for packets on. If you bind() to "0.0.0.0" then the socket will listen on all local IPs.

When a new packet arrives, the udp.onReceive event is triggered. The packet was received by a specific localAddress, however the Chrome API does not provide a means of directly querying which localAddress received the packet (the underlying recvfrom() socket function does not provide that information). To discover the receiving local IP, you would have to either:

  1. bind the socket to a specific localAddress, as that will be the only local IP that can trigger the onReceive event for that socket.

  2. check the list of interfaces reported by chrome.system.network.getNetworkInterfaces(). If 1 interface is reported, that will be the interface receiving all packets, so you can use its address. However, if 2+ interfaces are reported, you will have to parse the network prefix from the packet's remoteAddress and compare that value to the prefix of each reported interface until you find a match (assuming the sender is broadcasting from the same network subnet that the local machine is connected to, and not broadcasting across subnet boundaries).

That being said, all of this is only relevant if you need to put your local IP in the broadcast reply's data payload. Say there are 3 local interfaces, and the broadcast packet is received on interface 2. Replying with the localAddress of interface 1 or 3 would obviously be wrong, as they are on different networks than the broadcaster. However, if you DO NOT need to put your localAddress in the reply's payload, then the broadcaster can simply look at the remoteAddress of the reply to know what your IP address is. Then you do not need to figure out your local IP at all, the OS will handle everything for you when you send the reply to the remoteAddress/remotePort that you received the broadcast from.



来源:https://stackoverflow.com/questions/27533631/can-i-get-my-ip-address-in-a-chrome-app-without-using-an-external-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!