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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, 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. import * as yaml from 'yaml';
  11. import { appendArrayInPlace } from './lib/append-array-in-place';
  12. import { output, writeFile } from './lib/misc';
  13. import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR, SOURCE_DIR } from './constants/dir';
  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 res = 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. createRuleset(
  36. span,
  37. 'Sukka\'s Ruleset - Domestic Domains',
  38. [
  39. ...SHARED_DESCRIPTION,
  40. '',
  41. 'This file contains known addresses that are avaliable in the Mainland China.'
  42. ],
  43. new Date(),
  44. res[0],
  45. 'ruleset',
  46. output('domestic', 'non_ip')
  47. ),
  48. createRuleset(
  49. span,
  50. 'Sukka\'s Ruleset - Direct Rules',
  51. [
  52. ...SHARED_DESCRIPTION,
  53. '',
  54. 'This file contains domains and process that should not be proxied.'
  55. ],
  56. new Date(),
  57. res[1],
  58. 'ruleset',
  59. output('direct', 'non_ip')
  60. ),
  61. createRuleset(
  62. span,
  63. 'Sukka\'s Ruleset - LAN',
  64. [
  65. ...SHARED_DESCRIPTION,
  66. '',
  67. 'This file includes rules for LAN DOMAIN and reserved TLDs.'
  68. ],
  69. new Date(),
  70. res[2],
  71. 'ruleset',
  72. output('lan', 'non_ip')
  73. ),
  74. compareAndWriteFile(
  75. span,
  76. [
  77. '#!name=[Sukka] Local DNS Mapping',
  78. `#!desc=Last Updated: ${new Date().toISOString()}`,
  79. '',
  80. '[Host]',
  81. ...dataset.flatMap(([, { domains, dns, hosts }]) => [
  82. ...Object.entries(hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`),
  83. ...domains.flatMap((domain) => [
  84. `${domain} = server:${dns}`,
  85. `*.${domain} = server:${dns}`
  86. ])
  87. ])
  88. ],
  89. path.resolve(OUTPUT_MODULES_DIR, 'sukka_local_dns_mapping.sgmodule')
  90. ),
  91. writeFile(
  92. path.join(OUTPUT_INTERNAL_DIR, 'clash_nameserver_policy.yaml'),
  93. yaml.stringify(
  94. {
  95. dns: {
  96. 'nameserver-policy': dataset.reduce<Record<string, string | string[]>>(
  97. (acc, [, { domains, dns }]) => {
  98. domains.forEach((domain) => {
  99. acc[`+.${domain}`] = dns === 'system'
  100. ? [
  101. 'system://',
  102. 'system',
  103. 'dhcp://system'
  104. ]
  105. : dns;
  106. });
  107. return acc;
  108. },
  109. {}
  110. )
  111. },
  112. hosts: dataset.reduce<Record<string, string>>(
  113. (acc, [, { domains, dns, ...rest }]) => {
  114. if ('hosts' in rest) {
  115. Object.assign(acc, rest.hosts);
  116. }
  117. return acc;
  118. },
  119. {}
  120. )
  121. },
  122. { version: '1.1' }
  123. )
  124. )
  125. ]);
  126. });