Skip to content

Commit

Permalink
fix delay error
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Aug 4, 2024
1 parent 67f38bc commit 3730d09
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 40 deletions.
53 changes: 37 additions & 16 deletions src/main/core/mihomoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,60 +27,81 @@ export const getAxios = async (force: boolean = false): Promise<AxiosInstance> =

export async function mihomoVersion(): Promise<IMihomoVersion> {
const instance = await getAxios()
return instance.get('/version') as Promise<IMihomoVersion>
return instance.get('/version').catch((e) => {
return e.response.data
})
}

export const mihomoConfig = async (): Promise<IMihomoConfig> => {
const instance = await getAxios()
return instance.get('/configs') as Promise<IMihomoConfig>
return instance.get('/configs').catch((e) => {
return e.response.data
})
}

export const patchMihomoConfig = async (patch: Partial<IMihomoConfig>): Promise<void> => {
const instance = await getAxios()
return instance.patch('/configs', patch)
return instance.patch('/configs', patch).catch((e) => {
return e.response.data
})
}

export const mihomoConnections = async (): Promise<IMihomoConnectionsInfo> => {
const instance = await getAxios()
return instance.get('/connections') as Promise<IMihomoConnectionsInfo>
return instance.get('/connections').catch((e) => {
return e.response.data
})
}

export const mihomoCloseConnection = async (id: string): Promise<void> => {
const instance = await getAxios()
return instance.delete(`/connections/${encodeURIComponent(id)}`)
return instance.delete(`/connections/${encodeURIComponent(id)}`).catch((e) => {
return e.response.data
})
}

export const mihomoCloseAllConnections = async (): Promise<void> => {
const instance = await getAxios()
return instance.delete('/connections')
return instance.delete('/connections').catch((e) => {
return e.response.data
})
}

export const mihomoRules = async (): Promise<IMihomoRulesInfo> => {
const instance = await getAxios()
return instance.get('/rules') as Promise<IMihomoRulesInfo>
return instance.get('/rules').catch((e) => {
return e.response.data
})
}

export const mihomoProxies = async (): Promise<IMihomoProxies> => {
const instance = await getAxios()
return instance.get('/proxies') as Promise<IMihomoProxies>
return instance.get('/proxies').catch((e) => {
return e.response.data
})
}

export const mihomoChangeProxy = async (group: string, proxy: string): Promise<IMihomoProxy> => {
const instance = await getAxios()
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy })
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }).catch((e) => {
return e.response.data
})
}

export const mihomoProxyDelay = async (proxy: string, url?: string): Promise<IMihomoDelay> => {
const appConfig = getAppConfig()
const { delayTestUrl, delayTestTimeout } = appConfig
const instance = await getAxios()
return instance.get(`/proxies/${encodeURIComponent(proxy)}/delay`, {
params: {
url: url || delayTestUrl || 'https://www.gstatic.com/generate_204',
timeout: delayTestTimeout || 5000
},
timeout: delayTestTimeout || 5000
})
return instance
.get(`/proxies/${encodeURIComponent(proxy)}/delay`, {
params: {
url: url || delayTestUrl || 'https://www.gstatic.com/generate_204',
timeout: delayTestTimeout || 5000
}
})
.catch((e) => {
return e.response.data
})
}

export const startMihomoTraffic = (): void => {
Expand Down
40 changes: 19 additions & 21 deletions src/renderer/src/components/proxies/proxy-item.tsx
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@
import { Button, Card, CardBody } from '@nextui-org/react'
import { useAppConfig } from '@renderer/hooks/use-app-config'
import PubSub from 'pubsub-js'
import React, { useEffect, useState } from 'react'
import PubSub from 'pubsub-js'

interface Props {
mutateProxies: () => void
onProxyDelay: (proxy: string, url?: string) => Promise<IMihomoDelay>
proxyDisplayMode: 'simple' | 'full'
proxy: IMihomoProxy | IMihomoGroup
group: string
group: IMihomoGroup
onSelect: (group: string, proxy: string) => void
selected: boolean
}

const ProxyItem: React.FC<Props> = (props) => {
const { proxyDisplayMode, group, proxy, selected, onSelect, onProxyDelay } = props
const { appConfig } = useAppConfig()
const { delayTestTimeout = 5000 } = appConfig || {}
const { mutateProxies, proxyDisplayMode, group, proxy, selected, onSelect, onProxyDelay } = props
const [delay, setDelay] = useState(() => {
if (proxy.history.length > 0) {
return proxy.history[0].delay
return proxy.history[proxy.history.length - 1].delay
}
return 0
return -1
})
const [loading, setLoading] = useState(false)

function delayColor(delay: number): 'primary' | 'success' | 'warning' | 'danger' {
if (delay < 0) return 'danger'
if (delay === 0) return 'primary'
if (delay === -1) return 'primary'
if (delay === 0) return 'danger'
if (delay < 500) return 'success'
if (delay < delayTestTimeout) return 'warning'
return 'danger'
return 'warning'
}

function delayText(delay: number): string {
if (delay < 0) return 'Error'
if (delay === 0) return 'Delay'
if (delay < delayTestTimeout) return delay.toString()
return 'Timeout'
if (delay === -1) return 'Delay'
if (delay === 0) return 'Timeout'
return delay.toString()
}

const onDelay = (): void => {
setLoading(true)
onProxyDelay(proxy.name).then(
onProxyDelay(proxy.name, group.testUrl).then(
(delay) => {
setDelay(delay.delay || delayTestTimeout + 1)
setDelay(delay.delay || 0)
mutateProxies()
setLoading(false)
},
() => {
setDelay(-1)
setDelay(0)
setLoading(false)
}
)
}
console.log(delay)

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

return (): void => {
PubSub.unsubscribe(token)
}
}, [])
return (
<Card
onPress={() => onSelect(group, proxy.name)}
onPress={() => onSelect(group.name, proxy.name)}
isPressable
fullWidth
className={`${selected ? 'bg-primary/30' : ''}`}
Expand Down
9 changes: 7 additions & 2 deletions src/renderer/src/pages/proxies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const Proxies: React.FC = () => {
return await mihomoProxyDelay(proxy, url)
}

const onGroupDelay = async (group: string): Promise<void> => {
PubSub.publish(`${group}-delay`)
}

return (
<BasePage
title="代理组"
Expand Down Expand Up @@ -128,7 +132,7 @@ const Proxies: React.FC = () => {
size="sm"
isIconOnly
onPress={() => {
PubSub.publish(`${groups[index].name}-delay`)
onGroupDelay(groups[index].name)
}}
>
<MdOutlineSpeed className="text-lg text-default-500" />
Expand All @@ -147,10 +151,11 @@ const Proxies: React.FC = () => {
return allProxies[index] ? (
<div className="pt-2 mx-2">
<ProxyItem
mutateProxies={mutate}
onProxyDelay={onProxyDelay}
onSelect={onChangeProxy}
proxy={allProxies[index]}
group={groups[groupIndex].name}
group={groups[groupIndex]}
proxyDisplayMode={proxyDisplayMode}
selected={allProxies[index]?.name === groups[groupIndex].now}
/>
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/src/utils/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export async function mihomoChangeProxy(group: string, proxy: string): Promise<I
}

export async function mihomoProxyDelay(proxy: string, url?: string): Promise<IMihomoDelay> {
return await window.electron.ipcRenderer.invoke('mihomoProxyDelay', proxy, url)
const res = await window.electron.ipcRenderer.invoke('mihomoProxyDelay', proxy, url)
console.log(res)
return res
}

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

0 comments on commit 3730d09

Please sign in to comment.