Skip to content

Commit

Permalink
fix: latency not correct when set custom url in config
Browse files Browse the repository at this point in the history
To thoroughly fix this issue, all latency-related logic needs to be strongly bound to testurl
  • Loading branch information
njzydark committed Nov 26, 2024
1 parent f610333 commit fa19303
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 77 deletions.
16 changes: 13 additions & 3 deletions src/components/Latency.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import { JSX, ParentComponent } from 'solid-js'
import { twMerge } from 'tailwind-merge'
import { getLatencyClassName } from '~/helpers'
import { useProxies } from '~/signals'
import { urlForLatencyTest, useProxies } from '~/signals'

interface Props extends JSX.HTMLAttributes<HTMLSpanElement> {
proxyName: string
testUrl: string | null
}

export const Latency: ParentComponent<Props> = (props) => {
const [local, others] = splitProps(props, ['class'])
const { getLatencyByName } = useProxies()
const [textClassName, setTextClassName] = createSignal('')
const latency = createMemo(() => getLatencyByName(others.proxyName || ''))
const latency = createMemo(() =>
getLatencyByName(
others.proxyName || '',
others.testUrl || urlForLatencyTest(),
),
)

createEffect(() => {
setTextClassName(getLatencyClassName(latency()))
})

return (
<span
class={twMerge('badge w-11 whitespace-nowrap', textClassName(), local.class)}
class={twMerge(
'badge w-11 whitespace-nowrap',
textClassName(),
local.class,
)}
{...others}
>
{latency() || '---'}
Expand Down
27 changes: 16 additions & 11 deletions src/components/ProxyNodeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
formatProxyType,
getLatencyClassName,
} from '~/helpers'
import { rootElement, useProxies } from '~/signals'
import { rootElement, urlForLatencyTest, useProxies } from '~/signals'

export const ProxyNodeCard = (props: {
proxyName: string
testUrl: string | null
timeout: number | null
isSelected?: boolean
onClick?: () => void
}) => {
Expand All @@ -36,6 +38,9 @@ export const ProxyNodeCard = (props: {
[proxyName, specialTypes()].filter(Boolean).join(' - '),
)

const latencyTestHistory =
proxyNode().latencyTestHistory[props.testUrl || urlForLatencyTest()] || []

return (
<Tooltip
placement="top"
Expand Down Expand Up @@ -67,13 +72,19 @@ export const ProxyNodeCard = (props: {

<Latency
proxyName={props.proxyName}
testUrl={props.testUrl || null}
class={twMerge(
proxyLatencyTestingMap()[proxyName] && 'animate-pulse',
)}
onClick={(e) => {
e.stopPropagation()

void proxyLatencyTest(proxyName, proxyNode().provider)
void proxyLatencyTest(
proxyName,
proxyNode().provider,
props.testUrl,
props.timeout,
)
}}
/>
</div>
Expand All @@ -87,12 +98,10 @@ export const ProxyNodeCard = (props: {
<div class="flex flex-col items-center gap-2 rounded-box bg-neutral bg-gradient-to-br from-primary to-secondary p-2.5 text-primary-content shadow-lg">
<h2 class="text-lg font-bold">{proxyName}</h2>

<div class="w-full text-xs uppercase">
{specialTypes()}
</div>
<div class="w-full text-xs uppercase">{specialTypes()}</div>

<ul class="timeline timeline-vertical timeline-compact timeline-snap-icon">
<For each={proxyNode().latencyTestHistory}>
<For each={latencyTestHistory}>
{(latencyTestResult, index) => (
<li>
<Show when={index() > 0}>
Expand Down Expand Up @@ -120,11 +129,7 @@ export const ProxyNodeCard = (props: {
<IconCircleCheckFilled class="size-4" />
</div>

<Show
when={
index() !== proxyNode().latencyTestHistory.length - 1
}
>
<Show when={index() !== latencyTestHistory.length - 1}>
<hr />
</Show>
</li>
Expand Down
3 changes: 3 additions & 0 deletions src/components/ProxyNodePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { proxiesPreviewType } from '~/signals'

export const ProxyNodePreview = (props: {
proxyNameList: string[]
testUrl: string | null
now?: string
}) => {
const off = () => proxiesPreviewType() === PROXIES_PREVIEW_TYPE.OFF
Expand Down Expand Up @@ -34,13 +35,15 @@ export const ProxyNodePreview = (props: {
<Match when={isShowBar()}>
<ProxyPreviewBar
proxyNameList={props.proxyNameList}
testUrl={props.testUrl}
now={props.now}
/>
</Match>

<Match when={isShowDots()}>
<ProxyPreviewDots
proxyNameList={props.proxyNameList}
testUrl={props.testUrl}
now={props.now}
/>
</Match>
Expand Down
5 changes: 3 additions & 2 deletions src/components/ProxyPreviewBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { latencyQualityMap, useProxies } from '~/signals'

export const ProxyPreviewBar = (props: {
proxyNameList: string[]
testUrl: string | null
now?: string
}) => {
const { getLatencyByName } = useProxies()
const latencyList = createMemo(() =>
props.proxyNameList.map((name) => getLatencyByName(name)),
props.proxyNameList.map((name) => getLatencyByName(name, props.testUrl)),
)

const all = createMemo(() => latencyList().length)
Expand Down Expand Up @@ -69,7 +70,7 @@ export const ProxyPreviewBar = (props: {
</div>

<Show when={props.now}>
<Latency proxyName={props.now!} />
<Latency proxyName={props.now!} testUrl={props.testUrl} />
</Show>
</div>
)
Expand Down
5 changes: 3 additions & 2 deletions src/components/ProxyPreviewDots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const LatencyDot = (props: {

export const ProxyPreviewDots = (props: {
proxyNameList: string[]
testUrl: string | null
now?: string
}) => {
const { getLatencyByName } = useProxies()
Expand All @@ -48,7 +49,7 @@ export const ProxyPreviewDots = (props: {
<For
each={props.proxyNameList.map((name): [string, number] => [
name,
getLatencyByName(name),
getLatencyByName(name, props.testUrl),
])}
>
{([name, latency]) => (
Expand All @@ -62,7 +63,7 @@ export const ProxyPreviewDots = (props: {
</div>

<Show when={props.now}>
<Latency proxyName={props.now!} />
<Latency proxyName={props.now!} testUrl={props.testUrl} />
</Show>
</div>
)
Expand Down
39 changes: 27 additions & 12 deletions src/helpers/proxies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LATENCY_QUALITY_MAP_HTTP, PROXIES_ORDERING_TYPE } from '~/constants'
import { useI18n } from '~/i18n'
import { latencyQualityMap, useProxies } from '~/signals'
import { latencyQualityMap, urlForLatencyTest, useProxies } from '~/signals'

export const formatProxyType = (type = '') => {
const [t] = useI18n()
Expand Down Expand Up @@ -55,19 +55,26 @@ export const filterSpecialProxyType = (type = '') => {
return !conditions.includes(type.toLowerCase())
}

export const sortProxiesByOrderingType = (
proxyNames: string[],
orderingType: PROXIES_ORDERING_TYPE,
) => {
export const sortProxiesByOrderingType = ({
proxyNames,
orderingType,
testUrl,
}: {
proxyNames: string[]
orderingType: PROXIES_ORDERING_TYPE
testUrl: string | null
}) => {
const { getLatencyByName } = useProxies()

if (orderingType === PROXIES_ORDERING_TYPE.NATURAL) {
return proxyNames
}

const finalTestUrl = testUrl || urlForLatencyTest()

return proxyNames.sort((a, b) => {
const prevLatency = getLatencyByName(a)
const nextLatency = getLatencyByName(b)
const prevLatency = getLatencyByName(a, finalTestUrl)
const nextLatency = getLatencyByName(b, finalTestUrl)

switch (orderingType) {
case PROXIES_ORDERING_TYPE.LATENCY_ASC:
Expand Down Expand Up @@ -96,19 +103,27 @@ export const sortProxiesByOrderingType = (
})
}

export const filterProxiesByAvailability = (
proxyNames: string[],
enabled?: boolean,
) => {
export const filterProxiesByAvailability = ({
proxyNames,
enabled,
testUrl,
}: {
proxyNames: string[]
enabled?: boolean
testUrl: string | null
}) => {
const { getLatencyByName, isProxyGroup } = useProxies()

const finalTestUrl = testUrl || urlForLatencyTest()

return enabled
? proxyNames.filter(
// we need proxy node with connected or the node is a group it self
(name) => {
return (
isProxyGroup(name) ||
getLatencyByName(name) !== latencyQualityMap().NOT_CONNECTED
getLatencyByName(name, finalTestUrl) !==
latencyQualityMap().NOT_CONNECTED
)
},
)
Expand Down
42 changes: 29 additions & 13 deletions src/pages/Proxies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,15 @@ export default () => {
<For each={renderProxies()}>
{(proxyGroup) => {
const sortedProxyNames = createMemo(() =>
filterProxiesByAvailability(
sortProxiesByOrderingType(
proxyGroup.all ?? [],
proxiesOrderingType(),
),
hideUnAvailableProxies(),
),
filterProxiesByAvailability({
proxyNames: sortProxiesByOrderingType({
proxyNames: proxyGroup.all ?? [],
orderingType: proxiesOrderingType(),
testUrl: proxyGroup.testUrl || null,
}),
enabled: hideUnAvailableProxies(),
testUrl: proxyGroup.testUrl || null,
}),
)

const title = (
Expand Down Expand Up @@ -291,6 +293,7 @@ export default () => {
<ProxyNodePreview
proxyNameList={sortedProxyNames()}
now={proxyGroup.now}
testUrl={proxyGroup.testUrl || null}
/>
</Show>
</div>
Expand All @@ -308,6 +311,8 @@ export default () => {
{(proxyName) => (
<ProxyNodeCard
proxyName={proxyName}
testUrl={proxyGroup.testUrl || null}
timeout={proxyGroup.timeout ?? null}
isSelected={proxyGroup.now === proxyName}
onClick={() =>
void selectProxyInGroup(proxyGroup, proxyName)
Expand All @@ -327,10 +332,12 @@ export default () => {
<For each={proxyProviders()}>
{(proxyProvider) => {
const sortedProxyNames = createMemo(() =>
sortProxiesByOrderingType(
proxyProvider.proxies.map((i) => i.name) ?? [],
proxiesOrderingType(),
),
sortProxiesByOrderingType({
orderingType: proxiesOrderingType(),
proxyNames:
proxyProvider.proxies.map((i) => i.name) ?? [],
testUrl: proxyProvider.testUrl,
}),
)

const title = (
Expand Down Expand Up @@ -397,7 +404,10 @@ export default () => {
</div>

<Show when={!collapsedMap()[proxyProvider.name]}>
<ProxyNodePreview proxyNameList={sortedProxyNames()} />
<ProxyNodePreview
proxyNameList={sortedProxyNames()}
testUrl={proxyProvider.testUrl}
/>
</Show>
</>
)
Expand All @@ -411,7 +421,13 @@ export default () => {
}
>
<For each={sortedProxyNames()}>
{(proxyName) => <ProxyNodeCard proxyName={proxyName} />}
{(proxyName) => (
<ProxyNodeCard
proxyName={proxyName}
testUrl={proxyProvider.testUrl}
timeout={proxyProvider.timeout ?? null}
/>
)}
</For>
</Collapse>
)
Expand Down
Loading

0 comments on commit fa19303

Please sign in to comment.