always keep websocket

This commit is contained in:
pompurin404 2024-09-03 14:53:47 +08:00
parent 5438aef528
commit 4a067c4f51
No known key found for this signature in database
8 changed files with 12 additions and 70 deletions

View File

@ -1,7 +1,7 @@
import { controledMihomoConfigPath } from '../utils/dirs'
import { readFile, writeFile } from 'fs/promises'
import yaml from 'yaml'
import { getAxios, startMihomoMemory, startMihomoTraffic } from '../core/mihomoApi'
import { getAxios } from '../core/mihomoApi'
import { generateProfile } from '../core/factory'
import { getAppConfig } from './app'
import { defaultControledMihomoConfig } from '../utils/template'
@ -50,8 +50,6 @@ export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>):
if (patch['external-controller'] || patch.secret) {
await getAxios(true)
await startMihomoMemory()
await startMihomoTraffic()
}
await generateProfile()
await writeFile(controledMihomoConfigPath(), yaml.stringify(controledMihomoConfig), 'utf-8')

View File

@ -10,7 +10,12 @@ import {
import { generateProfile } from './factory'
import { getAppConfig, getControledMihomoConfig, patchAppConfig } from '../config'
import { dialog, safeStorage } from 'electron'
import { pauseWebsockets, startMihomoTraffic } from './mihomoApi'
import {
startMihomoTraffic,
startMihomoConnections,
startMihomoLogs,
startMihomoMemory
} from './mihomoApi'
import chokidar from 'chokidar'
import { writeFile } from 'fs/promises'
import { promisify } from 'util'
@ -96,6 +101,9 @@ export async function startCore(): Promise<Promise<void>[]> {
})
])
await startMihomoTraffic()
await startMihomoConnections()
await startMihomoLogs()
await startMihomoMemory()
retry = 10
}
})
@ -121,9 +129,7 @@ export async function stopCore(force = false): Promise<void> {
export async function restartCore(): Promise<void> {
try {
const recover = pauseWebsockets()
await startCore()
recover()
} catch (e) {
dialog.showErrorBox('内核启动出错', `${e}`)
}

View File

@ -347,24 +347,3 @@ const mihomoConnections = async (): Promise<void> => {
}
}
}
export const pauseWebsockets = () => {
const recoverList: (() => void)[] = []
// Traffic 内核启动时始终开启
stopMihomoTraffic()
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

@ -2,7 +2,6 @@ import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import { registerIpcMainHandlers } from './utils/ipc'
import windowStateKeeper from 'electron-window-state'
import { app, shell, BrowserWindow, Menu, dialog, Notification } from 'electron'
import { pauseWebsockets, startMihomoMemory, stopMihomoMemory } from './core/mihomoApi'
import { addProfileItem, getAppConfig } from './config'
import { startCore, stopCore } from './core/manager'
import { triggerSysProxy } from './sys/sysproxy'
@ -73,7 +72,6 @@ app.on('window-all-closed', (e) => {
})
app.on('before-quit', async () => {
pauseWebsockets()
await stopCore()
triggerSysProxy(false)
app.exit()
@ -186,13 +184,8 @@ export async function createWindow(): Promise<void> {
mainWindow?.webContents.reload()
})
mainWindow.on('show', () => {
startMihomoMemory()
})
mainWindow.on('close', (event) => {
event.preventDefault()
stopMihomoMemory()
mainWindow?.hide()
})

View File

@ -15,11 +15,7 @@ import {
mihomoUpgrade,
mihomoUpgradeGeo,
mihomoVersion,
patchMihomoConfig,
startMihomoConnections,
startMihomoLogs,
stopMihomoConnections,
stopMihomoLogs
patchMihomoConfig
} from '../core/mihomoApi'
import { checkAutoRun, disableAutoRun, enableAutoRun } from '../sys/autoRun'
import {
@ -112,10 +108,6 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('mihomoGroupDelay', (_e, group, url) =>
ipcErrorWrapper(mihomoGroupDelay)(group, url)
)
ipcMain.handle('startMihomoLogs', ipcErrorWrapper(startMihomoLogs))
ipcMain.handle('stopMihomoLogs', stopMihomoLogs)
ipcMain.handle('startMihomoConnections', ipcErrorWrapper(startMihomoConnections))
ipcMain.handle('stopMihomoConnections', stopMihomoConnections)
ipcMain.handle('patchMihomoConfig', (_e, patch) => ipcErrorWrapper(patchMihomoConfig)(patch))
ipcMain.handle('checkAutoRun', ipcErrorWrapper(checkAutoRun))
ipcMain.handle('enableAutoRun', ipcErrorWrapper(enableAutoRun))

View File

@ -1,10 +1,5 @@
import BasePage from '@renderer/components/base/base-page'
import {
mihomoCloseAllConnections,
mihomoCloseConnection,
startMihomoConnections,
stopMihomoConnections
} from '@renderer/utils/ipc'
import { mihomoCloseAllConnections, mihomoCloseConnection } from '@renderer/utils/ipc'
import { useEffect, useMemo, useState } from 'react'
import { Badge, Button, Divider, Input, Select, SelectItem } from '@nextui-org/react'
import { calcTraffic } from '@renderer/utils/calc'
@ -66,7 +61,6 @@ const Connections: React.FC = () => {
}, [connections, filter])
useEffect(() => {
startMihomoConnections()
window.electron.ipcRenderer.on('mihomoConnections', (_e, info: IMihomoConnectionsInfo) => {
setConnectionsInfo(info)
const newConns: IMihomoConnectionDetail[] = []
@ -84,7 +78,6 @@ const Connections: React.FC = () => {
})
return (): void => {
stopMihomoConnections()
window.electron.ipcRenderer.removeAllListeners('mihomoConnections')
}
}, [])

View File

@ -1,5 +1,4 @@
import BasePage from '@renderer/components/base/base-page'
import { startMihomoLogs, stopMihomoLogs } from '@renderer/utils/ipc'
import LogItem from '@renderer/components/logs/log-item'
import { useEffect, useMemo, useRef, useState } from 'react'
import { Button, Divider, Input } from '@nextui-org/react'
@ -29,7 +28,6 @@ const Logs: React.FC = () => {
}, [filteredLogs, trace])
useEffect(() => {
startMihomoLogs()
window.electron.ipcRenderer.on('mihomoLogs', (_e, log: IMihomoLogInfo) => {
log.time = new Date().toLocaleString()
setLogs((prevLogs) => {
@ -38,7 +36,6 @@ const Logs: React.FC = () => {
})
return (): void => {
stopMihomoLogs()
window.electron.ipcRenderer.removeAllListeners('mihomoLogs')
}
}, [])

View File

@ -75,22 +75,6 @@ export async function mihomoGroupDelay(group: string, url?: string): Promise<IMi
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('mihomoGroupDelay', group, url))
}
export async function startMihomoLogs(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('startMihomoLogs'))
}
export async function stopMihomoLogs(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('stopMihomoLogs'))
}
export async function startMihomoConnections(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('startMihomoConnections'))
}
export async function stopMihomoConnections(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('stopMihomoConnections'))
}
export async function patchMihomoConfig(patch: Partial<IMihomoConfig>): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('patchMihomoConfig', patch))
}