build-internal-cdn-rules.js 2.9 KB

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