Skip to content

Commit

Permalink
fix: latency test for selector and ipv6 test
Browse files Browse the repository at this point in the history
  • Loading branch information
YetAnotherZephyruso committed Jul 25, 2024
1 parent e84b348 commit 4b2e4ad
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 126 deletions.
14 changes: 8 additions & 6 deletions src/components/IPv6Support.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useProxies } from '~/signals'
import { proxyIPv6SupportMap } from '~/signals/ipv6'

export const IPv6Support = (props: { name?: string }) => {
const { proxyIPv6SupportMap } = useProxies()
const support = createMemo(() => proxyIPv6SupportMap()[props.name!] === true)
export const IPv6Support = (props: { name?: string; class?: string }) => {
const { getRealProxyNodeName } = useProxies()

const support = createMemo(() => {
return proxyIPv6SupportMap()[getRealProxyNodeName(props.name || '')]
})

return (
<Show when={support()}>
<span class="badge badge-sm p-px">
<span class="scale-75">IPv6</span>
</span>
<span class={`scale-75 ${props.class}`}>IPv6</span>
</Show>
)
}
6 changes: 4 additions & 2 deletions src/components/Latency.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { latencyQualityMap, useProxies } from '~/signals'

export const Latency = (props: { name?: string; class?: string }) => {
const [t] = useI18n()
const { latencyMap } = useProxies()
const { getRealProxyNodeName, latencyMap } = useProxies()
const [textClassName, setTextClassName] = createSignal('')
const latency = createMemo(() => latencyMap()[props.name!])
const latency = createMemo(() => {
return latencyMap()[getRealProxyNodeName(props.name || '')]
})

createEffect(() => {
setTextClassName('text-success')
Expand Down
4 changes: 3 additions & 1 deletion src/signals/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export type WsMsg = {
downloadTotal: number
} | null

// DIRECT is from clash
// direct and dns-out is from the example of sing-box official site
export const [quickFilterRegex, setQuickFilterRegex] = makePersisted(
createSignal<string>('Direct|direct|dns-out'),
createSignal<string>('DIRECT|direct|dns-out'),
{
name: 'quickFilterRegex',
storage: localStorage,
Expand Down
66 changes: 66 additions & 0 deletions src/signals/ipv6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { proxyGroupLatencyTestAPI, proxyLatencyTestAPI } from '~/apis'
import {
latencyQualityMap,
latencyTestTimeoutDuration,
urlForIPv6SupportTest,
} from './config'

export const [proxyIPv6SupportMap, setProxyIPv6SupportMap] = createSignal<
Record<string, boolean>
>({})

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,
}))
}
171 changes: 54 additions & 117 deletions src/signals/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import {
urlForLatencyTest,
} from '~/signals'
import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
import {
proxyGroupIPv6SupportTest,
proxyIPv6SupportMap,
proxyIPv6SupportTest,
setProxyIPv6SupportMap,
} from './ipv6'

type ProxyInfo = {
name: string
Expand Down Expand Up @@ -58,82 +64,54 @@ const [proxyProviders, setProxyProviders] = createSignal<
>([])

const [latencyMap, setLatencyMap] = createSignal<Record<string, number>>({})
const [proxyIPv6SupportMap, setProxyIPv6SupportMap] = createSignal<
Record<string, boolean>
>({})
const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, ProxyInfo>>(
{},
)

const setProxiesInfo = (
proxies: (ProxyWithProvider | ProxyNodeWithProvider)[],
const getLatencyFromProxy = (
proxy: Pick<Proxy, 'extra' | 'history'>,
url: string,
fallbackDefault = true,
) => {
const newProxyNodeMap = { ...proxyNodeMap() }
const newLatencyMap = { ...latencyMap() }
const newProxyIPv6SupportMap = { ...proxyIPv6SupportMap() }
const extra = proxy.extra?.[url] as Proxy['history'] | undefined

const lastDelay = (
proxy: Pick<Proxy, 'extra' | 'history'>,
url: string,
fallbackDefault = true,
) => {
const extra = proxy.extra?.[url] as Proxy['history'] | undefined
if (Array.isArray(extra)) {
const delay = extra.at(-1)?.delay

if (Array.isArray(extra)) {
const delay = extra.at(-1)?.delay

if (delay) {
return delay
}
}

if (!fallbackDefault) {
return undefined
if (delay) {
return delay
}
}

return proxy.history?.at(-1)?.delay
if (!fallbackDefault) {
return latencyQualityMap().NOT_CONNECTED
}

const dependedLatencyProxies = {} as Record<string, Set<string>>
return proxy.history?.at(-1)?.delay ?? latencyQualityMap().NOT_CONNECTED
}

const setProxiesInfo = (
proxies: (ProxyWithProvider | ProxyNodeWithProvider)[],
) => {
const newProxyNodeMap = { ...proxyNodeMap() }
const newLatencyMap = { ...latencyMap() }
const newProxyIPv6SupportMap = { ...proxyIPv6SupportMap() }

proxies.forEach((proxy) => {
const { udp, xudp, type, now, name, provider = '' } = proxy
newProxyNodeMap[proxy.name] = { udp, xudp, type, now, name, provider }

// to solve the problem of the ProxyGroup cannot obtain the latency of the currently used proxy node
// it seems that only clash.core and clash.premium have issues
if (!now) {
newLatencyMap[proxy.name] =
lastDelay(proxy, urlForLatencyTest()) ||
latencyQualityMap().NOT_CONNECTED
} else if (newLatencyMap[now] !== undefined) {
newLatencyMap[proxy.name] = newLatencyMap[now]
} else {
const dependencies = dependedLatencyProxies[now] ?? new Set()
dependencies.add(proxy.name)
dependedLatencyProxies[now] = dependencies
newProxyNodeMap[proxy.name] = { udp, xudp, type, now, name, provider }
newLatencyMap[proxy.name] = getLatencyFromProxy(proxy, urlForLatencyTest())

// we don't set it when false because sing-box didn't have "extra" so it will always be false
if (
getLatencyFromProxy(proxy, urlForIPv6SupportTest(), false) >
latencyQualityMap().NOT_CONNECTED
) {
newProxyIPv6SupportMap[proxy.name] = true
}

const proxyIPv6Support =
(lastDelay(proxy, urlForIPv6SupportTest(), false) ?? 0) > 0
newProxyIPv6SupportMap[proxy.name] = proxyIPv6Support
})

const independencies = Object.keys(dependedLatencyProxies).filter(
(now) => newLatencyMap[now] !== undefined,
)

// maybe we should use Union-Find to implement this
while (independencies.length > 0) {
const now = independencies.shift()!
const delay = newLatencyMap[now]!

for (const name of dependedLatencyProxies[now]?.values() ?? []) {
newLatencyMap[name] = delay
independencies.push(name)
}
}

batch(() => {
setProxyNodeMap(newProxyNodeMap)
setLatencyMap(newLatencyMap)
Expand Down Expand Up @@ -206,58 +184,9 @@ 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 > 0
} catch {
support = false
}
setProxyIPv6SupportMap((supportMap) => ({
...supportMap,
[proxyName]: support,
}))
}
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 > 0]),
)
setProxyIPv6SupportMap((supportMap) => ({
...supportMap,
...newSupportMap,
}))
}

const proxyLatencyTest = async (proxyName: string, provider: string) => {
await proxyIPv6SupportTest(proxyName, provider)
setProxyLatencyTestingMap(proxyName, async () => {
await proxyIPv6SupportTest(proxyName, provider)
const { delay } = await proxyLatencyTestAPI(
proxyName,
provider,
Expand All @@ -273,19 +202,13 @@ export const useProxies = () => {
}

const proxyGroupLatencyTest = async (proxyGroupName: string) => {
await proxyGroupIPv6SupportTest(proxyGroupName)
setProxyGroupLatencyTestingMap(proxyGroupName, async () => {
const newLatencyMap = await proxyGroupLatencyTestAPI(
await proxyGroupIPv6SupportTest(proxyGroupName)
await proxyGroupLatencyTestAPI(
proxyGroupName,
urlForLatencyTest(),
latencyTestTimeoutDuration(),
)

setLatencyMap((latencyMap) => ({
...latencyMap,
...newLatencyMap,
}))

await fetchProxies()
})
}
Expand Down Expand Up @@ -320,8 +243,21 @@ export const useProxies = () => {
await fetchProxies()
})

const getRealProxyNodeName = (name: string) => {
let node = proxyNodeMap()[name]

if (!name || !node) {
return name
}

while (node.now && node.now !== node.name) {
node = proxyNodeMap()[node.now]
}

return node.name
}

return {
proxyIPv6SupportMap,
proxyLatencyTestingMap,
proxyGroupLatencyTestingMap,
proxyProviderLatencyTestingMap,
Expand All @@ -339,5 +275,6 @@ export const useProxies = () => {
updateProviderByProviderName,
updateAllProvider,
proxyProviderLatencyTest,
getRealProxyNodeName,
}
}

0 comments on commit 4b2e4ad

Please sign in to comment.