Skip to content

Commit 3730d09

Browse files
author
pompurin404
committed
fix delay error
1 parent 67f38bc commit 3730d09

File tree

4 files changed

+66
-40
lines changed

4 files changed

+66
-40
lines changed

src/main/core/mihomoApi.ts

+37-16
Original file line numberDiff line numberDiff line change
@@ -27,60 +27,81 @@ export const getAxios = async (force: boolean = false): Promise<AxiosInstance> =
2727

2828
export async function mihomoVersion(): Promise<IMihomoVersion> {
2929
const instance = await getAxios()
30-
return instance.get('/version') as Promise<IMihomoVersion>
30+
return instance.get('/version').catch((e) => {
31+
return e.response.data
32+
})
3133
}
3234

3335
export const mihomoConfig = async (): Promise<IMihomoConfig> => {
3436
const instance = await getAxios()
35-
return instance.get('/configs') as Promise<IMihomoConfig>
37+
return instance.get('/configs').catch((e) => {
38+
return e.response.data
39+
})
3640
}
3741

3842
export const patchMihomoConfig = async (patch: Partial<IMihomoConfig>): Promise<void> => {
3943
const instance = await getAxios()
40-
return instance.patch('/configs', patch)
44+
return instance.patch('/configs', patch).catch((e) => {
45+
return e.response.data
46+
})
4147
}
4248

4349
export const mihomoConnections = async (): Promise<IMihomoConnectionsInfo> => {
4450
const instance = await getAxios()
45-
return instance.get('/connections') as Promise<IMihomoConnectionsInfo>
51+
return instance.get('/connections').catch((e) => {
52+
return e.response.data
53+
})
4654
}
4755

4856
export const mihomoCloseConnection = async (id: string): Promise<void> => {
4957
const instance = await getAxios()
50-
return instance.delete(`/connections/${encodeURIComponent(id)}`)
58+
return instance.delete(`/connections/${encodeURIComponent(id)}`).catch((e) => {
59+
return e.response.data
60+
})
5161
}
5262

5363
export const mihomoCloseAllConnections = async (): Promise<void> => {
5464
const instance = await getAxios()
55-
return instance.delete('/connections')
65+
return instance.delete('/connections').catch((e) => {
66+
return e.response.data
67+
})
5668
}
5769

5870
export const mihomoRules = async (): Promise<IMihomoRulesInfo> => {
5971
const instance = await getAxios()
60-
return instance.get('/rules') as Promise<IMihomoRulesInfo>
72+
return instance.get('/rules').catch((e) => {
73+
return e.response.data
74+
})
6175
}
6276

6377
export const mihomoProxies = async (): Promise<IMihomoProxies> => {
6478
const instance = await getAxios()
65-
return instance.get('/proxies') as Promise<IMihomoProxies>
79+
return instance.get('/proxies').catch((e) => {
80+
return e.response.data
81+
})
6682
}
6783

6884
export const mihomoChangeProxy = async (group: string, proxy: string): Promise<IMihomoProxy> => {
6985
const instance = await getAxios()
70-
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy })
86+
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch((e) => {
87+
return e.response.data
88+
})
7189
}
7290

7391
export const mihomoProxyDelay = async (proxy: string, url?: string): Promise<IMihomoDelay> => {
7492
const appConfig = getAppConfig()
7593
const { delayTestUrl, delayTestTimeout } = appConfig
7694
const instance = await getAxios()
77-
return instance.get(`/proxies/${encodeURIComponent(proxy)}/delay`, {
78-
params: {
79-
url: url || delayTestUrl || 'https://www.gstatic.com/generate_204',
80-
timeout: delayTestTimeout || 5000
81-
},
82-
timeout: delayTestTimeout || 5000
83-
})
95+
return instance
96+
.get(`/proxies/${encodeURIComponent(proxy)}/delay`, {
97+
params: {
98+
url: url || delayTestUrl || 'https://www.gstatic.com/generate_204',
99+
timeout: delayTestTimeout || 5000
100+
}
101+
})
102+
.catch((e) => {
103+
return e.response.data
104+
})
84105
}
85106

