perf: reload config without kernel restart

This commit is contained in:
Memory 2025-09-29 23:42:40 +08:00 committed by GitHub
parent 6832db788a
commit f488cc3643
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 53 additions and 1 deletions

View File

@ -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<void>
// 读取最新的配置
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<IMihomoConfig> {

View File

@ -188,6 +188,33 @@ export const mihomoUpgradeUI = async (): Promise<void> => {
return await instance.post('/upgrade/ui')
}
export const mihomoUpgradeConfig = async (): Promise<void> => {
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

View File

@ -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)
)

View File

@ -88,6 +88,10 @@ export async function mihomoUpgradeUI(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('mihomoUpgradeUI'))
}
export async function mihomoUpgradeConfig(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('mihomoUpgradeConfig'))
}
export async function mihomoProxyDelay(proxy: string, url?: string): Promise<IMihomoDelay> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('mihomoProxyDelay', proxy, url))
}