From 70df7d33c6a82b71479e1917bf34e268e7f0b0d0 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 20 Oct 2024 00:26:17 +0800 Subject: [PATCH] Update how DNS mapping building --- ...c-direct-lan-ruleset-dns-mapping-module.ts | 103 ++++++++++++++---- Build/build-sgmodule-always-realip.ts | 4 +- Build/constants/reject-data-source.ts | 2 +- Source/non_ip/direct.ts | 6 +- Source/non_ip/domestic.ts | 75 ++++++++++--- 5 files changed, 144 insertions(+), 46 deletions(-) diff --git a/Build/build-domestic-direct-lan-ruleset-dns-mapping-module.ts b/Build/build-domestic-direct-lan-ruleset-dns-mapping-module.ts index 8314d1c8..e347f3cc 100644 --- a/Build/build-domestic-direct-lan-ruleset-dns-mapping-module.ts +++ b/Build/build-domestic-direct-lan-ruleset-dns-mapping-module.ts @@ -1,7 +1,8 @@ // @ts-check import path from 'node:path'; -import { DOMESTICS } from '../Source/non_ip/domestic'; +import { DOMESTICS, DOH_BOOTSTRAP } from '../Source/non_ip/domestic'; import { DIRECTS } 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'; @@ -57,9 +58,12 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a const getDnsMappingRuleWithWildcard = createGetDnsMappingRule(true); - Object.values(DOMESTICS).forEach(({ domains }) => { - appendArrayInPlace(domestics, domains.flatMap(getDnsMappingRuleWithWildcard)); + [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)); }); @@ -70,7 +74,7 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a export const buildDomesticRuleset = task(require.main === module, __filename)(async (span) => { const [domestics, directs, lans] = await getDomesticAndDirectDomainsRulesetPromise(); - const dataset = appendArrayInPlace(Object.values(DOMESTICS), Object.values(DIRECTS)); + const dataset: DNSMapping[] = ([DOH_BOOTSTRAP, DOMESTICS, DIRECTS] as const).flatMap(Object.values); return Promise.all([ new RulesetOutput(span, 'domestic', 'non_ip') @@ -107,25 +111,43 @@ export const buildDomesticRuleset = task(require.main === module, __filename)(as `#!desc=Last Updated: ${new Date().toISOString()}`, '', '[Host]', - ...dataset.flatMap(({ domains, dns, hosts }) => [ - ...Object.entries(hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`), - ...domains.flatMap((domain) => { - if (domain[0] === '$') { - return [ - `${domain.slice(1)} = server:${dns}` - ]; - } - if (domain[0] === '+') { - return [ - `*.${domain.slice(1)} = server:${dns}` - ]; - } - return [ - `${domain} = server:${dns}`, - `*.${domain} = server:${dns}` - ]; - }) - ]) + ...Object.entries( + // I use an object to deduplicate the domains + // Otherwise I could just construct an array directly + dataset.reduce>((acc, cur) => { + const { domains, dns, hosts } = cur; + + Object.entries(hosts).forEach(([dns, ips]) => { + if (!(dns in acc)) { + acc[dns] = ips.join(', '); + } + }); + + domains.forEach((domain) => { + if (domain[0] === '$') { + const d = domain.slice(1); + if (!(d in acc)) { + acc[d] = `server:${dns}`; + } + } else if (domain[0] === '+') { + const d = `*.${domain.slice(1)}`; + if (!(d in acc)) { + acc[d] = `server:${dns}`; + } + } else { + if (!(domain in acc)) { + acc[domain] = `server:${dns}`; + } + const d = `*.${domain}`; + if (!(d in acc)) { + acc[d] = `server:${dns}`; + } + } + }); + + return acc; + }, {}) + ).map(([dns, ips]) => `${dns} = ${ips}`) ], path.resolve(OUTPUT_MODULES_DIR, 'sukka_local_dns_mapping.sgmodule') ), @@ -164,6 +186,41 @@ export const buildDomesticRuleset = task(require.main === module, __filename)(as { version: '1.1' } ).split('\n'), path.join(OUTPUT_INTERNAL_DIR, 'clash_nameserver_policy.yaml') + ), + compareAndWriteFile( + span, + [ + '# Local DNS Mapping for AdGuard Home', + '', + '[//]udp://10.10.1.1:53', + ...dataset.flatMap(({ domains, dns: _dns }) => domains.flatMap((domain) => { + const dns = _dns === 'system' + ? 'udp://10.10.1.1:53' + : _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') ) ]); }); diff --git a/Build/build-sgmodule-always-realip.ts b/Build/build-sgmodule-always-realip.ts index a4b61f1c..0108ad45 100644 --- a/Build/build-sgmodule-always-realip.ts +++ b/Build/build-sgmodule-always-realip.ts @@ -3,7 +3,7 @@ import { task } from './trace'; import { compareAndWriteFile, DomainsetOutput } from './lib/create-file'; import { DIRECTS } from '../Source/non_ip/direct'; import type { DNSMapping } from '../Source/non_ip/direct'; -import { DOMESTICS } from '../Source/non_ip/domestic'; +import { DOMESTICS, DOH_BOOTSTRAP } from '../Source/non_ip/domestic'; import * as yaml from 'yaml'; import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR } from './constants/dir'; import { appendArrayInPlace } from './lib/append-array-in-place'; @@ -47,7 +47,7 @@ export const buildAlwaysRealIPModule = task(require.main === module, __filename) ]); // Intranet, Router Setup, and mant more - const dataset = [DIRECTS, DOMESTICS].reduce((acc, item) => { + const dataset = [DIRECTS, DOMESTICS, DOH_BOOTSTRAP].reduce((acc, item) => { Object.values(item).forEach((i: DNSMapping) => { if (i.realip) { acc.push(i); diff --git a/Build/constants/reject-data-source.ts b/Build/constants/reject-data-source.ts index 6de3055b..c406d473 100644 --- a/Build/constants/reject-data-source.ts +++ b/Build/constants/reject-data-source.ts @@ -343,7 +343,7 @@ export const PREDEFINED_WHITELIST = [ ...CRASHLYTICS_WHITELIST, '.localhost', '.local', - '.localhost.localdomain', + '.localdomain', '.broadcasthost', '.ip6-loopback', '.ip6-localnet', diff --git a/Source/non_ip/direct.ts b/Source/non_ip/direct.ts index 489031a5..e9d3f0a6 100644 --- a/Source/non_ip/direct.ts +++ b/Source/non_ip/direct.ts @@ -50,7 +50,7 @@ export const DIRECTS: Record = { 'routerlogin.net', 'routerlogin.com', // Tenda WiFi - 'tendawifi.com', + // 'tendawifi.com', // TP-Link Router 'tplinkwifi.net', 'tplogin.cn', @@ -62,7 +62,7 @@ export const DIRECTS: Record = { '+ui.direct', '$unifi', // Other Router - '$router.com', + // '$router.com', '+huaweimobilewifi.com', '+router', // 'my.router', @@ -109,7 +109,7 @@ export const DIRECTS: Record = { '+lan', // 'amplifi.lan', // '$localhost', - 'localdomain', + '+localdomain', 'home.arpa', // AS112 '10.in-addr.arpa', diff --git a/Source/non_ip/domestic.ts b/Source/non_ip/domestic.ts index de10274d..4cba0167 100644 --- a/Source/non_ip/domestic.ts +++ b/Source/non_ip/domestic.ts @@ -2,9 +2,7 @@ import type { DNSMapping } from './direct'; export const DOMESTICS: Record = { ALIBABA: { - hosts: { - 'dns.alidns.com': ['223.5.5.5', '223.6.6.6', '2400:3200:baba::1', '2400:3200::1'] - }, + hosts: {}, dns: 'quic://dns.alidns.com:853', realip: false, domains: [ @@ -84,11 +82,7 @@ export const DOMESTICS: Record = { ] }, TENCENT: { - hosts: { - 'dot.pub': ['120.53.53.53', '1.12.12.12', '1.12.34.56'], - 'doh.pub': ['120.53.53.53', '1.12.12.12', '1.12.34.56'], - 'dns.pub': ['120.53.53.53', '1.12.12.12', '1.12.34.56'] - }, + hosts: {}, dns: 'https://doh.pub/dns-query', realip: false, domains: [ @@ -287,15 +281,8 @@ export const DOMESTICS: Record = { ] }, QIHOO360: { - hosts: { - 'doh.360.cn': ['101.198.198.198', '101.198.199.200'], - 'dot.360.cn': ['101.198.198.198', '101.198.199.200'], - 'dns.360.cn': ['101.198.198.198', '101.198.199.200'], - 'doh.360.net': ['101.198.198.198', '101.198.199.200'], - 'dot.360.net': ['101.198.198.198', '101.198.199.200'], - 'dns.360.net': ['101.198.198.198', '101.198.199.200'] - }, - dns: 'https://dns.360.net/dns-query', + hosts: {}, + dns: 'https://doh.360.cn/dns-query', realip: false, domains: [ '+qhimg.com', @@ -333,3 +320,57 @@ export const DOMESTICS: Record = { ] } }; + +/** This should only be used to build AfGu */ +export const DOH_BOOTSTRAP: Record = { + ALIBABA: { + hosts: { + 'dns.alidns.com': ['223.5.5.5', '223.6.6.6', '2400:3200:baba::1', '2400:3200::1'] + }, + realip: false, + dns: 'quic://223.5.5.5:853', + domains: [ + '$dns.alidns.com' + ] + }, + DNSPOD: { + hosts: { + 'dot.pub': ['120.53.53.53', '1.12.12.12'], + 'doh.pub': ['120.53.53.53', '1.12.12.12'], + 'dns.pub': ['120.53.53.53', '1.12.12.12'] + }, + realip: false, + dns: 'https://1.12.12.12/dns-query', + domains: [ + '$dot.pub', + '$doh.pub', + '$dns.pub' + ] + }, + QIHOO360: { + hosts: { + // dot.360.cn + // doh.360.cn + + // sdns.360.net + // dns.360.cn CNAME sdns.360.net + + // dns.360.net + // doh.360.net CNAME dns.360.net + // dot.360.net CNAME dns.360.net + }, + realip: false, + dns: 'https://101.198.198.198/dns-query', // https://101.198.199.200/dns-query + domains: [ + '$dns.360.cn', + '$dot.360.cn', + '$doh.360.cn', + + '$sdns.360.net', + + '$dns.360.net', + '$dot.360.net', + '$doh.360.net' + ] + } +};