build-domestic-ruleset.ts 1.9 KB

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