现在如果在设置或者启动参数配置了代理服务,那么应用内的图片、音频加载,歌曲下载也将走代理

This commit is contained in:
lyswhut
2024-06-11 19:41:10 +08:00
parent 1fefb2bcaf
commit bd4dbb55e4
13 changed files with 94 additions and 55 deletions
+4 -1
View File
@@ -1,7 +1,7 @@
import initRendererEvent, { handleKeyDown, hotKeyConfigUpdate } from './rendererEvent'
import { APP_EVENT_NAMES } from '@common/constants'
import { createWindow, minimize, setProgressBar, setThumbarButtons, toggleHide, toggleMinimize } from './main'
import { createWindow, minimize, setProgressBar, setProxy, setThumbarButtons, toggleHide, toggleMinimize } from './main'
import initUpdate from './autoUpdate'
import { HOTKEY_COMMON } from '@common/hotKey'
import { quitApp } from '@main/app'
@@ -108,6 +108,9 @@ export default () => {
setProgressBar(-1, { mode: 'none' })
}
}
if (keys.includes('network.proxy.enable') || (global.lx.appSetting['network.proxy.enable'] && keys.some(k => k.includes('network.proxy.')))) {
setProxy()
}
})
}
+22 -1
View File
@@ -2,7 +2,7 @@ import { BrowserWindow, dialog, session } from 'electron'
import path from 'node:path'
import { createTaskBarButtons, getWindowSizeInfo } from './utils'
import { isLinux, isWin } from '@common/utils'
import { openDevTools as handleOpenDevTools } from '@main/utils'
import { getProxy, openDevTools as handleOpenDevTools } from '@main/utils'
import { mainSend } from '@common/mainIpc'
import { sendFocus, sendTaskbarButtonClick } from './rendererEvent'
import { encodePath } from '@common/utils/electron'
@@ -65,6 +65,12 @@ export const createWindow = () => {
const { shouldUseDarkColors, theme } = global.lx.theme
const ses = session.fromPartition('persist:win-main')
const proxy = getProxy()
if (proxy) {
void ses.setProxy({
proxyRules: `http://${proxy.host}:${proxy.port}`,
})
}
/**
* Initial window options
@@ -124,6 +130,21 @@ export const closeWindow = () => {
browserWindow.close()
}
export const setProxy = () => {
if (!browserWindow) return
const proxy = getProxy()
if (proxy) {
void browserWindow.webContents.session.setProxy({
proxyRules: `http://${proxy.host}:${proxy.port}`,
})
} else {
void browserWindow.webContents.session.setProxy({
proxyRules: '',
})
}
}
export const sendEvent = <T = any>(name: string, params?: T) => {
if (!browserWindow) return
mainSend(browserWindow, name, params)
+28
View File
@@ -294,3 +294,31 @@ export const setPowerSaveBlocker = (enabled: boolean) => {
powerSaveBlockerId = null
}
}
let envProxy: null | { host: string, port: number } = null
export const getProxy = () => {
if (global.lx.appSetting['network.proxy.enable'] && global.lx.appSetting['network.proxy.host']) {
return {
host: global.lx.appSetting['network.proxy.host'],
port: parseInt(global.lx.appSetting['network.proxy.port'] || '80'),
}
}
if (envProxy) {
return {
host: envProxy.host,
port: envProxy.port,
}
} else {
const envProxyStr = envParams.cmdParams['proxy-server']
if (envProxyStr && typeof envProxyStr == 'string') {
const [host, port = ''] = envProxyStr.split(':')
return envProxy = {
host,
port: parseInt(port || '80'),
}
}
}
return null
}
+3 -35
View File
@@ -2,6 +2,7 @@ import needle, { type NeedleHttpVerbs, type NeedleOptions, type BodyData, type N
// import progress from 'request-progress'
import { httpOverHttp, httpsOverHttp } from 'tunnel'
import { type ClientRequest } from 'node:http'
import { getProxy } from './index'
// import fs from 'fs'
export const requestMsg = {
@@ -15,42 +16,9 @@ export const requestMsg = {
const httpsRxp = /^https:/
let envProxy: null | { host: string, port: string } = null
const getRequestAgent = (url: string) => {
if (envProxy == null) {
if (global.envParams.cmdParams['proxy-server'] && typeof global.envParams.cmdParams['proxy-server'] == 'string') {
const [host, port = ''] = global.envParams.cmdParams['proxy-server'].split(':')
envProxy = {
host,
port,
}
}
}
const proxy = {
enable: global.lx.appSetting['network.proxy.enable'],
host: global.lx.appSetting['network.proxy.host'],
port: global.lx.appSetting['network.proxy.port'],
envProxy,
}
let options
if (global.lx.appSetting['network.proxy.enable'] && proxy.host) {
options = {
proxy: {
host: proxy.host,
port: parseInt(proxy.port || '80'),
},
}
} else if (proxy.envProxy) {
options = {
proxy: {
host: proxy.envProxy.host,
port: parseInt(proxy.envProxy.port || '80'),
},
}
}
return options ? (httpsRxp.test(url) ? httpsOverHttp : httpOverHttp)(options) : undefined
const proxy = getProxy()
return proxy ? (httpsRxp.test(url) ? httpsOverHttp : httpOverHttp)({ proxy }) : undefined
}
export interface RequestOptions extends NeedleOptions {