stop and recover websocket

This commit is contained in:
pompurin404 2024-08-10 15:31:33 +08:00
parent f1916c1027
commit 7f539ad75a
No known key found for this signature in database
3 changed files with 34 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import yaml from 'yaml'
import fs from 'fs'
import { dialog } from 'electron'
import { addProfileUpdater } from '../core/profileUpdater'
import { pauseWebsockets } from '../core/mihomoApi'
let profileConfig: IProfileConfig // profile.yaml
@ -27,7 +28,9 @@ export async function changeCurrentProfile(id: string): Promise<void> {
const oldId = getProfileConfig().current
profileConfig.current = id
try {
const recover = pauseWebsockets()
await startCore()
recover()
} catch (e) {
profileConfig.current = oldId
} finally {
@ -177,7 +180,9 @@ export function getProfileStr(id: string): string {
export async function setProfileStr(id: string, content: string): Promise<void> {
fs.writeFileSync(profilePath(id), content, 'utf-8')
if (id === getProfileConfig().current) {
const recover = pauseWebsockets()
await startCore()
recover()
}
}

View File

@ -340,3 +340,26 @@ const mihomoConnections = (): void => {
}
}
}
export const pauseWebsockets = () => {
const recoverList: (() => void)[] = []
if (mihomoTrafficWs) {
stopMihomoTraffic()
recoverList.push(startMihomoTraffic)
}
if (mihomoMemoryWs) {
stopMihomoMemory()
recoverList.push(startMihomoMemory)
}
if (mihomoLogsWs) {
stopMihomoLogs()
recoverList.push(startMihomoLogs)
}
if (mihomoConnectionsWs) {
stopMihomoConnections()
recoverList.push(startMihomoConnections)
}
return (): void => {
recoverList.forEach((recover) => recover())
}
}

View File

@ -14,6 +14,7 @@ import {
mihomoUpgradeGeo,
mihomoVersion,
patchMihomoConfig,
pauseWebsockets,
startMihomoConnections,
startMihomoLogs,
stopMihomoConnections,
@ -79,7 +80,11 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('changeCurrentProfile', (_e, id) => changeCurrentProfile(id))
ipcMain.handle('addProfileItem', (_e, item) => addProfileItem(item))
ipcMain.handle('removeProfileItem', (_e, id) => removeProfileItem(id))
ipcMain.handle('restartCore', startCore)
ipcMain.handle('restartCore', async () => {
const recover = pauseWebsockets()
await startCore()
recover()
})
ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable))
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str))