86107
export const startMihomoTraffic = (): void => {

src/renderer/src/components/proxies/proxy-item.tsx

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,66 @@
11
import { Button, Card, CardBody } from '@nextui-org/react'
2-
import { useAppConfig } from '@renderer/hooks/use-app-config'
3-
import PubSub from 'pubsub-js'
42
import React, { useEffect, useState } from 'react'
3+
import PubSub from 'pubsub-js'
54

65
interface Props {
6+
mutateProxies: () => void
77
onProxyDelay: (proxy: string, url?: string) => Promise<IMihomoDelay>
88
proxyDisplayMode: 'simple' | 'full'
99
proxy: IMihomoProxy | IMihomoGroup
10-
group: string
10+
group: IMihomoGroup
1111
onSelect: (group: string, proxy: string) => void
1212
selected: boolean
1313
}
1414

1515
const ProxyItem: React.FC<Props> = (props) => {
16-
const { proxyDisplayMode, group, proxy, selected, onSelect, onProxyDelay } = props
17-
const { appConfig } = useAppConfig()
18-
const { delayTestTimeout = 5000 } = appConfig || {}
16+
const { mutateProxies, proxyDisplayMode, group, proxy, selected, onSelect, onProxyDelay } = props
1917
const [delay, setDelay] = useState(() => {
2018
if (proxy.history.length > 0) {
21-
return proxy.history[0].delay
19+
return proxy.history[proxy.history.length - 1].delay
2220
}
23-
return 0
21+
return -1
2422
})
2523
const [loading, setLoading] = useState(false)
2624

2725
function delayColor(delay: number): 'primary' | 'success' | 'warning' | 'danger' {
28-
if (delay < 0) return 'danger'
29-
if (delay === 0) return 'primary'
26+
if (delay === -1) return 'primary'
27+
if (delay === 0) return 'danger'
3028
if (delay < 500) return 'success'
31-
if (delay < delayTestTimeout) return 'warning'
32-
return 'danger'
29+
return 'warning'
3330
}
3431

3532
function delayText(delay: number): string {
36-
if (delay < 0) return 'Error'
37-
if (delay === 0) return 'Delay'
38-
if (delay < delayTestTimeout) return delay.toString()
39-
return 'Timeout'
33+
if (delay === -1) return 'Delay'
34+
if (delay === 0) return 'Timeout'
35+
return delay.toString()
4036
}
4137

4238
const onDelay = (): void => {
4339
setLoading(true)
44-
onProxyDelay(proxy.name).then(
40+
onProxyDelay(proxy.name, group.testUrl).then(
4541
(delay) => {
46-
setDelay(delay.delay || delayTestTimeout + 1)
42+
setDelay(delay.delay || 0)
43+
mutateProxies()
4744
setLoading(false)
4845
},
4946
() => {
50-
setDelay(-1)
47+
setDelay(0)
5148
setLoading(false)
5249
}
5350
)
5451
}
52+
console.log(delay)
5553

5654
useEffect(() => {
57-
const token = PubSub.subscribe(`${group}-delay`, onDelay)
55+
const token = PubSub.subscribe(`${group.name}-delay`, onDelay)
5856

5957
return (): void => {
6058
PubSub.unsubscribe(token)
6159
}
6260
}, [])
6361
return (
6462
<Card
65-
onPress={() => onSelect(group, proxy.name)}
63+
onPress={() => onSelect(group.name, proxy.name)}
6664
isPressable
6765
fullWidth
6866
className={`${selected ? 'bg-primary/30' : ''}`}

src/renderer/src/pages/proxies.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ const Proxies: React.FC = () => {
6161
return await mihomoProxyDelay(proxy, url)
6262
}
6363

64+
const onGroupDelay = async (group: string): Promise<void> => {
65+
PubSub.publish(`${group}-delay`)
66+
}
67+
6468
return (
6569
<BasePage
6670
title="代理组"
@@ -128,7 +132,7 @@ const Proxies: React.FC = () => {
128132
size="sm"
129133
isIconOnly
130134
onPress={() => {
131-
PubSub.publish(`${groups[index].name}-delay`)
135+
onGroupDelay(groups[index].name)
132136
}}
133137
>
134138
<MdOutlineSpeed className="text-lg text-default-500" />
@@ -147,10 +151,11 @@ const Proxies: React.FC = () => {
147151
return allProxies[index] ? (
148152
<div className="pt-2 mx-2">
149153
<ProxyItem
154+
mutateProxies={mutate}
150155
onProxyDelay={onProxyDelay}
151156
onSelect={onChangeProxy}
152157
proxy={allProxies[index]}
153-
group={groups[groupIndex].name}
158+
group={groups[groupIndex]}
154159
proxyDisplayMode={proxyDisplayMode}
155160
selected={allProxies[index]?.name === groups[groupIndex].now}
156161
/>

src/renderer/src/utils/ipc.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export async function mihomoChangeProxy(group: string, proxy: string): Promise<I
3131
}
3232

3333
export async function mihomoProxyDelay(proxy: string, url?: string): Promise<IMihomoDelay> {
34-
return await window.electron.ipcRenderer.invoke('mihomoProxyDelay', proxy, url)
34+
const res = await window.electron.ipcRenderer.invoke('mihomoProxyDelay', proxy, url)
35+
console.log(res)
36+
return res
3537
}
3638

3739
export async function startMihomoLogs(): Promise<void> {

0 commit comments

Comments
 (0)