Skip to content

Commit 7f539ad

Browse files
author
pompurin404
committed
stop and recover websocket
1 parent f1916c1 commit 7f539ad

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/main/config/profile.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import yaml from 'yaml'
88
import fs from 'fs'
99
import { dialog } from 'electron'
1010
import { addProfileUpdater } from '../core/profileUpdater'
11+
import { pauseWebsockets } from '../core/mihomoApi'
1112

1213
let profileConfig: IProfileConfig // profile.yaml
1314

@@ -27,7 +28,9 @@ export async function changeCurrentProfile(id: string): Promise<void> {
2728
const oldId = getProfileConfig().current
2829
profileConfig.current = id
2930
try {
31+
const recover = pauseWebsockets()
3032
await startCore()
33+
recover()
3134
} catch (e) {
3235
profileConfig.current = oldId
3336
} finally {
@@ -177,7 +180,9 @@ export function getProfileStr(id: string): string {
177180
export async function setProfileStr(id: string, content: string): Promise<void> {
178181
fs.writeFileSync(profilePath(id), content, 'utf-8')
179182
if (id === getProfileConfig().current) {
183+
const recover = pauseWebsockets()
180184
await startCore()
185+
recover()
181186
}
182187
}
183188

src/main/core/mihomoApi.ts

+23
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,26 @@ const mihomoConnections = (): void => {
340340
}
341341
}
342342
}
343+
344+
export const pauseWebsockets = () => {
345+
const recoverList: (() => void)[] = []
346+
if (mihomoTrafficWs) {
347+
stopMihomoTraffic()
348+
recoverList.push(startMihomoTraffic)
349+
}
350+
if (mihomoMemoryWs) {
351+
stopMihomoMemory()
352+
recoverList.push(startMihomoMemory)
353+
}
354+
if (mihomoLogsWs) {
355+
stopMihomoLogs()
356+
recoverList.push(startMihomoLogs)
357+
}
358+
if (mihomoConnectionsWs) {
359+
stopMihomoConnections()
360+
recoverList.push(startMihomoConnections)
361+
}
362+
return (): void => {
363+
recoverList.forEach((recover) => recover())
364+
}
365+
}

src/main/utils/ipc.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
mihomoUpgradeGeo,
1515
mihomoVersion,
1616
patchMihomoConfig,
17+
pauseWebsockets,
1718
startMihomoConnections,
1819
startMihomoLogs,
1920
stopMihomoConnections,
@@ -79,7 +80,11 @@ export function registerIpcMainHandlers(): void {
7980
ipcMain.handle('changeCurrentProfile', (_e, id) => changeCurrentProfile(id))
8081
ipcMain.handle('addProfileItem', (_e, item) => addProfileItem(item))
8182
ipcMain.handle('removeProfileItem', (_e, id) => removeProfileItem(id))
82-
ipcMain.handle('restartCore', startCore)
83+
ipcMain.handle('restartCore', async () => {
84+
const recover = pauseWebsockets()
85+
await startCore()
86+
recover()
87+
})
8388
ipcMain.handle('triggerSysProxy', (_e, enable) => triggerSysProxy(enable))
8489
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
8590
ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str))

0 commit comments

Comments
 (0)