build-domestic-direct-lan-ruleset-dns-mapping-module.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // @ts-check
  2. import path from 'node:path';
  3. import { DOMESTICS } from '../Source/non_ip/domestic';
  4. import { DIRECTS, LANS } from '../Source/non_ip/direct';
  5. import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
  6. import { compareAndWriteFile } from './lib/create-file';
  7. import { task } from './trace';
  8. import { SHARED_DESCRIPTION } from './lib/constants';
  9. import { createMemoizedPromise } from './lib/memo-promise';
  10. import * as yaml from 'yaml';
  11. import { appendArrayInPlace } from './lib/append-array-in-place';
  12. import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR, SOURCE_DIR } from './constants/dir';
  13. import { RulesetOutput } from './lib/create-file';
  14. export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(async () => {
  15. const domestics = await readFileIntoProcessedArray(path.join(SOURCE_DIR, 'non_ip/domestic.conf'));
  16. const directs = await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'non_ip/direct.conf'));
  17. const lans: string[] = [];
  18. Object.entries(DOMESTICS).forEach(([, { domains }]) => {
  19. appendArrayInPlace(domestics, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
  20. });
  21. Object.entries(DIRECTS).forEach(([, { domains }]) => {
  22. appendArrayInPlace(directs, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
  23. });
  24. Object.entries(LANS).forEach(([, { domains }]) => {
  25. appendArrayInPlace(lans, domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
  26. });
  27. return [domestics, directs, lans] as const;
  28. });
  29. export const buildDomesticRuleset = task(require.main === module, __filename)(async (span) => {
  30. const [domestics, directs, lans] = await getDomesticAndDirectDomainsRulesetPromise();
  31. const dataset = Object.entries(DOMESTICS);
  32. appendArrayInPlace(dataset, Object.entries(DIRECTS));
  33. appendArrayInPlace(dataset, Object.entries(LANS));
  34. return Promise.all([
  35. new RulesetOutput(span, 'domestic', 'non_ip')
  36. .withTitle('Sukka\'s Ruleset - Domestic Domains')
  37. .withDescription([
  38. ...SHARED_DESCRIPTION,
  39. '',
  40. 'This file contains known addresses that are avaliable in the Mainland China.'
  41. ])
  42. .addFromRuleset(domestics)
  43. .write(),
  44. new RulesetOutput(span, 'direct', 'non_ip')
  45. .withTitle('Sukka\'s Ruleset - Direct Rules')
  46. .withDescription([
  47. ...SHARED_DESCRIPTION,
  48. '',
  49. 'This file contains domains and process that should not be proxied.'
  50. ])
  51. .addFromRuleset(directs)
  52. .write(),
  53. new RulesetOutput(span, 'lan', 'non_ip')
  54. .withTitle('Sukka\'s Ruleset - LAN')
  55. .withDescription([
  56. ...SHARED_DESCRIPTION,
  57. '',
  58. 'This file includes rules for LAN DOMAIN and reserved TLDs.'
  59. ])
  60. .addFromRuleset(lans)
  61. .write(),
  62. compareAndWriteFile(
  63. span,
  64. [
  65. '#!name=[Sukka] Local DNS Mapping',
  66. `#!desc=Last Updated: ${new Date().toISOString()}`,
  67. '',
  68. '[Host]',
  69. ...dataset.flatMap(([, { domains, dns, hosts }]) => [
  70. ...Object.entries(hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`),
  71. ...domains.flatMap((domain) => [
  72. `${domain} = server:${dns}`,
  73. `*.${domain} = server:${dns}`
  74. ])
  75. ])
  76. ],
  77. path.resolve(OUTPUT_MODULES_DIR, 'sukka_local_dns_mapping.sgmodule')
  78. ),
  79. compareAndWriteFile(
  80. span,
  81. yaml.stringify(
  82. {
  83. dns: {
  84. 'nameserver-policy': dataset.reduce<Record<string, string | string[]>>(
  85. (acc, [, { domains, dns }]) => {
  86. domains.forEach((domain) => {
  87. acc[`+.${domain}`] = dns === 'system'
  88. ? [
  89. 'system://',
  90. 'system',
  91. 'dhcp://system'
  92. ]
  93. : dns;
  94. });
  95. return acc;
  96. },
  97. {}
  98. )
  99. },
  100. hosts: dataset.reduce<Record<string, string>>(
  101. (acc, [, { domains, dns, ...rest }]) => {
  102. if ('hosts' in rest) {
  103. Object.assign(acc, rest.hosts);
  104. }
  105. return acc;
  106. },
  107. {}
  108. )
  109. },
  110. { version: '1.1' }
  111. ).split('\n'),
  112. path.join(OUTPUT_INTERNAL_DIR, 'clash_nameserver_policy.yaml')
  113. )
  114. ]);
  115. });