mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 05:00:30 +08:00
fix delay error
This commit is contained in:
parent
f9ba7996c5
commit
0ed0931b75
@ -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 => {
|
||||
|
||||
@ -1,60 +1,58 @@
|
||||
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)
|
||||
@ -62,7 +60,7 @@ const ProxyItem: React.FC<Props> = (props) => {
|
||||
}, [])
|
||||
return (
|
||||
<Card
|
||||
onPress={() => onSelect(group, proxy.name)}
|
||||
onPress={() => onSelect(group.name, proxy.name)}
|
||||
isPressable
|
||||
fullWidth
|
||||
className={`${selected ? 'bg-primary/30' : ''}`}
|
||||
|
||||
@ -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="代理组"
|
||||
@ -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" />
|
||||
@ -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}
|
||||
/>
|
||||
|
||||
@ -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> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user