From b79f10f528e5a501f8d2b2e3d7e66eae820a00dd Mon Sep 17 00:00:00 2001 From: kunish Date: Sat, 2 Sep 2023 15:55:38 +0800 Subject: [PATCH] feat: auto close all connections before switch proxy --- src/components/ProxyNodePreview.tsx | 2 +- src/i18n/en.ts | 1 + src/i18n/zh.ts | 1 + src/pages/Config.tsx | 76 +++++++++++++++++------------ src/signals/config.ts | 16 ++++++ src/signals/proxies.ts | 9 +++- 6 files changed, 71 insertions(+), 34 deletions(-) create mode 100644 src/signals/config.ts diff --git a/src/components/ProxyNodePreview.tsx b/src/components/ProxyNodePreview.tsx index d0b6421..df0b4f2 100644 --- a/src/components/ProxyNodePreview.tsx +++ b/src/components/ProxyNodePreview.tsx @@ -1,6 +1,6 @@ import { Show, createMemo } from 'solid-js' import { PROXIES_PREVIEW_TYPE } from '~/config/enum' -import { proxiesPreviewType } from '~/pages/Config' +import { proxiesPreviewType } from '~/signals/config' import ProxyPreviewBar from './ProxyPreviewBar' import ProxyPreviewDots from './ProxyPreviewDots' diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 4d9f6da..cf626ab 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -39,4 +39,5 @@ export default { auto: 'Auto', proxiesPreviewType: 'Proxies preview type', urlForDelayTest: 'Url for delay test', + autoCloseConns: 'Automatically close all connections', } diff --git a/src/i18n/zh.ts b/src/i18n/zh.ts index d5d93b2..6f1aeb7 100644 --- a/src/i18n/zh.ts +++ b/src/i18n/zh.ts @@ -39,4 +39,5 @@ export default { auto: '自适应', proxiesPreviewType: '节点组预览样式', urlForDelayTest: '测速链接', + autoCloseConns: '切换代理时自动断开全部连接', } diff --git a/src/pages/Config.tsx b/src/pages/Config.tsx index 7800a04..b4ade0c 100644 --- a/src/pages/Config.tsx +++ b/src/pages/Config.tsx @@ -1,11 +1,18 @@ import { createForm } from '@felte/solid' import { validator } from '@felte/validator-zod' import { useI18n } from '@solid-primitives/i18n' -import { makePersisted } from '@solid-primitives/storage' import { For, Show, createSignal, onMount } from 'solid-js' import { z } from 'zod' import { PROXIES_PREVIEW_TYPE } from '~/config/enum' import { useRequest } from '~/signals' +import { + autoCloseConns, + proxiesPreviewType, + setAutoCloseConns, + setProxiesPreviewType, + setUrlForDelayTest, + urlForDelayTest, +} from '~/signals/config' import type { DNSQuery, Config as IConfig } from '~/types' const dnsQueryFormSchema = z.object({ @@ -128,47 +135,54 @@ const ConfigForm = () => { ) } -export const [proxiesPreviewType, setProxiesPreviewType] = makePersisted( - createSignal(PROXIES_PREVIEW_TYPE.BAR), - { name: 'proxiesPreviewType', storage: localStorage }, -) -export const [urlForDelayTest, setUrlForDelayTest] = makePersisted( - createSignal('https://www.gstatic.com/generate_204'), - { name: 'urlForDelayTest', storage: localStorage }, -) - const ConfigForXd = () => { const [t] = useI18n() return ( - <> -
{t('proxiesPreviewType')}
-
- - {(value) => ( - - )} - -
-
{t('urlForDelayTest')}
+
+
{t('proxiesPreviewType')}
+ +
+ + {(value) => ( + + )} + +
+
+ +
+
{t('autoCloseConns')}
+ + setAutoCloseConns(e.target.checked)} + /> +
+ +
+
{t('urlForDelayTest')}
+ setUrlForDelayTest(e.target?.value!)} />
- +
) } diff --git a/src/signals/config.ts b/src/signals/config.ts new file mode 100644 index 0000000..d45320f --- /dev/null +++ b/src/signals/config.ts @@ -0,0 +1,16 @@ +import { makePersisted } from '@solid-primitives/storage' +import { createSignal } from 'solid-js' +import { PROXIES_PREVIEW_TYPE } from '~/config/enum' + +export const [proxiesPreviewType, setProxiesPreviewType] = makePersisted( + createSignal(PROXIES_PREVIEW_TYPE.BAR), + { name: 'proxiesPreviewType', storage: localStorage }, +) +export const [urlForDelayTest, setUrlForDelayTest] = makePersisted( + createSignal('https://www.gstatic.com/generate_204'), + { name: 'urlForDelayTest', storage: localStorage }, +) +export const [autoCloseConns, setAutoCloseConns] = makePersisted( + createSignal(false), + { name: 'autoCloseConns', storage: localStorage }, +) diff --git a/src/signals/proxies.ts b/src/signals/proxies.ts index 13717a2..52e313a 100644 --- a/src/signals/proxies.ts +++ b/src/signals/proxies.ts @@ -1,6 +1,6 @@ import { createSignal } from 'solid-js' -import { urlForDelayTest } from '~/pages/Config' import { useRequest } from '~/signals' +import { autoCloseConns, urlForDelayTest } from '~/signals/config' import type { Proxy, ProxyNode, ProxyProvider } from '~/types' type ProxyInfo = { @@ -37,6 +37,7 @@ export function useProxies() { }) }) } + const updateProxy = async () => { const { providers } = await request .get('providers/proxies') @@ -73,14 +74,18 @@ export function useProxies() { const proxyGroupList = proxies().slice() const proxyGroup = proxyGroupList.find((i) => i.name === proxy.name)! + if (autoCloseConns()) request.delete('connections') + await request.put(`proxies/${proxy.name}`, { body: JSON.stringify({ name: proxyName, }), }) + proxyGroup.now = proxyName + setProxies(proxyGroupList) - setTimeout(updateProxy) + queueMicrotask(updateProxy) } const delayTestByProxyGroupName = async (proxyGroupName: string) => {