build-domestic-ruleset.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // @ts-check
  2. import path from 'path';
  3. import { DOMESTICS } from '../Source/non_ip/domestic';
  4. import { readFileByLine } from './lib/fetch-remote-text-by-line';
  5. import { processLineFromReadline } from './lib/process-line';
  6. import { compareAndWriteFile, createRuleset } from './lib/create-file';
  7. import { task } from './lib/trace-runner';
  8. export 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<string[]>(
  13. (acc, [key, { domains }]) => {
  14. if (key === 'SYSTEM') return acc;
  15. return [...acc, ...domains];
  16. },
  17. []
  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. if (import.meta.main) {
  57. buildDomesticRuleset();
  58. }