tools-dedupe-src.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { fdir as Fdir } from 'fdir';
  2. import path from 'node:path';
  3. import fsp from 'node:fs/promises';
  4. import { SOURCE_DIR } from './constants/dir';
  5. import { readFileByLine } from './lib/fetch-text-by-line';
  6. import { processLine } from './lib/process-line';
  7. const ENFORCED_WHITELIST = [
  8. 'hola.sk',
  9. 'hola.org',
  10. 'iadmatapk.nosdn.127.net',
  11. 'httpdns.bilivideo.com',
  12. 'httpdns-v6.gslb.yy.com',
  13. 'twemoji.maxcdn.com',
  14. 'samsungcloudsolution.com',
  15. 'samsungcloudsolution.net',
  16. 'samsungqbe.com'
  17. ];
  18. const WHITELIST: string[] = ['ton.local.twitter.com', 'prod.msocdn.com', 'twemoji.maxcdn.com', 'img.urlnode.com', 'ipfsgate.com', 'googleplay.pro', 'iadmatapk.nosdn.127.net', 'hola-shopping.com', 'brdtest.co', 'mynextphone.io', 'hola.hk', 'holashop.org', 'hola.sk', 'hola.com.sg', 'c.medialytics.com', 'adstats.mgc-games.com', 'search.mgc-games.com', 'kissdoujin.com', 'newminersage.com', 'trossmining.de', 'hashncash.net', 'microsolt.ru', 'moneropool.ru', 'hashforcash.us', 'bitcoinn.biz', 'webmining.co', 'lamba.top', 'httpdns.bilivideo.com', 'httpdns-v6.gslb.yy.com', 'k-cdn.depot.dev', 'li-cdn.com'];
  19. (async () => {
  20. const files = await new Fdir()
  21. .withFullPaths()
  22. .filter((filepath, isDirectory) => {
  23. if (isDirectory) return true;
  24. const extname = path.extname(filepath);
  25. return extname !== '.js' && extname !== '.ts';
  26. })
  27. .crawl(SOURCE_DIR)
  28. .withPromise();
  29. const whitelist = WHITELIST.filter((item) => ENFORCED_WHITELIST.every((whitelistItem) => !isDomainSuffix(whitelistItem, item)));
  30. await Promise.all(files.map(file => dedupeFile(file, whitelist)));
  31. })();
  32. async function dedupeFile(file: string, whitelist: string[]) {
  33. const set = new Set<string>();
  34. const result: string[] = [];
  35. for await (const l of readFileByLine(file)) {
  36. const line = processLine(l);
  37. if (!line) {
  38. if (l.startsWith('# $ skip_dedupe_src')) {
  39. return;
  40. }
  41. result.push(l);
  42. continue;
  43. }
  44. if (set.has(line)) {
  45. continue;
  46. }
  47. // We can't use a trie here since we need to keep the order
  48. if (whitelist.some((item) => isDomainSuffix(item, line))) {
  49. continue;
  50. }
  51. set.add(line);
  52. result.push(line);
  53. }
  54. return fsp.writeFile(file, result.join('\n') + '\n');
  55. }
  56. function isDomainSuffix(suffixRule: string, domain: string) {
  57. if (suffixRule.length > domain.length + 1) {
  58. return false;
  59. }
  60. return suffixRule === domain || domain.endsWith('.' + suffixRule);
  61. }