我可以在不使用外部服务的情况下在Chrome应用程序中获得我的IP地址吗? [英] Can I get my IP address in a chrome app without using an external service?
问题描述
我正在构建一个 Chrome应用并创建了一个 UDP套接字通过。
<有没有办法在不使用外部服务的情况下检索自己的 IP地址?
我的意思是自己的IP地址:客户端和服务器在同一个网络上。 chrome应用需要使用自己的本地IP地址应答 UDP广播。
实际上,这个用例实际上是一个 Chrome套接字API 。但不幸的是,我只收到下面的回调对象:
Object {paused:false,persistent:false,socketId:17}
,它忽略了 localAddress 属性。它是根据 SocketInfo
对象中的(可选)属性/ sockets_udp#type-SocketInforel =nofollow>文档。这实际上是我正在运行的代码: chrome.sockets.udp.create({},function(socketInfo){
chrome.sockets.udp.getInfo(socketInfo.socketId,function(detailedInfo){
ipAddress = detailedInfo.localAddress;
console.debug(detailedInfo); // containts no`localAddress`
});
});
我也不认为我缺少任何 manifest-permissions ,因为 API文档。这是我使用的:
sockets:{
udp:{
send :*,
bind:*
}
}
当我使用 Python 时,我可以如下实现该目标:
import socket
ip_address = socket.gethostbyname(socket.gethostname())
很好!
解决方案事实证明我一直在使用错误的API。对于我的用例,我需要使用 chrome.system.network.getNetworkInterfaces 。
这将返回一个包含 IP地址的所有接口的数组。
是我的示例代码:
chrome.system.network.getNetworkInterfaces(function(interfaces){
console.log接口);
});
清单权限:
permissions:[
system.network
],
考虑到评论,一个完美的解决方案不容易提供,因为必须找到正确的UDP端口接口。
在这一点上,完美匹配接口的唯一方法是使用 bind
所有接口的所有地址 getNetworkInterfaces
。然后您就可以确切知道使用哪个接口接听电话,您可以使用正确的IP地址进行响应。
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!
解决方案 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.
这篇关于我可以在不使用外部服务的情况下在Chrome应用程序中获得我的IP地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!