diff --git a/src/main/config/controledMihomo.ts b/src/main/config/controledMihomo.ts index 4ecfd83..ab26822 100644 --- a/src/main/config/controledMihomo.ts +++ b/src/main/config/controledMihomo.ts @@ -19,7 +19,7 @@ export async function getControledMihomoConfig(force = false): Promise): Promise { - const { useNameserverPolicy, controlDns = true, controlSniff = true } = await getAppConfig() + const { controlDns = true, controlSniff = true } = await getAppConfig() if (patch.hosts) { controledMihomoConfig.hosts = patch.hosts @@ -30,36 +30,18 @@ export async function patchControledMihomoConfig(patch: Partial): } controledMihomoConfig = deepMerge(controledMihomoConfig, patch) - // 覆写开关控制 - let configForProfile = { ...controledMihomoConfig } - - if (!controlDns) { - delete configForProfile.dns - delete configForProfile.hosts - } else { - if (configForProfile.dns?.ipv6 === undefined) { - configForProfile.dns = defaultControledMihomoConfig.dns - } + // 从不接管状态恢复 + if (controlDns && controledMihomoConfig.dns?.ipv6 === undefined) { + controledMihomoConfig.dns = defaultControledMihomoConfig.dns } - if (!controlSniff) { - delete configForProfile.sniffer - } else { - if (!configForProfile.sniffer) { - configForProfile.sniffer = defaultControledMihomoConfig.sniffer - } + if (controlSniff && !controledMihomoConfig.sniffer) { + controledMihomoConfig.sniffer = defaultControledMihomoConfig.sniffer } - if (!useNameserverPolicy) { - delete configForProfile?.dns?.['nameserver-policy'] - } if (process.platform === 'darwin') { - delete configForProfile?.tun?.device + delete controledMihomoConfig?.tun?.device } - const originalConfig = controledMihomoConfig - controledMihomoConfig = configForProfile await generateProfile() - controledMihomoConfig = originalConfig - await writeFile(controledMihomoConfigPath(), yaml.stringify(controledMihomoConfig), 'utf-8') } diff --git a/src/main/core/factory.ts b/src/main/core/factory.ts index 54866c7..3e3979d 100644 --- a/src/main/core/factory.ts +++ b/src/main/core/factory.ts @@ -26,9 +26,23 @@ let runtimeConfig: IMihomoConfig export async function generateProfile(): Promise { const { current } = await getProfileConfig() - const { diffWorkDir = false } = await getAppConfig() + const { diffWorkDir = false, controlDns = true, controlSniff = true, useNameserverPolicy } = await getAppConfig() const currentProfile = await overrideProfile(current, await getProfile(current)) - const controledMihomoConfig = await getControledMihomoConfig() + let controledMihomoConfig = await getControledMihomoConfig() + + // 根据开关状态过滤控制配置 + controledMihomoConfig = { ...controledMihomoConfig } + if (!controlDns) { + delete controledMihomoConfig.dns + delete controledMihomoConfig.hosts + } + if (!controlSniff) { + delete controledMihomoConfig.sniffer + } + if (!useNameserverPolicy) { + delete controledMihomoConfig?.dns?.['nameserver-policy'] + } + const profile = deepMerge(currentProfile, controledMihomoConfig) // 确保可以拿到基础日志信息 // 使用 debug 可以调试内核相关问题 `debug/pprof`