Skip to content

Commit dfe5f94

Browse files
author
gui.for.singbox
committed
introduce getProxyPort function for cleaner proxy management
1 parent f7837df commit dfe5f94

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

frontend/src/stores/env.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,10 @@ export const useEnvStore = defineStore('env', () => {
4242
}
4343

4444
const setSystemProxy = async () => {
45-
const kernelApiStore = useKernelApiStore()
46-
47-
let port = 0
48-
let proxyType = 0 // 0: Mixed 1: Http 2: Socks
49-
const { port: _port, 'socks-port': socksPort, 'mixed-port': mixedPort } = kernelApiStore.config
50-
51-
if (mixedPort) {
52-
port = mixedPort
53-
proxyType = 0
54-
} else if (_port) {
55-
port = _port
56-
proxyType = 1
57-
} else if (socksPort) {
58-
port = socksPort
59-
proxyType = 2
60-
}
61-
62-
if (!port) throw 'home.overview.needPort'
45+
const proxyPort = useKernelApiStore().getProxyPort()
46+
if (!proxyPort) throw 'home.overview.needPort'
6347

64-
await SetSystemProxy(true, '127.0.0.1:' + port, proxyType)
48+
await SetSystemProxy(true, '127.0.0.1:' + proxyPort.port, proxyPort.proxyType)
6549

6650
systemProxy.value = true
6751
}

frontend/src/stores/kernelApi.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
useEnvStore
2121
} from '@/stores'
2222

23+
export type ProxyType = 'mixed' | 'http' | 'socks'
24+
2325
export const useKernelApiStore = defineStore('kernelApi', () => {
2426
/** RESTful API */
2527
const config = ref<KernelApiConfig>({
@@ -331,6 +333,35 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
331333
await startKernel()
332334
}
333335

336+
const getProxyPort = ():
337+
| {
338+
port: number
339+
proxyType: ProxyType
340+
}
341+
| undefined => {
342+
const { port, 'socks-port': socksPort, 'mixed-port': mixedPort } = config.value
343+
344+
if (mixedPort) {
345+
return {
346+
port: mixedPort,
347+
proxyType: 'mixed'
348+
}
349+
}
350+
if (port) {
351+
return {
352+
port,
353+
proxyType: 'http'
354+
}
355+
}
356+
if (socksPort) {
357+
return {
358+
port: socksPort,
359+
proxyType: 'socks'
360+
}
361+
}
362+
return undefined
363+
}
364+
334365
watch(
335366
[() => config.value.mode, () => config.value.tun.enable, () => proxies.value],
336367
updateTrayMenus
@@ -349,6 +380,7 @@ export const useKernelApiStore = defineStore('kernelApi', () => {
349380
refreshConfig,
350381
updateConfig,
351382
patchConfig,
352-
refreshProviderProxies
383+
refreshProviderProxies,
384+
getProxyPort
353385
}
354386
})

frontend/src/utils/helper.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { ignoredError, APP_TITLE } from '@/utils'
22
import { deleteConnection, getConnections, useProxy } from '@/api/kernel'
3-
import { useAppSettingsStore, useEnvStore, useKernelApiStore, usePluginsStore } from '@/stores'
3+
import {
4+
type ProxyType,
5+
useAppSettingsStore,
6+
useEnvStore,
7+
useKernelApiStore,
8+
usePluginsStore
9+
} from '@/stores'
410
import { AbsolutePath, Exec, ExitApp, Readfile, Writefile } from '@/bridge'
511
import { useConfirm, useMessage } from '@/hooks'
612

@@ -67,7 +73,11 @@ export const GrantTUNPermission = async (path: string) => {
6773
}
6874

6975
// SystemProxy Helper
70-
export const SetSystemProxy = async (enable: boolean, server: string, proxyType = 0) => {
76+
export const SetSystemProxy = async (
77+
enable: boolean,
78+
server: string,
79+
proxyType: ProxyType = 'mixed'
80+
) => {
7181
const { os } = useEnvStore().env
7282

7383
if (os === 'windows') {
@@ -85,8 +95,8 @@ export const SetSystemProxy = async (enable: boolean, server: string, proxyType
8595
}
8696
}
8797

88-
function setWindowsSystemProxy(server: string, enabled: boolean, proxyType: number) {
89-
if (proxyType === 2) throw 'home.overview.notSupportSocks'
98+
function setWindowsSystemProxy(server: string, enabled: boolean, proxyType: ProxyType) {
99+
if (proxyType === 'socks') throw 'home.overview.notSupportSocks'
90100

91101
ignoredError(
92102
Exec,
@@ -121,12 +131,12 @@ function setWindowsSystemProxy(server: string, enabled: boolean, proxyType: numb
121131
)
122132
}
123133

124-
function setDarwinSystemProxy(server: string, enabled: boolean, proxyType: number) {
134+
function setDarwinSystemProxy(server: string, enabled: boolean, proxyType: ProxyType) {
125135
function _set(device: string) {
126136
const state = enabled ? 'on' : 'off'
127137

128-
const httpState = [0, 1].includes(proxyType) ? state : 'off'
129-
const socksState = [0, 2].includes(proxyType) ? state : 'off'
138+
const httpState = ['mixed', 'http'].includes(proxyType) ? state : 'off'
139+
const socksState = ['mixed', 'socks'].includes(proxyType) ? state : 'off'
130140

131141
ignoredError(Exec, 'networksetup', ['-setwebproxystate', device, httpState])
132142
ignoredError(Exec, 'networksetup', ['-setsecurewebproxystate', device, httpState])
@@ -146,10 +156,10 @@ function setDarwinSystemProxy(server: string, enabled: boolean, proxyType: numbe
146156
_set('Wi-Fi')
147157
}
148158

149-
function setLinuxSystemProxy(server: string, enabled: boolean, proxyType: number) {
159+
function setLinuxSystemProxy(server: string, enabled: boolean, proxyType: ProxyType) {
150160
const [serverName, serverPort] = server.split(':')
151-
const httpEnabled = enabled && [0, 1].includes(proxyType)
152-
const socksEnabled = enabled && [0, 2].includes(proxyType)
161+
const httpEnabled = enabled && ['mixed', 'http'].includes(proxyType)
162+
const socksEnabled = enabled && ['mixed', 'socks'].includes(proxyType)
153163

154164
ignoredError(Exec, 'gsettings', [
155165
'set',

0 commit comments

Comments
 (0)