From fbd52a40f6e7044e5f0b5cdff7f7fec5e6686521 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Sun, 11 Aug 2024 13:14:43 +0800 Subject: [PATCH] auto close connection --- src/main/utils/template.ts | 1 + .../components/sider/outbound-mode-switcher.tsx | 8 +++++++- src/renderer/src/pages/proxies.tsx | 10 +++++++++- src/renderer/src/pages/settings.tsx | 14 ++++++++++++-- src/shared/types.d.ts | 1 + 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/utils/template.ts b/src/main/utils/template.ts index c0c6ff8..9a421b1 100644 --- a/src/main/utils/template.ts +++ b/src/main/utils/template.ts @@ -5,6 +5,7 @@ export const defaultConfig: IAppConfig = { proxyDisplayMode: 'simple', proxyDisplayOrder: 'default', autoCheckUpdate: true, + autoCloseConnection: true, useNameserverPolicy: false, nameserverPolicy: {}, sysProxy: { enable: false, mode: 'manual' } diff --git a/src/renderer/src/components/sider/outbound-mode-switcher.tsx b/src/renderer/src/components/sider/outbound-mode-switcher.tsx index c4c96a7..42b4ac7 100644 --- a/src/renderer/src/components/sider/outbound-mode-switcher.tsx +++ b/src/renderer/src/components/sider/outbound-mode-switcher.tsx @@ -1,15 +1,21 @@ import { Tabs, Tab } from '@nextui-org/react' +import { useAppConfig } from '@renderer/hooks/use-app-config' import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config' -import { patchMihomoConfig } from '@renderer/utils/ipc' +import { mihomoCloseAllConnections, patchMihomoConfig } from '@renderer/utils/ipc' import { Key } from 'react' const OutboundModeSwitcher: React.FC = () => { const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig(true) + const { appConfig } = useAppConfig() + const { autoCloseConnection = true } = appConfig || {} const { mode } = controledMihomoConfig || {} const onChangeMode = async (mode: OutboundMode): Promise => { await patchControledMihomoConfig({ mode }) await patchMihomoConfig({ mode }) + if (autoCloseConnection) { + await mihomoCloseAllConnections() + } } return ( diff --git a/src/renderer/src/pages/proxies.tsx b/src/renderer/src/pages/proxies.tsx index 406607d..4ed8e09 100644 --- a/src/renderer/src/pages/proxies.tsx +++ b/src/renderer/src/pages/proxies.tsx @@ -4,6 +4,7 @@ import { useAppConfig } from '@renderer/hooks/use-app-config' import { getRuntimeConfig, mihomoChangeProxy, + mihomoCloseAllConnections, mihomoGroupDelay, mihomoProxies, mihomoProxyDelay @@ -24,7 +25,11 @@ const Proxies: React.FC = () => { const { data: proxies, mutate } = useSWR('mihomoProxies', mihomoProxies) const { data: runtime } = useSWR('getRuntimeConfig', getRuntimeConfig) const { appConfig, patchAppConfig } = useAppConfig() - const { proxyDisplayMode = 'simple', proxyDisplayOrder = 'default' } = appConfig || {} + const { + proxyDisplayMode = 'simple', + proxyDisplayOrder = 'default', + autoCloseConnection = true + } = appConfig || {} const groups = useMemo(() => { const groups: IMihomoGroup[] = [] @@ -80,6 +85,9 @@ const Proxies: React.FC = () => { const onChangeProxy = (group: string, proxy: string): void => { mihomoChangeProxy(group, proxy).then(() => { + if (autoCloseConnection) { + mihomoCloseAllConnections() + } mutate() }) } diff --git a/src/renderer/src/pages/settings.tsx b/src/renderer/src/pages/settings.tsx index 15408bf..49d7929 100644 --- a/src/renderer/src/pages/settings.tsx +++ b/src/renderer/src/pages/settings.tsx @@ -30,6 +30,7 @@ const Settings: React.FC = () => { delayTestTimeout, autoCheckUpdate, userAgent, + autoCloseConnection = true, appTheme = 'system' } = appConfig || {} const [url, setUrl] = useState(delayTestUrl) @@ -169,7 +170,7 @@ const Settings: React.FC = () => { }} > - + { onValueChange={(v) => { patchAppConfig({ delayTestTimeout: parseInt(v) }) }} - > + /> + + + { + patchAppConfig({ autoCloseConnection: v }) + }} + /> diff --git a/src/shared/types.d.ts b/src/shared/types.d.ts index af44678..5342af1 100644 --- a/src/shared/types.d.ts +++ b/src/shared/types.d.ts @@ -197,6 +197,7 @@ interface IAppConfig { appTheme: AppTheme autoCheckUpdate: boolean silentStart: boolean + autoCloseConnection: boolean sysProxy: ISysProxyConfig userAgent?: string delayTestUrl?: string