build-internal-cdn-rules.js 2.9 KB

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