Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proxy selectable #19

Merged
merged 1 commit into from Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pages/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default () => {
onCleanup(() => clearInterval(preventLeakTimer))

const trafficWS = createReconnectingWS(
`${wsEndpointURL()}/traffic?token=${secret()}}`,
`${wsEndpointURL()}/traffic?token=${secret()}`,
)

const trafficWSMessageEvent = createEventSignal<{
Expand Down Expand Up @@ -148,7 +148,7 @@ export default () => {
])

const connectionsWS = createReconnectingWS(
`${wsEndpointURL()}/connections?token=${secret()}}`,
`${wsEndpointURL()}/connections?token=${secret()}`,
)

const connectionsWSMessageEvent = createEventSignal<{
Expand Down
86 changes: 52 additions & 34 deletions src/pages/Proxies.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { For, createSignal, onMount } from 'solid-js'
import { twMerge } from 'tailwind-merge'
import { useRequest } from '~/signals'
import type { Proxy, ProxyProvider } from '~/types'
import { useProxies } from '~/signals/proxies'
import type { Proxy } from '~/types'

export default () => {
const request = useRequest()
const [proxies, setProxies] = createSignal<Proxy[]>([])
const [delayMap, setDelayMap] = createSignal<Record<string, number>>({})
const [proxyProviders, setProxyProviders] = createSignal<ProxyProvider[]>([])
const {
proxies,
proxyProviders,
delayMap,
updateProxy,
setProxiesByProxyName,
} = useProxies()
const [collapseMap, setCollapseMap] = createSignal<Record<string, boolean>>(
{},
)

const renderDelay = (proxyname: string) => {
const delay = delayMap()[proxyname]
Expand All @@ -20,26 +26,23 @@ export default () => {
}

onMount(async () => {
const { providers } = await request
.get('providers/proxies')
.json<{ providers: Record<string, ProxyProvider> }>()
const delay = delayMap()
await updateProxy()
})

Object.values(providers).forEach((provider) => {
provider.proxies.forEach((proxy) => {
delay[proxy.name] = proxy.history[proxy.history.length - 1]?.delay
})
})
const onProxyNodeClick = async (proxy: Proxy, proxyName: string) => {
setProxiesByProxyName(proxy, proxyName)
}

setDelayMap(delay)
setProxyProviders(Object.values(providers))
const onCollapseTitleClick = (name: string) => {
const cMap = collapseMap()

const { proxies } = await request
.get('proxies')
.json<{ proxies: Record<string, Proxy> }>()
cMap[name] = !cMap[name]
setCollapseMap({ ...cMap })
}

setProxies(Object.values(proxies))
})
const getCollapseClassName = (name: string) => {
return collapseMap()[name] ? 'collapse-open' : 'collapse-close'
}

return (
<div class="flex flex-col gap-4">
Expand All @@ -49,23 +52,31 @@ export default () => {
<div class="grid grid-cols-1 gap-2 sm:grid-cols-1">
<For each={proxies()}>
{(proxy) => (
<div class="collapse collapse-arrow border-secondary bg-base-200 p-4">
<input type="checkbox" />
<div class="collapse-title text-xl font-medium">
<div
class={twMerge(
getCollapseClassName(proxy.name),
'collapse collapse-arrow border-secondary bg-base-200 p-4',
)}
>
<div
class="collapse-title text-xl font-medium"
onClick={() => onCollapseTitleClick(proxy.name)}
>
{proxy.name} {proxy.type}
</div>
<div class="collapse-content grid grid-cols-1 gap-2 sm:grid-cols-3 lg:grid-cols-5">
<For each={proxy.all}>
{(proxyPoint) => (
{(proxyName) => (
<div
class={twMerge(
proxy.now === proxyPoint
proxy.now === proxyName
? 'border-primary bg-success-content text-success'
: 'border-secondary',
'card card-bordered card-compact m-1 flex-row justify-between p-4',
'card card-bordered card-compact m-1 cursor-pointer flex-row justify-between p-4',
)}
onClick={() => onProxyNodeClick(proxy, proxyName)}
>
{proxyPoint} {renderDelay(proxyPoint)}
{proxyName} {renderDelay(proxyName)}
</div>
)}
</For>
Expand All @@ -82,16 +93,23 @@ export default () => {
<div class="grid grid-cols-1 gap-2 sm:grid-cols-1">
<For each={proxyProviders()}>
{(proxy) => (
<div class="collapse-arrow collapse border-secondary bg-base-200 p-4">
<input type="checkbox" />
<div class="collapse-title text-xl font-medium">
<div
class={twMerge(
getCollapseClassName(proxy.name),
'collapse-arrow collapse border-secondary bg-base-200 p-4',
)}
>
<div
class="collapse-title text-xl font-medium"
onClick={() => onCollapseTitleClick(proxy.name)}
>
{proxy.name}
</div>
<div class="collapse-content grid grid-cols-1 gap-2 sm:grid-cols-3 lg:grid-cols-5">
<For each={proxy.proxies}>
{(proxyPoint) => (
{(proxyNode) => (
<div class="card card-bordered card-compact m-1 flex-row justify-between border-secondary p-4">
{proxyPoint.name} {renderDelay(proxyPoint.name)}
{proxyNode.name} {renderDelay(proxyNode.name)}
</div>
)}
</For>
Expand Down
58 changes: 58 additions & 0 deletions src/signals/proxies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { createSignal } from 'solid-js'
import { useRequest } from '~/signals'
import type { Proxy, ProxyProvider } from '~/types'

export function useProxies() {
const request = useRequest()
const [proxies, setProxies] = createSignal<Proxy[]>([])
const [proxyProviders, setProxyProviders] = createSignal<ProxyProvider[]>([])
const [delayMap, setDelayMap] = createSignal<Record<string, number>>({})

const updateProxy = async () => {
const { providers } = await request
.get('providers/proxies')
.json<{ providers: Record<string, ProxyProvider> }>()
const delay = delayMap()

Object.values(providers).forEach((provider) => {
provider.proxies.forEach((proxy) => {
delay[proxy.name] = proxy.history[proxy.history.length - 1]?.delay
})
})

setDelayMap(delay)
setProxyProviders(
Object.values(providers).filter(
(provider) =>
provider.name !== 'default' && provider.vehicleType !== 'Compatible',
),
)

const { proxies } = await request
.get('proxies')
.json<{ proxies: Record<string, Proxy> }>()

setProxies(
Object.values(proxies).filter(
(proxy) => proxy.all && proxy.all.length > 0,
),
)
}

const setProxiesByProxyName = async (proxy: Proxy, proxyName: string) => {
await request.put(`proxies/${proxy.name}`, {
body: JSON.stringify({
name: proxyName,
}),
})
await updateProxy()
}

return {
proxies,
proxyProviders,
delayMap,
updateProxy,
setProxiesByProxyName,
}
}