Skip to content

Commit 3f7694f

Browse files
author
Pompurin404
committed
connections page
1 parent 18ef4a9 commit 3f7694f

File tree

8 files changed

+267
-75
lines changed

8 files changed

+267
-75
lines changed

src/main/core/mihomoApi.ts

+42-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let axiosIns: AxiosInstance = null!
77
let mihomoTrafficWs: WebSocket | null = null
88
let mihomoMemoryWs: WebSocket | null = null
99
let mihomoLogsWs: WebSocket | null = null
10+
let mihomoConnectionsWs: WebSocket | null = null
1011

1112
export const getAxios = async (force: boolean = false): Promise<AxiosInstance> => {
1213
if (axiosIns && !force) return axiosIns
@@ -46,13 +47,6 @@ export const patchMihomoConfig = async (patch: Partial<IMihomoConfig>): Promise<
4647
})) as Promise<void>
4748
}
4849

49-
export const mihomoConnections = async (): Promise<IMihomoConnectionsInfo> => {
50-
const instance = await getAxios()
51-
return (await instance.get('/connections').catch(() => {
52-
return { downloadTotal: 0, uploadTotal: 0, connections: [], memory: 0 }
53-
})) as IMihomoConnectionsInfo
54-
}
55-
5650
export const mihomoCloseConnection = async (id: string): Promise<void> => {
5751
const instance = await getAxios()
5852
return (await instance.delete(`/connections/${encodeURIComponent(id)}`).catch((e) => {
@@ -230,3 +224,44 @@ const mihomoLogs = (): void => {
230224
}
231225
}
232226
}
227+
228+
export const startMihomoConnections = (): void => {
229+
mihomoConnections()
230+
}
231+
232+
export const stopMihomoConnections = (): void => {
233+
if (mihomoConnectionsWs) {
234+
mihomoConnectionsWs.removeAllListeners()
235+
if (mihomoConnectionsWs.readyState === WebSocket.OPEN) {
236+
mihomoConnectionsWs.close()
237+
}
238+
mihomoConnectionsWs = null
239+
}
240+
}
241+
242+
const mihomoConnections = (): void => {
243+
let server = getControledMihomoConfig()['external-controller']
244+
const secret = getControledMihomoConfig().secret ?? ''
245+
if (server?.startsWith(':')) server = `127.0.0.1${server}`
246+
stopMihomoConnections()
247+
248+
mihomoConnectionsWs = new WebSocket(
249+
`ws://${server}/connections?token=${encodeURIComponent(secret)}`
250+
)
251+
252+
mihomoConnectionsWs.onmessage = (e): void => {
253+
const data = e.data as string
254+
window?.webContents.send('mihomoConnections', JSON.parse(data) as IMihomoConnectionsInfo)
255+
}
256+
257+
mihomoConnectionsWs.onclose = (): void => {
258+
mihomoConnections()
259+
}
260+
261+
mihomoConnectionsWs.onerror = (): void => {
262+
if (mihomoConnectionsWs) {
263+
mihomoConnectionsWs.close()
264+
mihomoConnectionsWs = null
265+
}
266+
}
267+
}

src/main/utils/ipc.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import {
44
mihomoCloseAllConnections,
55
mihomoCloseConnection,
66
mihomoConfig,
7-
mihomoConnections,
87
mihomoProxies,
98
mihomoProxyDelay,
109
mihomoRules,
1110
mihomoVersion,
1211
patchMihomoConfig,
12+
startMihomoConnections,
1313
startMihomoLogs,
14+
stopMihomoConnections,
1415
stopMihomoLogs
1516
} from '../core/mihomoApi'
1617
import { checkAutoRun, disableAutoRun, enableAutoRun } from '../resolve/autoRun'
@@ -36,7 +37,6 @@ import { checkUpdate } from '../resolve/autoUpdater'
3637
export function registerIpcMainHandlers(): void {
3738
ipcMain.handle('mihomoVersion', mihomoVersion)
3839
ipcMain.handle('mihomoConfig', mihomoConfig)
39-
ipcMain.handle('mihomoConnections', mihomoConnections)
4040
ipcMain.handle('mihomoCloseConnection', (_e, id) => mihomoCloseConnection(id))
4141
ipcMain.handle('mihomoCloseAllConnections', mihomoCloseAllConnections)
4242
ipcMain.handle('mihomoRules', mihomoRules)
@@ -45,6 +45,8 @@ export function registerIpcMainHandlers(): void {
4545
ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => mihomoProxyDelay(proxy, url))
4646
ipcMain.handle('startMihomoLogs', startMihomoLogs)
4747
ipcMain.handle('stopMihomoLogs', stopMihomoLogs)
48+
ipcMain.handle('startMihomoConnections', () => startMihomoConnections())
49+
ipcMain.handle('stopMihomoConnections', () => stopMihomoConnections())
4850
ipcMain.handle('patchMihomoConfig', (_e, patch) => patchMihomoConfig(patch))
4951
ipcMain.handle('checkAutoRun', checkAutoRun)
5052
ipcMain.handle('enableAutoRun', enableAutoRun)

src/renderer/src/assets/main.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
*::-webkit-scrollbar {
1010
width: 8px;
11-
height: 6px;
11+
height: 8px;
12+
}
13+
14+
*::-webkit-scrollbar-corner {
15+
background-color: transparent;
1216
}
1317

1418
/* Light mode */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, Button } from '@nextui-org/react'
2+
import React from 'react'
3+
interface Props {
4+
connection: IMihomoConnectionDetail
5+
onClose: () => void
6+
}
7+
const ConnectionDetailModal: React.FC<Props> = (props) => {
8+
const { connection, onClose } = props
9+
10+
return (
11+
<Modal size="xl" hideCloseButton isOpen={true} scrollBehavior="inside">
12+
<ModalContent>
13+
<ModalHeader className="flex">连接详情</ModalHeader>
14+
<ModalBody>
15+
<pre>
16+
<code>{JSON.stringify(connection, null, 2)}</code>
17+
</pre>
18+
</ModalBody>
19+
<ModalFooter>
20+
<Button variant="light" onPress={onClose}>
21+
关闭
22+
</Button>
23+
</ModalFooter>
24+
</ModalContent>
25+
</Modal>
26+
)
27+
}
28+
29+
export default ConnectionDetailModal

src/renderer/src/components/connections/connection-item.tsx

-40
This file was deleted.

0 commit comments

Comments
 (0)