From 93155cedfb95954fc7646289c81d9a93c1c5f062 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Wed, 21 Aug 2024 09:48:12 +0800 Subject: [PATCH] support upgrade core --- changelog.md | 1 + src/main/core/manager.ts | 4 ++++ src/main/core/mihomoApi.ts | 5 +++++ src/main/utils/ipc.ts | 14 +++++++++++++- src/renderer/src/pages/mihomo.tsx | 32 +++++++++++++++++++++++++++++-- src/renderer/src/utils/ipc.ts | 4 ++++ 6 files changed, 57 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index b7908ca..bc39628 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,7 @@ ### New Features - 自动删除过期日志与更新缓存 +- 添加更新内核功能 ### Bug Fixes diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index 3d95016..b6d516b 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -38,6 +38,10 @@ export async function startCore(): Promise { }) return new Promise((resolve, reject) => { child.stdout?.on('data', async (data) => { + if (data.toString().includes('updater: finished')) { + stopCore() + await startCore() + } if (data.toString().includes('External controller listen error')) { if (retry) { retry-- diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index a8f14bf..3557570 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -152,6 +152,11 @@ export const mihomoGroupDelay = async (group: string, url?: string): Promise => { + const instance = await getAxios() + return await instance.post('/upgrade') +} + export const startMihomoTraffic = async (): Promise => { await mihomoTraffic() } diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 69bbb0f..64d6340 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -12,6 +12,7 @@ import { mihomoRules, mihomoUpdateProxyProviders, mihomoUpdateRuleProviders, + mihomoUpgrade, mihomoUpgradeGeo, mihomoVersion, patchMihomoConfig, @@ -64,7 +65,17 @@ function ipcErrorWrapper( // eslint-disable-next-line @typescript-eslint/no-e try { return await fn(...args) } catch (e) { - return { invokeError: `${e}` } + if (e && typeof e === 'object') { + if ('message' in e) { + return { invokeError: e.message } + } else { + return { invokeError: JSON.stringify(e) } + } + } + if (e instanceof Error || typeof e === 'string') { + return { invokeError: e } + } + return { invokeError: 'Unknown Error' } } } } @@ -87,6 +98,7 @@ export function registerIpcMainHandlers(): void { ipcErrorWrapper(mihomoChangeProxy)(group, proxy) ) ipcMain.handle('mihomoUpgradeGeo', ipcErrorWrapper(mihomoUpgradeGeo)) + ipcMain.handle('mihomoUpgrade', ipcErrorWrapper(mihomoUpgrade)) ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => ipcErrorWrapper(mihomoProxyDelay)(proxy, url) ) diff --git a/src/renderer/src/pages/mihomo.tsx b/src/renderer/src/pages/mihomo.tsx index 5945451..7f00b2c 100644 --- a/src/renderer/src/pages/mihomo.tsx +++ b/src/renderer/src/pages/mihomo.tsx @@ -6,7 +6,8 @@ import { useAppConfig } from '@renderer/hooks/use-app-config' import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config' import { platform } from '@renderer/utils/init' import { FaNetworkWired } from 'react-icons/fa' -import { restartCore } from '@renderer/utils/ipc' +import { IoMdCloudDownload } from 'react-icons/io' +import { mihomoUpgrade, restartCore } from '@renderer/utils/ipc' import React, { useState } from 'react' import InterfaceModal from '@renderer/components/mihomo/interface-modal' @@ -45,6 +46,7 @@ const Mihomo: React.FC = () => { const [externalControllerInput, setExternalControllerInput] = useState(externalController) const [secretInput, setSecretInput] = useState(secret) + const [upgrading, setUpgrading] = useState(false) const [lanOpen, setLanOpen] = useState(false) const onChangeNeedRestart = async (patch: Partial): Promise => { @@ -57,7 +59,33 @@ const Mihomo: React.FC = () => { {lanOpen && setLanOpen(false)} />} - + { + try { + setUpgrading(true) + await mihomoUpgrade() + PubSub.publish('mihomo-core-changed') + } catch (e) { + alert(e) + } finally { + setUpgrading(false) + } + }} + > + + + } + divider + >