mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-14 02:00:37 +08:00
97 lines
2.8 KiB
TypeScript
97 lines
2.8 KiB
TypeScript
import { fdir as Fdir } from 'fdir';
|
|
import path from 'node:path';
|
|
import fsp from 'node:fs/promises';
|
|
import { SOURCE_DIR } from './constants/dir';
|
|
import { readFileByLine } from './lib/fetch-text-by-line';
|
|
import { processLine } from './lib/process-line';
|
|
import { HostnameSmolTrie, HostnameTrie } from './lib/trie';
|
|
import { task } from './trace';
|
|
|
|
const ENFORCED_WHITELIST = [
|
|
'hola.sk',
|
|
'hola.org',
|
|
'hola-shopping.com',
|
|
'mynextphone.io',
|
|
'iadmatapk.nosdn.127.net',
|
|
'httpdns.bilivideo.com',
|
|
'httpdns-v6.gslb.yy.com',
|
|
'twemoji.maxcdn.com',
|
|
'samsungcloudsolution.com',
|
|
'samsungcloudsolution.net',
|
|
'samsungqbe.com',
|
|
'ntp.api.bz',
|
|
'cdn.tuk.dev',
|
|
'vocadb-analytics.fly.dev'
|
|
];
|
|
|
|
const WHITELIST: string[] = ['ntp.api.bz', 'httpdns.bilivideo.com', 'httpdns-v6.gslb.yy.com', 'smd-cms.nasa.gov', 'ddcdn.comtucdncom.com', 'cpan.noris.de', 'mopnativeadv.037201.com', 'vocadb-analytics.fly.dev', 'iadmatapk.nosdn.127.net', 'ad-stat.ksosoft.com', 'adapi.lenovogame.com', 'img5.gelbooru.com'];
|
|
|
|
task(require.main === module, __filename)(async (span) => {
|
|
const files = await span.traceChildAsync('crawl thru all files', () => new Fdir()
|
|
.withFullPaths()
|
|
.filter((filepath, isDirectory) => {
|
|
if (isDirectory) return true;
|
|
|
|
const extname = path.extname(filepath);
|
|
|
|
return extname !== '.js' && extname !== '.ts';
|
|
})
|
|
.crawl(SOURCE_DIR)
|
|
.withPromise());
|
|
|
|
const whiteTrie = span.traceChildSync('build whitelist trie', () => {
|
|
const trie = new HostnameSmolTrie(WHITELIST);
|
|
ENFORCED_WHITELIST.forEach((item) => trie.whitelist(item));
|
|
return trie;
|
|
});
|
|
|
|
await Promise.all(files.map(file => span.traceChildAsync('dedupe ' + file, () => dedupeFile(file, whiteTrie))));
|
|
});
|
|
|
|
async function dedupeFile(file: string, whitelist: HostnameSmolTrie) {
|
|
const result: string[] = [];
|
|
|
|
const trie = new HostnameTrie();
|
|
|
|
let line: string | null = '';
|
|
|
|
for await (const l of readFileByLine(file)) {
|
|
line = processLine(l);
|
|
|
|
if (!line) {
|
|
if (l.startsWith('# $ skip_dedupe_src')) {
|
|
return;
|
|
}
|
|
|
|
result.push(l); // keep all comments and blank lines
|
|
continue;
|
|
}
|
|
|
|
if (trie.has(line)) {
|
|
continue; // drop duplicate
|
|
}
|
|
|
|
if (whitelist.has(line)) {
|
|
continue; // drop whitelisted items
|
|
}
|
|
|
|
trie.add(line);
|
|
result.push(line);
|
|
}
|
|
|
|
return fsp.writeFile(file, result.join('\n') + '\n');
|
|
}
|
|
|
|
// function isDomainSuffix(whiteItem: string, incomingItem: string) {
|
|
// const whiteIncludeDomain = whiteItem[0] === '.';
|
|
// whiteItem = whiteItem[0] === '.' ? whiteItem.slice(1) : whiteItem;
|
|
|
|
// if (whiteItem === incomingItem) {
|
|
// return true; // as long as exact match, we don't care if subdomain is included or not
|
|
// }
|
|
// if (whiteIncludeDomain) {
|
|
// return incomingItem.endsWith('.' + whiteItem);
|
|
// }
|
|
// return false;
|
|
// }
|