2023-09-03 02:18:05 +08:00
|
|
|
import { createSignal } from 'solid-js'
|
2023-09-03 05:40:39 +08:00
|
|
|
import { autoCloseConns, urlForLatencyTest, useRequest } from '~/signals'
|
2023-09-01 12:10:34 +08:00
|
|
|
import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
|
2023-08-29 20:20:01 +08:00
|
|
|
|
2023-09-01 12:10:34 +08:00
|
|
|
type ProxyInfo = {
|
|
|
|
name: string
|
|
|
|
udp: boolean
|
|
|
|
type: string
|
|
|
|
}
|
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-09-03 05:35:08 +08:00
|
|
|
const [latencyMap, setLatencyMap] = createSignal<Record<string, number>>({})
|
2023-09-01 12:10:34 +08:00
|
|
|
const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, ProxyInfo>>(
|
|
|
|
{},
|
|
|
|
)
|
2023-08-30 23:54:49 +08:00
|
|
|
|
2023-09-03 03:26:29 +08:00
|
|
|
export const useProxies = () => {
|
2023-08-29 20:20:01 +08:00
|
|
|
const request = useRequest()
|
2023-09-03 03:26:29 +08:00
|
|
|
|
2023-09-03 20:23:02 +08:00
|
|
|
const setProxyInfoByProxies = (proxies: Proxy[] | ProxyNode[]) => {
|
2023-09-01 12:10:34 +08:00
|
|
|
proxies.forEach((proxy) => {
|
2023-09-03 06:37:48 +08:00
|
|
|
const latency = proxy.history.at(-1)?.delay || -1
|
2023-09-02 15:00:13 +08:00
|
|
|
|
2023-09-01 12:10:34 +08:00
|
|
|
setProxyNodeMap({
|
|
|
|
...proxyNodeMap(),
|
|
|
|
[proxy.name]: {
|
|
|
|
udp: proxy.udp,
|
|
|
|
type: proxy.type,
|
|
|
|
name: proxy.name,
|
|
|
|
},
|
|
|
|
})
|
2023-09-03 05:35:08 +08:00
|
|
|
setLatencyMap({
|
|
|
|
...latencyMap(),
|
2023-09-03 05:40:39 +08:00
|
|
|
[proxy.name]: latency,
|
2023-09-02 15:00:13 +08:00
|
|
|
})
|
2023-09-01 12:10:34 +08:00
|
|
|
})
|
|
|
|
}
|
2023-09-02 15:55:38 +08:00
|
|
|
|
2023-09-03 05:35:08 +08:00
|
|
|
const updateProxies = async () => {
|
2023-08-29 20:20:01 +08:00
|
|
|
const { providers } = await request
|
|
|
|
.get('providers/proxies')
|
|
|
|
.json<{ providers: Record<string, ProxyProvider> }>()
|
|
|
|
|
2023-09-01 12:10:34 +08:00
|
|
|
Object.values(providers).forEach((provider) => {
|
2023-09-03 20:23:02 +08:00
|
|
|
setProxyInfoByProxies(provider.proxies)
|
2023-09-01 12:10:34 +08:00
|
|
|
})
|
|
|
|
|
2023-08-29 20:20:01 +08:00
|
|
|
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-09-03 05:35:08 +08:00
|
|
|
const proxiesArray = Object.values(proxies)
|
2023-08-29 20:20:01 +08:00
|
|
|
|
2023-09-03 20:23:02 +08:00
|
|
|
setProxyInfoByProxies(proxiesArray)
|
2023-08-29 20:20:01 +08:00
|
|
|
setProxies(
|
2023-09-03 05:35:08 +08:00
|
|
|
proxiesArray
|
2023-09-01 11:49:59 +08:00
|
|
|
.filter((proxy) => proxy.all?.length)
|
2023-08-30 11:01:19 +08:00
|
|
|
.sort(
|
2023-09-03 05:35:08 +08:00
|
|
|
(prev, next) =>
|
|
|
|
sortIndex.indexOf(prev.name) - sortIndex.indexOf(next.name),
|
2023-08-30 11:01:19 +08:00
|
|
|
),
|
2023-08-29 20:20:01 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-31 10:50:43 +08:00
|
|
|
const setProxyGroupByProxyName = async (proxy: Proxy, proxyName: string) => {
|
2023-09-02 13:50:24 +08:00
|
|
|
const proxyGroupList = proxies().slice()
|
|
|
|
const proxyGroup = proxyGroupList.find((i) => i.name === proxy.name)!
|
|
|
|
|
2023-09-02 15:55:38 +08:00
|
|
|
if (autoCloseConns()) request.delete('connections')
|
|
|
|
|
2023-08-29 20:20:01 +08:00
|
|
|
await request.put(`proxies/${proxy.name}`, {
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: proxyName,
|
|
|
|
}),
|
|
|
|
})
|
2023-09-02 15:55:38 +08:00
|
|
|
|
2023-09-02 13:50:24 +08:00
|
|
|
proxyGroup.now = proxyName
|
2023-09-02 15:55:38 +08:00
|
|
|
|
2023-09-02 13:50:24 +08:00
|
|
|
setProxies(proxyGroupList)
|
2023-08-29 20:20:01 +08:00
|
|
|
}
|
|
|
|
|
2023-09-03 05:40:39 +08:00
|
|
|
const latencyTestByProxyGroupName = async (proxyGroupName: string) => {
|
2023-08-30 11:01:19 +08:00
|
|
|
const data: Record<string, number> = await request
|
2023-09-01 16:30:23 +08:00
|
|
|
.get(`group/${proxyGroupName}/delay`, {
|
|
|
|
searchParams: {
|
2023-09-03 05:40:39 +08:00
|
|
|
url: urlForLatencyTest(),
|
2023-09-01 16:30:23 +08:00
|
|
|
timeout: 2000,
|
|
|
|
},
|
|
|
|
})
|
2023-08-30 11:01:19 +08:00
|
|
|
.json()
|
|
|
|
|
2023-09-03 05:35:08 +08:00
|
|
|
setLatencyMap({
|
|
|
|
...latencyMap(),
|
2023-09-02 15:00:13 +08:00
|
|
|
...data,
|
2023-09-01 12:10:34 +08:00
|
|
|
})
|
2023-08-31 12:02:23 +08:00
|
|
|
}
|
2023-08-30 11:01:19 +08:00
|
|
|
|
2023-08-31 12:02:23 +08:00
|
|
|
const updateProviderByProviderName = async (proxyProviderName: string) => {
|
2023-09-02 17:55:09 +08:00
|
|
|
try {
|
|
|
|
await request.put(`providers/proxies/${proxyProviderName}`)
|
|
|
|
} catch {}
|
2023-09-03 05:35:08 +08:00
|
|
|
await updateProxies()
|
2023-08-30 11:01:19 +08:00
|
|
|
}
|
|
|
|
|
2023-09-02 17:01:59 +08:00
|
|
|
const updateAllProvider = async () => {
|
|
|
|
await Promise.all(
|
|
|
|
proxyProviders().map((provider) => {
|
|
|
|
return request.put(`providers/proxies/${provider.name}`)
|
|
|
|
}),
|
|
|
|
)
|
2023-09-03 05:35:08 +08:00
|
|
|
await updateProxies()
|
2023-09-02 17:01:59 +08:00
|
|
|
}
|
|
|
|
|
2023-08-31 12:02:23 +08:00
|
|
|
const healthCheckByProviderName = async (providerName: string) => {
|
|
|
|
await request.get(`providers/proxies/${providerName}/healthcheck`, {
|
2023-09-03 20:23:02 +08:00
|
|
|
timeout: 30 * 1000, // this api is a little bit slow sometimes...
|
2023-08-31 12:02:23 +08:00
|
|
|
})
|
2023-09-03 05:35:08 +08:00
|
|
|
await updateProxies()
|
2023-08-31 10:50:43 +08:00
|
|
|
}
|
|
|
|
|
2023-08-29 20:20:01 +08:00
|
|
|
return {
|
|
|
|
proxies,
|
|
|
|
proxyProviders,
|
2023-09-03 05:40:39 +08:00
|
|
|
latencyTestByProxyGroupName,
|
2023-09-03 05:35:08 +08:00
|
|
|
latencyMap,
|
2023-08-30 23:54:49 +08:00
|
|
|
proxyNodeMap,
|
2023-09-03 05:35:08 +08:00
|
|
|
updateProxies,
|
2023-08-31 10:50:43 +08:00
|
|
|
setProxyGroupByProxyName,
|
2023-08-31 12:02:23 +08:00
|
|
|
updateProviderByProviderName,
|
2023-09-02 17:01:59 +08:00
|
|
|
updateAllProvider,
|
2023-08-31 12:02:23 +08:00
|
|
|
healthCheckByProviderName,
|
2023-08-29 20:20:01 +08:00
|
|
|
}
|
|
|
|
}
|