From 1f23212042588f1a55f4dc191b9a943a3592a9cb Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 31 Dec 2023 02:50:13 +0800 Subject: [PATCH] Perf: enable cache for nxdomain bogus source --- Build/build-anti-bogus-domain.ts | 33 ++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Build/build-anti-bogus-domain.ts b/Build/build-anti-bogus-domain.ts index 4c85f2f9..1e64d9e1 100644 --- a/Build/build-anti-bogus-domain.ts +++ b/Build/build-anti-bogus-domain.ts @@ -6,20 +6,33 @@ import { processLine } from './lib/process-line'; import { task } from './lib/trace-runner'; import { SHARED_DESCRIPTION } from './lib/constants'; import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip'; +import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem'; const getBogusNxDomainIPs = async () => { - const result: string[] = []; - for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) { - if (line && line.startsWith('bogus-nxdomain=')) { - const ip = line.slice(15).trim(); - if (isProbablyIpv4(ip)) { - result.push(`IP-CIDR,${ip}/32,no-resolve`); - } else if (isProbablyIpv6(ip)) { - result.push(`IP-CIDR6,${ip}/128,no-resolve`); + const url = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf'; + + return fsCache.apply( + url, + async () => { + const result: string[] = []; + for await (const line of await fetchRemoteTextByLine('https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf')) { + if (line && line.startsWith('bogus-nxdomain=')) { + const ip = line.slice(15).trim(); + if (isProbablyIpv4(ip)) { + result.push(`IP-CIDR,${ip}/32,no-resolve`); + } else if (isProbablyIpv6(ip)) { + result.push(`IP-CIDR6,${ip}/128,no-resolve`); + } + } } + return result; + }, + { + ttl: TTL.ONE_WEEK(), + serializer: serializeArray, + deserializer: deserializeArray } - } - return result; + ); }; export const buildAntiBogusDomain = task(import.meta.path, async () => {