如何从Firefox扩展代码获取本地IP [英] How to get local IP from Firefox extension code

查看:214
本文介绍了如何从Firefox扩展代码获取本地IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Firefox扩展,使用 nsIServerSocket 来监听套接字连接。我正在寻找扩展代码的方式来编程学习运行Firefox扩展的机器的本地网络地址。这些信息被传送到本地网络上的一个客户端,这样它就可以打开一个套接字连接到扩展。



我的研究到目前为止只有个人希望找到的IP地址加载的网站通过 DNS解析或使用 Java applets ),这将不适合FF扩展。 地理位置API 中的Mozilla开发者页面提到:


数据交换包括WiFi接入点数据,接入令牌(类似于2周的cookie)和用户的IP地址 < blockquote>

但并不表示API直接访问用户的IP地址。



我现在唯一的想法是使用 nsIProcess 调用本地进程,从那里解析IP。这似乎是非常骇人听闻,将不得不在每个操作系统的基础上处理。即我运行ifconfig,netcfg,ipconfig?用什么args?



有人知道有更好的解决方案吗?

解决方案

您可以使用 nsIDNSService 界面这里还有一个 myHostName 属性可以解决:

  var dns = Components.classes [@ mozilla.org/network/dns-service;1] 
.getService(Components.interfaces.nsIDNSService);
var myName = dns.myHostName;
var record = dns.resolve(myName,0);
while(record.hasMore())
alert(record.getNextAddrAsString());

您应该期望它产生多个地址,而不是所有地址都是有效的 - 即使是本地网络,你至少会得到两个地址(IPv6和IPv4),除此之外,你可能会得到一个Teredo地址,来自VMWare&所安装的虚拟适配器的地址。公司和更多。


I'm writing a Firefox extension that uses nsIServerSocket to listen for socket connections. I'm looking for a way for the extension code to programmatically learn the local network address of the machine running the Firefox extension. This information gets communicated to a client on the local network out-of-band such that it can open a socket connection to the extension.

My research into this has so far only turned up individuals wishing to find the ip address of loaded websites by way of DNS resolution or using Java applets) which won't be appropriate for a FF extension. The Mozilla Developer pages on the Geolocation API mentions:

"data is exchanged including WiFi Access Point data, an access token (similar to a 2 week cookie), and the user's IP address"

but does not indicate an API to access the user's IP address directly.

My only idea at this point is to invoke a local process using nsIProcess and parse the IP from there. This seems awfully hackish and would have to be handled on a per-OS basis. I.e. do I run ifconfig, netcfg, ipconfig? With what args?

Is anyone aware of a better solution?

解决方案

You use nsIDNSService interface here as well - it has a myHostName property that can be resolved:

var dns = Components.classes["@mozilla.org/network/dns-service;1"]
                    .getService(Components.interfaces.nsIDNSService);
var myName = dns.myHostName;
var record = dns.resolve(myName, 0);
while (record.hasMore())
  alert(record.getNextAddrAsString());

You should expect it to produce multiple addresses and not all of them will be valid - even for the local network you will get at least two addresses (IPv6 and IPv4), in addition to that you might get a Teredo address, addresses from virtual adapters installed by VMWare & Co. and more.

这篇关于如何从Firefox扩展代码获取本地IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