build-domestic-ruleset.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // @ts-check
  2. const path = require('path');
  3. const { DOMESTICS } = require('../Source/non_ip/domestic');
  4. const { readFileByLine } = require('./lib/fetch-remote-text-by-line');
  5. const { processLine } = require('./lib/process-line');
  6. const { compareAndWriteFile, createRuleset } = require('./lib/create-file');
  7. const domainSorter = require('./lib/stable-sort-domain');
  8. (async () => {
  9. const rl = readFileByLine(path.resolve(__dirname, '../Source/non_ip/domestic.conf'));
  10. const results = [];
  11. for await (const l of rl) {
  12. const line = processLine(l);
  13. if (line) {
  14. results.push(line);
  15. }
  16. }
  17. results.push(
  18. ...Object.entries(DOMESTICS)
  19. .filter(([key]) => key !== 'SYSTEM')
  20. .flatMap(([, { domains }]) => domains)
  21. .sort(domainSorter)
  22. .map((domain) => `DOMAIN-SUFFIX,${domain}`)
  23. );
  24. const rulesetDescription = [
  25. 'License: AGPL 3.0',
  26. 'Homepage: https://ruleset.skk.moe',
  27. 'GitHub: https://github.com/SukkaW/Surge',
  28. '',
  29. 'This file contains known addresses that are avaliable in the Mainland China.'
  30. ];
  31. await Promise.all([
  32. ...createRuleset(
  33. 'Sukka\'s Ruleset - Domestic Domains',
  34. rulesetDescription,
  35. new Date(),
  36. results,
  37. 'ruleset',
  38. path.resolve(__dirname, '../List/non_ip/domestic.conf'),
  39. path.resolve(__dirname, '../Clash/non_ip/domestic.txt')
  40. ),
  41. compareAndWriteFile(
  42. [
  43. '#!name=[Sukka] Local DNS Mapping',
  44. `#!desc=Last Updated: ${new Date().toISOString()}`,
  45. '',
  46. '[Host]',
  47. ...Object.entries(DOMESTICS)
  48. .flatMap(
  49. ([, { domains, dns }]) => domains.flatMap((domain) => [
  50. `${domain} = server:${dns}`,
  51. `*.${domain} = server:${dns}`
  52. ])
  53. ),
  54. ''
  55. ],
  56. path.resolve(__dirname, '../Modules/sukka_local_dns_mapping.sgmodule')
  57. )
  58. ]);
  59. })();