2022-09-20 06:24:01 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2023-12-04 10:10:34 -07:00
|
|
|
uBlock Origin Lite - a comprehensive, MV3-compliant content blocker
|
2022-09-20 06:24:01 -06:00
|
|
|
Copyright (C) 2022-present Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2022-09-29 17:51:33 -06:00
|
|
|
import { browser, dnr, i18n } from './ext.js';
|
2022-09-20 06:24:01 -06:00
|
|
|
import { fetchJSON } from './fetch.js';
|
2024-07-29 12:54:46 -06:00
|
|
|
import { ubolLog } from './debug.js';
|
2022-09-20 06:24:01 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const RULE_REALM_SIZE = 1000000;
|
|
|
|
const REGEXES_REALM_START = 1000000;
|
|
|
|
const REGEXES_REALM_END = REGEXES_REALM_START + RULE_REALM_SIZE;
|
2022-10-16 10:05:24 -06:00
|
|
|
const REMOVEPARAMS_REALM_START = REGEXES_REALM_END;
|
2022-09-29 17:51:33 -06:00
|
|
|
const REMOVEPARAMS_REALM_END = REMOVEPARAMS_REALM_START + RULE_REALM_SIZE;
|
2022-10-16 10:05:24 -06:00
|
|
|
const REDIRECT_REALM_START = REMOVEPARAMS_REALM_END;
|
|
|
|
const REDIRECT_REALM_END = REDIRECT_REALM_START + RULE_REALM_SIZE;
|
2023-07-10 09:56:57 -06:00
|
|
|
const MODIFYHEADERS_REALM_START = REDIRECT_REALM_END;
|
|
|
|
const MODIFYHEADERS_REALM_END = MODIFYHEADERS_REALM_START + RULE_REALM_SIZE;
|
2022-09-20 06:24:01 -06:00
|
|
|
const TRUSTED_DIRECTIVE_BASE_RULE_ID = 8000000;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function getRulesetDetails() {
|
2023-06-06 11:42:22 -06:00
|
|
|
if ( getRulesetDetails.rulesetDetailsPromise !== undefined ) {
|
|
|
|
return getRulesetDetails.rulesetDetailsPromise;
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
2023-06-06 11:42:22 -06:00
|
|
|
getRulesetDetails.rulesetDetailsPromise = fetchJSON('/rulesets/ruleset-details').then(entries => {
|
|
|
|
const rulesMap = new Map(
|
2022-09-20 06:24:01 -06:00
|
|
|
entries.map(entry => [ entry.id, entry ])
|
|
|
|
);
|
2023-06-06 11:42:22 -06:00
|
|
|
return rulesMap;
|
2022-09-20 06:24:01 -06:00
|
|
|
});
|
2023-06-06 11:42:22 -06:00
|
|
|
return getRulesetDetails.rulesetDetailsPromise;
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function getDynamicRules() {
|
2023-06-05 07:15:59 -06:00
|
|
|
if ( getDynamicRules.dynamicRuleMapPromise !== undefined ) {
|
|
|
|
return getDynamicRules.dynamicRuleMapPromise;
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
2023-06-05 07:15:59 -06:00
|
|
|
getDynamicRules.dynamicRuleMapPromise = dnr.getDynamicRules().then(rules => {
|
2023-06-06 11:42:22 -06:00
|
|
|
const rulesMap = new Map(rules.map(rule => [ rule.id, rule ]));
|
|
|
|
ubolLog(`Dynamic rule count: ${rulesMap.size}`);
|
|
|
|
ubolLog(`Available dynamic rule count: ${dnr.MAX_NUMBER_OF_DYNAMIC_AND_SESSION_RULES - rulesMap.size}`);
|
|
|
|
return rulesMap;
|
2022-09-20 06:24:01 -06:00
|
|
|
});
|
2023-06-05 07:15:59 -06:00
|
|
|
return getDynamicRules.dynamicRuleMapPromise;
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-10-03 08:51:47 -06:00
|
|
|
async function pruneInvalidRegexRules(realm, rulesIn) {
|
2022-09-20 06:24:01 -06:00
|
|
|
// Avoid testing already tested regexes
|
2023-10-03 08:51:47 -06:00
|
|
|
const dynamicRules = await dnr.getDynamicRules();
|
2022-09-20 06:24:01 -06:00
|
|
|
const validRegexSet = new Set(
|
|
|
|
dynamicRules.filter(rule =>
|
|
|
|
rule.condition?.regexFilter && true || false
|
|
|
|
).map(rule =>
|
|
|
|
rule.condition.regexFilter
|
|
|
|
)
|
|
|
|
);
|
2023-10-03 08:51:47 -06:00
|
|
|
|
|
|
|
// Validate regex-based rules
|
2022-09-20 06:24:01 -06:00
|
|
|
const toCheck = [];
|
2023-10-03 08:51:47 -06:00
|
|
|
const rejectedRegexRules = [];
|
|
|
|
for ( const rule of rulesIn ) {
|
|
|
|
if ( rule.condition?.regexFilter === undefined ) {
|
|
|
|
toCheck.push(true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const {
|
|
|
|
regexFilter: regex,
|
|
|
|
isUrlFilterCaseSensitive: isCaseSensitive
|
|
|
|
} = rule.condition;
|
|
|
|
if ( validRegexSet.has(regex) ) {
|
|
|
|
toCheck.push(true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
toCheck.push(
|
|
|
|
dnr.isRegexSupported({ regex, isCaseSensitive }).then(result => {
|
|
|
|
if ( result.isSupported ) { return true; }
|
|
|
|
rejectedRegexRules.push(`\t${regex} ${result.reason}`);
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collate results
|
|
|
|
const isValid = await Promise.all(toCheck);
|
|
|
|
|
|
|
|
if ( rejectedRegexRules.length !== 0 ) {
|
|
|
|
ubolLog(
|
|
|
|
`${realm} realm: rejected regexes:\n`,
|
|
|
|
rejectedRegexRules.join('\n')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rulesIn.filter((v, i) => isValid[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
async function updateRegexRules() {
|
|
|
|
const rulesetDetails = await getEnabledRulesetsDetails();
|
2022-09-20 06:24:01 -06:00
|
|
|
|
|
|
|
// Fetch regexes for all enabled rulesets
|
|
|
|
const toFetch = [];
|
2022-09-29 17:51:33 -06:00
|
|
|
for ( const details of rulesetDetails ) {
|
2022-10-16 10:05:24 -06:00
|
|
|
if ( details.rules.regex === 0 ) { continue; }
|
|
|
|
toFetch.push(fetchJSON(`/rulesets/regex/${details.id}`));
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
|
|
|
const regexRulesets = await Promise.all(toFetch);
|
|
|
|
|
2023-10-03 08:51:47 -06:00
|
|
|
// Collate all regexes rules
|
|
|
|
const allRules = [];
|
2022-09-20 06:24:01 -06:00
|
|
|
let regexRuleId = REGEXES_REALM_START;
|
|
|
|
for ( const rules of regexRulesets ) {
|
|
|
|
if ( Array.isArray(rules) === false ) { continue; }
|
|
|
|
for ( const rule of rules ) {
|
|
|
|
rule.id = regexRuleId++;
|
|
|
|
allRules.push(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 08:51:47 -06:00
|
|
|
const validatedRules = await pruneInvalidRegexRules('regexes', allRules);
|
2022-09-20 06:24:01 -06:00
|
|
|
|
|
|
|
// Add validated regex rules to dynamic ruleset without affecting rules
|
2022-09-29 17:51:33 -06:00
|
|
|
// outside regex rules realm.
|
2022-09-20 06:24:01 -06:00
|
|
|
const dynamicRuleMap = await getDynamicRules();
|
2023-10-03 08:51:47 -06:00
|
|
|
const newRuleMap = new Map(validatedRules.map(rule => [ rule.id, rule ]));
|
2022-09-20 06:24:01 -06:00
|
|
|
const addRules = [];
|
|
|
|
const removeRuleIds = [];
|
2022-09-29 17:51:33 -06:00
|
|
|
|
2022-09-20 06:24:01 -06:00
|
|
|
for ( const oldRule of dynamicRuleMap.values() ) {
|
|
|
|
if ( oldRule.id < REGEXES_REALM_START ) { continue; }
|
|
|
|
if ( oldRule.id >= REGEXES_REALM_END ) { continue; }
|
|
|
|
const newRule = newRuleMap.get(oldRule.id);
|
|
|
|
if ( newRule === undefined ) {
|
|
|
|
removeRuleIds.push(oldRule.id);
|
|
|
|
dynamicRuleMap.delete(oldRule.id);
|
|
|
|
} else if ( JSON.stringify(oldRule) !== JSON.stringify(newRule) ) {
|
|
|
|
removeRuleIds.push(oldRule.id);
|
|
|
|
addRules.push(newRule);
|
|
|
|
dynamicRuleMap.set(oldRule.id, newRule);
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 17:51:33 -06:00
|
|
|
|
2022-09-20 06:24:01 -06:00
|
|
|
for ( const newRule of newRuleMap.values() ) {
|
|
|
|
if ( dynamicRuleMap.has(newRule.id) ) { continue; }
|
|
|
|
addRules.push(newRule);
|
|
|
|
dynamicRuleMap.set(newRule.id, newRule);
|
|
|
|
}
|
2022-09-29 17:51:33 -06:00
|
|
|
|
|
|
|
if ( addRules.length === 0 && removeRuleIds.length === 0 ) { return; }
|
|
|
|
|
|
|
|
if ( removeRuleIds.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ubolLog(`Remove ${removeRuleIds.length} DNR regex rules`);
|
2022-09-29 17:51:33 -06:00
|
|
|
}
|
|
|
|
if ( addRules.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ubolLog(`Add ${addRules.length} DNR regex rules`);
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
2022-09-29 17:51:33 -06:00
|
|
|
|
2023-10-05 09:25:28 -06:00
|
|
|
return dnr.updateDynamicRules({ addRules, removeRuleIds }).catch(reason => {
|
|
|
|
console.error(`updateRegexRules() / ${reason}`);
|
|
|
|
});
|
2022-09-29 17:51:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
async function updateRemoveparamRules() {
|
|
|
|
const [
|
|
|
|
hasOmnipotence,
|
|
|
|
rulesetDetails,
|
|
|
|
dynamicRuleMap,
|
|
|
|
] = await Promise.all([
|
|
|
|
browser.permissions.contains({ origins: [ '<all_urls>' ] }),
|
|
|
|
getEnabledRulesetsDetails(),
|
|
|
|
getDynamicRules(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Fetch removeparam rules for all enabled rulesets
|
|
|
|
const toFetch = [];
|
|
|
|
for ( const details of rulesetDetails ) {
|
2022-10-16 10:05:24 -06:00
|
|
|
if ( details.rules.removeparam === 0 ) { continue; }
|
|
|
|
toFetch.push(fetchJSON(`/rulesets/removeparam/${details.id}`));
|
2022-09-29 17:51:33 -06:00
|
|
|
}
|
|
|
|
const removeparamRulesets = await Promise.all(toFetch);
|
|
|
|
|
|
|
|
// Removeparam rules can only be enforced with omnipotence
|
2023-10-03 08:51:47 -06:00
|
|
|
const allRules = [];
|
2022-09-29 17:51:33 -06:00
|
|
|
if ( hasOmnipotence ) {
|
|
|
|
let removeparamRuleId = REMOVEPARAMS_REALM_START;
|
|
|
|
for ( const rules of removeparamRulesets ) {
|
|
|
|
if ( Array.isArray(rules) === false ) { continue; }
|
|
|
|
for ( const rule of rules ) {
|
|
|
|
rule.id = removeparamRuleId++;
|
2023-10-03 08:51:47 -06:00
|
|
|
allRules.push(rule);
|
2022-09-29 17:51:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 08:51:47 -06:00
|
|
|
const validatedRules = await pruneInvalidRegexRules('removeparam', allRules);
|
|
|
|
|
2022-09-29 17:51:33 -06:00
|
|
|
// Add removeparam rules to dynamic ruleset without affecting rules
|
|
|
|
// outside removeparam rules realm.
|
2023-10-03 08:51:47 -06:00
|
|
|
const newRuleMap = new Map(validatedRules.map(rule => [ rule.id, rule ]));
|
2022-09-29 17:51:33 -06:00
|
|
|
const addRules = [];
|
|
|
|
const removeRuleIds = [];
|
|
|
|
|
|
|
|
for ( const oldRule of dynamicRuleMap.values() ) {
|
|
|
|
if ( oldRule.id < REMOVEPARAMS_REALM_START ) { continue; }
|
|
|
|
if ( oldRule.id >= REMOVEPARAMS_REALM_END ) { continue; }
|
|
|
|
const newRule = newRuleMap.get(oldRule.id);
|
|
|
|
if ( newRule === undefined ) {
|
|
|
|
removeRuleIds.push(oldRule.id);
|
|
|
|
dynamicRuleMap.delete(oldRule.id);
|
|
|
|
} else if ( JSON.stringify(oldRule) !== JSON.stringify(newRule) ) {
|
|
|
|
removeRuleIds.push(oldRule.id);
|
|
|
|
addRules.push(newRule);
|
|
|
|
dynamicRuleMap.set(oldRule.id, newRule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const newRule of newRuleMap.values() ) {
|
|
|
|
if ( dynamicRuleMap.has(newRule.id) ) { continue; }
|
|
|
|
addRules.push(newRule);
|
|
|
|
dynamicRuleMap.set(newRule.id, newRule);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( addRules.length === 0 && removeRuleIds.length === 0 ) { return; }
|
|
|
|
|
|
|
|
if ( removeRuleIds.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ubolLog(`Remove ${removeRuleIds.length} DNR removeparam rules`);
|
2022-09-29 17:51:33 -06:00
|
|
|
}
|
|
|
|
if ( addRules.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ubolLog(`Add ${addRules.length} DNR removeparam rules`);
|
2022-09-29 17:51:33 -06:00
|
|
|
}
|
|
|
|
|
2023-10-05 09:25:28 -06:00
|
|
|
return dnr.updateDynamicRules({ addRules, removeRuleIds }).catch(reason => {
|
|
|
|
console.error(`updateRemoveparamRules() / ${reason}`);
|
|
|
|
});
|
2022-09-29 17:51:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-10-16 10:05:24 -06:00
|
|
|
async function updateRedirectRules() {
|
|
|
|
const [
|
|
|
|
hasOmnipotence,
|
|
|
|
rulesetDetails,
|
|
|
|
dynamicRuleMap,
|
|
|
|
] = await Promise.all([
|
|
|
|
browser.permissions.contains({ origins: [ '<all_urls>' ] }),
|
|
|
|
getEnabledRulesetsDetails(),
|
|
|
|
getDynamicRules(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Fetch redirect rules for all enabled rulesets
|
|
|
|
const toFetch = [];
|
|
|
|
for ( const details of rulesetDetails ) {
|
|
|
|
if ( details.rules.redirect === 0 ) { continue; }
|
|
|
|
toFetch.push(fetchJSON(`/rulesets/redirect/${details.id}`));
|
|
|
|
}
|
|
|
|
const redirectRulesets = await Promise.all(toFetch);
|
|
|
|
|
|
|
|
// Redirect rules can only be enforced with omnipotence
|
2023-10-03 08:51:47 -06:00
|
|
|
const allRules = [];
|
2022-10-16 10:05:24 -06:00
|
|
|
if ( hasOmnipotence ) {
|
|
|
|
let redirectRuleId = REDIRECT_REALM_START;
|
|
|
|
for ( const rules of redirectRulesets ) {
|
|
|
|
if ( Array.isArray(rules) === false ) { continue; }
|
|
|
|
for ( const rule of rules ) {
|
|
|
|
rule.id = redirectRuleId++;
|
2023-10-03 08:51:47 -06:00
|
|
|
allRules.push(rule);
|
2022-10-16 10:05:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 08:51:47 -06:00
|
|
|
const validatedRules = await pruneInvalidRegexRules('redirect', allRules);
|
|
|
|
|
2022-10-16 10:05:24 -06:00
|
|
|
// Add redirect rules to dynamic ruleset without affecting rules
|
|
|
|
// outside redirect rules realm.
|
2023-10-03 08:51:47 -06:00
|
|
|
const newRuleMap = new Map(validatedRules.map(rule => [ rule.id, rule ]));
|
2022-10-16 10:05:24 -06:00
|
|
|
const addRules = [];
|
|
|
|
const removeRuleIds = [];
|
|
|
|
|
|
|
|
for ( const oldRule of dynamicRuleMap.values() ) {
|
|
|
|
if ( oldRule.id < REDIRECT_REALM_START ) { continue; }
|
|
|
|
if ( oldRule.id >= REDIRECT_REALM_END ) { continue; }
|
|
|
|
const newRule = newRuleMap.get(oldRule.id);
|
|
|
|
if ( newRule === undefined ) {
|
|
|
|
removeRuleIds.push(oldRule.id);
|
|
|
|
dynamicRuleMap.delete(oldRule.id);
|
|
|
|
} else if ( JSON.stringify(oldRule) !== JSON.stringify(newRule) ) {
|
|
|
|
removeRuleIds.push(oldRule.id);
|
|
|
|
addRules.push(newRule);
|
|
|
|
dynamicRuleMap.set(oldRule.id, newRule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const newRule of newRuleMap.values() ) {
|
|
|
|
if ( dynamicRuleMap.has(newRule.id) ) { continue; }
|
|
|
|
addRules.push(newRule);
|
|
|
|
dynamicRuleMap.set(newRule.id, newRule);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( addRules.length === 0 && removeRuleIds.length === 0 ) { return; }
|
|
|
|
|
|
|
|
if ( removeRuleIds.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ubolLog(`Remove ${removeRuleIds.length} DNR redirect rules`);
|
2022-10-16 10:05:24 -06:00
|
|
|
}
|
|
|
|
if ( addRules.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ubolLog(`Add ${addRules.length} DNR redirect rules`);
|
2022-10-16 10:05:24 -06:00
|
|
|
}
|
|
|
|
|
2023-10-05 09:25:28 -06:00
|
|
|
return dnr.updateDynamicRules({ addRules, removeRuleIds }).catch(reason => {
|
|
|
|
console.error(`updateRedirectRules() / ${reason}`);
|
|
|
|
});
|
2022-10-16 10:05:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2023-07-10 09:56:57 -06:00
|
|
|
async function updateModifyHeadersRules() {
|
2022-11-05 09:09:06 -06:00
|
|
|
const [
|
|
|
|
hasOmnipotence,
|
|
|
|
rulesetDetails,
|
|
|
|
dynamicRuleMap,
|
|
|
|
] = await Promise.all([
|
|
|
|
browser.permissions.contains({ origins: [ '<all_urls>' ] }),
|
|
|
|
getEnabledRulesetsDetails(),
|
|
|
|
getDynamicRules(),
|
|
|
|
]);
|
|
|
|
|
2023-07-10 09:56:57 -06:00
|
|
|
// Fetch modifyHeaders rules for all enabled rulesets
|
2022-11-05 09:09:06 -06:00
|
|
|
const toFetch = [];
|
|
|
|
for ( const details of rulesetDetails ) {
|
2023-07-10 09:56:57 -06:00
|
|
|
if ( details.rules.modifyHeaders === 0 ) { continue; }
|
|
|
|
toFetch.push(fetchJSON(`/rulesets/modify-headers/${details.id}`));
|
2022-11-05 09:09:06 -06:00
|
|
|
}
|
2023-07-10 09:56:57 -06:00
|
|
|
const rulesets = await Promise.all(toFetch);
|
2022-11-05 09:09:06 -06:00
|
|
|
|
|
|
|
// Redirect rules can only be enforced with omnipotence
|
2023-10-03 08:51:47 -06:00
|
|
|
const allRules = [];
|
2022-11-05 09:09:06 -06:00
|
|
|
if ( hasOmnipotence ) {
|
2023-07-10 09:56:57 -06:00
|
|
|
let ruleId = MODIFYHEADERS_REALM_START;
|
|
|
|
for ( const rules of rulesets ) {
|
2022-11-05 09:09:06 -06:00
|
|
|
if ( Array.isArray(rules) === false ) { continue; }
|
|
|
|
for ( const rule of rules ) {
|
2023-07-10 09:56:57 -06:00
|
|
|
rule.id = ruleId++;
|
2023-10-03 08:51:47 -06:00
|
|
|
allRules.push(rule);
|
2022-11-05 09:09:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 08:51:47 -06:00
|
|
|
const validatedRules = await pruneInvalidRegexRules('modify-headers', allRules);
|
|
|
|
|
2023-07-10 09:56:57 -06:00
|
|
|
// Add modifyHeaders rules to dynamic ruleset without affecting rules
|
|
|
|
// outside modifyHeaders realm.
|
2023-10-03 08:51:47 -06:00
|
|
|
const newRuleMap = new Map(validatedRules.map(rule => [ rule.id, rule ]));
|
2022-11-05 09:09:06 -06:00
|
|
|
const addRules = [];
|
|
|
|
const removeRuleIds = [];
|
|
|
|
|
|
|
|
for ( const oldRule of dynamicRuleMap.values() ) {
|
2023-07-10 09:56:57 -06:00
|
|
|
if ( oldRule.id < MODIFYHEADERS_REALM_START ) { continue; }
|
|
|
|
if ( oldRule.id >= MODIFYHEADERS_REALM_END ) { continue; }
|
2022-11-05 09:09:06 -06:00
|
|
|
const newRule = newRuleMap.get(oldRule.id);
|
|
|
|
if ( newRule === undefined ) {
|
|
|
|
removeRuleIds.push(oldRule.id);
|
|
|
|
dynamicRuleMap.delete(oldRule.id);
|
|
|
|
} else if ( JSON.stringify(oldRule) !== JSON.stringify(newRule) ) {
|
|
|
|
removeRuleIds.push(oldRule.id);
|
|
|
|
addRules.push(newRule);
|
|
|
|
dynamicRuleMap.set(oldRule.id, newRule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const newRule of newRuleMap.values() ) {
|
|
|
|
if ( dynamicRuleMap.has(newRule.id) ) { continue; }
|
|
|
|
addRules.push(newRule);
|
|
|
|
dynamicRuleMap.set(newRule.id, newRule);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( addRules.length === 0 && removeRuleIds.length === 0 ) { return; }
|
|
|
|
|
|
|
|
if ( removeRuleIds.length !== 0 ) {
|
2023-07-10 09:56:57 -06:00
|
|
|
ubolLog(`Remove ${removeRuleIds.length} DNR modifyHeaders rules`);
|
2022-11-05 09:09:06 -06:00
|
|
|
}
|
|
|
|
if ( addRules.length !== 0 ) {
|
2023-07-10 09:56:57 -06:00
|
|
|
ubolLog(`Add ${addRules.length} DNR modifyHeaders rules`);
|
2022-11-05 09:09:06 -06:00
|
|
|
}
|
|
|
|
|
2023-10-05 09:25:28 -06:00
|
|
|
return dnr.updateDynamicRules({ addRules, removeRuleIds }).catch(reason => {
|
|
|
|
console.error(`updateModifyHeadersRules() / ${reason}`);
|
|
|
|
});
|
2022-11-05 09:09:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// TODO: group all omnipotence-related rules into one realm.
|
|
|
|
|
2022-09-29 17:51:33 -06:00
|
|
|
async function updateDynamicRules() {
|
|
|
|
return Promise.all([
|
|
|
|
updateRegexRules(),
|
|
|
|
updateRemoveparamRules(),
|
2022-10-16 10:05:24 -06:00
|
|
|
updateRedirectRules(),
|
2023-07-10 09:56:57 -06:00
|
|
|
updateModifyHeadersRules(),
|
2022-09-29 17:51:33 -06:00
|
|
|
]);
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
async function defaultRulesetsFromLanguage() {
|
2023-11-28 09:02:55 -07:00
|
|
|
const out = [ 'default' ];
|
2022-09-20 06:24:01 -06:00
|
|
|
|
|
|
|
const dropCountry = lang => {
|
|
|
|
const pos = lang.indexOf('-');
|
|
|
|
if ( pos === -1 ) { return lang; }
|
|
|
|
return lang.slice(0, pos);
|
|
|
|
};
|
|
|
|
|
|
|
|
const langSet = new Set();
|
|
|
|
|
2022-10-17 06:13:48 -06:00
|
|
|
for ( const lang of navigator.languages.map(dropCountry) ) {
|
|
|
|
langSet.add(lang);
|
|
|
|
}
|
2022-09-20 06:24:01 -06:00
|
|
|
langSet.add(dropCountry(i18n.getUILanguage()));
|
|
|
|
|
|
|
|
const reTargetLang = new RegExp(
|
|
|
|
`\\b(${Array.from(langSet).join('|')})\\b`
|
|
|
|
);
|
|
|
|
|
|
|
|
const rulesetDetails = await getRulesetDetails();
|
|
|
|
for ( const [ id, details ] of rulesetDetails ) {
|
|
|
|
if ( typeof details.lang !== 'string' ) { continue; }
|
|
|
|
if ( reTargetLang.test(details.lang) === false ) { continue; }
|
|
|
|
out.push(id);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
async function enableRulesets(ids) {
|
|
|
|
const afterIds = new Set(ids);
|
|
|
|
const beforeIds = new Set(await dnr.getEnabledRulesets());
|
2022-09-27 05:46:24 -06:00
|
|
|
const enableRulesetSet = new Set();
|
|
|
|
const disableRulesetSet = new Set();
|
2022-09-20 06:24:01 -06:00
|
|
|
for ( const id of afterIds ) {
|
|
|
|
if ( beforeIds.has(id) ) { continue; }
|
2022-09-27 05:46:24 -06:00
|
|
|
enableRulesetSet.add(id);
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
|
|
|
for ( const id of beforeIds ) {
|
|
|
|
if ( afterIds.has(id) ) { continue; }
|
2022-09-27 05:46:24 -06:00
|
|
|
disableRulesetSet.add(id);
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
2022-09-27 05:46:24 -06:00
|
|
|
|
|
|
|
if ( enableRulesetSet.size === 0 && disableRulesetSet.size === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be sure the rulesets to enable/disable do exist in the current version,
|
|
|
|
// otherwise the API throws.
|
|
|
|
const rulesetDetails = await getRulesetDetails();
|
|
|
|
for ( const id of enableRulesetSet ) {
|
|
|
|
if ( rulesetDetails.has(id) ) { continue; }
|
|
|
|
enableRulesetSet.delete(id);
|
|
|
|
}
|
|
|
|
for ( const id of disableRulesetSet ) {
|
|
|
|
if ( rulesetDetails.has(id) ) { continue; }
|
|
|
|
disableRulesetSet.delete(id);
|
|
|
|
}
|
|
|
|
const enableRulesetIds = Array.from(enableRulesetSet);
|
|
|
|
const disableRulesetIds = Array.from(disableRulesetSet);
|
|
|
|
|
2022-09-20 06:24:01 -06:00
|
|
|
if ( enableRulesetIds.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ubolLog(`Enable rulesets: ${enableRulesetIds}`);
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
|
|
|
if ( disableRulesetIds.length !== 0 ) {
|
2023-06-04 09:32:55 -06:00
|
|
|
ubolLog(`Disable ruleset: ${disableRulesetIds}`);
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
2022-09-29 17:51:33 -06:00
|
|
|
await dnr.updateEnabledRulesets({ enableRulesetIds, disableRulesetIds });
|
|
|
|
|
2022-10-16 10:05:24 -06:00
|
|
|
return updateDynamicRules();
|
2022-09-20 06:24:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-29 17:51:33 -06:00
|
|
|
async function getEnabledRulesetsDetails() {
|
2022-09-20 06:24:01 -06:00
|
|
|
const [
|
|
|
|
ids,
|
2022-09-29 17:51:33 -06:00
|
|
|
rulesetDetails,
|
2022-09-20 06:24:01 -06:00
|
|
|
] = await Promise.all([
|
|
|
|
dnr.getEnabledRulesets(),
|
2022-09-29 17:51:33 -06:00
|
|
|
getRulesetDetails(),
|
2022-09-20 06:24:01 -06:00
|
|
|
]);
|
|
|
|
const out = [];
|
|
|
|
for ( const id of ids ) {
|
|
|
|
const ruleset = rulesetDetails.get(id);
|
|
|
|
if ( ruleset === undefined ) { continue; }
|
|
|
|
out.push(ruleset);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
export {
|
2022-10-10 10:28:24 -06:00
|
|
|
TRUSTED_DIRECTIVE_BASE_RULE_ID,
|
2022-09-20 06:24:01 -06:00
|
|
|
getRulesetDetails,
|
|
|
|
getDynamicRules,
|
|
|
|
enableRulesets,
|
|
|
|
defaultRulesetsFromLanguage,
|
2022-09-29 17:51:33 -06:00
|
|
|
getEnabledRulesetsDetails,
|
|
|
|
updateDynamicRules,
|
2022-09-20 06:24:01 -06:00
|
|
|
};
|