build-domestic-ruleset.ts 2.0 KB

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