diff --git a/src/main/config/controledMihomo.ts b/src/main/config/controledMihomo.ts index dc3d1eb..ab1fb4e 100644 --- a/src/main/config/controledMihomo.ts +++ b/src/main/config/controledMihomo.ts @@ -4,6 +4,7 @@ import yaml from 'yaml' import { getAxios, startMihomoMemory, startMihomoTraffic } from '../core/mihomoApi' import { generateProfile } from '../resolve/factory' import { getAppConfig } from './app' +import { defaultControledMihomoConfig } from '../utils/template' let controledMihomoConfig: Partial // mihomo.yaml @@ -16,12 +17,21 @@ export async function getControledMihomoConfig(force = false): Promise): Promise { - const { useNameserverPolicy } = await getAppConfig() + const { useNameserverPolicy, controlDns, controlSniff } = await getAppConfig() if (patch.tun) { const oldTun = controledMihomoConfig.tun || {} const newTun = Object.assign(oldTun, patch.tun) patch.tun = newTun } + if (!controlDns) { + delete controledMihomoConfig.dns + delete controledMihomoConfig.hosts + } else { + if (controledMihomoConfig.hosts === undefined) { + controledMihomoConfig.dns = defaultControledMihomoConfig.dns + controledMihomoConfig.hosts = defaultControledMihomoConfig.hosts + } + } if (patch.dns) { const oldDns = controledMihomoConfig.dns || {} const newDns = Object.assign(oldDns, patch.dns) @@ -30,6 +40,13 @@ export async function patchControledMihomoConfig(patch: Partial): } patch.dns = newDns } + if (!controlSniff) { + delete controledMihomoConfig.sniffer + } else { + if (!controledMihomoConfig.sniffer) { + controledMihomoConfig.sniffer = defaultControledMihomoConfig.sniffer + } + } if (patch.sniffer) { const oldSniffer = controledMihomoConfig.sniffer || {} const newSniffer = Object.assign(oldSniffer, patch.sniffer) diff --git a/src/main/utils/template.ts b/src/main/utils/template.ts index a59ea49..8394dd1 100644 --- a/src/main/utils/template.ts +++ b/src/main/utils/template.ts @@ -7,6 +7,8 @@ export const defaultConfig: IAppConfig = { autoCheckUpdate: true, autoCloseConnection: true, useNameserverPolicy: false, + controlDns: true, + controlSniff: true, nameserverPolicy: {}, siderOrder: [ 'mode', diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index e1fda3c..80adcb6 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -33,6 +33,8 @@ const App: React.FC = () => { const { appConfig, patchAppConfig } = useAppConfig() const { appTheme = 'system', + controlDns = true, + controlSniff = true, siderOrder = [ 'sysproxy', 'tun', @@ -119,6 +121,8 @@ const App: React.FC = () => { })} > {order.map((key: string) => { + if (key === 'dns' && controlDns === false) return null + if (key === 'sniff' && controlSniff === false) return null return componentMap[key] })} diff --git a/src/renderer/src/pages/settings.tsx b/src/renderer/src/pages/settings.tsx index 4c106f3..343aad0 100644 --- a/src/renderer/src/pages/settings.tsx +++ b/src/renderer/src/pages/settings.tsx @@ -8,7 +8,8 @@ import { enableAutoRun, disableAutoRun, quitApp, - checkUpdate + checkUpdate, + patchControledMihomoConfig } from '@renderer/utils/ipc' import { IoLogoGithub } from 'react-icons/io5' import { version } from '@renderer/utils/init' @@ -26,6 +27,8 @@ const Settings: React.FC = () => { const { appConfig, patchAppConfig } = useAppConfig() const { silentStart = false, + controlDns = true, + controlSniff = true, delayTestUrl, delayTestTimeout, autoCheckUpdate, @@ -188,6 +191,26 @@ const Settings: React.FC = () => { }} /> + + { + await patchAppConfig({ controlDns: v }) + await patchControledMihomoConfig({}) + }} + /> + + + { + await patchAppConfig({ controlSniff: v }) + await patchControledMihomoConfig({}) + }} + /> +