2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Interceptor
|
2020-06-30 18:41:58 +02:00
|
|
|
* Belongs to LocalCDN (since 2020-02-26)
|
|
|
|
* (Origin: Decentraleyes)
|
2020-02-27 13:45:29 +01:00
|
|
|
*
|
|
|
|
* @author Thomas Rientjes
|
|
|
|
* @since 2016-04-06
|
2020-04-14 07:43:25 +02:00
|
|
|
*
|
2020-06-30 18:41:58 +02:00
|
|
|
* @author nobody
|
2020-04-14 07:43:25 +02:00
|
|
|
* @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';
|
|
|
|
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Interceptor
|
|
|
|
*/
|
|
|
|
|
|
|
|
var interceptor = {};
|
|
|
|
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Public Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
|
2021-03-30 06:42:34 +02:00
|
|
|
let validCandidate, targetDetails, targetPath, targetDetailURL;
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab);
|
|
|
|
|
2020-08-19 09:58:28 +02:00
|
|
|
if (!validCandidate) {
|
|
|
|
return {
|
2020-08-22 14:58:57 +02:00
|
|
|
'cancel': false
|
2020-08-19 09:58:28 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-29 07:10:37 +02:00
|
|
|
if (interceptor._isBadResource(requestDetails.url)) {
|
|
|
|
console.log(`[ LocalCDN ] Evil resource blocked: ${requestDetails.url}`);
|
2021-09-29 06:50:53 +02:00
|
|
|
log.append(tab.url, requestDetails.url, '-', true);
|
|
|
|
return {
|
|
|
|
'cancel': true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-21 19:50:41 +01:00
|
|
|
targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url);
|
2020-08-22 14:58:57 +02:00
|
|
|
targetPath = targetDetails.path;
|
|
|
|
|
2021-03-12 07:22:49 +01:00
|
|
|
|
2020-08-23 07:37:58 +02:00
|
|
|
if (Regex.GOOGLE_FONTS.test(requestDetails.url)) {
|
2021-08-07 08:39:10 +02:00
|
|
|
let initiatorDomain, isListed;
|
|
|
|
|
|
|
|
initiatorDomain = helpers.extractDomainFromUrl(tab.url, true);
|
|
|
|
isListed = helpers.checkAllowlisted(initiatorDomain, interceptor.allowedDomainsGoogleFonts);
|
2020-08-22 14:58:57 +02:00
|
|
|
// Check if the website is allowed to load Google Fonts
|
2021-08-07 08:39:10 +02:00
|
|
|
if (interceptor.blockGoogleFonts === true && isListed === false) {
|
2020-07-03 08:37:00 +02:00
|
|
|
return {
|
2020-08-03 13:28:52 +02:00
|
|
|
'cancel': true
|
2020-07-03 08:37:00 +02:00
|
|
|
};
|
2021-08-07 08:39:10 +02:00
|
|
|
} else if (interceptor.blockGoogleFonts === false || isListed === true) {
|
2020-08-23 07:37:58 +02:00
|
|
|
return {
|
|
|
|
'cancel': false
|
|
|
|
};
|
2020-07-03 08:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-23 07:37:58 +02:00
|
|
|
|
2021-03-30 06:42:34 +02:00
|
|
|
targetDetailURL = helpers.extractDomainFromUrl(requestDetails.url, true);
|
|
|
|
|
|
|
|
if (targetDetails === false && !IgnoredHost[targetDetailURL]) {
|
2021-03-13 07:41:17 +01:00
|
|
|
++stateManager.tabs[tabIdentifier].missing;
|
|
|
|
}
|
|
|
|
|
2020-03-28 19:19:18 +01:00
|
|
|
if (!targetDetails) {
|
2021-03-13 06:55:20 +01:00
|
|
|
return interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier);
|
2020-02-27 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stateManager.requests[requestDetails.requestId] = {
|
|
|
|
tabIdentifier, targetDetails
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
2021-04-11 08:03:42 +02:00
|
|
|
'redirectUrl': chrome.runtime.getURL(targetPath + fileGuard.secret)
|
2020-02-27 13:45:29 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Private Methods
|
|
|
|
*/
|
|
|
|
|
2021-03-13 06:55:20 +01:00
|
|
|
interceptor._handleMissingCandidate = function (requestUrl, tabIdentifier) {
|
|
|
|
let requestUrlSegments, injectionCount, missingCount;
|
2020-02-27 13:45:29 +01:00
|
|
|
|
2021-03-13 06:55:20 +01:00
|
|
|
if (stateManager.showIconBadge === true) {
|
2021-03-14 11:03:34 +01:00
|
|
|
injectionCount = Object.keys(stateManager.tabs[tabIdentifier].injections).length || 0;
|
2021-03-13 06:55:20 +01:00
|
|
|
if (stateManager.changeBadgeColorMissingResources === true) {
|
|
|
|
missingCount = stateManager.tabs[tabIdentifier].missing || 0;
|
|
|
|
if (missingCount > 0 && injectionCount === 0) {
|
2021-08-11 06:29:09 +02:00
|
|
|
wrappers.setBadgeText(tabIdentifier, injectionCount);
|
2021-03-13 06:55:20 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-08-11 06:29:09 +02:00
|
|
|
wrappers.setBadgeText(tabIdentifier, injectionCount);
|
2021-03-13 06:55:20 +01:00
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:55:20 +01:00
|
|
|
if (interceptor.blockMissing === true) {
|
2020-02-27 13:45:29 +01:00
|
|
|
return {
|
2021-03-13 06:55:20 +01:00
|
|
|
'cancel': true
|
2020-02-27 13:45:29 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
requestUrlSegments = new URL(requestUrl);
|
|
|
|
|
|
|
|
if (requestUrlSegments.protocol === Address.HTTP) {
|
|
|
|
requestUrlSegments.protocol = Address.HTTPS;
|
|
|
|
requestUrl = requestUrlSegments.toString();
|
|
|
|
|
2020-03-28 11:17:02 +01:00
|
|
|
return {
|
|
|
|
'redirectUrl': requestUrl
|
|
|
|
};
|
|
|
|
} 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;
|
|
|
|
}
|
2020-07-03 08:37:00 +02:00
|
|
|
|
|
|
|
if (Setting.BLOCK_GOOGLE_FONTS in changes) {
|
|
|
|
interceptor.blockGoogleFonts = changes.blockGoogleFonts.newValue;
|
|
|
|
}
|
2020-08-22 14:58:57 +02:00
|
|
|
|
2021-02-10 06:36:01 +01:00
|
|
|
if (Setting.ALLOWED_DOMAINS_GOOGLE_FONTS in changes) {
|
|
|
|
interceptor.allowedDomainsGoogleFonts = changes.allowedDomainsGoogleFonts.newValue;
|
2020-08-22 14:58:57 +02:00
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
};
|
|
|
|
|
2021-09-29 06:50:53 +02:00
|
|
|
interceptor._isBadResource = function (requestUrl) {
|
|
|
|
requestUrl = requestUrl.replace(/(^\w+:|^)\/\//, '');
|
|
|
|
return Object.keys(BadResources).filter((path) => requestUrl.startsWith(path)).length !== 0;
|
|
|
|
};
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Event Handlers
|
|
|
|
*/
|
|
|
|
|
|
|
|
chrome.storage.onChanged.addListener(interceptor._handleStorageChanged);
|