LocalCDN-Firefox-Chrome-Brave/core/interceptor.js

152 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-02-27 13:45:29 +01:00
/**
* Interceptor
* Belongs to Decentraleyes.
*
* @author Thomas Rientjes
* @since 2016-04-06
2020-04-14 07:43:25 +02:00
*
* @author nobody42
* @since 2020-02-26
*
2020-02-27 13:45:29 +01:00
* @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';
/**
* Interceptor
*/
var interceptor = {};
/**
* Public Methods
*/
interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
let validCandidate, tabDomain, targetDetails, targetPath;
validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab);
if (!validCandidate) {
return {
'cancel': false
};
}
tabDomain = helpers.extractDomainFromUrl(tab.url, true);
if (tabDomain === null) {
tabDomain = Address.EXAMPLE;
}
if (interceptor.taintedDomains[tabDomain] || (/yandex\./).test(tabDomain) ||
(/wickedlocal\.com/).test(tabDomain)) {
return interceptor._handleMissingCandidate(requestDetails.url, true);
}
targetDetails = requestAnalyzer.getLocalTarget(requestDetails);
targetPath = targetDetails.path;
2020-03-28 19:19:18 +01:00
if (!targetDetails) {
2020-02-27 13:45:29 +01:00
return interceptor._handleMissingCandidate(requestDetails.url);
}
stateManager.requests[requestDetails.requestId] = {
tabIdentifier, targetDetails
};
return {
'redirectUrl': chrome.extension.getURL(targetPath + fileGuard.secret)
};
};
/**
* Private Methods
*/
interceptor._handleMissingCandidate = function (requestUrl, preserveUrl) {
let requestUrlSegments;
if (interceptor.blockMissing === true) {
return {
'cancel': true
};
}
if (preserveUrl === true) {
return {
'cancel': false
};
}
requestUrlSegments = new URL(requestUrl);
if (requestUrlSegments.protocol === Address.HTTP) {
requestUrlSegments.protocol = Address.HTTPS;
requestUrl = requestUrlSegments.toString();
return {
'redirectUrl': requestUrl
};
2020-03-20 09:20:43 +01:00
} else {
return {
'cancel': false
};
}
2020-02-27 13:45:29 +01:00
};
interceptor._handleStorageChanged = function (changes) {
if (Setting.XHR_TEST_DOMAIN in changes) {
interceptor.xhrTestDomain = changes.xhrTestDomain.newValue;
}
if (Setting.BLOCK_MISSING in changes) {
interceptor.blockMissing = changes.blockMissing.newValue;
}
};
/**
* Initializations
*/
// Temporary list of tainted domains.
2020-04-30 19:13:27 +02:00
interceptor.taintedDomains = {};
2020-02-27 13:45:29 +01:00
interceptor.amountInjected = 0;
interceptor.xhrTestDomain = Address.DECENTRALEYES;
interceptor.blockMissing = false;
interceptor.relatedSettings = [];
interceptor.relatedSettings.push(Setting.AMOUNT_INJECTED);
interceptor.relatedSettings.push(Setting.XHR_TEST_DOMAIN);
interceptor.relatedSettings.push(Setting.BLOCK_MISSING);
chrome.storage.sync.get(interceptor.relatedSettings, function (items) {
2020-02-27 13:45:29 +01:00
interceptor.amountInjected = items.amountInjected || 0;
interceptor.xhrTestDomain = items.xhrTestDomain || Address.DECENTRALEYES;
interceptor.blockMissing = items.blockMissing || false;
});
/**
* Event Handlers
*/
chrome.storage.onChanged.addListener(interceptor._handleStorageChanged);