Browse Source

Update how DNS mapping building

SukkaW 1 year ago
parent
commit
70df7d33c6

+ 80 - 23
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<Record<string, string>>((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')
     )
   ]);
 });

+ 2 - 2
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<DNSMapping[]>((acc, item) => {
+  const dataset = [DIRECTS, DOMESTICS, DOH_BOOTSTRAP].reduce<DNSMapping[]>((acc, item) => {
     Object.values(item).forEach((i: DNSMapping) => {
       if (i.realip) {
         acc.push(i);

+ 1 - 1
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',

+ 3 - 3
Source/non_ip/direct.ts

@@ -50,7 +50,7 @@ export const DIRECTS: Record<string, DNSMapping> = {
       '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<string, DNSMapping> = {
       '+ui.direct',
       '$unifi',
       // Other Router
-      '$router.com',
+      // '$router.com',
       '+huaweimobilewifi.com',
       '+router',
       // 'my.router',
@@ -109,7 +109,7 @@ export const DIRECTS: Record<string, DNSMapping> = {
       '+lan',
       // 'amplifi.lan',
       // '$localhost',
-      'localdomain',
+      '+localdomain',
       'home.arpa',
       // AS112
       '10.in-addr.arpa',

+ 58 - 17
Source/non_ip/domestic.ts

@@ -2,9 +2,7 @@ import type { DNSMapping } from './direct';
 
 export const DOMESTICS: Record<string, DNSMapping> = {
   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<string, DNSMapping> = {
     ]
   },
   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<string, DNSMapping> = {
     ]
   },
   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<string, DNSMapping> = {
     ]
   }
 };
+
+/** This should only be used to build AfGu */
+export const DOH_BOOTSTRAP: Record<string, DNSMapping> = {
+  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'
+    ]
+  }
+};