2023-08-29 20:20:01 +08:00
|
|
|
import { createSignal } from 'solid-js'
|
|
|
|
import { useRequest } from '~/signals'
|
2023-08-30 23:54:49 +08:00
|
|
|
import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
|
2023-08-29 20:20:01 +08:00
|
|
|
|
2023-08-30 23:02:55 +08:00
|
|
|
// these signals should be global state
|
|
|
|
const [proxies, setProxies] = createSignal<Proxy[]>([])
|
|
|
|
const [proxyProviders, setProxyProviders] = createSignal<ProxyProvider[]>([])
|
|
|
|
|
2023-08-30 23:54:49 +08:00
|
|
|
const [delayMap, setDelayMap] = createSignal<Record<string, number>>({})
|
|
|
|
const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, ProxyNode>>(
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
|
2023-08-29 20:20:01 +08:00
|
|
|
export function useProxies() {
|
|
|
|
const request = useRequest()
|
|
|
|
|
|
|
|
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) => {
|
2023-08-30 23:54:49 +08:00
|
|
|
setProxyNodeMap({ ...proxyNodeMap(), [proxy.name]: proxy })
|
2023-08-29 20:20:01 +08:00
|
|
|
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> }>()
|
2023-09-01 10:45:38 +08:00
|
|
|
const sortIndex = [...(proxies['GLOBAL'].all ?? []), 'GLOBAL']
|
2023-08-29 20:20:01 +08:00
|
|
|
|
|
|
|
setProxies(
|
2023-08-30 11:01:19 +08:00
|
|
|
Object.values(proxies)
|
|
|
|
.filter((proxy) => proxy.all && proxy.all.length > 0)
|
|
|
|
.sort(
|
|
|
|
(pre, next) =>
|
|
|
|
sortIndex.indexOf(pre.name) - sortIndex.indexOf(next.name),
|
|
|
|
),
|
2023-08-29 20:20:01 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-31 10:50:43 +08:00
|
|
|
const setProxyGroupByProxyName = async (proxy: Proxy, proxyName: string) => {
|
2023-08-29 20:20:01 +08:00
|
|
|
await request.put(`proxies/${proxy.name}`, {
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: proxyName,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
await updateProxy()
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:01:19 +08:00
|
|
|
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()
|
|
|
|
|
2023-08-31 12:02:23 +08:00
|
|
|
setDelayMap({ ...delayMap(), ...data })
|
|
|
|
}
|
2023-08-30 11:01:19 +08:00
|
|
|
|
2023-08-31 12:02:23 +08:00
|
|
|
const updateProviderByProviderName = async (proxyProviderName: string) => {
|
|
|
|
await request.put(`providers/proxies/${proxyProviderName}`)
|
|
|
|
await updateProxy()
|
2023-08-30 11:01:19 +08:00
|
|
|
}
|
|
|
|
|
2023-08-31 12:02:23 +08:00
|
|
|
const healthCheckByProviderName = async (providerName: string) => {
|
|
|
|
await request.get(`providers/proxies/${providerName}/healthcheck`, {
|
|
|
|
timeout: 30 * 1000, // thie api was a little bit slow sometimes...
|
|
|
|
})
|
2023-08-31 10:50:43 +08:00
|
|
|
await updateProxy()
|
|
|
|
}
|
|
|
|
|
2023-08-29 20:20:01 +08:00
|
|
|
return {
|
|
|
|
proxies,
|
|
|
|
proxyProviders,
|
2023-08-30 11:01:19 +08:00
|
|
|
delayTestByProxyGroupName,
|
2023-08-30 23:54:49 +08:00
|
|
|
proxyNodeMap,
|
2023-08-29 20:20:01 +08:00
|
|
|
delayMap,
|
|
|
|
updateProxy,
|
2023-08-31 10:50:43 +08:00
|
|
|
setProxyGroupByProxyName,
|
2023-08-31 12:02:23 +08:00
|
|
|
updateProviderByProviderName,
|
|
|
|
healthCheckByProviderName,
|
2023-08-29 20:20:01 +08:00
|
|
|
}
|
|
|
|
}
|