mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-28 17:41:54 +08:00
Chore: new util run against source file
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user