| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- // @ts-check
- import path from 'node:path';
- import { DOMESTICS, DOH_BOOTSTRAP, AdGuardHomeDNSMapping } from '../Source/non_ip/domestic';
- import { DIRECTS, HOSTS, LAN } from '../Source/non_ip/direct';
- import type { DNSMapping } from '../Source/non_ip/direct';
- import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
- import { compareAndWriteFile } from './lib/create-file';
- import { task } from './trace';
- import { SHARED_DESCRIPTION } from './constants/description';
- import { once } from 'foxts/once';
- import * as yaml from 'yaml';
- import { appendArrayInPlace } from 'foxts/append-array-in-place';
- import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR, OUTPUT_MODULES_RULES_DIR, SOURCE_DIR } from './constants/dir';
- import { MihomoNameserverPolicyOutput, RulesetOutput } from './lib/rules/ruleset';
- import { SurgeOnlyRulesetOutput } from './lib/rules/ruleset';
- export function createGetDnsMappingRule(allowWildcard: boolean) {
- const hasWildcard = (domain: string) => {
- if (domain.includes('*') || domain.includes('?')) {
- if (!allowWildcard) {
- throw new TypeError(`Wildcard domain is not supported: ${domain}`);
- }
- return true;
- }
- return false;
- };
- return (domain: string): string[] => {
- const results: string[] = [];
- if (domain[0] === '$') {
- const d = domain.slice(1);
- if (hasWildcard(domain)) {
- results.push(`DOMAIN-WILDCARD,${d}`);
- } else {
- results.push(`DOMAIN,${d}`);
- }
- } else if (domain[0] === '+') {
- const d = domain.slice(1);
- if (hasWildcard(domain)) {
- results.push(`DOMAIN-WILDCARD,*.${d}`);
- } else {
- results.push(`DOMAIN-SUFFIX,${d}`);
- }
- } else if (hasWildcard(domain)) {
- results.push(`DOMAIN-WILDCARD,${domain}`, `DOMAIN-WILDCARD,*.${domain}`);
- } else {
- results.push(`DOMAIN-SUFFIX,${domain}`);
- }
- return results;
- };
- }
- export const getDomesticAndDirectDomainsRulesetPromise = once(async () => {
- const domestics = await readFileIntoProcessedArray(path.join(SOURCE_DIR, 'non_ip/domestic.conf'));
- const directs = await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'non_ip/direct.conf'));
- const lans: string[] = [];
- const getDnsMappingRuleWithWildcard = createGetDnsMappingRule(true);
- [DOH_BOOTSTRAP, DOMESTICS].forEach((item) => {
- Object.values(item).forEach(({ domains }) => {
- appendArrayInPlace(domestics, domains.flatMap(getDnsMappingRuleWithWildcard));
- });
- });
- Object.values(DIRECTS).forEach(({ domains }) => {
- appendArrayInPlace(directs, domains.flatMap(getDnsMappingRuleWithWildcard));
- });
- Object.values(LAN).forEach(({ domains }) => {
- appendArrayInPlace(directs, domains.flatMap(getDnsMappingRuleWithWildcard));
- });
- // backward compatible, add lan.conf
- Object.values(LAN).forEach(({ domains }) => {
- appendArrayInPlace(lans, domains.flatMap(getDnsMappingRuleWithWildcard));
- });
- return [domestics, directs, lans] as const;
- });
- export const buildDomesticRuleset = task(require.main === module, __filename)(async (span) => {
- const [domestics, directs, lans] = await getDomesticAndDirectDomainsRulesetPromise();
- const dataset: Array<[name: string, DNSMapping]> = ([DOH_BOOTSTRAP, DOMESTICS, DIRECTS, LAN, HOSTS] as const).flatMap(Object.entries);
- return Promise.all([
- new RulesetOutput(span, 'domestic', 'non_ip')
- .withTitle('Sukka\'s Ruleset - Domestic Domains')
- .appendDescription(
- SHARED_DESCRIPTION,
- '',
- 'This file contains known addresses that are avaliable in the Mainland China.'
- )
- .addFromRuleset(domestics)
- .write(),
- new RulesetOutput(span, 'direct', 'non_ip')
- .withTitle('Sukka\'s Ruleset - Direct Rules')
- .appendDescription(
- SHARED_DESCRIPTION,
- '',
- 'This file contains domains and process that should not be proxied.'
- )
- .addFromRuleset(directs)
- .write(),
- new RulesetOutput(span, 'lan', 'non_ip')
- .withTitle('Sukka\'s Ruleset - LAN')
- .appendDescription(
- SHARED_DESCRIPTION,
- '',
- 'This file includes rules for LAN DOMAIN and reserved TLDs.'
- )
- .addFromRuleset(lans)
- .write(),
- ...dataset.map(([name, { ruleset, domains }]) => {
- if (!ruleset) {
- return;
- }
- const surgeOutput = new SurgeOnlyRulesetOutput(
- span,
- name.toLowerCase(),
- 'sukka_local_dns_mapping',
- OUTPUT_MODULES_RULES_DIR
- )
- .withTitle(`Sukka's Ruleset - Local DNS Mapping (${name})`)
- .appendDescription(
- SHARED_DESCRIPTION,
- '',
- 'This is an internal rule that is only referenced by sukka_local_dns_mapping.sgmodule',
- 'Do not use this file in your Rule section, all entries are included in non_ip/domestic.conf already.'
- );
- const mihomoOutput = new MihomoNameserverPolicyOutput(
- span,
- name.toLowerCase(),
- 'mihomo_nameserver_policy',
- OUTPUT_INTERNAL_DIR
- )
- .withTitle(`Sukka's Ruleset - Local DNS Mapping for Mihomo NameServer Policy (${name})`)
- .appendDescription(
- SHARED_DESCRIPTION,
- '',
- 'This ruleset is only used for mihomo\'s nameserver-policy feature, which',
- 'is similar to the RULE-SET referenced by sukka_local_dns_mapping.sgmodule.',
- 'Do not use this file in your Rule section, all entries are included in non_ip/domestic.conf already.'
- );
- domains.forEach((domain) => {
- switch (domain[0]) {
- case '$':
- surgeOutput.addDomain(domain.slice(1));
- mihomoOutput.addDomain(domain.slice(1));
- break;
- case '+':
- surgeOutput.addDomainSuffix(domain.slice(1));
- mihomoOutput.addDomainSuffix(domain.slice(1));
- break;
- default:
- surgeOutput.addDomainSuffix(domain);
- mihomoOutput.addDomainSuffix(domain);
- break;
- }
- });
- return Promise.all([
- surgeOutput.write(),
- mihomoOutput.write()
- ]);
- }),
- compareAndWriteFile(
- span,
- [
- '#!name=[Sukka] Local DNS Mapping',
- `#!desc=Last Updated: ${new Date().toISOString()}`,
- '',
- '[Host]',
- ...Object.entries(
- // I use an object to deduplicate the domains
- // Otherwise I could just construct an array directly
- dataset.reduce<Record<string, string>>((acc, cur) => {
- const ruleset_name = cur[0].toLowerCase();
- const { domains, dns, hosts, ruleset } = cur[1];
- Object.entries(hosts).forEach(([dns, ips]) => {
- acc[dns] ||= ips.join(', ');
- });
- if (ruleset) {
- acc[`RULE-SET:https://ruleset.skk.moe/Modules/Rules/sukka_local_dns_mapping/${ruleset_name}.conf`] ||= `server:${dns}`;
- } else {
- domains.forEach((domain) => {
- switch (domain[0]) {
- case '$':
- acc[domain.slice(1)] ||= `server:${dns}`;
- break;
- case '+':
- acc[`*.${domain.slice(1)}`] ||= `server:${dns}`;
- break;
- default:
- acc[domain] ||= `server:${dns}`;
- acc[`*.${domain}`] ||= `server:${dns}`;
- break;
- }
- });
- }
- return acc;
- }, {})
- ).map(([dns, ips]) => `${dns} = ${ips}`)
- ],
- path.resolve(OUTPUT_MODULES_DIR, 'sukka_local_dns_mapping.sgmodule')
- ),
- compareAndWriteFile(
- span,
- yaml.stringify(
- dataset.reduce<{
- dns: { 'nameserver-policy': Record<string, string | string[]> },
- hosts: Record<string, string | string[]>,
- 'rule-providers': Record<string, {
- type: 'http',
- path: `./sukkaw_ruleset/${string}`,
- url: string,
- behavior: 'classical',
- format: 'text',
- interval: number
- }>
- }>((acc, cur) => {
- const { domains, dns, ruleset, ...rest } = cur[1];
- if (ruleset) {
- const ruleset_name = cur[0].toLowerCase();
- const mihomo_ruleset_id = `mihomo_nameserver_policy_${ruleset_name}`;
- acc.dns['nameserver-policy'][`rule-set:${mihomo_ruleset_id}`] = dns;
- acc['rule-providers'][mihomo_ruleset_id] = {
- type: 'http',
- path: `./sukkaw_ruleset/${mihomo_ruleset_id}.txt`,
- url: `https://ruleset.skk.moe/Internal/mihomo_nameserver_policy/${ruleset_name}.txt`,
- behavior: 'classical',
- format: 'text',
- interval: 43200
- };
- } else {
- domains.forEach((domain) => {
- switch (domain[0]) {
- case '$':
- domain = domain.slice(1);
- break;
- case '+':
- domain = `*.${domain.slice(1)}`;
- break;
- default:
- domain = `+.${domain}`;
- break;
- }
- acc.dns['nameserver-policy'][domain] = dns;
- });
- }
- if ('hosts' in rest) {
- // eslint-disable-next-line guard-for-in -- known plain object
- for (const domain in rest.hosts) {
- const dest = rest.hosts[domain];
- if (domain in acc.hosts) {
- if (typeof acc.hosts[domain] === 'string') {
- acc.hosts[domain] = [acc.hosts[domain]];
- }
- acc.hosts[domain].push(...dest);
- } else if (dest.length === 1) {
- acc.hosts[domain] = dest[0];
- } else {
- acc.hosts[domain] = dest;
- }
- }
- }
- return acc;
- }, {
- dns: { 'nameserver-policy': {} },
- 'rule-providers': {},
- hosts: {}
- }),
- { version: '1.1' }
- ).split('\n'),
- path.join(OUTPUT_INTERNAL_DIR, 'clash_nameserver_policy.yaml')
- ),
- compareAndWriteFile(
- span,
- [
- '# Local DNS Mapping for AdGuard Home',
- 'tls://dot.pub',
- 'https://doh.pub/dns-query',
- '[//]udp://10.10.1.1:53',
- ...(([DOMESTICS, DIRECTS, LAN, HOSTS] as const).flatMap(Object.values) as DNSMapping[]).flatMap(({ domains, dns: _dns }) => domains.flatMap((domain) => {
- let dns;
- if (_dns in AdGuardHomeDNSMapping) {
- dns = AdGuardHomeDNSMapping[_dns as keyof typeof AdGuardHomeDNSMapping].join(' ');
- } else {
- console.warn(`Unknown DNS "${_dns}" not in AdGuardHomeDNSMapping`);
- dns = _dns;
- }
- // if (
- // // AdGuard Home has built-in AS112 / private PTR handling
- // domain.endsWith('.arpa')
- // // Ignore simple hostname
- // || !domain.includes('.')
- // ) {
- // return [];
- // }
- if (domain[0] === '$') {
- return [
- `[/${domain.slice(1)}/]${dns}`
- ];
- }
- if (domain[0] === '+') {
- return [
- `[/${domain.slice(1)}/]${dns}`
- ];
- }
- return [
- `[/${domain}/]${dns}`
- ];
- }))
- ],
- path.resolve(OUTPUT_INTERNAL_DIR, 'dns_mapping_adguardhome.conf')
- )
- ]);
- });
|