From 8341588a0869609dedd54cb11871540958860125 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Thu, 5 Sep 2024 12:02:12 +0800 Subject: [PATCH] optimized tun enable logic --- src/main/core/manager.ts | 12 ++++++++++-- src/renderer/src/components/sider/tun-switcher.tsx | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index d4b8333..7a499f9 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -8,8 +8,13 @@ import { mihomoWorkDir } from '../utils/dirs' import { generateProfile } from './factory' -import { getAppConfig, getControledMihomoConfig, patchAppConfig } from '../config' -import { dialog, safeStorage } from 'electron' +import { + getAppConfig, + getControledMihomoConfig, + patchAppConfig, + patchControledMihomoConfig +} from '../config' +import { dialog, ipcMain, safeStorage } from 'electron' import { startMihomoTraffic, startMihomoConnections, @@ -74,6 +79,9 @@ export async function startCore(): Promise[]> { return new Promise((resolve, reject) => { child.stdout?.on('data', async (data) => { if (data.toString().includes('configure tun interface: operation not permitted')) { + patchControledMihomoConfig({ tun: { enable: false } }) + mainWindow?.webContents.send('controledMihomoConfigUpdated') + ipcMain.emit('updateTrayMenu') reject('虚拟网卡启动失败, 请尝试手动授予内核权限') } if (data.toString().includes('External controller listen error')) { diff --git a/src/renderer/src/components/sider/tun-switcher.tsx b/src/renderer/src/components/sider/tun-switcher.tsx index 0bb7c91..33d3f7b 100644 --- a/src/renderer/src/components/sider/tun-switcher.tsx +++ b/src/renderer/src/components/sider/tun-switcher.tsx @@ -71,7 +71,9 @@ const TunSwitcher: React.FC = () => { try { const encrypted = await encryptString(password) await patchAppConfig({ encryptedPassword: encrypted }) + await patchControledMihomoConfig({ tun: { enable }, dns: { enable: true } }) await restartCore() + window.electron.ipcRenderer.send('updateTrayMenu') setOpenPasswordModal(false) } catch (e) { alert(e)