mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 13:10:30 +08:00
fix delay error
This commit is contained in:
parent
f9ba7996c5
commit
0ed0931b75
@ -27,59 +27,80 @@ export const getAxios = async (force: boolean = false): Promise<AxiosInstance> =
|
|||||||
|
|
||||||
export async function mihomoVersion(): Promise<IMihomoVersion> {
|
export async function mihomoVersion(): Promise<IMihomoVersion> {
|
||||||
const instance = await getAxios()
|
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> => {
|
export const mihomoConfig = async (): Promise<IMihomoConfig> => {
|
||||||
const instance = await getAxios()
|
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> => {
|
export const patchMihomoConfig = async (patch: Partial<IMihomoConfig>): Promise<void> => {
|
||||||
const instance = await getAxios()
|
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> => {
|
export const mihomoConnections = async (): Promise<IMihomoConnectionsInfo> => {
|
||||||
const instance = await getAxios()
|
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> => {
|
export const mihomoCloseConnection = async (id: string): Promise<void> => {
|
||||||
const instance = await getAxios()
|
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> => {
|
export const mihomoCloseAllConnections = async (): Promise<void> => {
|
||||||
const instance = await getAxios()
|
const instance = await getAxios()
|
||||||
return instance.delete('/connections')
|
return instance.delete('/connections').catch((e) => {
|
||||||
|
return e.response.data
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const mihomoRules = async (): Promise<IMihomoRulesInfo> => {
|
export const mihomoRules = async (): Promise<IMihomoRulesInfo> => {
|
||||||
const instance = await getAxios()
|
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> => {
|
export const mihomoProxies = async (): Promise<IMihomoProxies> => {
|
||||||
const instance = await getAxios()
|
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> => {
|
export const mihomoChangeProxy = async (group: string, proxy: string): Promise<IMihomoProxy> => {
|
||||||
const instance = await getAxios()
|
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> => {
|
export const mihomoProxyDelay = async (proxy: string, url?: string): Promise<IMihomoDelay> => {
|
||||||
const appConfig = getAppConfig()
|
const appConfig = getAppConfig()
|
||||||
const { delayTestUrl, delayTestTimeout } = appConfig
|
const { delayTestUrl, delayTestTimeout } = appConfig
|
||||||
const instance = await getAxios()
|
const instance = await getAxios()
|
||||||
return instance.get(`/proxies/${encodeURIComponent(proxy)}/delay`, {
|
return instance
|
||||||
|
.get(`/proxies/${encodeURIComponent(proxy)}/delay`, {
|
||||||
params: {
|
params: {
|
||||||
url: url || delayTestUrl || 'https://www.gstatic.com/generate_204',
|
url: url || delayTestUrl || 'https://www.gstatic.com/generate_204',
|
||||||
timeout: delayTestTimeout || 5000
|
timeout: delayTestTimeout || 5000
|
||||||
},
|
}
|
||||||
timeout: delayTestTimeout || 5000
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
return e.response.data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,60 +1,58 @@
|
|||||||
import { Button, Card, CardBody } from '@nextui-org/react'
|
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 React, { useEffect, useState } from 'react'
|
||||||
|
import PubSub from 'pubsub-js'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
|
mutateProxies: () => void
|
||||||
onProxyDelay: (proxy: string, url?: string) => Promise<IMihomoDelay>
|
onProxyDelay: (proxy: string, url?: string) => Promise<IMihomoDelay>
|
||||||
proxyDisplayMode: 'simple' | 'full'
|
proxyDisplayMode: 'simple' | 'full'
|
||||||
proxy: IMihomoProxy | IMihomoGroup
|
proxy: IMihomoProxy | IMihomoGroup
|
||||||
group: string
|
group: IMihomoGroup
|
||||||
onSelect: (group: string, proxy: string) => void
|
onSelect: (group: string, proxy: string) => void
|
||||||
selected: boolean
|
selected: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const ProxyItem: React.FC<Props> = (props) => {
|
const ProxyItem: React.FC<Props> = (props) => {
|
||||||
const { proxyDisplayMode, group, proxy, selected, onSelect, onProxyDelay } = props
|
const { mutateProxies, proxyDisplayMode, group, proxy, selected, onSelect, onProxyDelay } = props
|
||||||
const { appConfig } = useAppConfig()
|
|
||||||
const { delayTestTimeout = 5000 } = appConfig || {}
|
|
||||||
const [delay, setDelay] = useState(() => {
|
const [delay, setDelay] = useState(() => {
|
||||||
if (proxy.history.length > 0) {
|
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)
|
const [loading, setLoading] = useState(false)
|
||||||
|
|
||||||
function delayColor(delay: number): 'primary' | 'success' | 'warning' | 'danger' {
|
function delayColor(delay: number): 'primary' | 'success' | 'warning' | 'danger' {
|
||||||
if (delay < 0) return 'danger'
|
if (delay === -1) return 'primary'
|
||||||
if (delay === 0) return 'primary'
|
if (delay === 0) return 'danger'
|
||||||
if (delay < 500) return 'success'
|
if (delay < 500) return 'success'
|
||||||
if (delay < delayTestTimeout) return 'warning'
|
return 'warning'
|
||||||
return 'danger'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function delayText(delay: number): string {
|
function delayText(delay: number): string {
|
||||||
if (delay < 0) return 'Error'
|
if (delay === -1) return 'Delay'
|
||||||
if (delay === 0) return 'Delay'
|
if (delay === 0) return 'Timeout'
|
||||||
if (delay < delayTestTimeout) return delay.toString()
|
return delay.toString()
|
||||||
return 'Timeout'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const onDelay = (): void => {
|
const onDelay = (): void => {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
onProxyDelay(proxy.name).then(
|
onProxyDelay(proxy.name, group.testUrl).then(
|
||||||
(delay) => {
|
(delay) => {
|
||||||
setDelay(delay.delay || delayTestTimeout + 1)
|
setDelay(delay.delay || 0)
|
||||||
|
mutateProxies()
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
setDelay(-1)
|
setDelay(0)
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
console.log(delay)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const token = PubSub.subscribe(`${group}-delay`, onDelay)
|
const token = PubSub.subscribe(`${group.name}-delay`, onDelay)
|
||||||
|
|
||||||
return (): void => {
|
return (): void => {
|
||||||
PubSub.unsubscribe(token)
|
PubSub.unsubscribe(token)
|
||||||
@ -62,7 +60,7 @@ const ProxyItem: React.FC<Props> = (props) => {
|
|||||||
}, [])
|
}, [])
|
||||||
return (
|
return (
|
||||||
<Card
|
<Card
|
||||||
onPress={() => onSelect(group, proxy.name)}
|
onPress={() => onSelect(group.name, proxy.name)}
|
||||||
isPressable
|
isPressable
|
||||||
fullWidth
|
fullWidth
|
||||||
className={`${selected ? 'bg-primary/30' : ''}`}
|
className={`${selected ? 'bg-primary/30' : ''}`}
|
||||||
|
|||||||
@ -61,6 +61,10 @@ const Proxies: React.FC = () => {
|
|||||||
return await mihomoProxyDelay(proxy, url)
|
return await mihomoProxyDelay(proxy, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const onGroupDelay = async (group: string): Promise<void> => {
|
||||||
|
PubSub.publish(`${group}-delay`)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BasePage
|
<BasePage
|
||||||
title="代理组"
|
title="代理组"
|
||||||
@ -128,7 +132,7 @@ const Proxies: React.FC = () => {
|
|||||||
size="sm"
|
size="sm"
|
||||||
isIconOnly
|
isIconOnly
|
||||||
onPress={() => {
|
onPress={() => {
|
||||||
PubSub.publish(`${groups[index].name}-delay`)
|
onGroupDelay(groups[index].name)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<MdOutlineSpeed className="text-lg text-default-500" />
|
<MdOutlineSpeed className="text-lg text-default-500" />
|
||||||
@ -147,10 +151,11 @@ const Proxies: React.FC = () => {
|
|||||||
return allProxies[index] ? (
|
return allProxies[index] ? (
|
||||||
<div className="pt-2 mx-2">
|
<div className="pt-2 mx-2">
|
||||||
<ProxyItem
|
<ProxyItem
|
||||||
|
mutateProxies={mutate}
|
||||||
onProxyDelay={onProxyDelay}
|
onProxyDelay={onProxyDelay}
|
||||||
onSelect={onChangeProxy}
|
onSelect={onChangeProxy}
|
||||||
proxy={allProxies[index]}
|
proxy={allProxies[index]}
|
||||||
group={groups[groupIndex].name}
|
group={groups[groupIndex]}
|
||||||
proxyDisplayMode={proxyDisplayMode}
|
proxyDisplayMode={proxyDisplayMode}
|
||||||
selected={allProxies[index]?.name === groups[groupIndex].now}
|
selected={allProxies[index]?.name === groups[groupIndex].now}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -31,7 +31,9 @@ export async function mihomoChangeProxy(group: string, proxy: string): Promise<I
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function mihomoProxyDelay(proxy: string, url?: string): Promise<IMihomoDelay> {
|
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> {
|
export async function startMihomoLogs(): Promise<void> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user