build-domestic-ruleset.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // @ts-check
  2. import path from 'path';
  3. import { DOMESTICS } from '../Source/non_ip/domestic';
  4. import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
  5. import { compareAndWriteFile, createRuleset } from './lib/create-file';
  6. import { task } from './trace';
  7. import { SHARED_DESCRIPTION } from './lib/constants';
  8. import { createMemoizedPromise } from './lib/memo-promise';
  9. export const getDomesticDomainsRulesetPromise = createMemoizedPromise(async () => {
  10. const results = await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../Source/non_ip/domestic.conf'));
  11. results.push(
  12. ...Object.entries(DOMESTICS).reduce<string[]>((acc, [key, { domains }]) => {
  13. if (key === 'SYSTEM') return acc;
  14. return [...acc, ...domains];
  15. }, []).map((domain) => `DOMAIN-SUFFIX,${domain}`)
  16. );
  17. return results;
  18. });
  19. export const buildDomesticRuleset = task(import.meta.main, import.meta.path)(async (span) => {
  20. const rulesetDescription = [
  21. ...SHARED_DESCRIPTION,
  22. '',
  23. 'This file contains known addresses that are avaliable in the Mainland China.'
  24. ];
  25. const res = await getDomesticDomainsRulesetPromise();
  26. return Promise.all([
  27. createRuleset(
  28. span,
  29. 'Sukka\'s Ruleset - Domestic Domains',
  30. rulesetDescription,
  31. new Date(),
  32. res,
  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. span,
  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, ...rest }]) => [
  47. ...(
  48. 'hosts' in rest
  49. ? Object.entries(rest.hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`)
  50. : []
  51. ),
  52. ...domains.flatMap((domain) => [
  53. `${domain} = server:${dns}`,
  54. `*.${domain} = server:${dns}`
  55. ])
  56. ]
  57. )
  58. ],
  59. path.resolve(import.meta.dir, '../Modules/sukka_local_dns_mapping.sgmodule')
  60. )
  61. ]);
  62. });