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

210 lines
6.7 KiB
JavaScript
Raw Normal View History

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
*/
let interceptor = {};
2020-02-27 13:45:29 +01:00
2021-02-17 07:01:08 +01:00
2020-02-27 13:45:29 +01:00
/**
* Public Methods
*/
interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
2023-01-20 06:48:18 +01:00
let validCandidate, targetDetails, targetDomain, isGoogleFont, isGoogleMaterialIcons, initiatorDomain, isListed,
iframe, isGoogleDomain;
2023-01-20 06:48:18 +01:00
iframe = '';
if (tab.url !== requestDetails.originUrl) {
console.log(`${LogString.PREFIX} ${LogString.IFRAME} ${tab.url} -> ${requestDetails.originUrl}`);
iframe = requestDetails.originUrl;
}
2020-02-27 13:45:29 +01:00
if (requestDetails['type'] === WebRequestType.MAIN_FRAME &&
2022-09-19 06:20:22 +02:00
helpers.checkAllowlisted(
helpers.extractDomainFromUrl(tab.url, true),
requestAnalyzer.allowlistedDomains
)) {
return {
'cancel': false
};
}
targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url);
2022-07-10 06:55:41 +02:00
if (targetDetails['result'] === 'blocked') {
let source, resource;
source = helpers.extractDomainFromUrl(tab.url, true);
resource = tab.url;
2022-07-10 06:55:41 +02:00
return {
'redirectUrl': chrome.runtime.getURL(`resources/blocked/index.html?source=${source}&resource=${resource}`)
2022-07-10 06:55:41 +02:00
};
}
stateManager.requests[requestDetails.requestId] = {
tabIdentifier, targetDetails
};
// Block POST, HEAD, PUT, DELETE, TRACE, OPTIONS
if (BlockedRequestMethods[requestDetails.method]) {
console.warn(`${LogString.PREFIX} ${LogString.NON_GET_REQUEST_BLOCKED}`);
2023-01-20 06:48:18 +01:00
log.append(tab.url, requestDetails.url, LogString.NON_GET_REQUEST_BLOCKED, true, iframe);
return {
'cancel': true
};
}
2020-02-27 13:45:29 +01:00
validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab);
if (!validCandidate) {
return {
'cancel': false
};
}
2021-09-29 07:10:37 +02:00
if (interceptor._isBadResource(requestDetails.url)) {
2021-12-04 07:46:03 +01:00
console.log(`${LogString.PREFIX} ${LogString.EVIL_RESOURCE_BLOCKED} ${requestDetails.url}`);
2023-01-20 06:48:18 +01:00
log.append(tab.url, requestDetails.url, '-', true, iframe);
2021-09-29 06:50:53 +02:00
return {
'cancel': true
};
}
2021-12-04 07:46:03 +01:00
targetDomain = helpers.extractDomainFromUrl(requestDetails.url, true);
isGoogleFont = requestAnalyzer.isGoogleFont(targetDomain);
isGoogleMaterialIcons = requestAnalyzer.isGoogleMaterialIcons(requestDetails.url);
2022-01-30 09:37:42 +01:00
if (BrowserType.FIREFOX && isGoogleFont && !isGoogleMaterialIcons) {
initiatorDomain = helpers.extractDomainFromUrl(tab.url, true);
isListed = helpers.checkAllowlisted(initiatorDomain, interceptor.allowedDomainsGoogleFonts);
isGoogleDomain = helpers.isGoogleDomain(initiatorDomain);
// Check if the website is allowed to load Google Fonts
if (interceptor.blockGoogleFonts === true && isListed === false && isGoogleDomain === false) {
console.log(`${LogString.PREFIX} Google fonts blocked ${requestDetails.url}`);
log.append(tab.url, requestDetails.url, '-', true, iframe);
interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier);
++stateManager.tabs[tabIdentifier].blocked;
return {
2021-11-13 07:20:06 +01:00
'redirectUrl': chrome.runtime.getURL('resources/google-fonts-placeholder.css')
};
} else if (isGoogleDomain === true) {
console.log(`${LogString.PREFIX} Google fonts allowed, because Google Website ${initiatorDomain}`);
return {
'cancel': false
};
} else if (interceptor.blockGoogleFonts === false || isListed === true) {
2020-08-23 07:37:58 +02:00
return {
'cancel': false
};
}
}
2020-08-23 07:37:58 +02:00
2021-12-04 07:46:03 +01:00
if (targetDetails['result'] === false) {
if (!IgnoredHost[targetDomain]) {
++stateManager.tabs[tabIdentifier].missing;
}
2021-03-13 06:55:20 +01:00
return interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier);
2020-02-27 13:45:29 +01:00
}
console.log(`${LogString.PREFIX} ${LogString.REPLACED_RESOURCE} ${targetDetails.path}`);
2023-01-20 06:48:18 +01:00
log.append(tab.url, requestDetails.url, targetDetails.path, false, iframe);
2020-02-27 13:45:29 +01:00
return {
2021-12-04 07:46:03 +01:00
'redirectUrl': chrome.runtime.getURL(targetDetails.path + 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, blockedCount;
2020-02-27 13:45:29 +01:00
2021-03-13 06:55:20 +01:00
if (stateManager.showIconBadge === true) {
injectionCount = Object.keys(stateManager.tabs[tabIdentifier].injections).length || 0;
2023-07-13 09:07:02 +02:00
missingCount = stateManager.tabs[tabIdentifier].missing || 0;
blockedCount = stateManager.tabs[tabIdentifier].blocked || 0;
injectionCount = injectionCount + missingCount + blockedCount;
2021-03-13 06:55:20 +01:00
if (stateManager.changeBadgeColorMissingResources === true) {
2023-07-13 09:07:02 +02:00
if (injectionCount === 0) {
wrappers.setBadgeText(tabIdentifier, injectionCount);
wrappers.setBadgeColoring(tabIdentifier, BadgeSettingMissingResource.TYPE);
2021-03-13 06:55:20 +01:00
}
} else {
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();
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;
}
if (Setting.BLOCK_GOOGLE_FONTS in changes) {
interceptor.blockGoogleFonts = changes.blockGoogleFonts.newValue;
}
2021-02-10 06:36:01 +01:00
if (Setting.ALLOWED_DOMAINS_GOOGLE_FONTS in changes) {
interceptor.allowedDomainsGoogleFonts = changes.allowedDomainsGoogleFonts.newValue;
}
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);