From b0a7a0b6826dac4f72a146217811a2e519dacf79 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 11 Jan 2025 23:40:04 +0800 Subject: [PATCH] Refactor: track download stats and enable HTTP/2 --- Build/build-reject-ip-list.ts | 4 +++- Build/lib/fetch-retry.ts | 2 +- Build/lib/parse-filter/domainlists.ts | 5 ++--- Build/lib/parse-filter/filters.ts | 2 +- Build/lib/rules/base.ts | 13 +++++++------ 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Build/build-reject-ip-list.ts b/Build/build-reject-ip-list.ts index ed659ce2c..eb059d0bf 100644 --- a/Build/build-reject-ip-list.ts +++ b/Build/build-reject-ip-list.ts @@ -49,6 +49,8 @@ const getBotNetFilterIPsPromise: Promise<[ipv4: string[], ipv6: string[]]> = fet return acc; }, [[], []])); +const readLocalRejectIpListPromise = readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'ip/reject.conf')); + export const buildRejectIPList = task(require.main === module, __filename)(async (span) => { const [bogusNxDomainIPs, botNetIPs] = await Promise.all([ span.traceChildPromise('get bogus nxdomain ips', getBogusNxDomainIPsPromise), @@ -66,7 +68,7 @@ export const buildRejectIPList = task(require.main === module, __filename)(async ' - https://github.com/felixonmars/dnsmasq-china-list', ' - https://github.com/curbengh/botnet-filter' ]) - .addFromRuleset(await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'ip/reject.conf'))) + .addFromRuleset(readLocalRejectIpListPromise) .bulkAddCIDR4NoResolve(bogusNxDomainIPs[0]) .bulkAddCIDR6NoResolve(bogusNxDomainIPs[1]) .bulkAddCIDR4NoResolve(botNetIPs[0]) diff --git a/Build/lib/fetch-retry.ts b/Build/lib/fetch-retry.ts index c472bd3de..7842f8210 100644 --- a/Build/lib/fetch-retry.ts +++ b/Build/lib/fetch-retry.ts @@ -23,7 +23,7 @@ if (!fs.existsSync(CACHE_DIR)) { fs.mkdirSync(CACHE_DIR, { recursive: true }); } -const agent = new Agent({}); +const agent = new Agent({ allowH2: true }); setGlobalDispatcher(agent.compose( interceptors.retry({ diff --git a/Build/lib/parse-filter/domainlists.ts b/Build/lib/parse-filter/domainlists.ts index 9eca7ed01..0587f12df 100644 --- a/Build/lib/parse-filter/domainlists.ts +++ b/Build/lib/parse-filter/domainlists.ts @@ -6,9 +6,8 @@ import { fetchAssetsWithout304 } from '../fetch-assets'; import type { Span } from '../../trace'; function domainListLineCb(l: string, set: string[], includeAllSubDomain: boolean, meta: string) { - let line = processLine(l); + const line = processLine(l); if (!line) return; - line = line.toLowerCase(); const domain = normalizeDomain(line); if (!domain) return; @@ -33,7 +32,7 @@ export function processDomainLists( domainListsUrl: string, mirrors: string[] | null, includeAllSubDomain = false ) { return span.traceChildAsync(`process domainlist: ${domainListsUrl}`, async (span) => { - const text = await span.traceChildAsync(`process domainlist: ${domainListsUrl}`, () => fetchAssetsWithout304( + const text = await span.traceChildAsync('download', () => fetchAssetsWithout304( domainListsUrl, mirrors )); diff --git a/Build/lib/parse-filter/filters.ts b/Build/lib/parse-filter/filters.ts index 8731a3b91..eb5fc4434 100644 --- a/Build/lib/parse-filter/filters.ts +++ b/Build/lib/parse-filter/filters.ts @@ -27,7 +27,7 @@ export async function processFilterRules( allowThirdParty = false ): Promise<{ white: string[], black: string[] }> { const [white, black, warningMessages] = await parentSpan.traceChild(`process filter rules: ${filterRulesUrl}`).traceAsyncFn(async (span) => { - const text = await fetchAssetsWithout304(filterRulesUrl, fallbackUrls); + const text = await span.traceChildAsync('download', () => fetchAssetsWithout304(filterRulesUrl, fallbackUrls)); const whitelistDomainSets = new Set(); const blacklistDomainSets = new Set(); diff --git a/Build/lib/rules/base.ts b/Build/lib/rules/base.ts index ae6de6823..1d681a382 100644 --- a/Build/lib/rules/base.ts +++ b/Build/lib/rules/base.ts @@ -404,15 +404,16 @@ export async function fileEqual(linesA: string[], source: AsyncIterable) } export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) { - let isEqual = true; const linesALen = linesA.length; - if (fs.existsSync(filePath)) { - isEqual = await fileEqual(linesA, readFileByLine(filePath)); - } else { + const isEqual = await span.traceChildAsync(`compare ${filePath}`, async () => { + if (fs.existsSync(filePath)) { + return fileEqual(linesA, readFileByLine(filePath)); + } + console.log(`${filePath} does not exists, writing...`); - isEqual = false; - } + return false; + }); if (isEqual) { console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));