Skip to content
Snippets Groups Projects
interceptor.js 3.35 KiB
Newer Older
Thomas Rientjes's avatar
Thomas Rientjes committed
/**
 * Interceptor
 * Belongs to Decentraleyes.
 *
 * @author      Thomas Rientjes
 * @since       2014-05-30
 * @license     MPL 2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */

'use strict';

/**
 * Imports
 */

var { Class } = require('sdk/core/heritage');
var { Unknown } = require('sdk/platform/xpcom');
var { Cc, Ci, Cr } = require('chrome');

//noinspection JSUnresolvedFunction
Thomas Rientjes's avatar
Thomas Rientjes committed
var observerService = Cc['@mozilla.org/observer-service;1']
    .getService(Ci.nsIObserverService);
Thomas Rientjes's avatar
Thomas Rientjes committed

var preferences = require('sdk/simple-prefs').prefs;

Thomas Rientjes's avatar
Thomas Rientjes committed
var requestAnalyzer = require('./request-analyzer');
var dataHandler = require('./data-handler');

/**
 * Interceptor Class
 */

var Interceptor = new Class({

    extends: Unknown,
    interfaces: ['nsIObserver'],
    topic: 'http-on-modify-request',

    register: function () {
        observerService.addObserver(this, this.topic, false);
    },

    unregister: function () {
        observerService.removeObserver(this, this.topic);
    },

Thomas Rientjes's avatar
Thomas Rientjes committed
    /**
     * Called whenever an HTTP request is made.
     * @param httpChannel
     */
Thomas Rientjes's avatar
Thomas Rientjes committed
    observe: function (httpChannel) {

        var validCandidate, target, characterSet, redirectionURI;

        // Enable runtime discovery.
        httpChannel.QueryInterface(Ci.nsIHttpChannel);

        // Determine the validity of the candidate.
        validCandidate = requestAnalyzer.isValidCandidate(httpChannel);

        if (!validCandidate) {
            return;
        }
Thomas Rientjes's avatar
Thomas Rientjes committed

        // Remove referer header from request.
        httpChannel.setRequestHeader('Referer', null, false);

Thomas Rientjes's avatar
Thomas Rientjes committed
        // Temporary fixes for reported edge-case issues with specific websites.
        var initiatorDomain =
            httpChannel.loadInfo && httpChannel.loadInfo.loadingDocument && httpChannel.loadInfo.loadingDocument.domain ||
            httpChannel.referrer && httpChannel.referrer.host;
Thomas Rientjes's avatar
Thomas Rientjes committed
        if (initiatorDomain === 'play.google.com' || initiatorDomain === 'passport.twitch.tv' || initiatorDomain === 'report-uri.io'
            || initiatorDomain === 'minigames.mail.ru' || initiatorDomain === 'stefansundin.github.io') {
            return this.handleMissingCandidate(httpChannel);
Thomas Rientjes's avatar
Thomas Rientjes committed
        // Convert the original request URI to a local target.
        target = requestAnalyzer.getLocalTarget(httpChannel.URI.host, httpChannel.URI.path);

Thomas Rientjes's avatar
Thomas Rientjes committed
        if (!target) {
Thomas Rientjes's avatar
Thomas Rientjes committed
            return this.handleMissingCandidate(httpChannel);
Thomas Rientjes's avatar
Thomas Rientjes committed
        }

        characterSet = httpChannel.URI.originCharset;

        // Fetch local data and create a redirection URI.
        try {
Thomas Rientjes's avatar
Thomas Rientjes committed
            redirectionURI = dataHandler.getRedirectionURI(target.path, characterSet, target.type);
        } catch (exception) {
Thomas Rientjes's avatar
Thomas Rientjes committed
            return this.handleMissingCandidate(httpChannel);
Thomas Rientjes's avatar
Thomas Rientjes committed
        }
Thomas Rientjes's avatar
Thomas Rientjes committed
        httpChannel.redirectTo(redirectionURI);

        //noinspection JSUnresolvedVariable
        preferences.amountInjected++;
Thomas Rientjes's avatar
Thomas Rientjes committed
    },

Thomas Rientjes's avatar
Thomas Rientjes committed
    /**
     * Called when a valid candidate cannot be injected.
     * @param httpChannel
     */
Thomas Rientjes's avatar
Thomas Rientjes committed
    handleMissingCandidate: function (httpChannel) {

        //noinspection JSUnresolvedVariable
        if (preferences.blockMissing) {
Thomas Rientjes's avatar
Thomas Rientjes committed
            httpChannel.cancel(Cr.NS_ERROR_NOT_AVAILABLE);
        }
    }
});

/**
 * Exports
 */

module.exports = Interceptor;