From f488cc3643074ac5fb7f9001232c117901a1d0df Mon Sep 17 00:00:00 2001 From: Memory <134070804+Memory2314@users.noreply.github.com> Date: Mon, 29 Sep 2025 23:42:40 +0800 Subject: [PATCH] perf: reload config without kernel restart --- src/main/config/profile.ts | 21 ++++++++++++++++++++- src/main/core/mihomoApi.ts | 27 +++++++++++++++++++++++++++ src/main/utils/ipc.ts | 2 ++ src/renderer/src/utils/ipc.ts | 4 ++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/main/config/profile.ts b/src/main/config/profile.ts index b57fb3c..01ee214 100644 --- a/src/main/config/profile.ts +++ b/src/main/config/profile.ts @@ -11,6 +11,7 @@ import { defaultProfile } from '../utils/template' import { subStorePort } from '../resolve/server' import { join } from 'path' import { app } from 'electron' +import { mihomoUpgradeConfig } from '../core/mihomoApi' let profileConfig: IProfileConfig // profile.yaml // 最终选中订阅ID @@ -221,7 +222,25 @@ export async function setProfileStr(id: string, content: string): Promise // 读取最新的配置 const { current } = await getProfileConfig(true) await writeFile(profilePath(id), content, 'utf-8') - if (current === id) await restartCore() + if (current === id) { + try { + const { generateProfile } = await import('../core/factory') + await generateProfile() + await mihomoUpgradeConfig() + console.log('[Profile] Config reloaded successfully using mihomoUpgradeConfig') + } catch (error) { + console.error('[Profile] Failed to reload config with mihomoUpgradeConfig:', error) + try { + console.log('[Profile] Falling back to restart core') + const { restartCore } = await import('../core/manager') + await restartCore() + console.log('[Profile] Core restarted successfully') + } catch (restartError) { + console.error('[Profile] Failed to restart core:', restartError) + throw restartError + } + } + } } export async function getProfile(id: string | undefined): Promise { diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 936e6a2..b66c922 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -188,6 +188,33 @@ export const mihomoUpgradeUI = async (): Promise => { return await instance.post('/upgrade/ui') } +export const mihomoUpgradeConfig = async (): Promise => { + console.log('[mihomoApi] mihomoUpgradeConfig called') + + try { + const instance = await getAxios() + console.log('[mihomoApi] axios instance obtained') + const { diffWorkDir = false } = await getAppConfig() + const { current } = await import('../config').then(mod => mod.getProfileConfig(true)) + const { mihomoWorkConfigPath } = await import('../utils/dirs') + const configPath = diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work') + console.log('[mihomoApi] config path:', configPath) + const { existsSync } = await import('fs') + if (!existsSync(configPath)) { + console.log('[mihomoApi] config file does not exist, generating...') + const { generateProfile } = await import('./factory') + await generateProfile() + } + const response = await instance.put('/configs?force=true', { + path: configPath + }) + console.log('[mihomoApi] config upgrade request completed', response?.status || 'no status') + } catch (error) { + console.error('[mihomoApi] Failed to upgrade config:', error) + throw error + } +} + // Smart 内核 API export const mihomoSmartGroupWeights = async ( groupName: string diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 20e3752..543ae22 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -16,6 +16,7 @@ import { mihomoUpgrade, mihomoUpgradeGeo, mihomoUpgradeUI, + mihomoUpgradeConfig, mihomoVersion, patchMihomoConfig, mihomoSmartGroupWeights, @@ -169,6 +170,7 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('mihomoUpgradeGeo', ipcErrorWrapper(mihomoUpgradeGeo)) ipcMain.handle('mihomoUpgrade', ipcErrorWrapper(mihomoUpgrade)) ipcMain.handle('mihomoUpgradeUI', ipcErrorWrapper(mihomoUpgradeUI)) + ipcMain.handle('mihomoUpgradeConfig', ipcErrorWrapper(mihomoUpgradeConfig)) ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => ipcErrorWrapper(mihomoProxyDelay)(proxy, url) ) diff --git a/src/renderer/src/utils/ipc.ts b/src/renderer/src/utils/ipc.ts index a45d924..3d8fe3f 100644 --- a/src/renderer/src/utils/ipc.ts +++ b/src/renderer/src/utils/ipc.ts @@ -88,6 +88,10 @@ export async function mihomoUpgradeUI(): Promise { return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('mihomoUpgradeUI')) } +export async function mihomoUpgradeConfig(): Promise { + return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('mihomoUpgradeConfig')) +} + export async function mihomoProxyDelay(proxy: string, url?: string): Promise { return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('mihomoProxyDelay', proxy, url)) }