build-internal-cdn-rules.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // @ts-check
  2. const fs = require('fs');
  3. const fse = require('fs-extra');
  4. const path = require('path');
  5. const { isDomainLoose } = require('./lib/is-domain-loose');
  6. const tldts = require('tldts');
  7. const { processLine } = require('./lib/process-line');
  8. const { readFileByLine } = require('./lib/fetch-remote-text-by-line');
  9. const domainSorter = require('./lib/stable-sort-domain');
  10. /**
  11. * @param {string} string
  12. */
  13. const escapeRegExp = (string) => {
  14. return string.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
  15. };
  16. (async () => {
  17. const set = new Set();
  18. const keywords = new Set();
  19. /**
  20. * @param {string} input
  21. */
  22. const addApexDomain = (input) => {
  23. const d = tldts.getDomain(input, { allowPrivateDomains: true });
  24. if (d) {
  25. set.add(d);
  26. }
  27. };
  28. /**
  29. * @param {string} domainSetPath
  30. */
  31. const processLocalDomainSet = async (domainSetPath) => {
  32. for await (const line of readFileByLine(domainSetPath)) {
  33. if (line[0] === '.') {
  34. addApexDomain(line.slice(1));
  35. } else if (isDomainLoose(line)) {
  36. addApexDomain(line);
  37. } else if (processLine(line)) {
  38. console.warn('[drop line from domainset]', line);
  39. }
  40. }
  41. };
  42. /**
  43. * @param {string} ruleSetPath
  44. */
  45. const processLocalRuleSet = async (ruleSetPath) => {
  46. for await (const line of readFileByLine(ruleSetPath)) {
  47. if (line.startsWith('DOMAIN-SUFFIX,')) {
  48. addApexDomain(line.replace('DOMAIN-SUFFIX,', ''));
  49. } else if (line.startsWith('DOMAIN,')) {
  50. addApexDomain(line.replace('DOMAIN,', ''));
  51. } else if (line.startsWith('DOMAIN-KEYWORD')) {
  52. keywords.add(escapeRegExp(line.replace('DOMAIN-KEYWORD,', '')));
  53. } else if (line.startsWith('USER-AGENT,') || line.startsWith('PROCESS-NAME,')) {
  54. // do nothing
  55. } else if (processLine(line)) {
  56. console.warn('[drop line from ruleset]', line);
  57. }
  58. }
  59. };
  60. await Promise.all([
  61. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/cdn.conf')),
  62. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global.conf')),
  63. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global_plus.conf')),
  64. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/my_proxy.conf')),
  65. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/stream.conf')),
  66. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/telegram.conf')),
  67. processLocalDomainSet(path.resolve(__dirname, '../List/domainset/cdn.conf')),
  68. processLocalDomainSet(path.resolve(__dirname, '../List/domainset/download.conf')),
  69. fse.ensureDir(path.resolve(__dirname, '../List/internal'))
  70. ]);
  71. await fs.promises.writeFile(
  72. path.resolve(__dirname, '../List/internal/cdn.txt'),
  73. [
  74. ...Array.from(set).sort(domainSorter).map(i => `SUFFIX,${i}`),
  75. ...Array.from(keywords).sort().map(i => `REGEX,${i}`),
  76. ''
  77. ].join('\n')
  78. );
  79. })();