From c43b84b1149939bf604e6798931f221fa191bc6b Mon Sep 17 00:00:00 2001 From: YetAnotherZephyruso <176294927+YetAnotherZephyruso@users.noreply.github.com> Date: Thu, 12 Sep 2024 17:24:55 +0800 Subject: [PATCH] fix: proxy group ipv6 test failed (#996) --- src/components/IPv6Support.tsx | 3 +- src/signals/ipv6.ts | 71 ---------------------------- src/signals/proxies.ts | 85 +++++++++++++++++++++++++++++++--- 3 files changed, 80 insertions(+), 79 deletions(-) delete mode 100644 src/signals/ipv6.ts diff --git a/src/components/IPv6Support.tsx b/src/components/IPv6Support.tsx index da315a7..1b3767e 100644 --- a/src/components/IPv6Support.tsx +++ b/src/components/IPv6Support.tsx @@ -1,8 +1,7 @@ import { useProxies } from '~/signals' -import { proxyIPv6SupportMap } from '~/signals/ipv6' export const IPv6Support = (props: { name?: string }) => { - const { getNowProxyNodeName } = useProxies() + const { getNowProxyNodeName, proxyIPv6SupportMap } = useProxies() const support = createMemo(() => { return proxyIPv6SupportMap()[getNowProxyNodeName(props.name || '')] diff --git a/src/signals/ipv6.ts b/src/signals/ipv6.ts deleted file mode 100644 index 233a042..0000000 --- a/src/signals/ipv6.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { makePersisted } from '@solid-primitives/storage' -import { proxyGroupLatencyTestAPI, proxyLatencyTestAPI } from '~/apis' -import { - latencyQualityMap, - latencyTestTimeoutDuration, - urlForIPv6SupportTest, -} from './config' - -export const [proxyIPv6SupportMap, setProxyIPv6SupportMap] = makePersisted( - createSignal>({}), - { - name: 'proxyIPv6SupportMap', - storage: localStorage, - }, -) - -export const proxyIPv6SupportTest = async ( - proxyName: string, - provider: string, -) => { - const urlForTest = urlForIPv6SupportTest() - - if (!urlForTest || urlForTest.length === 0) { - setProxyIPv6SupportMap({}) - - return - } - - let support = false - try { - const { delay } = await proxyLatencyTestAPI( - proxyName, - provider, - urlForTest, - latencyTestTimeoutDuration(), - ) - support = delay > latencyQualityMap().NOT_CONNECTED - } catch { - support = false - } - setProxyIPv6SupportMap((supportMap) => ({ - ...supportMap, - [proxyName]: support, - })) -} - -export const proxyGroupIPv6SupportTest = async (proxyGroupName: string) => { - const urlForTest = urlForIPv6SupportTest() - - if (!urlForTest || urlForTest.length === 0) { - setProxyIPv6SupportMap({}) - - return - } - - const newLatencyMap = await proxyGroupLatencyTestAPI( - proxyGroupName, - urlForTest, - latencyTestTimeoutDuration(), - ) - const newSupportMap = Object.fromEntries( - Object.entries(newLatencyMap).map(([k, v]) => [ - k, - v > latencyQualityMap().NOT_CONNECTED, - ]), - ) - setProxyIPv6SupportMap((supportMap) => ({ - ...supportMap, - ...newSupportMap, - })) -} diff --git a/src/signals/proxies.ts b/src/signals/proxies.ts index 0e15ff5..da45d88 100644 --- a/src/signals/proxies.ts +++ b/src/signals/proxies.ts @@ -1,3 +1,4 @@ +import { makePersisted } from '@solid-primitives/storage' import { closeSingleConnectionAPI, fetchProxiesAPI, @@ -19,12 +20,6 @@ import { urlForLatencyTest, } from '~/signals' import type { Proxy, ProxyNode, ProxyProvider } from '~/types' -import { - proxyGroupIPv6SupportTest, - proxyIPv6SupportMap, - proxyIPv6SupportTest, - setProxyIPv6SupportMap, -} from './ipv6' type ProxyInfo = { name: string @@ -66,6 +61,14 @@ const [proxyNodeMap, setProxyNodeMap] = createSignal>( {}, ) +const [proxyIPv6SupportMap, setProxyIPv6SupportMap] = makePersisted( + createSignal>({}), + { + name: 'proxyIPv6SupportMap', + storage: localStorage, + }, +) + const getLatencyFromProxy = ( proxy: Pick, url: string, @@ -217,6 +220,75 @@ export const useProxies = () => { }) } + const proxyIPv6SupportTest = async (proxyName: string, provider: string) => { + const urlForTest = urlForIPv6SupportTest() + + if (!urlForTest || urlForTest.length === 0) { + setProxyIPv6SupportMap({}) + + return + } + + let support = false + try { + const { delay } = await proxyLatencyTestAPI( + proxyName, + provider, + urlForTest, + latencyTestTimeoutDuration(), + ) + support = delay > latencyQualityMap().NOT_CONNECTED + } catch { + support = false + } + setProxyIPv6SupportMap((supportMap) => ({ + ...supportMap, + [proxyName]: support, + })) + } + + const proxyGroupIPv6SupportTest = async (proxyGroupName: string) => { + const urlForTest = urlForIPv6SupportTest() + + if (!urlForTest || urlForTest.length === 0) { + setProxyIPv6SupportMap({}) + + return + } + + try { + const newLatencyMap = await proxyGroupLatencyTestAPI( + proxyGroupName, + urlForTest, + latencyTestTimeoutDuration(), + ) + + const newSupportMap = Object.fromEntries( + Object.entries(newLatencyMap).map(([nodeName, latency]) => [ + getNowProxyNodeName(nodeName), + latency > latencyQualityMap().NOT_CONNECTED, + ]), + ) + setProxyIPv6SupportMap((supportMap) => ({ + ...supportMap, + ...newSupportMap, + })) + } catch { + const allNodes = proxies().find((p) => p.name === proxyGroupName)?.all + + if (!allNodes) { + return + } + + setProxyIPv6SupportMap((proxyIPv6SupportMap) => ({ + ...proxyIPv6SupportMap, + ...Object.fromEntries( + allNodes.map((name) => [getNowProxyNodeName(name), false]), + ), + })) + } + } + const updateProviderByProviderName = (providerName: string) => setUpdatingMap(providerName, async () => { try { @@ -305,5 +377,6 @@ export const useProxies = () => { getNowProxyNodeName, getLatencyByName, isProxyGroup, + proxyIPv6SupportMap, } }