是否有可能在插件中控制firefox的DNS请求?

Is it possible to control Firefox's DNS requests in an addon?

本文关键字:DNS 请求 firefox 控制 有可能 插件 是否      更新时间:2023-09-26

我想知道是否有可能拦截和控制/重定向由Firefox发出的DNS请求?
目的是在Firefox中设置一个独立的DNS服务器(而不是系统的DNS服务器)

没有。通过nsIDNSService接口提供DNS解析器。该接口不是完全可脚本化的,所以您不能用自己的Javascript实现替换内置实现。

但是你能不能重写DNS服务器?

内置实现从nsDNSServicensHostResolver再到PR_GetAddrByName (nspr),最后到getaddrinfo/gethostbyname。这将使用系统(或实现它的库)所配置的任何内容。

还有其他选择吗?

没有。您可以安装代理并让它解析域名(当然需要某种代理服务器)。但这是一个非常黑客,我不建议(如果用户已经有一个真正的,非解析代理配置;也需要处理)

您可以检测到"问题加载页面",然后可能使用 redirectTo 方法。

基本上他们都加载about:neterror url后面有一堆信息。即:

  • about:neterror?e=dnsNotFound&u=http%3A//www.cu.reporterror%28%27afew/&c=UTF-8&d=Firefox%20can%27t%20find%20the%20server%20at%20www.cu.reporterror%28%27afew.
  • about:neterror?e=malformedURI&u=about%3Abalk&c=&d=The%20URL%20is%20not%20valid%20and%20cannot%

但是这个信息保存在文档中。所以你必须这样做。下面的示例代码将检测加载页面时出现的问题:

var listenToPageLoad_IfProblemLoadingPage = function(event) {
    var win = event.originalTarget.defaultView;
    var docuri = window.gBrowser.webNavigation.document.documentURI; //this is bad practice, it returns the documentUri of the currently focused tab, need to make it get the linkedBrowser for the tab by going through the event. so use like event.originalTarget.linkedBrowser.webNavigation.document.documentURI <<i didnt test this linkedBrowser theory but its gotta be something like that
    var location = win.location + ''; //I add a " + ''" at the end so it makes it a string so we can use string functions like location.indexOf etc
    if (win.frameElement) {
      // Frame within a tab was loaded. win should be the top window of
      // the frameset. If you don't want do anything when frames/iframes
      // are loaded in this web page, uncomment the following line:
      // return;
      // Find the root document:
      //win = win.top;
      if (docuri.indexOf('about:neterror') == 0) {
          Components.utils.reportError('IN FRAME - PROBLEM LOADING PAGE LOADED docuri = "' + docuri + '"');
      }
    } else {
        if (docuri.indexOf('about:neterror') == 0) {
            Components.utils.reportError('IN TAB - PROBLEM LOADING PAGE LOADED docuri = "' + docuri + '"');
        }
    }
}

window.gBrowser.addEventListener('DOMContentLoaded', listenToPageLoad_IfProblemLoadingPage, true);