From 9255658e5408b0c48e1f35bdaf8fecf407bba869 Mon Sep 17 00:00:00 2001 From: kunish Date: Sun, 24 Sep 2023 17:10:01 +0800 Subject: [PATCH] feat(proxy): single proxy node latency test --- src/App.tsx | 4 +--- src/apis/index.ts | 17 ++++++++++++++++ src/components/ProxyNodeCard.tsx | 23 ++++++++++++++++++--- src/pages/Proxies.tsx | 12 +++++------ src/signals/proxies.ts | 34 +++++++++++++++++++++++++++----- 5 files changed, 73 insertions(+), 17 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c48193c..cd70a2a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -30,9 +30,7 @@ const Config = lazy(() => import('~/pages/Config')) const ProtectedResources = () => { const latestConnectionMsg = useWsRequest('connections') - createEffect(() => { - setLatestConnectionMsg(latestConnectionMsg()) - }) + createEffect(() => setLatestConnectionMsg(latestConnectionMsg())) return null } diff --git a/src/apis/index.ts b/src/apis/index.ts index 48e336f..34cf7d1 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -146,6 +146,23 @@ export const selectProxyInGroupAPI = (groupName: string, proxyName: string) => { }) } +export const proxyLatencyTestAPI = ( + proxyName: string, + url: string, + timeout: number, +) => { + const request = useRequest() + + return request + .get(`proxies/${proxyName}/delay`, { + searchParams: { + url, + timeout, + }, + }) + .json<{ delay: number }>() +} + export const proxyGroupLatencyTestAPI = ( groupName: string, url: string, diff --git a/src/components/ProxyNodeCard.tsx b/src/components/ProxyNodeCard.tsx index f67281c..0356821 100644 --- a/src/components/ProxyNodeCard.tsx +++ b/src/components/ProxyNodeCard.tsx @@ -1,6 +1,7 @@ +import { IconBrandSpeedtest } from '@tabler/icons-solidjs' import { createMemo, Show } from 'solid-js' import { twMerge } from 'tailwind-merge' -import { Latency } from '~/components' +import { Button, Latency } from '~/components' import { filterSpecialProxyType, formatProxyType } from '~/helpers' import { useProxies } from '~/signals' @@ -9,6 +10,7 @@ export const ProxyNodeCard = (props: { isSelected?: boolean onClick?: () => void }) => { + const { proxyLatencyTest } = useProxies() const { proxyName, isSelected, onClick } = props const { proxyNodeMap } = useProxies() const proxyNode = createMemo(() => proxyNodeMap()[proxyName]) @@ -28,10 +30,23 @@ export const ProxyNodeCard = (props: { isSelected && 'border-primary bg-primary-content text-primary', onClick && 'cursor-pointer', )} - onClick={() => onClick?.()} + onClick={onClick} data-tip={proxyName} > -
{proxyName}
+
+ {proxyName} + +
+
{formatProxyType(proxyNode()?.type)} + {` :: ${specialType()}`}
+
diff --git a/src/pages/Proxies.tsx b/src/pages/Proxies.tsx index eaa9035..ca95c6b 100644 --- a/src/pages/Proxies.tsx +++ b/src/pages/Proxies.tsx @@ -41,23 +41,23 @@ export default () => { fetchProxies, proxies, selectProxyInGroup, - latencyTestByProxyGroupName, + proxyGroupLatencyTest, latencyMap, proxyProviders, updateProviderByProviderName, updateAllProvider, healthCheckByProviderName, + collapsedMap, + setCollapsedMap, + latencyTestingMap, + setLatencyTestingMap, } = useProxies() onMount(fetchProxies) - const { map: collapsedMap, set: setCollapsedMap } = useStringBooleanMap() - const { map: latencyTestingMap, setWithCallback: setLatencyTestingMap } = - useStringBooleanMap() - const onLatencyTestClick = async (e: MouseEvent, name: string) => { e.stopPropagation() - void setLatencyTestingMap(name, () => latencyTestByProxyGroupName(name)) + void setLatencyTestingMap(name, () => proxyGroupLatencyTest(name)) } const { map: healthCheckingMap, setWithCallback: setHealthCheckingMap } = diff --git a/src/signals/proxies.ts b/src/signals/proxies.ts index 1cadb93..d306118 100644 --- a/src/signals/proxies.ts +++ b/src/signals/proxies.ts @@ -4,10 +4,12 @@ import { fetchProxiesAPI, fetchProxyProvidersAPI, proxyGroupLatencyTestAPI, + proxyLatencyTestAPI, proxyProviderHealthCheck, selectProxyInGroupAPI, updateProxyProviderAPI, } from '~/apis' +import { useStringBooleanMap } from '~/helpers' import { autoCloseConns, latencyQualityMap, @@ -26,6 +28,10 @@ type ProxyInfo = { type: string } +const { map: collapsedMap, set: setCollapsedMap } = useStringBooleanMap() +const { map: latencyTestingMap, setWithCallback: setLatencyTestingMap } = + useStringBooleanMap() + // these signals should be global state const [proxies, setProxies] = createSignal([]) const [proxyProviders, setProxyProviders] = createSignal([]) @@ -113,17 +119,30 @@ export const useProxies = () => { } } - const latencyTestByProxyGroupName = async (proxyGroupName: string) => { + const proxyLatencyTest = async (proxyName: string) => { + const { delay } = await proxyLatencyTestAPI( + proxyName, + urlForLatencyTest(), + latencyTestTimeoutDuration(), + ) + + setLatencyMap((latencyMap) => ({ + ...latencyMap, + [proxyName]: delay, + })) + } + + const proxyGroupLatencyTest = async (proxyGroupName: string) => { const data = await proxyGroupLatencyTestAPI( proxyGroupName, urlForLatencyTest(), latencyTestTimeoutDuration(), ) - setLatencyMap({ - ...latencyMap(), + setLatencyMap((latencyMap) => ({ + ...latencyMap, ...data, - }) + })) await fetchProxies() } @@ -148,9 +167,14 @@ export const useProxies = () => { } return { + collapsedMap, + setCollapsedMap, + latencyTestingMap, + setLatencyTestingMap, proxies, proxyProviders, - latencyTestByProxyGroupName, + proxyLatencyTest, + proxyGroupLatencyTest, latencyMap, proxyNodeMap, fetchProxies,