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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // @ts-check
  2. import path from 'node:path';
  3. import { DOMESTICS, DOH_BOOTSTRAP, AdGuardHomeDNSMapping } from '../Source/non_ip/domestic';
  4. import { DIRECTS, HOSTS, LAN } from '../Source/non_ip/direct';
  5. import type { DNSMapping } from '../Source/non_ip/direct';
  6. import { fetchRemoteTextByLine, readFileIntoProcessedArray } from './lib/fetch-text-by-line';
  7. import { compareAndWriteFile } from './lib/create-file';
  8. import { task } from './trace';
  9. import type { Span } from './trace';
  10. import { SHARED_DESCRIPTION } from './constants/description';
  11. import { once } from 'foxts/once';
  12. import * as yaml from 'yaml';
  13. import { appendArrayInPlace } from 'foxts/append-array-in-place';
  14. import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR, OUTPUT_MODULES_RULES_DIR, SOURCE_DIR } from './constants/dir';
  15. import { MihomoNameserverPolicyOutput, RulesetOutput, SurgeOnlyRulesetOutput } from './lib/rules/ruleset';
  16. import { $$fetch } from './lib/fetch-retry';
  17. export function createGetDnsMappingRule(allowWildcard: boolean) {
  18. const hasWildcard = (domain: string) => {
  19. if (domain.includes('*') || domain.includes('?')) {
  20. if (!allowWildcard) {
  21. throw new TypeError(`Wildcard domain is not supported: ${domain}`);
  22. }
  23. return true;
  24. }
  25. return false;
  26. };
  27. return (domain: string): string[] => {
  28. const results: string[] = [];
  29. if (domain[0] === '$') {
  30. const d = domain.slice(1);
  31. if (hasWildcard(domain)) {
  32. results.push(`DOMAIN-WILDCARD,${d}`);
  33. } else {
  34. results.push(`DOMAIN,${d}`);
  35. }
  36. } else if (domain[0] === '+') {
  37. const d = domain.slice(1);
  38. if (hasWildcard(domain)) {
  39. results.push(`DOMAIN-WILDCARD,*.${d}`);
  40. } else {
  41. results.push(`DOMAIN-SUFFIX,${d}`);
  42. }
  43. } else if (hasWildcard(domain)) {
  44. results.push(`DOMAIN-WILDCARD,${domain}`, `DOMAIN-WILDCARD,*.${domain}`);
  45. } else {
  46. results.push(`DOMAIN-SUFFIX,${domain}`);
  47. }
  48. return results;
  49. };
  50. }
  51. export const getDomesticAndDirectDomainsRulesetPromise = once(async () => {
  52. const domestics = await readFileIntoProcessedArray(path.join(SOURCE_DIR, 'non_ip/domestic.conf'));
  53. const directs = await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'non_ip/direct.conf'));
  54. const lans: string[] = [];
  55. const getDnsMappingRuleWithWildcard = createGetDnsMappingRule(true);
  56. [DOH_BOOTSTRAP, DOMESTICS].forEach((item) => {
  57. Object.values(item).forEach(({ domains }) => {
  58. appendArrayInPlace(domestics, domains.flatMap(getDnsMappingRuleWithWildcard));
  59. });
  60. });
  61. Object.values(DIRECTS).forEach(({ domains }) => {
  62. appendArrayInPlace(directs, domains.flatMap(getDnsMappingRuleWithWildcard));
  63. });
  64. Object.values(LAN).forEach(({ domains }) => {
  65. appendArrayInPlace(directs, domains.flatMap(getDnsMappingRuleWithWildcard));
  66. // backward compatible, add lan.conf
  67. appendArrayInPlace(lans, domains.flatMap(getDnsMappingRuleWithWildcard));
  68. });
  69. return [domestics, directs, lans] as const;
  70. });
  71. export const buildDomesticRuleset = task(require.main === module, __filename)(async (span) => {
  72. const [domestics, directs, lans] = await getDomesticAndDirectDomainsRulesetPromise();
  73. const dataset: Array<[name: string, DNSMapping]> = ([DOH_BOOTSTRAP, DOMESTICS, DIRECTS, LAN, HOSTS] as const).flatMap(Object.entries);
  74. return Promise.all([
  75. new RulesetOutput(span, 'domestic', 'non_ip')
  76. .withTitle('Sukka\'s Ruleset - Domestic Domains')
  77. .appendDescription(
  78. SHARED_DESCRIPTION,
  79. '',
  80. 'This file contains known addresses that are avaliable in the Mainland China.'
  81. )
  82. .addFromRuleset(domestics)
  83. .write(),
  84. new RulesetOutput(span, 'direct', 'non_ip')
  85. .withTitle('Sukka\'s Ruleset - Direct Rules')
  86. .appendDescription(
  87. SHARED_DESCRIPTION,
  88. '',
  89. 'This file contains domains and process that should not be proxied.'
  90. )
  91. .addFromRuleset(directs)
  92. .write(),
  93. new RulesetOutput(span, 'lan', 'non_ip')
  94. .withTitle('Sukka\'s Ruleset - LAN')
  95. .appendDescription(
  96. SHARED_DESCRIPTION,
  97. '',
  98. 'This file includes rules for LAN DOMAIN and reserved TLDs.'
  99. )
  100. .addFromRuleset(lans)
  101. .write(),
  102. buildLANCacheRuleset(span),
  103. ...dataset.map(([name, { ruleset, domains }]) => {
  104. if (!ruleset) {
  105. return;
  106. }
  107. const surgeOutput = new SurgeOnlyRulesetOutput(
  108. span,
  109. name.toLowerCase(),
  110. 'sukka_local_dns_mapping',
  111. OUTPUT_MODULES_RULES_DIR
  112. )
  113. .withTitle(`Sukka's Ruleset - Local DNS Mapping (${name})`)
  114. .appendDescription(
  115. SHARED_DESCRIPTION,
  116. '',
  117. 'This is an internal rule that is only referenced by sukka_local_dns_mapping.sgmodule',
  118. 'Do not use this file in your Rule section, all entries are included in non_ip/domestic.conf already.'
  119. );
  120. const mihomoOutput = new MihomoNameserverPolicyOutput(
  121. span,
  122. name.toLowerCase(),
  123. 'mihomo_nameserver_policy',
  124. OUTPUT_INTERNAL_DIR
  125. )
  126. .withTitle(`Sukka's Ruleset - Local DNS Mapping for Mihomo NameServer Policy (${name})`)
  127. .appendDescription(
  128. SHARED_DESCRIPTION,
  129. '',
  130. 'This ruleset is only used for mihomo\'s nameserver-policy feature, which',
  131. 'is similar to the RULE-SET referenced by sukka_local_dns_mapping.sgmodule.',
  132. 'Do not use this file in your Rule section, all entries are included in non_ip/domestic.conf already.'
  133. );
  134. domains.forEach((domain) => {
  135. switch (domain[0]) {
  136. case '$':
  137. surgeOutput.addDomain(domain.slice(1));
  138. mihomoOutput.addDomain(domain.slice(1));
  139. break;
  140. case '+':
  141. surgeOutput.addDomainSuffix(domain.slice(1));
  142. mihomoOutput.addDomainSuffix(domain.slice(1));
  143. break;
  144. default:
  145. surgeOutput.addDomainSuffix(domain);
  146. mihomoOutput.addDomainSuffix(domain);
  147. break;
  148. }
  149. });
  150. return Promise.all([
  151. surgeOutput.write(),
  152. mihomoOutput.write()
  153. ]);
  154. }),
  155. compareAndWriteFile(
  156. span,
  157. [
  158. '#!name=[Sukka] Local DNS Mapping',
  159. `#!desc=Last Updated: ${new Date().toISOString()}`,
  160. '',
  161. '[Host]',
  162. ...Object.entries(
  163. // I use an object to deduplicate the domains
  164. // Otherwise I could just construct an array directly
  165. dataset.reduce<Record<string, string>>((acc, cur) => {
  166. const ruleset_name = cur[0].toLowerCase();
  167. const { domains, dns, hosts, ruleset } = cur[1];
  168. Object.entries(hosts).forEach(([dns, ips]) => {
  169. acc[dns] ||= ips.join(', ');
  170. });
  171. if (ruleset) {
  172. acc[`RULE-SET:https://ruleset.skk.moe/Modules/Rules/sukka_local_dns_mapping/${ruleset_name}.conf`] ||= `server:${dns}`;
  173. } else {
  174. domains.forEach((domain) => {
  175. switch (domain[0]) {
  176. case '$':
  177. acc[domain.slice(1)] ||= `server:${dns}`;
  178. break;
  179. case '+':
  180. acc[`*.${domain.slice(1)}`] ||= `server:${dns}`;
  181. break;
  182. default:
  183. acc[domain] ||= `server:${dns}`;
  184. acc[`*.${domain}`] ||= `server:${dns}`;
  185. break;
  186. }
  187. });
  188. }
  189. return acc;
  190. }, {})
  191. ).map(([dns, ips]) => `${dns} = ${ips}`)
  192. ],
  193. path.resolve(OUTPUT_MODULES_DIR, 'sukka_local_dns_mapping.sgmodule')
  194. ),
  195. compareAndWriteFile(
  196. span,
  197. yaml.stringify(
  198. dataset.reduce<{
  199. dns: { 'nameserver-policy': Record<string, string | string[]> },
  200. hosts: Record<string, string | string[]>,
  201. 'rule-providers': Record<string, {
  202. type: 'http',
  203. path: `./sukkaw_ruleset/${string}`,
  204. url: string,
  205. behavior: 'classical',
  206. format: 'text',
  207. interval: number
  208. }>
  209. }>((acc, cur) => {
  210. const { domains, dns, ruleset, ...rest } = cur[1];
  211. if (ruleset) {
  212. const ruleset_name = cur[0].toLowerCase();
  213. const mihomo_ruleset_id = `mihomo_nameserver_policy_${ruleset_name}`;
  214. if (dns) {
  215. acc.dns['nameserver-policy'][`rule-set:${mihomo_ruleset_id}`] = dns;
  216. }
  217. acc['rule-providers'][mihomo_ruleset_id] = {
  218. type: 'http',
  219. path: `./sukkaw_ruleset/${mihomo_ruleset_id}.txt`,
  220. url: `https://ruleset.skk.moe/Internal/mihomo_nameserver_policy/${ruleset_name}.txt`,
  221. behavior: 'classical',
  222. format: 'text',
  223. interval: 43200
  224. };
  225. } else {
  226. domains.forEach((domain) => {
  227. switch (domain[0]) {
  228. case '$':
  229. domain = domain.slice(1);
  230. break;
  231. case '+':
  232. domain = `*.${domain.slice(1)}`;
  233. break;
  234. default:
  235. domain = `+.${domain}`;
  236. break;
  237. }
  238. if (dns) {
  239. acc.dns['nameserver-policy'][domain] = dns;
  240. }
  241. });
  242. }
  243. if ('hosts' in rest) {
  244. // eslint-disable-next-line guard-for-in -- known plain object
  245. for (const domain in rest.hosts) {
  246. const dest = rest.hosts[domain];
  247. if (domain in acc.hosts) {
  248. if (typeof acc.hosts[domain] === 'string') {
  249. acc.hosts[domain] = [acc.hosts[domain]];
  250. }
  251. acc.hosts[domain].push(...dest);
  252. } else if (dest.length === 1) {
  253. acc.hosts[domain] = dest[0];
  254. } else {
  255. acc.hosts[domain] = dest;
  256. }
  257. }
  258. }
  259. return acc;
  260. }, {
  261. dns: { 'nameserver-policy': {} },
  262. 'rule-providers': {},
  263. hosts: {}
  264. }),
  265. { version: '1.1' }
  266. ).split('\n'),
  267. path.join(OUTPUT_INTERNAL_DIR, 'clash_nameserver_policy.yaml')
  268. ),
  269. compareAndWriteFile(
  270. span,
  271. [
  272. '# Local DNS Mapping for AdGuard Home',
  273. 'https://doh.pub/dns-query',
  274. 'https://dns.alidns.com/dns-query',
  275. '[//]udp://10.10.1.1:53',
  276. ...(([DOMESTICS, DIRECTS, LAN, HOSTS] as const).flatMap(Object.values) as DNSMapping[]).flatMap(({ domains, dns: _dns }) => domains.flatMap((domain) => {
  277. if (!_dns) {
  278. return [];
  279. }
  280. let dns;
  281. if (_dns in AdGuardHomeDNSMapping) {
  282. dns = AdGuardHomeDNSMapping[_dns as keyof typeof AdGuardHomeDNSMapping].join(' ');
  283. } else {
  284. console.warn(`Unknown DNS "${_dns}" not in AdGuardHomeDNSMapping`);
  285. dns = _dns;
  286. }
  287. // if (
  288. // // AdGuard Home has built-in AS112 / private PTR handling
  289. // domain.endsWith('.arpa')
  290. // // Ignore simple hostname
  291. // || !domain.includes('.')
  292. // ) {
  293. // return [];
  294. // }
  295. if (domain[0] === '$') {
  296. return [
  297. `[/${domain.slice(1)}/]${dns}`
  298. ];
  299. }
  300. if (domain[0] === '+') {
  301. return [
  302. `[/${domain.slice(1)}/]${dns}`
  303. ];
  304. }
  305. return [
  306. `[/${domain}/]${dns}`
  307. ];
  308. }))
  309. ],
  310. path.resolve(OUTPUT_INTERNAL_DIR, 'dns_mapping_adguardhome.conf')
  311. )
  312. ]);
  313. });
  314. async function buildLANCacheRuleset(span: Span) {
  315. const childSpan = span.traceChild('build LAN cache ruleset');
  316. const cacheDomainsData = await childSpan.traceChildAsync('fetch cache_domains.json', async () => (await $$fetch('https://cdn.jsdelivr.net/gh/uklans/cache-domains@master/cache_domains.json')).json());
  317. if (!cacheDomainsData || typeof cacheDomainsData !== 'object' || !('cache_domains' in cacheDomainsData) || !Array.isArray(cacheDomainsData.cache_domains)) {
  318. throw new TypeError('Invalid cache domains data');
  319. }
  320. const allDomainFiles = cacheDomainsData.cache_domains.reduce<string[]>((acc, { domain_files }) => {
  321. if (Array.isArray(domain_files)) {
  322. appendArrayInPlace(acc, domain_files);
  323. }
  324. return acc;
  325. }, []);
  326. const allDomains = (
  327. await Promise.all(
  328. allDomainFiles.map(
  329. async (file) => childSpan.traceChildAsync(
  330. 'download ' + file,
  331. async () => Array.fromAsync(await fetchRemoteTextByLine('https://cdn.jsdelivr.net/gh/uklans/cache-domains@master/' + file, true))
  332. )
  333. )
  334. )
  335. ).flat();
  336. const surgeOutput = new SurgeOnlyRulesetOutput(
  337. span,
  338. 'lancache',
  339. 'sukka_local_dns_mapping',
  340. OUTPUT_MODULES_RULES_DIR
  341. )
  342. .withTitle('Sukka\'s Ruleset - Local DNS Mapping (lancache)')
  343. .appendDescription(
  344. SHARED_DESCRIPTION,
  345. '',
  346. 'This is an internal rule that is only referenced by sukka_local_dns_mapping.sgmodule',
  347. 'Do not use this file in your Rule section.'
  348. );
  349. const mihomoOutput = new MihomoNameserverPolicyOutput(
  350. span,
  351. 'lancache',
  352. 'mihomo_nameserver_policy',
  353. OUTPUT_INTERNAL_DIR
  354. )
  355. .withTitle('Sukka\'s Ruleset - Local DNS Mapping for Mihomo NameServer Policy (lancache)')
  356. .appendDescription(
  357. SHARED_DESCRIPTION,
  358. '',
  359. 'This ruleset is only used for mihomo\'s nameserver-policy feature, which',
  360. 'is similar to the RULE-SET referenced by sukka_local_dns_mapping.sgmodule.',
  361. 'Do not use this file in your Rule section.'
  362. );
  363. for (let i = 0, len = allDomains.length; i < len; i++) {
  364. const domain = allDomains[i];
  365. if (domain.includes('*')) {
  366. // If only *. prefix is used, we can convert it to DOMAIN-SUFFIX
  367. if (domain.startsWith('*.') && !domain.slice(2).includes('*')) {
  368. const domainSuffix = domain.slice(2);
  369. surgeOutput.addDomainSuffix(domainSuffix);
  370. mihomoOutput.addDomainSuffix(domainSuffix);
  371. continue;
  372. }
  373. surgeOutput.addDomainWildcard(domain);
  374. mihomoOutput.addDomainWildcard(domain);
  375. continue;
  376. }
  377. surgeOutput.addDomain(domain);
  378. mihomoOutput.addDomain(domain);
  379. }
  380. return Promise.all([
  381. surgeOutput.write(),
  382. mihomoOutput.write()
  383. ]);
  384. }