build-internal-cdn-rules.ts 3.3 KB

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