support upgrade core

This commit is contained in:
pompurin404 2024-08-21 09:48:12 +08:00
parent 48028a43c1
commit 93155cedfb
No known key found for this signature in database
6 changed files with 57 additions and 3 deletions

View File

@ -1,6 +1,7 @@
### New Features ### New Features
- 自动删除过期日志与更新缓存 - 自动删除过期日志与更新缓存
- 添加更新内核功能
### Bug Fixes ### Bug Fixes

View File

@ -38,6 +38,10 @@ export async function startCore(): Promise<void> {
}) })
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
child.stdout?.on('data', async (data) => { child.stdout?.on('data', async (data) => {
if (data.toString().includes('updater: finished')) {
stopCore()
await startCore()
}
if (data.toString().includes('External controller listen error')) { if (data.toString().includes('External controller listen error')) {
if (retry) { if (retry) {
retry-- retry--

View File

@ -152,6 +152,11 @@ export const mihomoGroupDelay = async (group: string, url?: string): Promise<IMi
}) })
} }
export const mihomoUpgrade = async (): Promise<void> => {
const instance = await getAxios()
return await instance.post('/upgrade')
}
export const startMihomoTraffic = async (): Promise<void> => { export const startMihomoTraffic = async (): Promise<void> => {
await mihomoTraffic() await mihomoTraffic()
} }

View File

@ -12,6 +12,7 @@ import {
mihomoRules, mihomoRules,
mihomoUpdateProxyProviders, mihomoUpdateProxyProviders,
mihomoUpdateRuleProviders, mihomoUpdateRuleProviders,
mihomoUpgrade,
mihomoUpgradeGeo, mihomoUpgradeGeo,
mihomoVersion, mihomoVersion,
patchMihomoConfig, patchMihomoConfig,
@ -64,7 +65,17 @@ function ipcErrorWrapper<T>( // eslint-disable-next-line @typescript-eslint/no-e
try { try {
return await fn(...args) return await fn(...args)
} catch (e) { } 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) ipcErrorWrapper(mihomoChangeProxy)(group, proxy)
) )
ipcMain.handle('mihomoUpgradeGeo', ipcErrorWrapper(mihomoUpgradeGeo)) ipcMain.handle('mihomoUpgradeGeo', ipcErrorWrapper(mihomoUpgradeGeo))
ipcMain.handle('mihomoUpgrade', ipcErrorWrapper(mihomoUpgrade))
ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) =>
ipcErrorWrapper(mihomoProxyDelay)(proxy, url) ipcErrorWrapper(mihomoProxyDelay)(proxy, url)
) )

View File

@ -6,7 +6,8 @@ import { useAppConfig } from '@renderer/hooks/use-app-config'
import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config' import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config'
import { platform } from '@renderer/utils/init' import { platform } from '@renderer/utils/init'
import { FaNetworkWired } from 'react-icons/fa' 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 React, { useState } from 'react'
import InterfaceModal from '@renderer/components/mihomo/interface-modal' import InterfaceModal from '@renderer/components/mihomo/interface-modal'
@ -45,6 +46,7 @@ const Mihomo: React.FC = () => {
const [externalControllerInput, setExternalControllerInput] = useState(externalController) const [externalControllerInput, setExternalControllerInput] = useState(externalController)
const [secretInput, setSecretInput] = useState(secret) const [secretInput, setSecretInput] = useState(secret)
const [upgrading, setUpgrading] = useState(false)
const [lanOpen, setLanOpen] = useState(false) const [lanOpen, setLanOpen] = useState(false)
const onChangeNeedRestart = async (patch: Partial<IMihomoConfig>): Promise<void> => { const onChangeNeedRestart = async (patch: Partial<IMihomoConfig>): Promise<void> => {
@ -57,7 +59,33 @@ const Mihomo: React.FC = () => {
{lanOpen && <InterfaceModal onClose={() => setLanOpen(false)} />} {lanOpen && <InterfaceModal onClose={() => setLanOpen(false)} />}
<BasePage title="内核设置"> <BasePage title="内核设置">
<SettingCard> <SettingCard>
<SettingItem title="内核版本" divider> <SettingItem
title="内核版本"
actions={
<Button
size="sm"
isIconOnly
title="升级内核"
variant="light"
className="ml-2"
isLoading={upgrading}
onPress={async () => {
try {
setUpgrading(true)
await mihomoUpgrade()
PubSub.publish('mihomo-core-changed')
} catch (e) {
alert(e)
} finally {
setUpgrading(false)
}
}}
>
<IoMdCloudDownload className="text-lg" />
</Button>
}
divider
>
<Select <Select
className="w-[100px]" className="w-[100px]"
size="sm" size="sm"

View File

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