build-domestic-ruleset.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 { processLineFromReadline } = require('./lib/process-line');
  6. const { compareAndWriteFile, createRuleset } = require('./lib/create-file');
  7. const { task } = require('./lib/trace-runner');
  8. const buildDomesticRuleset = task(__filename, async () => {
  9. const results = await processLineFromReadline(readFileByLine(path.resolve(__dirname, '../Source/non_ip/domestic.conf')));
  10. results.push(
  11. ...Object.entries(DOMESTICS)
  12. .reduce(
  13. (acc, [key, { domains }]) => {
  14. if (key === 'SYSTEM') return acc;
  15. return [...acc, ...domains];
  16. },
  17. /** @type {string[]} */([])
  18. )
  19. .map((domain) => `DOMAIN-SUFFIX,${domain}`)
  20. );
  21. const rulesetDescription = [
  22. 'License: AGPL 3.0',
  23. 'Homepage: https://ruleset.skk.moe',
  24. 'GitHub: https://github.com/SukkaW/Surge',
  25. '',
  26. 'This file contains known addresses that are avaliable in the Mainland China.'
  27. ];
  28. return Promise.all([
  29. ...createRuleset(
  30. 'Sukka\'s Ruleset - Domestic Domains',
  31. rulesetDescription,
  32. new Date(),
  33. results,
  34. 'ruleset',
  35. path.resolve(__dirname, '../List/non_ip/domestic.conf'),
  36. path.resolve(__dirname, '../Clash/non_ip/domestic.txt')
  37. ),
  38. compareAndWriteFile(
  39. [
  40. '#!name=[Sukka] Local DNS Mapping',
  41. `#!desc=Last Updated: ${new Date().toISOString()}`,
  42. '',
  43. '[Host]',
  44. ...Object.entries(DOMESTICS)
  45. .flatMap(
  46. ([, { domains, dns }]) => domains.flatMap((domain) => [
  47. `${domain} = server:${dns}`,
  48. `*.${domain} = server:${dns}`
  49. ])
  50. )
  51. ],
  52. path.resolve(__dirname, '../Modules/sukka_local_dns_mapping.sgmodule')
  53. )
  54. ]);
  55. });
  56. module.exports.buildDomesticRuleset = buildDomesticRuleset;
  57. if (import.meta.main) {
  58. buildDomesticRuleset();
  59. }