diff --git a/src/apis/index.ts b/src/apis/index.ts index 5961dc9..5447ef5 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -131,9 +131,11 @@ export const updateProxyProviderAPI = (providerName: string) => { export const proxyProviderHealthCheckAPI = (providerName: string) => { const request = useRequest() - return request.get(`providers/proxies/${providerName}/healthcheck`, { - timeout: 20 * 1000, - }) + return request + .get(`providers/proxies/${providerName}/healthcheck`, { + timeout: 5 * 1000, + }) + .json>() } export const selectProxyInGroupAPI = (groupName: string, proxyName: string) => { @@ -148,11 +150,18 @@ export const selectProxyInGroupAPI = (groupName: string, proxyName: string) => { export const proxyLatencyTestAPI = ( proxyName: string, + provider: string, url: string, timeout: number, ) => { const request = useRequest() + if (provider !== '') { + return proxyProviderHealthCheckAPI(provider).then((latencyMap) => ({ + delay: latencyMap[proxyName], + })) + } + return request .get(`proxies/${proxyName}/delay`, { searchParams: { diff --git a/src/components/ProxyNodeCard.tsx b/src/components/ProxyNodeCard.tsx index 5b3e15d..e44f82d 100644 --- a/src/components/ProxyNodeCard.tsx +++ b/src/components/ProxyNodeCard.tsx @@ -49,7 +49,7 @@ export const ProxyNodeCard = (props: { onClick={(e) => { e.stopPropagation() - void proxyLatencyTest(proxyName) + void proxyLatencyTest(proxyName, proxyNode().provider) }} /> diff --git a/src/signals/config.ts b/src/signals/config.ts index 4882138..b9465af 100644 --- a/src/signals/config.ts +++ b/src/signals/config.ts @@ -127,7 +127,7 @@ export const tableSizeClassName = (size: TAILWINDCSS_SIZE) => { } export const [latencyTestTimeoutDuration, setLatencyTestTimeoutDuration] = - makePersisted(createSignal(2000), { + makePersisted(createSignal(5000), { name: 'latencyTestTimeoutDuration', storage: localStorage, }) diff --git a/src/signals/proxies.ts b/src/signals/proxies.ts index 4584a40..116c9d6 100644 --- a/src/signals/proxies.ts +++ b/src/signals/proxies.ts @@ -26,8 +26,12 @@ type ProxyInfo = { now: string xudp: boolean type: string + provider: string } +export type ProxyWithProvider = Proxy & { provider?: string } +export type ProxyNodeWithProvider = ProxyNode & { provider?: string } + const { map: collapsedMap, set: setCollapsedMap } = useStringBooleanMap() const { map: proxyLatencyTestingMap, @@ -46,29 +50,29 @@ const { map: updatingMap, setWithCallback: setUpdatingMap } = const [isAllProviderUpdating, setIsAllProviderUpdating] = createSignal(false) // these signals should be global state -const [proxies, setProxies] = createSignal([]) -const [proxyProviders, setProxyProviders] = createSignal([]) +const [proxies, setProxies] = createSignal([]) +const [proxyProviders, setProxyProviders] = createSignal< + (ProxyProvider & { proxies: ProxyNodeWithProvider[] })[] +>([]) const [latencyMap, setLatencyMap] = createSignal>({}) const [proxyNodeMap, setProxyNodeMap] = createSignal>( {}, ) -const setProxiesInfo = (proxies: (Proxy | ProxyNode)[]) => { +const setProxiesInfo = ( + proxies: (ProxyWithProvider | ProxyNodeWithProvider)[], +) => { const newProxyNodeMap = { ...proxyNodeMap() } const newLatencyMap = { ...latencyMap() } proxies.forEach((proxy) => { + const { udp, xudp, type, now, name, provider = '' } = proxy + const latency = proxy.history.at(-1)?.delay || latencyQualityMap().NOT_CONNECTED - newProxyNodeMap[proxy.name] = { - udp: proxy.udp, - xudp: proxy.xudp, - type: proxy.type, - now: proxy.now, - name: proxy.name, - } + newProxyNodeMap[proxy.name] = { udp, xudp, type, now, name, provider } newLatencyMap[proxy.name] = latency }) @@ -96,9 +100,17 @@ export const useProxies = () => { (provider) => provider.name !== 'default' && provider.vehicleType !== 'Compatible', ) - const allProxies: (Proxy | ProxyNode)[] = [ + + const allProxies = [ ...Object.values(proxies), - ...sortedProviders.flatMap((provider) => provider.proxies), + ...sortedProviders.flatMap((provider) => + provider.proxies + .filter((proxy) => !(proxy.name in proxies)) + .map((proxy) => ({ + ...proxy, + provider: provider.name, + })), + ), ] batch(() => { @@ -132,10 +144,11 @@ export const useProxies = () => { } } - const proxyLatencyTest = (proxyName: string) => + const proxyLatencyTest = (proxyName: string, provider: string) => setProxyLatencyTestingMap(proxyName, async () => { const { delay } = await proxyLatencyTestAPI( proxyName, + provider, urlForLatencyTest(), latencyTestTimeoutDuration(), )