Skip to content

Commit 9a692a9

Browse files
committed
完善代理设置
1 parent fa9b84a commit 9a692a9

16 files changed

Lines changed: 106 additions & 95 deletions

File tree

src/common/defaultSetting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ const defaultSetting: LX.AppSetting = {
128128
'search.isShowHistorySearch': false,
129129
'search.isFocusSearchBox': false,
130130

131-
'network.proxy.enable': false,
131+
'network.proxy.type': 'disable',
132132
'network.proxy.host': '',
133133
'network.proxy.port': '',
134134

src/common/types/app_setting.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,9 @@ declare global {
596596
'search.isFocusSearchBox': boolean
597597

598598
/**
599-
* 是否启用代理
599+
* 代理类型
600600
*/
601-
'network.proxy.enable': boolean
601+
'network.proxy.type': 'disable' | 'system' | 'custom'
602602

603603
/**
604604
* 代理服务器地址

src/common/utils/migrateSetting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export default (setting: any): Partial<LX.AppSetting> => {
113113
setting['search.isShowHistorySearch'] = setting.search?.isShowHistorySearch
114114
setting['search.isFocusSearchBox'] = setting.search?.isFocusSearchBox
115115

116-
setting['network.proxy.enable'] = setting.network?.proxy?.enable
116+
// setting['network.proxy.enable'] = setting.network?.proxy?.enable
117117
setting['network.proxy.host'] = setting.network?.proxy?.host
118118
setting['network.proxy.port'] = setting.network?.proxy?.port
119119

src/lang/en-us.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@
472472
"setting__list_scroll": "Remember position of scroll bar of playlist (Only valid for \"Your Library\" page)",
473473
"setting__list_source": "Show which music streaming service the song is from (Only valid for \"Your Library\" page)",
474474
"setting__network": "Network",
475+
"setting__network_proxy_type_disable": "Do not use proxy",
476+
"setting__network_proxy_type_system": "Use system proxy",
477+
"setting__network_proxy_type_custom": "Custom proxy",
475478
"setting__network_proxy_host": "Host",
476479
"setting__network_proxy_password": "Password",
477480
"setting__network_proxy_port": "Port",

src/lang/zh-cn.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@
472472
"setting__list_scroll": "记住播放列表滚动条位置(仅对「我的列表」有效)",
473473
"setting__list_source": "显示歌曲来源平台(仅对「我的列表」有效)",
474474
"setting__network": "网络设置",
475+
"setting__network_proxy_type_disable": "不使用代理",
476+
"setting__network_proxy_type_system": "使用系统代理",
477+
"setting__network_proxy_type_custom": "自定义代理",
475478
"setting__network_proxy_host": "主机",
476479
"setting__network_proxy_password": "密码",
477480
"setting__network_proxy_port": "端口",

src/lang/zh-tw.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@
472472
"setting__list_scroll": "記住播放清單滾動條位置(僅對「我的清單」有效)",
473473
"setting__list_source": "顯示歌曲來自哪個音樂串流平台(僅對「我的清單」有效)",
474474
"setting__network": "網路設定",
475+
"setting__network_proxy_type_disable": "不使用代理",
476+
"setting__network_proxy_type_system": "使用系統代理",
477+
"setting__network_proxy_type_custom": "自訂代理",
475478
"setting__network_proxy_host": "主機",
476479
"setting__network_proxy_password": "密碼",
477480
"setting__network_proxy_port": "",

src/main/modules/userApi/main.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,41 @@ const denyEvents = [
1919
'media-started-playing',
2020
] as const
2121

22-
2322
export const getProxy = () => {
24-
if (global.lx.appSetting['network.proxy.enable'] && global.lx.appSetting['network.proxy.host']) {
23+
const envProxyStr = envParams.cmdParams['proxy-server']
24+
if (envProxyStr && typeof envProxyStr == 'string') {
25+
const [host, port = ''] = envProxyStr.split(':')
2526
return {
26-
host: global.lx.appSetting['network.proxy.host'],
27-
port: global.lx.appSetting['network.proxy.port'],
27+
host,
28+
port: parseInt(port || '80'),
2829
}
2930
}
30-
const envProxy = envParams.cmdParams['proxy-server']
31-
if (envProxy) {
32-
if (envProxy && typeof envProxy == 'string') {
33-
const [host, port = ''] = envProxy.split(':')
31+
32+
switch (global.lx.appSetting['network.proxy.type']) {
33+
case 'custom':
34+
const custom = {
35+
enable: global.lx.appSetting['network.proxy.type'] === 'custom',
36+
host: global.lx.appSetting['network.proxy.host'],
37+
port: global.lx.appSetting['network.proxy.port'],
38+
}
39+
return custom.enable && custom.host
40+
? {
41+
host: custom.host,
42+
port: parseInt(custom.port || '80'),
43+
}
44+
: {
45+
host: '',
46+
port: '',
47+
}
48+
default:
3449
return {
35-
host,
36-
port,
50+
host: '',
51+
port: '',
3752
}
38-
}
39-
}
40-
return {
41-
host: '',
42-
port: '',
4353
}
4454
}
4555
const handleUpdateProxy = (keys: Array<keyof LX.AppSetting>) => {
46-
if (keys.includes('network.proxy.enable') || (global.lx.appSetting['network.proxy.enable'] && keys.some(k => k.startsWith('network.proxy.')))) {
56+
if (keys.some(k => k.startsWith('network.proxy.'))) {
4757
sendEvent(USER_API_RENDERER_EVENT_NAME.proxyUpdate, getProxy())
4858
}
4959
}

src/main/modules/winMain/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export default () => {
108108
setProgressBar(-1, { mode: 'none' })
109109
}
110110
}
111-
if (keys.includes('network.proxy.enable') || (global.lx.appSetting['network.proxy.enable'] && keys.some(k => k.includes('network.proxy.')))) {
111+
if (keys.some(k => k.includes('network.proxy.'))) {
112112
setProxy()
113113
}
114114
})

src/main/modules/winMain/main.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ export const createWindow = () => {
6565

6666
const { shouldUseDarkColors, theme } = global.lx.theme
6767
const ses = session.fromPartition('persist:win-main')
68-
const proxy = getProxy()
69-
if (proxy) {
70-
void ses.setProxy({
71-
proxyRules: `http://${proxy.host}:${proxy.port}`,
72-
})
73-
}
7468

7569
/**
7670
* Initial window options
@@ -108,11 +102,12 @@ export const createWindow = () => {
108102
}
109103
browserWindow = new BrowserWindow(options)
110104

105+
setProxy()
106+
winEvent()
107+
111108
const winURL = process.env.NODE_ENV !== 'production' ? 'http://localhost:9080' : `file://${path.join(encodePath(__dirname), 'index.html')}`
112109
void browserWindow.loadURL(winURL + `?os=${getPlatform()}&dt=${global.envParams.cmdParams.dt}&dark=${shouldUseDarkColors}&theme=${encodeURIComponent(JSON.stringify(theme))}`)
113110

114-
winEvent()
115-
116111
if (global.envParams.cmdParams.odt) handleOpenDevTools(browserWindow.webContents)
117112

118113
// global.lx.mainWindowClosed = false
@@ -133,19 +128,27 @@ export const closeWindow = () => {
133128

134129
export const setProxy = () => {
135130
if (!browserWindow) return
136-
const proxy = getProxy()
137-
if (proxy) {
138-
void browserWindow.webContents.session.setProxy({
139-
proxyRules: `http://${proxy.host}:${proxy.port}`,
140-
})
141-
} else {
142-
void browserWindow.webContents.session.setProxy({
143-
proxyRules: '',
144-
})
131+
switch (global.lx.appSetting['network.proxy.type']) {
132+
case 'system':
133+
void browserWindow.webContents.session.setProxy({
134+
mode: 'system',
135+
})
136+
break
137+
case 'disable':
138+
void browserWindow.webContents.session.setProxy({
139+
mode: 'direct',
140+
})
141+
break
142+
default:
143+
const proxy = getProxy()
144+
if (!proxy) break
145+
void browserWindow.webContents.session.setProxy({
146+
proxyRules: `http://${proxy.host}:${proxy.port}`,
147+
})
148+
break
145149
}
146150
}
147151

148-
149152
export const sendEvent = <T = any>(name: string, params?: T) => {
150153
if (!browserWindow) return
151154
mainSend(browserWindow, name, params)

src/main/utils/index.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -295,30 +295,25 @@ export const setPowerSaveBlocker = (enabled: boolean) => {
295295
}
296296
}
297297

298-
299-
let envProxy: null | { host: string, port: number } = null
300298
export const getProxy = () => {
301-
if (global.lx.appSetting['network.proxy.enable'] && global.lx.appSetting['network.proxy.host']) {
299+
const envProxyStr = envParams.cmdParams['proxy-server']
300+
if (envProxyStr && typeof envProxyStr == 'string') {
301+
const [host, port = ''] = envProxyStr.split(':')
302302
return {
303-
host: global.lx.appSetting['network.proxy.host'],
304-
port: parseInt(global.lx.appSetting['network.proxy.port'] || '80'),
305-
}
306-
}
307-
if (envProxy) {
308-
return {
309-
host: envProxy.host,
310-
port: envProxy.port,
311-
}
312-
} else {
313-
const envProxyStr = envParams.cmdParams['proxy-server']
314-
if (envProxyStr && typeof envProxyStr == 'string') {
315-
const [host, port = ''] = envProxyStr.split(':')
316-
return envProxy = {
317-
host,
318-
port: parseInt(port || '80'),
319-
}
303+
host,
304+
port: parseInt(port || '80'),
320305
}
321306
}
322307

323-
return null
308+
const custom = {
309+
enable: global.lx.appSetting['network.proxy.type'] === 'custom',
310+
host: global.lx.appSetting['network.proxy.host'],
311+
port: global.lx.appSetting['network.proxy.port'],
312+
}
313+
return custom.enable && custom.host
314+
? {
315+
host: custom.host,
316+
port: parseInt(custom.port || '80'),
317+
}
318+
: null
324319
}

0 commit comments

Comments
 (0)