metacubexd/src/signals/proxies.ts

156 lines
4.1 KiB
TypeScript
Raw Normal View History

import { createSignal } from 'solid-js'
2023-09-06 11:17:00 +08:00
import {
autoCloseConns,
latencyTestTimeoutDuration,
2023-09-06 11:17:00 +08:00
urlForLatencyTest,
useRequest,
} from '~/signals'
import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
import { useConnections } from './connections'
2023-08-29 20:20:01 +08:00
type ProxyInfo = {
name: string
udp: boolean
now: string
xudp: 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[]>([])
const [latencyMap, setLatencyMap] = createSignal<Record<string, number>>({})
const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, ProxyInfo>>(
{},
)
2023-08-30 23:54:49 +08:00
const setProxiesInfo = (proxies: (Proxy | ProxyNode)[]) => {
const newProxyNodeMap = { ...proxyNodeMap() }
const newLatencyMap = { ...latencyMap() }
proxies.forEach((proxy) => {
const latency = proxy.history.at(-1)?.delay || -1
newProxyNodeMap[proxy.name] = {
udp: proxy.udp,
xudp: proxy.xudp,
type: proxy.type,
now: proxy.now,
name: proxy.name,
}
newLatencyMap[proxy.name] = latency
})
setProxyNodeMap(newProxyNodeMap)
setLatencyMap(newLatencyMap)
}
export const useProxies = () => {
2023-08-29 20:20:01 +08:00
const request = useRequest()
const { activeConnectionsWithSpeed } = useConnections()
const updateProxies = async () => {
const [{ providers }, { proxies }] = await Promise.all([
request
.get('providers/proxies')
.json<{ providers: Record<string, ProxyProvider> }>(),
request.get('proxies').json<{ proxies: Record<string, Proxy> }>(),
])
2023-08-29 20:20:01 +08:00
2023-09-01 10:45:38 +08:00
const sortIndex = [...(proxies['GLOBAL'].all ?? []), 'GLOBAL']
const sortedProxies = Object.values(proxies)
.filter((proxy) => proxy.all?.length)
.sort(
(prev, next) =>
sortIndex.indexOf(prev.name) - sortIndex.indexOf(next.name),
)
const sortedProviders = Object.values(providers).filter(
(provider) =>
provider.name !== 'default' && provider.vehicleType !== 'Compatible',
2023-08-29 20:20:01 +08:00
)
const allProxies: (Proxy | ProxyNode)[] = [
...Object.values(proxies),
...sortedProviders.flatMap((provider) => provider.proxies),
]
setProxies(sortedProxies)
setProxyProviders(sortedProviders)
setProxiesInfo(allProxies)
2023-08-29 20:20:01 +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)!
if (autoCloseConns()) {
activeConnectionsWithSpeed().forEach(({ id, chains }) => {
if (chains.includes(proxy.name)) {
request.delete(`connections/${id}`)
}
})
}
2023-08-29 20:20:01 +08:00
await request.put(`proxies/${proxy.name}`, {
body: JSON.stringify({
name: proxyName,
}),
})
2023-09-02 13:50:24 +08:00
proxyGroup.now = proxyName
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`, {
2023-09-06 10:41:30 +08:00
searchParams: {
url: urlForLatencyTest(),
timeout: latencyTestTimeoutDuration(),
2023-09-06 10:41:30 +08:00
},
2023-09-01 16:30:23 +08:00
})
2023-08-30 11:01:19 +08:00
.json()
setLatencyMap({
...latencyMap(),
...data,
})
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) => {
try {
await request.put(`providers/proxies/${proxyProviderName}`)
} catch {}
await updateProxies()
2023-08-30 11:01:19 +08:00
}
2023-09-02 17:01:59 +08:00
const updateAllProvider = async () => {
await Promise.allSettled(
proxyProviders().map((provider) =>
request.put(`providers/proxies/${provider.name}`),
),
2023-09-02 17:01:59 +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) => {
2023-09-06 11:17:00 +08:00
await request.get(`providers/proxies/${providerName}/healthcheck`, {
timeout: 20 * 1000,
})
await updateProxies()
}
2023-08-29 20:20:01 +08:00
return {
proxies,
proxyProviders,
2023-09-03 05:40:39 +08:00
latencyTestByProxyGroupName,
latencyMap,
2023-08-30 23:54:49 +08:00
proxyNodeMap,
updateProxies,
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
}
}