build-internal-cdn-rules.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  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 (const line of readFileByLine(domainSetPath)) {
  32. if (line[0] === '.') {
  33. addApexDomain(line.slice(1));
  34. } else if (isDomainLoose(line)) {
  35. addApexDomain(line);
  36. } else if (processLine(line)) {
  37. console.warn('[drop line from domainset]', line);
  38. }
  39. }
  40. };
  41. /**
  42. * @param {string} ruleSetPath
  43. */
  44. const processLocalRuleSet = async (ruleSetPath) => {
  45. for await (const line of readFileByLine(ruleSetPath)) {
  46. if (line.startsWith('DOMAIN-SUFFIX,')) {
  47. addApexDomain(line.replace('DOMAIN-SUFFIX,', ''));
  48. } else if (line.startsWith('DOMAIN,')) {
  49. addApexDomain(line.replace('DOMAIN,', ''));
  50. } else if (line.startsWith('DOMAIN-KEYWORD')) {
  51. keywords.add(escapeRegExp(line.replace('DOMAIN-KEYWORD,', '')));
  52. } else if (line.startsWith('USER-AGENT,') || line.startsWith('PROCESS-NAME,')) {
  53. // do nothing
  54. } else if (processLine(line)) {
  55. console.warn('[drop line from ruleset]', line);
  56. }
  57. }
  58. };
  59. await Promise.all([
  60. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/cdn.conf')),
  61. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global.conf')),
  62. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global_plus.conf')),
  63. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/my_proxy.conf')),
  64. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/stream.conf')),
  65. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/telegram.conf')),
  66. processLocalDomainSet(path.resolve(__dirname, '../List/domainset/cdn.conf')),
  67. processLocalDomainSet(path.resolve(__dirname, '../List/domainset/download.conf')),
  68. fse.ensureDir(path.resolve(__dirname, '../List/internal'))
  69. ]);
  70. await fs.promises.writeFile(
  71. path.resolve(__dirname, '../List/internal/cdn.txt'),
  72. [
  73. ...Array.from(set).map(i => `SUFFIX,${i}`),
  74. ...Array.from(keywords).map(i => `REGEX,${i}`),
  75. ''
  76. ].join('\n')
  77. );
  78. })();