From 1b7c2ee01e4bccd55b3cf90c900905f654171c15 Mon Sep 17 00:00:00 2001 From: ezequielnick <107352853+ezequielnick@users.noreply.github.com> Date: Mon, 4 Aug 2025 13:20:24 +0800 Subject: [PATCH] refactor: rewrite DNS control module with override logic --- src/main/config/controledMihomo.ts | 22 ++++++++--- src/main/utils/template.ts | 6 ++- .../src/components/sider/dns-card.tsx | 38 ++++++++++++------- .../src/components/sider/sniff-card.tsx | 7 ++-- .../components/sider/sysproxy-switcher.tsx | 3 +- .../src/components/sider/tun-switcher.tsx | 3 +- src/renderer/src/locales/en-US.json | 9 +++-- src/renderer/src/locales/zh-CN.json | 9 +++-- src/renderer/src/pages/dns.tsx | 12 ++++++ 9 files changed, 77 insertions(+), 32 deletions(-) diff --git a/src/main/config/controledMihomo.ts b/src/main/config/controledMihomo.ts index b58af84..e6203fd 100644 --- a/src/main/config/controledMihomo.ts +++ b/src/main/config/controledMihomo.ts @@ -20,15 +20,22 @@ export async function getControledMihomoConfig(force = false): Promise): Promise { const { useNameserverPolicy, controlDns = true, controlSniff = true } = await getAppConfig() - if (!controlDns) { + + // DNS 配置覆写逻辑 + if (controlDns) { + if (!controledMihomoConfig.dns || controledMihomoConfig.dns?.ipv6 === undefined) { + controledMihomoConfig.dns = { ...defaultControledMihomoConfig.dns } + } + const originalEnable = controledMihomoConfig.dns?.enable + controledMihomoConfig.dns = deepMerge(controledMihomoConfig.dns || {}, defaultControledMihomoConfig.dns || {}) + if (originalEnable !== undefined) { + controledMihomoConfig.dns.enable = originalEnable + } + } else { delete controledMihomoConfig.dns delete controledMihomoConfig.hosts - } else { - // 从不接管状态恢复 - if (controledMihomoConfig.dns?.ipv6 === undefined) { - controledMihomoConfig.dns = defaultControledMihomoConfig.dns - } } + if (!controlSniff) { delete controledMihomoConfig.sniffer } else { @@ -37,6 +44,7 @@ export async function patchControledMihomoConfig(patch: Partial): controledMihomoConfig.sniffer = defaultControledMihomoConfig.sniffer } } + if (patch.hosts) { controledMihomoConfig.hosts = patch.hosts } @@ -44,7 +52,9 @@ export async function patchControledMihomoConfig(patch: Partial): controledMihomoConfig.dns = controledMihomoConfig.dns || {} controledMihomoConfig.dns['nameserver-policy'] = patch.dns['nameserver-policy'] } + controledMihomoConfig = deepMerge(controledMihomoConfig, patch) + if (!useNameserverPolicy) { delete controledMihomoConfig?.dns?.['nameserver-policy'] } diff --git a/src/main/utils/template.ts b/src/main/utils/template.ts index d9b86f2..26bc90f 100644 --- a/src/main/utils/template.ts +++ b/src/main/utils/template.ts @@ -79,8 +79,10 @@ export const defaultControledMihomoConfig: Partial = { 'fake-ip-filter': ['*', '+.lan', '+.local', 'time.*.com', 'ntp.*.com', '+.market.xiaomi.com'], 'use-hosts': false, 'use-system-hosts': false, - nameserver: ['https://120.53.53.53/dns-query', 'https://223.5.5.5/dns-query'], - 'proxy-server-nameserver': ['https://120.53.53.53/dns-query', 'https://223.5.5.5/dns-query'], + 'respect-rules': false, + 'default-nameserver': ['tls://223.5.5.5'], + nameserver: ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'], + 'proxy-server-nameserver': ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'], 'direct-nameserver': [] }, sniffer: { diff --git a/src/renderer/src/components/sider/dns-card.tsx b/src/renderer/src/components/sider/dns-card.tsx index 72d2cc9..6cf57f2 100644 --- a/src/renderer/src/components/sider/dns-card.tsx +++ b/src/renderer/src/components/sider/dns-card.tsx @@ -3,7 +3,7 @@ import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-c import BorderSwitch from '@renderer/components/base/border-swtich' import { LuServer } from 'react-icons/lu' import { useLocation, useNavigate } from 'react-router-dom' -import { patchMihomoConfig } from '@renderer/utils/ipc' +import { restartCore } from '@renderer/utils/ipc' import { useSortable } from '@dnd-kit/sortable' import { CSS } from '@dnd-kit/utilities' import { useAppConfig } from '@renderer/hooks/use-app-config' @@ -15,15 +15,14 @@ interface Props { } const DNSCard: React.FC = (props) => { const { t } = useTranslation() - const { appConfig } = useAppConfig() + const { appConfig, patchAppConfig } = useAppConfig() const { iconOnly } = props const { dnsCardStatus = 'col-span-1', controlDns = true } = appConfig || {} const location = useLocation() const navigate = useNavigate() const match = location.pathname.includes('/dns') const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig() - const { dns, tun } = controledMihomoConfig || {} - const { enable = true } = dns || {} + const { tun } = controledMihomoConfig || {} const { attributes, listeners, @@ -35,20 +34,33 @@ const DNSCard: React.FC = (props) => { id: 'dns' }) const transform = tf ? { x: tf.x, y: tf.y, scaleX: 1, scaleY: 1 } : null - const onChange = async (enable: boolean): Promise => { - await patchControledMihomoConfig({ dns: { enable } }) - await patchMihomoConfig({ dns: { enable } }) + const onChange = async (controlDnsEnabled: boolean): Promise => { + try { + await patchAppConfig({ controlDns: controlDnsEnabled }) + await patchControledMihomoConfig({}) + await restartCore() + } catch (e) { + alert(e) + } } if (iconOnly) { return ( -
- +
+ diff --git a/src/renderer/src/components/sider/sniff-card.tsx b/src/renderer/src/components/sider/sniff-card.tsx index 7173094..7d7c26f 100644 --- a/src/renderer/src/components/sider/sniff-card.tsx +++ b/src/renderer/src/components/sider/sniff-card.tsx @@ -42,13 +42,14 @@ const SniffCard: React.FC = (props) => { if (iconOnly) { return ( -
+