admin管理员组文章数量:1410730
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!
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!
Share edited Dec 17, 2014 at 21:12 asked Dec 17, 2014 at 19:47 user2210287user2210287 3-
If the calling machine has multiple network interfaces,
gethostbyname()
can return multiple local IPs. – Remy Lebeau Commented Dec 18, 2014 at 2:28 - Wrong - It will only return exactly one IP address. Python picks up the interface which is currently the "highest" in the interface list, which is provided by the OS. This is exactly what I want and 99% of all other users as well. – user2210287 Commented Dec 18, 2014 at 2:36
-
Trust me when I say that the "highest" interface in the OS list is NOT always the correct one, and besides that,
gethostbyname()
performs a DNS lookup, which can report unexpected results in some circumstances, even for a local hostname. Thegethostbyname(gethostname())
bination has been widely abused for years because of a misconception that it always returns a local IP, and that is not always true. That is whynetwork.getNetworkInterfaces()
exists, to get the correct and accurate local IPs without involving DNS. – Remy Lebeau Commented Dec 18, 2014 at 2:43
3 Answers
Reset to default 4turns out that I have been using the wrong API. For my use case I needed to use chrome.systemwork.getNetworkInterfaces.
This will return an array of all interfaces with their IP address.
This is my sample code:
chrome.systemwork.getNetworkInterfaces(function(interfaces){
console.log(interfaces);
});
manifest-permissions:
"permissions": [
"systemwork"
],
Considering the ments 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.
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).
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:
bind the socket to a specific
localAddress
, as that will be the only local IP that can trigger theonReceive
event for that socket.check the list of interfaces reported by
chrome.systemwork.getNetworkInterfaces()
. If 1 interface is reported, that will be the interface receiving all packets, so you can use itsaddress
. However, if 2+ interfaces are reported, you will have to parse the network prefix from the packet'sremoteAddress
and pare 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.
本文标签: javascriptCan I get my IP address in a chrome app without using an external serviceStack Overflow
版权声明:本文标题:javascript - Can I get my IP address in a chrome app without using an external service? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744953155a2634199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论