build-internal-cdn-rules.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import fsp from 'fs/promises';
  2. import path from 'path';
  3. import * as tldts from 'tldts';
  4. import { processLine } from './lib/process-line';
  5. import { readFileByLine } from './lib/fetch-remote-text-by-line';
  6. import { createDomainSorter } from './lib/stable-sort-domain';
  7. import { task } from './lib/trace-runner';
  8. import { compareAndWriteFile } from './lib/create-file';
  9. import { getGorhillPublicSuffixPromise } from './lib/get-gorhill-publicsuffix';
  10. // const { createCachedGorhillGetDomain } = require('./lib/cached-tld-parse');
  11. const escapeRegExp = (string = '') => string.replaceAll(/[$()*+.?[\\\]^{|}]/g, '\\$&');
  12. export const buildInternalCDNDomains = task(import.meta.path, async () => {
  13. const set = new Set<string>();
  14. const keywords = new Set<string>();
  15. const gorhill = await getGorhillPublicSuffixPromise();
  16. const domainSorter = createDomainSorter(gorhill);
  17. const addApexDomain = (input: string) => {
  18. // We are including the private domains themselves
  19. const d = tldts.getDomain(input, { allowPrivateDomains: false });
  20. if (d) {
  21. set.add(d);
  22. }
  23. };
  24. const processLocalDomainSet = async (domainSetPath: string) => {
  25. for await (const line of readFileByLine(domainSetPath)) {
  26. // console.log({ line });
  27. const parsed = tldts.parse(line, { allowPrivateDomains: true, detectIp: false });
  28. if (parsed.isIp) continue;
  29. if (parsed.isIcann || parsed.isPrivate) {
  30. if (parsed.domain) {
  31. set.add(parsed.domain);
  32. }
  33. continue;
  34. }
  35. if (processLine(line)) {
  36. console.warn('[drop line from domainset]', line);
  37. }
  38. }
  39. };
  40. const processLocalRuleSet = async (ruleSetPath: string) => {
  41. for await (const line of readFileByLine(ruleSetPath)) {
  42. if (line.startsWith('DOMAIN-SUFFIX,')) {
  43. addApexDomain(line.replace('DOMAIN-SUFFIX,', ''));
  44. } else if (line.startsWith('DOMAIN,')) {
  45. addApexDomain(line.replace('DOMAIN,', ''));
  46. } else if (line.startsWith('DOMAIN-KEYWORD')) {
  47. keywords.add(escapeRegExp(line.replace('DOMAIN-KEYWORD,', '')));
  48. } else if (line.startsWith('USER-AGENT,') || line.startsWith('PROCESS-NAME,')) {
  49. // do nothing
  50. } else if (processLine(line)) {
  51. console.warn('[drop line from ruleset]', line);
  52. }
  53. }
  54. };
  55. await Promise.all([
  56. processLocalRuleSet(path.resolve(import.meta.dir, '../List/non_ip/cdn.conf')),
  57. processLocalRuleSet(path.resolve(import.meta.dir, '../List/non_ip/global.conf')),
  58. processLocalRuleSet(path.resolve(import.meta.dir, '../List/non_ip/global_plus.conf')),
  59. processLocalRuleSet(path.resolve(import.meta.dir, '../List/non_ip/my_proxy.conf')),
  60. processLocalRuleSet(path.resolve(import.meta.dir, '../List/non_ip/stream.conf')),
  61. processLocalRuleSet(path.resolve(import.meta.dir, '../List/non_ip/telegram.conf')),
  62. processLocalDomainSet(path.resolve(import.meta.dir, '../List/domainset/cdn.conf')),
  63. processLocalDomainSet(path.resolve(import.meta.dir, '../List/domainset/download.conf')),
  64. fsp.mkdir(path.resolve(import.meta.dir, '../List/internal'), { recursive: true })
  65. ]);
  66. return compareAndWriteFile(
  67. [
  68. ...Array.from(set).sort(domainSorter).map(i => `SUFFIX,${i}`),
  69. ...Array.from(keywords).sort().map(i => `REGEX,${i}`)
  70. ],
  71. path.resolve(import.meta.dir, '../List/internal/cdn.txt')
  72. );
  73. });
  74. if (import.meta.main) {
  75. buildInternalCDNDomains();
  76. }