feat: speed test (#38)

This commit is contained in:
Zephyruso 2023-08-30 11:01:19 +08:00 committed by GitHub
parent 4eeb3458cc
commit a57741baf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 7 deletions

View File

@ -30,12 +30,12 @@ export const App = () => {
<div class="flex-1 overflow-y-auto p-4"> <div class="flex-1 overflow-y-auto p-4">
<Routes> <Routes>
<Show when={selectedEndpoint()}> <Show when={selectedEndpoint()}>
<Route path="/" component={Overview} />
<Route path="/proxies" component={Proxies} /> <Route path="/proxies" component={Proxies} />
<Route path="/rules" component={Rules} /> <Route path="/rules" component={Rules} />
<Route path="/conns" component={Connections} /> <Route path="/conns" component={Connections} />
<Route path="/logs" component={Logs} /> <Route path="/logs" component={Logs} />
<Route path="/config" component={Config} /> <Route path="/config" component={Config} />
<Route path="*" component={Overview} />
</Show> </Show>
<Route path="/setup" component={Setup} /> <Route path="/setup" component={Setup} />

View File

@ -65,7 +65,7 @@ const ThemeSwitcher = () => (
const navs = () => [ const navs = () => [
{ {
href: '/', href: '/overview',
name: 'Overview', name: 'Overview',
icon: <IconHome />, icon: <IconHome />,
}, },

View File

@ -1,3 +1,4 @@
import { IconBrandSpeedtest } from '@tabler/icons-solidjs'
import { For, createSignal, onMount } from 'solid-js' import { For, createSignal, onMount } from 'solid-js'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
import { useProxies } from '~/signals/proxies' import { useProxies } from '~/signals/proxies'
@ -10,6 +11,7 @@ export default () => {
delayMap, delayMap,
updateProxy, updateProxy,
setProxiesByProxyName, setProxiesByProxyName,
delayTestByProxyGroupName,
} = useProxies() } = useProxies()
const [collapseMap, setCollapseMap] = createSignal<Record<string, boolean>>( const [collapseMap, setCollapseMap] = createSignal<Record<string, boolean>>(
{}, {},
@ -40,6 +42,11 @@ export default () => {
setCollapseMap({ ...cMap }) setCollapseMap({ ...cMap })
} }
const onSpeedTestClick = (e: MouseEvent, name: string) => {
e.stopPropagation()
delayTestByProxyGroupName(name)
}
const getCollapseClassName = (name: string) => { const getCollapseClassName = (name: string) => {
return collapseMap()[name] ? 'collapse-open' : 'collapse-close' return collapseMap()[name] ? 'collapse-open' : 'collapse-close'
} }
@ -59,10 +66,14 @@ export default () => {
)} )}
> >
<div <div
class="collapse-title text-xl font-medium" class="collapse-title flex items-center text-xl font-medium"
onClick={() => onCollapseTitleClick(proxy.name)} onClick={() => onCollapseTitleClick(proxy.name)}
> >
{proxy.name} {proxy.type} {proxy.name} {proxy.type}
<IconBrandSpeedtest
class="m-4 cursor-pointer"
onClick={(e) => onSpeedTestClick(e, proxy.name)}
/>
</div> </div>
<div class="collapse-content grid grid-cols-1 gap-2 sm:grid-cols-3 lg:grid-cols-5"> <div class="collapse-content grid grid-cols-1 gap-2 sm:grid-cols-3 lg:grid-cols-5">
<For each={proxy.all}> <For each={proxy.all}>

View File

@ -82,7 +82,7 @@ export default () => {
class="badge badge-info flex w-full cursor-pointer items-center gap-4 py-4" class="badge badge-info flex w-full cursor-pointer items-center gap-4 py-4"
onClick={() => { onClick={() => {
setSelectedEndpoint(id) setSelectedEndpoint(id)
navigate('/') navigate('/overview')
}} }}
> >
{url} {url}

View File

@ -31,10 +31,14 @@ export function useProxies() {
const { proxies } = await request const { proxies } = await request
.get('proxies') .get('proxies')
.json<{ proxies: Record<string, Proxy> }>() .json<{ proxies: Record<string, Proxy> }>()
const sortIndex = proxies['GLOBAL'].all ?? []
setProxies( setProxies(
Object.values(proxies).filter( Object.values(proxies)
(proxy) => proxy.all && proxy.all.length > 0, .filter((proxy) => proxy.all && proxy.all.length > 0)
.sort(
(pre, next) =>
sortIndex.indexOf(pre.name) - sortIndex.indexOf(next.name),
), ),
) )
} }
@ -48,9 +52,25 @@ export function useProxies() {
await updateProxy() await updateProxy()
} }
const delayTestByProxyGroupName = async (proxyGroupName: string) => {
const data: Record<string, number> = await request
.get(
`group/${proxyGroupName}/delay?url=https%3A%2F%2Fwww.gstatic.com%2Fgenerate_204&timeout=2000`,
)
.json()
const dMap = delayMap()
Object.entries(data).forEach(([name, time]) => {
dMap[name] = time
})
setDelayMap({ ...dMap })
}
return { return {
proxies, proxies,
proxyProviders, proxyProviders,
delayTestByProxyGroupName,
delayMap, delayMap,
updateProxy, updateProxy,
setProxiesByProxyName, setProxiesByProxyName,