Skip to content

Commit fbd52a4

Browse files
author
pompurin404
committed
auto close connection
1 parent 0d6a65b commit fbd52a4

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

src/main/utils/template.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const defaultConfig: IAppConfig = {
55
proxyDisplayMode: 'simple',
66
proxyDisplayOrder: 'default',
77
autoCheckUpdate: true,
8+
autoCloseConnection: true,
89
useNameserverPolicy: false,
910
nameserverPolicy: {},
1011
sysProxy: { enable: false, mode: 'manual' }

src/renderer/src/components/sider/outbound-mode-switcher.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { Tabs, Tab } from '@nextui-org/react'
2+
import { useAppConfig } from '@renderer/hooks/use-app-config'
23
import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config'
3-
import { patchMihomoConfig } from '@renderer/utils/ipc'
4+
import { mihomoCloseAllConnections, patchMihomoConfig } from '@renderer/utils/ipc'
45
import { Key } from 'react'
56

67
const OutboundModeSwitcher: React.FC = () => {
78
const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig(true)
9+
const { appConfig } = useAppConfig()
10+
const { autoCloseConnection = true } = appConfig || {}
811
const { mode } = controledMihomoConfig || {}
912

1013
const onChangeMode = async (mode: OutboundMode): Promise<void> => {
1114
await patchControledMihomoConfig({ mode })
1215
await patchMihomoConfig({ mode })
16+
if (autoCloseConnection) {
17+
await mihomoCloseAllConnections()
18+
}
1319
}
1420

1521
return (

src/renderer/src/pages/proxies.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useAppConfig } from '@renderer/hooks/use-app-config'
44
import {
55
getRuntimeConfig,
66
mihomoChangeProxy,
7+
mihomoCloseAllConnections,
78
mihomoGroupDelay,
89
mihomoProxies,
910
mihomoProxyDelay
@@ -24,7 +25,11 @@ const Proxies: React.FC = () => {
2425
const { data: proxies, mutate } = useSWR('mihomoProxies', mihomoProxies)
2526
const { data: runtime } = useSWR('getRuntimeConfig', getRuntimeConfig)
2627
const { appConfig, patchAppConfig } = useAppConfig()
27-
const { proxyDisplayMode = 'simple', proxyDisplayOrder = 'default' } = appConfig || {}
28+
const {
29+
proxyDisplayMode = 'simple',
30+
proxyDisplayOrder = 'default',
31+
autoCloseConnection = true
32+
} = appConfig || {}
2833

2934
const groups = useMemo(() => {
3035
const groups: IMihomoGroup[] = []
@@ -80,6 +85,9 @@ const Proxies: React.FC = () => {
8085

8186
const onChangeProxy = (group: string, proxy: string): void => {
8287
mihomoChangeProxy(group, proxy).then(() => {
88+
if (autoCloseConnection) {
89+
mihomoCloseAllConnections()
90+
}
8391
mutate()
8492
})
8593
}

src/renderer/src/pages/settings.tsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const Settings: React.FC = () => {
3030
delayTestTimeout,
3131
autoCheckUpdate,
3232
userAgent,
33+
autoCloseConnection = true,
3334
appTheme = 'system'
3435
} = appConfig || {}
3536
const [url, setUrl] = useState(delayTestUrl)
@@ -169,7 +170,7 @@ const Settings: React.FC = () => {
169170
}}
170171
></Input>
171172
</SettingItem>
172-
<SettingItem title="延迟测试超时时间">
173+
<SettingItem title="延迟测试超时时间" divider>
173174
<Input
174175
type="number"
175176
size="sm"
@@ -179,7 +180,16 @@ const Settings: React.FC = () => {
179180
onValueChange={(v) => {
180181
patchAppConfig({ delayTestTimeout: parseInt(v) })
181182
}}
182-
></Input>
183+
/>
184+
</SettingItem>
185+
<SettingItem title="自动断开连接">
186+
<Switch
187+
size="sm"
188+
isSelected={autoCloseConnection}
189+
onValueChange={(v) => {
190+
patchAppConfig({ autoCloseConnection: v })
191+
}}
192+
/>
183193
</SettingItem>
184194
</SettingCard>
185195
<SettingCard>

src/shared/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ interface IAppConfig {
197197
appTheme: AppTheme
198198
autoCheckUpdate: boolean
199199
silentStart: boolean
200+
autoCloseConnection: boolean
200201
sysProxy: ISysProxyConfig
201202
userAgent?: string
202203
delayTestUrl?: string

0 commit comments

Comments
 (0)