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