Refactor request handling (#787)

This commit is contained in:
nobody 2021-12-04 07:46:03 +01:00
parent 8f9d501c51
commit f0baf0e81b
No known key found for this signature in database
GPG Key ID: 8F6DE3D614FCFD7A
4 changed files with 45 additions and 24 deletions

View File

@ -401,8 +401,7 @@ const MathJaxFiles = {
/**
* To block bad resources, e.g. fingerprint
*
* Necessary if the user has selected the option "Block requests for missing
* resources Advanced" option is not enabled.
* Required if the user has not enabled the "Block requests for missing resources" option.
*
* https://codeberg.org/nobody/LocalCDN/issues/703
*/
@ -420,6 +419,7 @@ const LogString = {
'YANDEX': 'Workaround. Disable LocalCDN if website and CDN are the same',
'REPLACED_RESOURCE': 'Replaced resource:',
'MISSING_RESOURCE': 'Missing resource:',
'EVIL_RESOURCE_BLOCKED': 'Evil resource blocked:',
};
// Supported charsets for TextDecoder()

View File

@ -31,10 +31,9 @@ var interceptor = {};
*/
interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
let validCandidate, targetDetails, targetPath, targetDetailURL;
let validCandidate, targetDetails, targetDomain;
validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab);
if (!validCandidate) {
return {
'cancel': false
@ -42,20 +41,17 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
}
if (interceptor._isBadResource(requestDetails.url)) {
console.log(`[ LocalCDN ] Evil resource blocked: ${requestDetails.url}`);
console.log(`${LogString.PREFIX} ${LogString.EVIL_RESOURCE_BLOCKED} ${requestDetails.url}`);
log.append(tab.url, requestDetails.url, '-', true);
return {
'cancel': true
};
}
targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url);
targetPath = targetDetails.path;
targetDomain = helpers.extractDomainFromUrl(requestDetails.url, true);
if (Regex.GOOGLE_FONTS.test(requestDetails.url)) {
if (requestAnalyzer.isGoogleFont(targetDomain) && !requestAnalyzer.isGoogleMaterialIcons(requestDetails.url)) {
let initiatorDomain, isListed;
initiatorDomain = helpers.extractDomainFromUrl(tab.url, true);
isListed = helpers.checkAllowlisted(initiatorDomain, interceptor.allowedDomainsGoogleFonts);
// Check if the website is allowed to load Google Fonts
@ -70,13 +66,13 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
}
}
targetDetailURL = helpers.extractDomainFromUrl(requestDetails.url, true);
targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url);
console.log(targetDetails['result']);
if (targetDetails === false && !IgnoredHost[targetDetailURL]) {
++stateManager.tabs[tabIdentifier].missing;
}
if (!targetDetails) {
if (targetDetails['result'] === false) {
if (!IgnoredHost[targetDomain]) {
++stateManager.tabs[tabIdentifier].missing;
}
return interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier);
}
@ -85,7 +81,7 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
};
return {
'redirectUrl': chrome.runtime.getURL(targetPath + fileGuard.secret)
'redirectUrl': chrome.runtime.getURL(targetDetails.path + fileGuard.secret)
};
};

View File

@ -31,7 +31,7 @@ var requestAnalyzer = {};
*/
requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
let initiatorDomain, isAllowlisted;
let initiatorDomain, requestedDomain, isAllowlisted;
initiatorDomain = helpers.extractDomainFromUrl(tabDetails.url, true);
@ -39,6 +39,12 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
initiatorDomain = Address.EXAMPLE;
}
// If requested Domain not in mappings.js it is not relevant
requestedDomain = helpers.extractDomainFromUrl(requestDetails.url, true);
if (mappings['cdn'][requestedDomain] === undefined) {
return false;
}
isAllowlisted = helpers.checkAllowlisted(initiatorDomain, requestAnalyzer.allowlistedDomains);
if (isAllowlisted) {
return false;
@ -46,11 +52,12 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
// Font Awesome injections in Chromium deactivated (https://gitlab.com/nobody42/localcdn/-/issues/67)
if (BrowserType.CHROMIUM) {
if ((/(font-awesome|fontawesome)/).test(requestDetails.url)) {
if (requestDetails.url.includes('font-awesome') || requestDetails.url.includes('fontawesome')) {
console.warn(`${LogString.PREFIX} ${LogString.FONT_AWESOME}`);
log.append(tabDetails.url, requestDetails.url, LogString.FONT_AWESOME, true);
return false;
} else if (requestDetails.url.startsWith('https://fonts.googleapis.com')) {
}
if (requestAnalyzer._isGoogleMaterialIcons(requestedDomain, requestDetails.url)) {
// also valid for Google Material icons
console.warn(`${LogString.PREFIX} ${LogString.GOOGLE_MATERIAL_ICONS}`);
log.append(tabDetails.url, requestDetails.url, LogString.GOOGLE_MATERIAL_ICONS, true);
@ -58,7 +65,7 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
}
}
// Disable LocalCDN if website is 'yandex.com' and CDN is 'yastatic.net', because website and CDN are the same.
// Ignore requests if website is 'yandex.com' and CDN is 'yastatic.net', because website and CDN are the same.
if (tabDetails.url.includes('yandex.com') && requestDetails.url.includes('yastatic.net')) {
log.append(tabDetails.url, requestDetails.url, LogString.YANDEX, true);
return false;
@ -68,6 +75,14 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
return requestDetails.method === WebRequest.GET;
};
requestAnalyzer.isGoogleMaterialIcons = function (url) {
return url.includes('Material+Icons') || url.includes('materialicons');
};
requestAnalyzer.isGoogleFont = function (domain) {
return domain.includes('fonts.googleapis.com') || domain.includes('fonts.gstatic.com');
};
requestAnalyzer.getLocalTarget = function (requestDetails, initiator) {
let destinationUrl, destinationHost, destinationPath, hostMappings, basePath,
resourceMappings, destinationSearchString;
@ -155,7 +170,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
}
if (resourcePattern === undefined) {
return false;
return {
'result': false,
};
}
for (let resourceMold of Object.keys(resourceMappings)) {
@ -166,7 +183,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
// Replace the requested version with the latest depending on major version
versionDelivered = targets.setLastVersion(targetPath, versionNumber);
if (versionDelivered === false) {
return false;
return {
'result': false,
};
}
versionDelivered = versionDelivered.toString();
targetPath = targetPath.replace(versionNumber, versionDelivered);
@ -206,7 +225,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
console.warn(`${LogString.PREFIX} ${LogString.MISSING_RESOURCE} ${channelHost}${channelPath}`);
log.append(initiator, channelHost + channelPath, '-', true);
}
return false;
return {
'result': false,
};
};
requestAnalyzer._getPathOfBundle = function (initiator, channelHost, channelPath, targetPath, bundle) {

View File

@ -29,6 +29,10 @@
<ul>
<li>bootstrap.bundle.min.js -> stackpath.bootstrapcdn.com (<a href="https://codeberg.org/nobody/LocalCDN/issues/785">#785</a>)</li>
</ul>
<p>Improved</p>
<ul>
<li>Request handling (<a href="https://codeberg.org/nobody/LocalCDN/issues/787">#787</a>)</li>
</ul>
</div>
<div id="generator-section">
<div class="topic-label">