Skip to content

Commit

Permalink
Refactor: track download stats and enable HTTP/2
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 11, 2025
1 parent 29410eb commit b0a7a0b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Build/build-reject-ip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion Build/lib/fetch-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 2 additions & 3 deletions Build/lib/parse-filter/domainlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
));
Expand Down
2 changes: 1 addition & 1 deletion Build/lib/parse-filter/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
const blacklistDomainSets = new Set<string>();
Expand Down
13 changes: 7 additions & 6 deletions Build/lib/rules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,16 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>)
}

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}`)));
Expand Down

0 comments on commit b0a7a0b

Please sign in to comment.