fix: set proxy both from proxies and provider

This commit is contained in:
Zephyruso 2023-09-01 12:10:34 +08:00
parent fb7f07d962
commit 2549bfe89c
2 changed files with 43 additions and 21 deletions

View File

@ -8,12 +8,10 @@ export default (props: {
onClick?: () => void onClick?: () => void
}) => { }) => {
const { proxyName, isSelected, onClick } = props const { proxyName, isSelected, onClick } = props
const { delayMap, proxyNodeMap } = useProxies() const { proxyNodeMap } = useProxies()
const proxyNode = createMemo(() => proxyNodeMap()[proxyName]) const proxyNode = createMemo(() => proxyNodeMap()[proxyName])
const Delay = (proxyname: string) => { const Delay = (delay: number | undefined) => {
const delay = delayMap()[proxyname]
if (typeof delay !== 'number' || delay === 0) { if (typeof delay !== 'number' || delay === 0) {
return '' return ''
} }
@ -29,7 +27,7 @@ export default (props: {
return <span class={textClassName}>{delay}ms</span> return <span class={textClassName}>{delay}ms</span>
} }
const formatProxyType = (type: string) => { const formatProxyType = (type = '') => {
const t = type.toLowerCase() const t = type.toLowerCase()
if (t.includes('shadowsocks')) { if (t.includes('shadowsocks')) {
@ -57,10 +55,10 @@ export default (props: {
isSelected && 'text-primary', isSelected && 'text-primary',
)} )}
> >
{formatProxyType(proxyNode().type)} {formatProxyType(proxyNode()?.type)}
{proxyNode().udp && ' :: udp'} {proxyNode()?.udp && ' :: udp'}
</div> </div>
<div class="text-xs">{Delay(proxyName)}</div> <div class="text-xs">{Delay(proxyNode()?.delay)}</div>
</div> </div>
</div> </div>
) )

View File

@ -1,22 +1,45 @@
import { createSignal } from 'solid-js' import { createSignal } from 'solid-js'
import { useRequest } from '~/signals' import { useRequest } from '~/signals'
import type { Proxy, ProxyProvider } from '~/types' import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
type ProxyInfo = {
name: string
udp: boolean
type: string
delay?: number
}
// these signals should be global state // these signals should be global state
const [proxies, setProxies] = createSignal<Proxy[]>([]) const [proxies, setProxies] = createSignal<Proxy[]>([])
const [proxyProviders, setProxyProviders] = createSignal<ProxyProvider[]>([]) const [proxyProviders, setProxyProviders] = createSignal<ProxyProvider[]>([])
const [delayMap, setDelayMap] = createSignal<Record<string, number>>({}) const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, ProxyInfo>>(
const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, Proxy>>({}) {},
)
export function useProxies() { export function useProxies() {
const request = useRequest() const request = useRequest()
const setProxyInfoByProixes = (proxies: Proxy[] | ProxyNode[]) => {
proxies.forEach((proxy) => {
setProxyNodeMap({
...proxyNodeMap(),
[proxy.name]: {
udp: proxy.udp,
type: proxy.type,
delay: proxy.history.at(-1)?.delay ?? 0,
name: proxy.name,
},
})
})
}
const updateProxy = async () => { const updateProxy = async () => {
const { providers } = await request const { providers } = await request
.get('providers/proxies') .get('providers/proxies')
.json<{ providers: Record<string, ProxyProvider> }>() .json<{ providers: Record<string, ProxyProvider> }>()
Object.values(providers).forEach((provider) => {
setProxyInfoByProixes(provider.proxies)
})
setProxyProviders( setProxyProviders(
Object.values(providers).filter( Object.values(providers).filter(
(provider) => (provider) =>
@ -28,14 +51,8 @@ export function useProxies() {
.get('proxies') .get('proxies')
.json<{ proxies: Record<string, Proxy> }>() .json<{ proxies: Record<string, Proxy> }>()
const sortIndex = [...(proxies['GLOBAL'].all ?? []), 'GLOBAL'] const sortIndex = [...(proxies['GLOBAL'].all ?? []), 'GLOBAL']
const delay = delayMap()
Object.values(proxies).forEach((proxy) => { setProxyInfoByProixes(Object.values(proxies))
setProxyNodeMap({ ...proxyNodeMap(), [proxy.name]: proxy })
delay[proxy.name] = proxy.history.at(-1)?.delay ?? 0
})
setDelayMap(delay)
setProxies( setProxies(
Object.values(proxies) Object.values(proxies)
.filter((proxy) => proxy.all?.length) .filter((proxy) => proxy.all?.length)
@ -62,7 +79,15 @@ export function useProxies() {
) )
.json() .json()
setDelayMap({ ...delayMap(), ...data }) Object.entries(data).forEach(([name, delay]) => {
setProxyNodeMap({
...proxyNodeMap(),
[name]: {
...proxyNodeMap()[name],
delay: delay,
},
})
})
} }
const updateProviderByProviderName = async (proxyProviderName: string) => { const updateProviderByProviderName = async (proxyProviderName: string) => {
@ -82,7 +107,6 @@ export function useProxies() {
proxyProviders, proxyProviders,
delayTestByProxyGroupName, delayTestByProxyGroupName,
proxyNodeMap, proxyNodeMap,
delayMap,
updateProxy, updateProxy,
setProxyGroupByProxyName, setProxyGroupByProxyName,
updateProviderByProviderName, updateProviderByProviderName,