build-domestic-ruleset.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 { withBannerArray } = require('./lib/with-banner');
  7. const { compareAndWriteFile } = require('./lib/string-array-compare');
  8. const domainSorter = require('./lib/stable-sort-domain');
  9. const { surgeRulesetToClashClassicalTextRuleset } = require('./lib/clash');
  10. (async () => {
  11. const rl = readFileByLine(path.resolve(__dirname, '../Source/non_ip/domestic.conf'));
  12. const results = [];
  13. for await (const l of rl) {
  14. const line = processLine(l);
  15. if (line) {
  16. results.push(line);
  17. }
  18. }
  19. results.push(
  20. ...Object.entries(DOMESTICS)
  21. .filter(([key]) => key !== 'SYSTEM')
  22. .flatMap(([, { domains }]) => domains)
  23. .sort(domainSorter)
  24. .map((domain) => `DOMAIN-SUFFIX,${domain}`)
  25. );
  26. const rulesetDescription = [
  27. 'License: AGPL 3.0',
  28. 'Homepage: https://ruleset.skk.moe',
  29. 'GitHub: https://github.com/SukkaW/Surge',
  30. '',
  31. 'This file contains known addresses that are avaliable in the Mainland China.'
  32. ];
  33. await Promise.all([
  34. compareAndWriteFile(
  35. withBannerArray(
  36. 'Sukka\'s Ruleset - Domestic Domains',
  37. rulesetDescription,
  38. new Date(),
  39. results
  40. ),
  41. path.resolve(__dirname, '../List/non_ip/domestic.conf')
  42. ),
  43. compareAndWriteFile(
  44. withBannerArray(
  45. 'Sukka\'s Ruleset - Domestic Domains',
  46. rulesetDescription,
  47. new Date(),
  48. surgeRulesetToClashClassicalTextRuleset(results)
  49. ),
  50. path.resolve(__dirname, '../Clash/non_ip/domestic.txt')
  51. ),
  52. compareAndWriteFile(
  53. [
  54. '#!name=[Sukka] Local DNS Mapping',
  55. `#!desc=Last Updated: ${new Date().toISOString()}`,
  56. '',
  57. '[Host]',
  58. ...Object.entries(DOMESTICS)
  59. .flatMap(
  60. ([, { domains, dns }]) => domains.flatMap((domain) => [
  61. `${domain} = server:${dns}`,
  62. `*.${domain} = server:${dns}`
  63. ])
  64. ),
  65. ''
  66. ],
  67. path.resolve(__dirname, '../Modules/sukka_local_dns_mapping.sgmodule')
  68. )
  69. ]);
  70. })();