Chore: new util run against source file
Some checks failed
Build / Build (push) Has been cancelled
Build / Diff output (push) Has been cancelled
Build / Deploy to Cloudflare Pages (3.114.6) (push) Has been cancelled
Build / Deploy to GitHub and GitLab (push) Has been cancelled

This commit is contained in:
SukkaW
2025-04-27 23:33:56 +08:00
parent 505f7544ed
commit 2d706f4775
8 changed files with 132 additions and 177 deletions

View File

@@ -1,11 +1,9 @@
import { readFileByLine } from './lib/fetch-text-by-line';
import { processLine } from './lib/process-line';
import { SOURCE_DIR } from './constants/dir';
import path from 'node:path';
import { newQueue } from '@henrygd/queue';
import { isDomainAlive, keyedAsyncMutexWithQueue } from './lib/is-domain-alive';
import { fdir as Fdir } from 'fdir';
import runAgainstSourceFile from './lib/run-against-source-file';
const queue = newQueue(24);
@@ -19,10 +17,20 @@ function onDomain(args: [string, boolean]) {
(async () => {
const domainSets = await new Fdir()
.withFullPaths()
.filter((filePath, isDirectory) => {
if (isDirectory) return false;
const extname = path.extname(filePath);
return extname === '.txt' || extname === '.conf';
})
.crawl(SOURCE_DIR + path.sep + 'domainset')
.withPromise();
const domainRules = await new Fdir()
.withFullPaths()
.filter((filePath, isDirectory) => {
if (isDirectory) return false;
const extname = path.extname(filePath);
return extname === '.txt' || extname === '.conf';
})
.crawl(SOURCE_DIR + path.sep + 'non_ip')
.withPromise();
@@ -37,53 +45,29 @@ function onDomain(args: [string, boolean]) {
})();
export async function runAgainstRuleset(filepath: string) {
const extname = path.extname(filepath);
if (extname !== '.conf') {
console.log('[skip]', filepath);
return;
}
const promises: Array<Promise<void>> = [];
for await (const l of readFileByLine(filepath)) {
const line = processLine(l);
if (!line) continue;
const [type, domain] = line.split(',');
switch (type) {
case 'DOMAIN-SUFFIX':
case 'DOMAIN': {
promises.push(
queue.add(() => keyedAsyncMutexWithQueue(domain, () => isDomainAlive(domain, type === 'DOMAIN-SUFFIX')))
.then(onDomain)
);
break;
}
// no default
}
}
await runAgainstSourceFile(
filepath,
(domain: string, includeAllSubdomain: boolean) => queue.add(() => keyedAsyncMutexWithQueue(
domain,
() => isDomainAlive(domain, includeAllSubdomain)
).then(onDomain))
);
await Promise.all(promises);
console.log('[done]', filepath);
}
export async function runAgainstDomainset(filepath: string) {
const extname = path.extname(filepath);
if (extname !== '.conf') {
console.log('[skip]', filepath);
return;
}
const promises: Array<Promise<void>> = [];
for await (const l of readFileByLine(filepath)) {
const line = processLine(l);
if (!line) continue;
promises.push(
queue.add(() => keyedAsyncMutexWithQueue(line, () => isDomainAlive(line, line[0] === '.')))
.then(onDomain)
);
}
await runAgainstSourceFile(
filepath,
(domain: string, includeAllSubdomain: boolean) => queue.add(() => keyedAsyncMutexWithQueue(
domain,
() => isDomainAlive(domain, includeAllSubdomain)
).then(onDomain))
);
await Promise.all(promises);
console.log('[done]', filepath);
}