build-internal-cdn-rules.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/cdn.conf'));
  60. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global.conf'));
  61. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global_plus.conf'));
  62. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/my_proxy.conf'));
  63. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/stream.conf'));
  64. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/telegram.conf'));
  65. await processLocalDomainSet(path.resolve(__dirname, '../List/domainset/cdn.conf'));
  66. await processLocalDomainSet(path.resolve(__dirname, '../List/domainset/download.conf'));
  67. await fse.ensureDir(path.resolve(__dirname, '../List/internal'));
  68. await fs.promises.writeFile(
  69. path.resolve(__dirname, '../List/internal/cdn.txt'),
  70. [
  71. ...Array.from(set).map(i => `SUFFIX,${i}`),
  72. ...Array.from(keywords).map(i => `REGEX,${i}`),
  73. ''
  74. ].join('\n')
  75. );
  76. })();