如何识别谁在Firefox中发起了http请求?


105

我正在开发一个新的Firefox插件,该插件可以拦截所有Firefox的网络流量(带有的http(s)请求http-on-modify-request

使用当前的代码,我可以将来自网页/标签页和所有其他组件(RSS提要更新,来自XPCOM组件的XHR请求,扩展,扩展管理器等)的请求分开。

我想确定是谁发起了除制表符流量以外的请求,而不仅仅是整个组?(RSS,XPCOM组件,扩展,扩展管理器等)

示例:假设的自定义变量requestRequestor将具有一个值,以标识特定的插件或RSS更新等。

我发现了类似的问题,但没有解决方案。

标识整个组的当前代码(获取触发http-on-modify-request通知的浏览器)是:

Components.utils.import('resource://gre/modules/Services.jsm');
Services.obs.addObserver(httpObs, 'http-on-modify-request', false);
//Services.obs.removeObserver(httpObs, 'http-on-modify-request'); //uncomment this line, or run this line when you want to remove the observer

var httpObs = {
    observe: function (aSubject, aTopic, aData) {
        if (aTopic == 'http-on-modify-request') {
            /*start - do not edit here*/
            var oHttp = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel); //i used nsIHttpChannel but i guess you can use nsIChannel, im not sure why though
            var interfaceRequestor = oHttp.notificationCallbacks.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
            //var DOMWindow = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow); //not to be done anymore because: https://developer.mozilla.org/en-US/docs/Updating_extensions_for_Firefox_3.5#Getting_a_load_context_from_a_request //instead do the loadContext stuff below
            var loadContext;
            try {
                loadContext = interfaceRequestor.getInterface(Components.interfaces.nsILoadContext);
            } catch (ex) {
                try {
                    loadContext = aSubject.loadGroup.notificationCallbacks.getInterface(Components.interfaces.nsILoadContext);
                    //in ff26 aSubject.loadGroup.notificationCallbacks was null for me, i couldnt find a situation where it wasnt null, but whenever this was null, and i knew a loadContext is supposed to be there, i found that "interfaceRequestor.getInterface(Components.interfaces.nsILoadContext);" worked fine, so im thinking in ff26 it doesnt use aSubject.loadGroup.notificationCallbacks anymore, but im not sure
                } catch (ex2) {
                    loadContext = null;
                    //this is a problem i dont know why it would get here
                }
            }
            /*end do not edit here*/
            /*start - do all your edits below here*/
            var url = oHttp.URI.spec; //can get url without needing loadContext
            if (loadContext) {
                var contentWindow = loadContext.associatedWindow; //this is the HTML window of the page that just loaded
                //aDOMWindow this is the firefox window holding the tab
                var aDOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem).rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
                var gBrowser = aDOMWindow.gBrowser; //this is the gBrowser object of the firefox window this tab is in
                var aTab = gBrowser._getTabForContentWindow(contentWindow.top); //this is the clickable tab xul element, the one found in the tab strip of the firefox window, aTab.linkedBrowser is same as browser var above //can stylize tab like aTab.style.backgroundColor = 'blue'; //can stylize the tab like aTab.style.fontColor = 'red';
                var browser = aTab.linkedBrowser; //this is the browser within the tab //this is what the example in the previous section gives
                //end getting other useful stuff
            } else {
                Components.utils.reportError('EXCEPTION: Load Context Not Found!!');
                //this is likely no big deal as the channel proably has no associated window, ie: the channel was loading some resource. but if its an ajax call you may end up here
            }
        }
    }
};

9
哇,这里的工作很棒,您看到过以下主题吗:stackoverflow.com/questions/27483651/…它可能是您可能会为您做贡献的一个主题,我非常有兴趣了解您到目前为止如何识别来源。
Noitidart

2
谢谢您的链接,兄弟,我对firefox请求等有更多的信息。我的插件越多越好,使用高级安全插件,虽然不容易,但是(这是业余爱好,插件是公开的;)我会让您在github上知道)
intika 2015年

1
另外,这是loadContextAndGoodies我编写的函数,可以进行一些改进,我前一阵子编写了它,但请尽可能进行增强。gist.github.com/Noitidart/…您似乎在上面的代码中使用了该代码段的较旧版本,因此使用此代码段将清除上面的代码,并且该代码段可能会有一些增强(我不知道我没有进行比较) :P)
Noitidart

2
是的,我在另一个问题中看到您的代码刚刚添加并测试了它;)如果需要,将发布拉取请求;)
intika 2015年

2
感谢您的公关,我对您的公关做了公关:) gist.github.com/Noitidart/644494bdc26f996739ef#comment-1483890
Noitidart

Answers:


1

截至2020年6月,尚无实现http请求请求者过滤/标识的官方方法/方式。

当前,唯一的可能性是对问题的代码执行什么操作,即将网页/选项卡的请求与其他Firefox组件(提要更新,扩展请求,来自XPCOM组件的XHR请求等)分开。

如评论所述,这是Firefox的内部限制。当前Firefox的核心代码未实现请求者跟踪,因此不知道是谁发起了请求以及原因。了解Chrome开发人员工具最近已获得此功能可能很有用。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.