build-internal-rules.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. (async () => {
  10. const set = new Set();
  11. /**
  12. * @param {string} input
  13. */
  14. const addApexDomain = (input) => {
  15. const d = tldts.getDomain(input, { allowPrivateDomains: true });
  16. if (d) {
  17. set.add(d);
  18. }
  19. };
  20. /**
  21. * @param {string} domainSetPath
  22. */
  23. const processLocalDomainSet = async (domainSetPath) => {
  24. for await (
  25. const line of readline.createInterface({
  26. input: fs.createReadStream(domainSetPath),
  27. crlfDelay: Infinity
  28. })
  29. ) {
  30. if (line[0] === '.') {
  31. addApexDomain(line.slice(1));
  32. } else if (isDomainLoose(line)) {
  33. addApexDomain(line);
  34. } else if (processLine(line)) {
  35. console.warn('[drop line from domainset]', line);
  36. }
  37. }
  38. };
  39. /**
  40. * @param {string} ruleSetPath
  41. */
  42. const processLocalRuleSet = async (ruleSetPath) => {
  43. for await (
  44. const line of readline.createInterface({
  45. input: fs.createReadStream(ruleSetPath),
  46. crlfDelay: Infinity
  47. })
  48. ) {
  49. if (line.startsWith('DOMAIN-SUFFIX,')) {
  50. addApexDomain(line.replace('DOMAIN-SUFFIX,', ''));
  51. } else if (line.startsWith('DOMAIN,')) {
  52. addApexDomain(line.replace('DOMAIN,', ''));
  53. } else if (processLine(line)) {
  54. console.warn('[drop line from ruleset]', line);
  55. }
  56. }
  57. };
  58. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/cdn.conf'));
  59. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global.conf'));
  60. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/global_plus.conf'));
  61. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/my_proxy.conf'));
  62. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/stream.conf'));
  63. await processLocalRuleSet(path.resolve(__dirname, '../List/non_ip/telegram.conf'));
  64. await processLocalDomainSet(path.resolve(__dirname, '../List/domainset/cdn.conf'));
  65. await processLocalDomainSet(path.resolve(__dirname, '../List/domainset/download.conf'));
  66. await fse.ensureDir(path.resolve(__dirname, '../List/internal'));
  67. await fs.promises.writeFile(
  68. path.resolve(__dirname, '../List/internal/cdn.txt'),
  69. `${Array.from(set).map(i => `SUFFIX,${i}`).join('\n')}\n`
  70. );
  71. })();