build-internal-cdn-rules.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // @ts-check
  2. const fse = require('fs-extra');
  3. const path = require('path');
  4. const tldts = require('tldts');
  5. const { processLine } = require('./lib/process-line');
  6. const { readFileByLine } = require('./lib/fetch-remote-text-by-line');
  7. const domainSorter = require('./lib/stable-sort-domain');
  8. const { runner, task } = require('./lib/trace-runner');
  9. const { compareAndWriteFile } = require('./lib/create-file');
  10. /**
  11. * @param {string} string
  12. */
  13. const escapeRegExp = (string) => {
  14. return string.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
  15. };
  16. const buildInternalCDNDomains = task(__filename, 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. const parsed = tldts.parse(line, { allowPrivateDomains: true });
  34. if (!parsed.isIp && (parsed.isIcann || parsed.isPrivate)) {
  35. if (parsed.domain) {
  36. set.add(parsed.domain);
  37. }
  38. continue;
  39. }
  40. if (processLine(line)) {
  41. console.warn('[drop line from domainset]', line);
  42. }
  43. }
  44. };
  45. /**
  46. * @param {string} ruleSetPath
  47. */
  48. const processLocalRuleSet = async (ruleSetPath) => {
  49. for await (const line of readFileByLine(ruleSetPath)) {
  50. if (line.startsWith('DOMAIN-SUFFIX,')) {
  51. addApexDomain(line.replace('DOMAIN-SUFFIX,', ''));
  52. } else if (line.startsWith('DOMAIN,')) {
  53. addApexDomain(line.replace('DOMAIN,', ''));
  54. } else if (line.startsWith('DOMAIN-KEYWORD')) {
  55. keywords.add(escapeRegExp(line.replace('DOMAIN-KEYWORD,', '')));
  56. } else if (line.startsWith('USER-AGENT,') || line.startsWith('PROCESS-NAME,')) {
  57. // do nothing
  58. } else if (processLine(line)) {
  59. console.warn('[drop line from ruleset]', line);
  60. }
  61. }
  62. };
  63. await Promise.all([
  64. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/cdn.conf')),
  65. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global.conf')),
  66. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global_plus.conf')),
  67. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/my_proxy.conf')),
  68. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/stream.conf')),
  69. processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/telegram.conf')),
  70. processLocalDomainSet(path.resolve(__dirname, '../List/domainset/cdn.conf')),
  71. processLocalDomainSet(path.resolve(__dirname, '../List/domainset/download.conf')),
  72. fse.ensureDir(path.resolve(__dirname, '../List/internal'))
  73. ]);
  74. await compareAndWriteFile(
  75. [
  76. ...Array.from(set).sort(domainSorter).map(i => `SUFFIX,${i}`),
  77. ...Array.from(keywords).sort().map(i => `REGEX,${i}`)
  78. ],
  79. path.resolve(__dirname, '../List/internal/cdn.txt')
  80. );
  81. });
  82. module.exports.buildInternalCDNDomains = buildInternalCDNDomains;
  83. if (require.main === module) {
  84. runner(__filename, buildInternalCDNDomains);
  85. }