用JavaScript区分Intranet和外部IP地址 [英] Distinguish Intranet and external IP addresses in JavaScript

查看:168
本文介绍了用JavaScript区分Intranet和外部IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个浏览器插件(Chrome和Firefox),简而言之,它根据用户访问的每个页面的内容进行一些计算(即我们不是在谈论用户的自己的页面),向用户呈现分数。在得分特别高的情况下,页面标题,页面URL等被自动地提交给中央服务,以便可以显示一种联赛表。



所有这些都很完美,但我希望在可能的情况下从我们的用户碰巧访问的Intranet页面中去除所有流量。 (我们不存储或传输任何页面内容,但存在隐私问题,我们不希望与内部/公司文档有任何关系。)



在理论,我可以弄清楚(以合理的准确程度)IP是否可能来自内联网@ 区分内部网和官方IP地址,但由于DOM不提供对文档主机IP的访问,试图快速确定IP是否切实可行,然后应用这些IP规则,考虑到查找服务可能会降低/缓慢的可能性?

更简单的替代方法 - 比如文档主机名的TLD的模式匹配 - 是几乎一样好?



任何建议?



更新:



我正要用自己回答这个问题我只是在服务器上做IP检查,wh如果IP不在内部范围内,则只提交页面统计信息 - 这很容易。 ...不幸的是我不能这样做:因为我的后端是Google AppEngine [Java]和 InetAddress 类是受限制的,我不能做任意的IP查找。

解决方案

您应该使用 nsIDNSService 来解析Firefox中的主机名称。沿着这些路线:

var threadManager = Components.classes [@ mozilla.org/thread- manager; 1]
.getService(Components.interfaces.nsIThreadManager);
var dnsService = Components.classes [@ mozilla.org/network/dns-service;1]
.createInstance(Components.interfaces.nsIDNSService);
var listener = {
onLookupComplete:function(request,record,status)
{
if(Components.isSuccessCode(status))
alert(主机IP地址:+ record.getNextAddrAsString());
else
alert(查找失败);
}
};
dnsService.asyncResolve(www.google.com,0,listener,
threadManager.currentThread);

这通常是一个非常快的操作,因为主机名已被缓存。注意:如果您使用附加SDK,则必须使用chrome权限,并用各自的别名替换对 Components 属性的访问。



至于Chrome,我怀疑你可以在那里正确解决这个问题。正如Korvin Szanto在他的评论中指出的,任何主机名都可以指向本地地址。而且Chrome不会让你获得与之通话的IP地址。


I'm writing a browser add-on (Chrome and Firefox) which, in a nutshell, does "some calculations" based upon the content of each page the user visits (i.e. we're not talking about the user's own pages), presenting the user with a score. In the case of particularly high scores, the page title, page URL, etc. is submitted (automatically) to a central service so that a kind of league table can be shown.

All of this works perfectly, but I want - where possible - to strip out all traffic from the pages of Intranets that our users happen to visit. (We don't store or transmit any page content, nonetheless there are privacy concerns, and we don't want anything to do with internal / corporate documents.)

In theory I can work out (to a reasonable degree of accuracy) whether an IP is likely to be from an intranet @ Distinguish the between intranet and official IP addresses, but as the DOM doesn't provide access to the document's host IP, is it practical to try to determine the IP on the fly and then apply those IP rules, given the possibility that lookup services might be down/slow?

Would a simpler alternative - like pattern-matching for the TLD of the document's hostname - be nearly as good?

Any suggestions?

Update:

I was about to answer this myself with "I'll just do the IP check on the server, when the page stats are submitted, and only complete the submission if the IP is not within the internal range - it's much easier." ... unfortunately I can't do this: because my back-end is Google AppEngine [Java] and the InetAddress class is restricted, I can't do arbitrary IP lookups.

解决方案

You should use nsIDNSService to resolve the host name in Firefox. Along these lines:

var threadManager = Components.classes["@mozilla.org/thread-manager;1"]
                              .getService(Components.interfaces.nsIThreadManager);
var dnsService = Components.classes["@mozilla.org/network/dns-service;1"]
                           .createInstance(Components.interfaces.nsIDNSService);
var listener = {
  onLookupComplete: function(request, record, status)
  {
    if (Components.isSuccessCode(status))
      alert("Host IP address: " + record.getNextAddrAsString());
    else
      alert("Lookup failed");
  }
};
dnsService.asyncResolve("www.google.com", 0, listener,
                        threadManager.currentThread);

This is usually a very fast operation because the host name is already cached. Note: if you use the Add-on SDK then you will have to use chrome authority and replace access to Components properties by the respective aliases.

As to Chrome, I doubt that you can solve this problem properly there. As Korvin Szanto notes in his comment, any host name could point to a local address. And Chrome won't let you get the IP address that it talks to.

这篇关于用JavaScript区分Intranet和外部IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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