From 2c1b4acfcc1cdd8058412ff155e5a394a95532b8 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Thu, 14 Mar 2024 18:23:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BD=91=E7=BB=9C=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E8=AE=BE=E7=BD=AE=E6=B2=A1=E6=9C=89=E5=AF=B9=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=BA=90=E7=9A=84=E7=BD=91=E7=BB=9C=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=94=9F=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=88?= =?UTF-8?q?#1814=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 1 + src/main/modules/userApi/main.ts | 33 ++++++++++++++++++- src/main/modules/userApi/renderer/preload.js | 31 ++++++++++++++--- .../modules/userApi/rendererEvent/name.js | 2 ++ .../userApi/rendererEvent/rendererEvent.ts | 6 +++- 5 files changed, 67 insertions(+), 6 deletions(-) diff --git a/publish/changeLog.md b/publish/changeLog.md index 52437b385f..376fd7472d 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -18,6 +18,7 @@ - 修复mg歌单搜索(@helloplhm-qwq) - 修复kg最新评论无法获取的问题(@helloplhm-qwq) - 修复更新超时弹窗在非更新阶段意外弹出的问题(#1797) +- 修复网络代理设置没有对自定义源的网络请求生效的问题(#1814) ### 其他 diff --git a/src/main/modules/userApi/main.ts b/src/main/modules/userApi/main.ts index 50cb6834a0..586efcd856 100644 --- a/src/main/modules/userApi/main.ts +++ b/src/main/modules/userApi/main.ts @@ -20,6 +20,35 @@ const denyEvents = [ 'media-started-playing', ] as const + +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: global.lx.appSetting['network.proxy.port'], + } + } + const envProxy = envParams.cmdParams['proxy-server'] + if (envProxy) { + if (envProxy && typeof envProxy == 'string') { + const [host, port = ''] = envProxy.split(':') + return { + host, + port, + } + } + } + return { + host: '', + port: '', + } +} +const handleUpdateProxy = (keys: Array) => { + if (keys.includes('network.proxy.enable') || (global.lx.appSetting['network.proxy.enable'] && keys.some(k => k.startsWith('network.proxy.')))) { + sendEvent(USER_API_RENDERER_EVENT_NAME.proxyUpdate, getProxy()) + } +} + const winEvent = () => { if (!browserWindow) return browserWindow.on('closed', () => { @@ -93,7 +122,8 @@ export const createWindow = async(userApi: LX.UserApi.UserApiInfo) => { await browserWindow.loadURL('data:text/html;charset=UTF-8,' + encodeURIComponent(html)) browserWindow.on('ready-to-show', async() => { - sendEvent(USER_API_RENDERER_EVENT_NAME.initEnv, { ...userApi, script: await getScript(userApi.id) }) + global.lx.event_app.on('updated_config', handleUpdateProxy) + sendEvent(USER_API_RENDERER_EVENT_NAME.initEnv, { ...userApi, script: await getScript(userApi.id), proxy: getProxy() }) }) // global.modules.userApiWindow.loadFile(join(dir, 'renderer/user-api.html')) @@ -101,6 +131,7 @@ export const createWindow = async(userApi: LX.UserApi.UserApiInfo) => { } export const closeWindow = async() => { + global.lx.event_app.off('updated_config', handleUpdateProxy) if (!browserWindow) return await Promise.all([ browserWindow.webContents.session.clearAuthCache(), diff --git a/src/main/modules/userApi/renderer/preload.js b/src/main/modules/userApi/renderer/preload.js index 3b6a2d0c66..b53ae92efb 100644 --- a/src/main/modules/userApi/renderer/preload.js +++ b/src/main/modules/userApi/renderer/preload.js @@ -3,16 +3,18 @@ import needle from 'needle' import zlib from 'zlib' import { createCipheriv, publicEncrypt, constants, randomBytes, createHash } from 'crypto' import USER_API_RENDERER_EVENT_NAME from '../rendererEvent/name' +import { httpOverHttp, httpsOverHttp } from 'tunnel' -for (const key of Object.keys(process.env)) { - if (/^(?:http_proxy|https_proxy|NO_PROXY)$/i.test(key)) delete process.env[key] -} const sendMessage = (action, data, status, message) => { ipcRenderer.send(action, { data, status, message }) } let isInitedApi = false +const proxy = { + host: '', + port: '', +} let isShowedUpdateAlert = false const EVENT_NAMES = { request: 'request', @@ -42,6 +44,16 @@ const supportActions = { local: ['musicUrl', 'lyric', 'pic'], } +const httpsRxp = /^https:/ +const getRequestAgent = url => { + return proxy.host ? (httpsRxp.test(url) ? httpsOverHttp : httpOverHttp)({ + proxy: { + host: proxy.host, + port: proxy.port, + }, + }) : undefined +} + const verifyLyricInfo = (info) => { if (typeof info != 'object' || typeof info.lyric != 'string') throw new Error('failed') if (info.lyric.length > 51200) throw new Error('failed') @@ -174,10 +186,16 @@ const onError = (errorMessage) => { } const initEnv = (userApi) => { + proxy.host = userApi.proxy.host + proxy.port = userApi.proxy.port + contextBridge.exposeInMainWorld('lx', { EVENT_NAMES, request(url, { method = 'get', timeout, headers, body, form, formData }, callback) { - let options = { headers } + let options = { + headers, + agent: getRequestAgent(url), + } let data if (body) { data = body @@ -352,3 +370,8 @@ window.addEventListener('unhandledrejection', (event) => { ipcRenderer.on(USER_API_RENDERER_EVENT_NAME.initEnv, (event, data) => { initEnv(data) }) + +ipcRenderer.on(USER_API_RENDERER_EVENT_NAME.proxyUpdate, (event, data) => { + proxy.host = data.host + proxy.port = data.port +}) diff --git a/src/main/modules/userApi/rendererEvent/name.js b/src/main/modules/userApi/rendererEvent/name.js index 542cc2e2d9..3e5ca8b12f 100644 --- a/src/main/modules/userApi/rendererEvent/name.js +++ b/src/main/modules/userApi/rendererEvent/name.js @@ -5,6 +5,8 @@ const names = { response: '', openDevTools: '', showUpdateAlert: '', + getProxy: '', + proxyUpdate: '', } diff --git a/src/main/modules/userApi/rendererEvent/rendererEvent.ts b/src/main/modules/userApi/rendererEvent/rendererEvent.ts index fb10be95b2..098cc77ae7 100644 --- a/src/main/modules/userApi/rendererEvent/rendererEvent.ts +++ b/src/main/modules/userApi/rendererEvent/rendererEvent.ts @@ -1,7 +1,7 @@ import { mainOn } from '@common/mainIpc' import USER_API_RENDERER_EVENT_NAME from './name' -import { createWindow, openDevTools, sendEvent } from '../main' +import { createWindow, getProxy, openDevTools, sendEvent } from '../main' import { getUserApis } from '../utils' import { sendShowUpdateAlert, sendStatusChange } from '@main/modules/winMain' @@ -71,10 +71,14 @@ export const init = () => { updateUrl: data.updateUrl, }) } + const handleGetProxy = () => { + sendEvent(USER_API_RENDERER_EVENT_NAME.proxyUpdate, getProxy()) + } mainOn(USER_API_RENDERER_EVENT_NAME.init, handleInit) mainOn(USER_API_RENDERER_EVENT_NAME.response, handleResponse) mainOn(USER_API_RENDERER_EVENT_NAME.openDevTools, handleOpenDevTools) mainOn(USER_API_RENDERER_EVENT_NAME.showUpdateAlert, handleShowUpdateAlert) + mainOn(USER_API_RENDERER_EVENT_NAME.getProxy, handleGetProxy) } export const clearRequestTimeout = (requestKey: string) => {