mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Perf: minor optimization here and there
This commit is contained in:
parent
9bb0c14d5f
commit
eaf993deca
@ -7,6 +7,8 @@ import { createTrie } from './lib/trie';
|
|||||||
import { SHARED_DESCRIPTION } from './lib/constants';
|
import { SHARED_DESCRIPTION } from './lib/constants';
|
||||||
import { createMemoizedPromise } from './lib/memo-promise';
|
import { createMemoizedPromise } from './lib/memo-promise';
|
||||||
|
|
||||||
|
const PROBE_DOMAINS = ['.microsoft.com', '.windows.net', '.windows.com', '.windowsupdate.com', '.windowssearch.com', '.office.net'];
|
||||||
|
|
||||||
const WHITELIST = [
|
const WHITELIST = [
|
||||||
'DOMAIN-SUFFIX,download.prss.microsoft.com',
|
'DOMAIN-SUFFIX,download.prss.microsoft.com',
|
||||||
'DOMAIN,res.cdn.office.net'
|
'DOMAIN,res.cdn.office.net'
|
||||||
@ -29,7 +31,7 @@ export const getMicrosoftCdnRulesetPromise = createMemoizedPromise(async () => {
|
|||||||
trie.add(domain);
|
trie.add(domain);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new Set(['.microsoft.com', '.windows.net', '.windows.com', '.windowsupdate.com', '.windowssearch.com', '.office.net'].flatMap(domain => trie.find(domain, false)));
|
return new Set(PROBE_DOMAINS.flatMap(domain => trie.find(domain, false)));
|
||||||
});
|
});
|
||||||
|
|
||||||
const trie2 = createTrie(set);
|
const trie2 = createTrie(set);
|
||||||
|
|||||||
@ -21,38 +21,36 @@ import * as SetHelpers from 'mnemonist/set';
|
|||||||
import { setAddFromArray } from './lib/set-add-from-array';
|
import { setAddFromArray } from './lib/set-add-from-array';
|
||||||
|
|
||||||
export const buildRejectDomainSet = task(import.meta.path, async (span) => {
|
export const buildRejectDomainSet = task(import.meta.path, async (span) => {
|
||||||
|
const gorhill = await getGorhillPublicSuffixPromise();
|
||||||
|
|
||||||
/** Whitelists */
|
/** Whitelists */
|
||||||
const filterRuleWhitelistDomainSets = new Set(PREDEFINED_WHITELIST);
|
const filterRuleWhitelistDomainSets = new Set(PREDEFINED_WHITELIST);
|
||||||
|
|
||||||
const domainSets = new Set<string>();
|
const domainSets = new Set<string>();
|
||||||
|
|
||||||
|
let shouldStop = false;
|
||||||
// Parse from AdGuard Filters
|
// Parse from AdGuard Filters
|
||||||
const [gorhill, shouldStop] = await span
|
await span
|
||||||
.traceChild('download and process hosts / adblock filter rules')
|
.traceChild('download and process hosts / adblock filter rules')
|
||||||
.traceAsyncFn(async (childSpan) => {
|
.traceAsyncFn(async (childSpan) => {
|
||||||
let shouldStop = false;
|
await Promise.all([
|
||||||
|
|
||||||
const [gorhill] = await Promise.all([
|
|
||||||
getGorhillPublicSuffixPromise(),
|
|
||||||
// Parse from remote hosts & domain lists
|
// Parse from remote hosts & domain lists
|
||||||
...HOSTS.map(entry => processHosts(childSpan, entry[0], entry[1], entry[2]).then(hosts => {
|
...HOSTS.map(entry => processHosts(childSpan, entry[0], entry[1], entry[2]).then(hosts => SetHelpers.add(domainSets, hosts))),
|
||||||
SetHelpers.add(domainSets, hosts);
|
|
||||||
})),
|
|
||||||
...DOMAIN_LISTS.map(entry => processDomainLists(childSpan, entry[0], entry[1], entry[2])),
|
|
||||||
...ADGUARD_FILTERS.map(input => {
|
|
||||||
const promise = typeof input === 'string'
|
|
||||||
? processFilterRules(childSpan, input)
|
|
||||||
: processFilterRules(childSpan, input[0], input[1], input[2]);
|
|
||||||
|
|
||||||
return promise.then(({ white, black, foundDebugDomain }) => {
|
...DOMAIN_LISTS.map(entry => processDomainLists(childSpan, entry[0], entry[1], entry[2]).then(hosts => SetHelpers.add(domainSets, hosts))),
|
||||||
|
|
||||||
|
...ADGUARD_FILTERS.map(input => (
|
||||||
|
typeof input === 'string'
|
||||||
|
? processFilterRules(childSpan, input)
|
||||||
|
: processFilterRules(childSpan, input[0], input[1], input[2])
|
||||||
|
).then(({ white, black, foundDebugDomain }) => {
|
||||||
if (foundDebugDomain) {
|
if (foundDebugDomain) {
|
||||||
shouldStop = true;
|
shouldStop = true;
|
||||||
// we should not break here, as we want to see full matches from all data source
|
// we should not break here, as we want to see full matches from all data source
|
||||||
}
|
}
|
||||||
setAddFromArray(filterRuleWhitelistDomainSets, white);
|
setAddFromArray(filterRuleWhitelistDomainSets, white);
|
||||||
setAddFromArray(domainSets, black);
|
setAddFromArray(domainSets, black);
|
||||||
});
|
})),
|
||||||
}),
|
|
||||||
...([
|
...([
|
||||||
'https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exceptions.txt',
|
'https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exceptions.txt',
|
||||||
'https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exclusions.txt'
|
'https://raw.githubusercontent.com/AdguardTeam/AdGuardSDNSFilter/master/Filters/exclusions.txt'
|
||||||
@ -67,10 +65,9 @@ export const buildRejectDomainSet = task(import.meta.path, async (span) => {
|
|||||||
childSpan.traceChild('process reject_sukka.conf').traceAsyncFn(async () => {
|
childSpan.traceChild('process reject_sukka.conf').traceAsyncFn(async () => {
|
||||||
for await (const l of readFileByLine(path.resolve(import.meta.dir, '../Source/domainset/reject_sukka.conf'))) {
|
for await (const l of readFileByLine(path.resolve(import.meta.dir, '../Source/domainset/reject_sukka.conf'))) {
|
||||||
const line = processLine(l);
|
const line = processLine(l);
|
||||||
if (line) {
|
if (!line) continue;
|
||||||
domainSets.add(line);
|
domainSets.add(line);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
})
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -85,7 +82,7 @@ export const buildRejectDomainSet = task(import.meta.path, async (span) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return [gorhill, shouldStop] as const;
|
return shouldStop;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (shouldStop) {
|
if (shouldStop) {
|
||||||
@ -124,15 +121,14 @@ export const buildRejectDomainSet = task(import.meta.path, async (span) => {
|
|||||||
// remove pre-defined enforced blacklist from whitelist
|
// remove pre-defined enforced blacklist from whitelist
|
||||||
const kwfilter = createKeywordFilter(domainKeywordsSet);
|
const kwfilter = createKeywordFilter(domainKeywordsSet);
|
||||||
|
|
||||||
// Build whitelist trie, to handle case like removing `g.msn.com` due to white `.g.msn.com` (`@@||g.msn.com`)
|
// handle case like removing `g.msn.com` due to white `.g.msn.com` (`@@||g.msn.com`)
|
||||||
const trieWhite = createTrie(filterRuleWhitelistDomainSets);
|
|
||||||
for (const domain of domainSets) {
|
for (const domain of domainSets) {
|
||||||
if (domain[0] === '.') {
|
if (domain[0] === '.') {
|
||||||
if (trieWhite.contains(domain)) {
|
if (filterRuleWhitelistDomainSets.has(domain)) {
|
||||||
domainSets.delete(domain);
|
domainSets.delete(domain);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else if (trieWhite.has(`.${domain}`)) {
|
} else if (filterRuleWhitelistDomainSets.has(`.${domain}`)) {
|
||||||
domainSets.delete(domain);
|
domainSets.delete(domain);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -154,8 +150,9 @@ export const buildRejectDomainSet = task(import.meta.path, async (span) => {
|
|||||||
console.log(`Final size ${dudupedDominArray.length}`);
|
console.log(`Final size ${dudupedDominArray.length}`);
|
||||||
|
|
||||||
// Create reject stats
|
// Create reject stats
|
||||||
const rejectDomainsStats: Array<[string, number]> = span.traceChild('create reject stats').traceSyncFn(
|
const rejectDomainsStats: Array<[string, number]> = span
|
||||||
() => Object.entries(
|
.traceChild('create reject stats')
|
||||||
|
.traceSyncFn(() => Object.entries(
|
||||||
dudupedDominArray.reduce<Record<string, number>>((acc, cur) => {
|
dudupedDominArray.reduce<Record<string, number>>((acc, cur) => {
|
||||||
const suffix = tldts.getDomain(cur, { allowPrivateDomains: false, detectIp: false, validateHostname: false });
|
const suffix = tldts.getDomain(cur, { allowPrivateDomains: false, detectIp: false, validateHostname: false });
|
||||||
if (suffix) {
|
if (suffix) {
|
||||||
@ -163,15 +160,7 @@ export const buildRejectDomainSet = task(import.meta.path, async (span) => {
|
|||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
}, {})
|
}, {})
|
||||||
).filter(a => a[1] > 10).sort((a, b) => {
|
).filter(a => a[1] > 5).sort((a, b) => (b[1] - a[1]) || a[0].localeCompare(b[0])));
|
||||||
const t = b[1] - a[1];
|
|
||||||
if (t !== 0) {
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
return a[0].localeCompare(b[0]);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
const description = [
|
const description = [
|
||||||
...SHARED_DESCRIPTION,
|
...SHARED_DESCRIPTION,
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import { isCI } from 'ci-info';
|
|||||||
import { add as SetAdd } from 'mnemonist/set';
|
import { add as SetAdd } from 'mnemonist/set';
|
||||||
import type { Span } from '../trace';
|
import type { Span } from '../trace';
|
||||||
|
|
||||||
const WHITELIST_DOMAIN = new Set([
|
const WHITELIST_DOMAIN = [
|
||||||
'w3s.link',
|
'w3s.link',
|
||||||
'dweb.link',
|
'dweb.link',
|
||||||
'nftstorage.link',
|
'nftstorage.link',
|
||||||
@ -20,7 +20,7 @@ const WHITELIST_DOMAIN = new Set([
|
|||||||
'page.link', // Firebase URL Shortener
|
'page.link', // Firebase URL Shortener
|
||||||
'fleek.cool',
|
'fleek.cool',
|
||||||
'notion.site'
|
'notion.site'
|
||||||
]);
|
];
|
||||||
const BLACK_TLD = new Set([
|
const BLACK_TLD = new Set([
|
||||||
'autos',
|
'autos',
|
||||||
'bar',
|
'bar',
|
||||||
@ -101,12 +101,15 @@ export const getPhishingDomains = (parentSpan: Span) => parentSpan.traceChild('g
|
|||||||
|
|
||||||
span.traceChild('whitelisting phishing domains').traceSyncFn(() => {
|
span.traceChild('whitelisting phishing domains').traceSyncFn(() => {
|
||||||
const trieForRemovingWhiteListed = createTrie(domainSet);
|
const trieForRemovingWhiteListed = createTrie(domainSet);
|
||||||
for (const white of WHITELIST_DOMAIN) {
|
|
||||||
const found = trieForRemovingWhiteListed.find(`.${white}`, false);
|
const needToBeWhite = WHITELIST_DOMAIN.flatMap(white => {
|
||||||
for (let i = 0, len = found.length; i < len; i++) {
|
const found = trieForRemovingWhiteListed.find(`.${white}`, true);
|
||||||
domainSet.delete(found[i]);
|
found.push(white);
|
||||||
}
|
return found;
|
||||||
domainSet.delete(white);
|
});
|
||||||
|
|
||||||
|
for (let i = 0, len = needToBeWhite.length; i < len; i++) {
|
||||||
|
domainSet.delete(needToBeWhite[i]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -58,7 +58,7 @@ export const createTrie = (from?: string[] | Set<string>) => {
|
|||||||
/**
|
/**
|
||||||
* Method used to retrieve every item in the trie with the given prefix.
|
* Method used to retrieve every item in the trie with the given prefix.
|
||||||
*/
|
*/
|
||||||
const find = (suffix: string, includeEqualWithSuffix = true): string[] => {
|
const find = (suffix: string, /** @default true */ includeEqualWithSuffix = true): string[] => {
|
||||||
let node: TrieNode = root;
|
let node: TrieNode = root;
|
||||||
let token: string;
|
let token: string;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user