From 3730d09045b3ccb74c0413c9c42970fc642c68a1 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Sun, 4 Aug 2024 21:29:24 +0800 Subject: [PATCH] fix delay error --- src/main/core/mihomoApi.ts | 53 +++++++++++++------ .../src/components/proxies/proxy-item.tsx | 40 +++++++------- src/renderer/src/pages/proxies.tsx | 9 +++- src/renderer/src/utils/ipc.ts | 4 +- 4 files changed, 66 insertions(+), 40 deletions(-) diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 8eb61192..4cb665e0 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -27,60 +27,81 @@ export const getAxios = async (force: boolean = false): Promise = export async function mihomoVersion(): Promise { const instance = await getAxios() - return instance.get('/version') as Promise + return instance.get('/version').catch((e) => { + return e.response.data + }) } export const mihomoConfig = async (): Promise => { const instance = await getAxios() - return instance.get('/configs') as Promise + return instance.get('/configs').catch((e) => { + return e.response.data + }) } export const patchMihomoConfig = async (patch: Partial): Promise => { 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 => { const instance = await getAxios() - return instance.get('/connections') as Promise + return instance.get('/connections').catch((e) => { + return e.response.data + }) } export const mihomoCloseConnection = async (id: string): Promise => { 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 => { const instance = await getAxios() - return instance.delete('/connections') + return instance.delete('/connections').catch((e) => { + return e.response.data + }) } export const mihomoRules = async (): Promise => { const instance = await getAxios() - return instance.get('/rules') as Promise + return instance.get('/rules').catch((e) => { + return e.response.data + }) } export const mihomoProxies = async (): Promise => { const instance = await getAxios() - return instance.get('/proxies') as Promise + return instance.get('/proxies').catch((e) => { + return e.response.data + }) } export const mihomoChangeProxy = async (group: string, proxy: string): Promise => { 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 => { 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 => { diff --git a/src/renderer/src/components/proxies/proxy-item.tsx b/src/renderer/src/components/proxies/proxy-item.tsx index 722e586f..2ad38585 100644 --- a/src/renderer/src/components/proxies/proxy-item.tsx +++ b/src/renderer/src/components/proxies/proxy-item.tsx @@ -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 proxyDisplayMode: 'simple' | 'full' proxy: IMihomoProxy | IMihomoGroup - group: string + group: IMihomoGroup onSelect: (group: string, proxy: string) => void selected: boolean } const ProxyItem: React.FC = (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) => { }, []) return ( onSelect(group, proxy.name)} + onPress={() => onSelect(group.name, proxy.name)} isPressable fullWidth className={`${selected ? 'bg-primary/30' : ''}`} diff --git a/src/renderer/src/pages/proxies.tsx b/src/renderer/src/pages/proxies.tsx index 7eca643d..47d5931e 100644 --- a/src/renderer/src/pages/proxies.tsx +++ b/src/renderer/src/pages/proxies.tsx @@ -61,6 +61,10 @@ const Proxies: React.FC = () => { return await mihomoProxyDelay(proxy, url) } + const onGroupDelay = async (group: string): Promise => { + PubSub.publish(`${group}-delay`) + } + return ( { size="sm" isIconOnly onPress={() => { - PubSub.publish(`${groups[index].name}-delay`) + onGroupDelay(groups[index].name) }} > @@ -147,10 +151,11 @@ const Proxies: React.FC = () => { return allProxies[index] ? (
diff --git a/src/renderer/src/utils/ipc.ts b/src/renderer/src/utils/ipc.ts index bcbfbf3f..0af9914a 100644 --- a/src/renderer/src/utils/ipc.ts +++ b/src/renderer/src/utils/ipc.ts @@ -31,7 +31,9 @@ export async function mihomoChangeProxy(group: string, proxy: string): Promise { - 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 {