Skip to content

Commit

Permalink
修复网络代理设置没有对自定义源的网络请求生效的问题(#1814
Browse files Browse the repository at this point in the history
  • Loading branch information
lyswhut committed Mar 14, 2024
1 parent b14883c commit 2c1b4ac
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
1 change: 1 addition & 0 deletions publish/changeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- 修复mg歌单搜索(@helloplhm-qwq
- 修复kg最新评论无法获取的问题(@helloplhm-qwq
- 修复更新超时弹窗在非更新阶段意外弹出的问题(#1797
- 修复网络代理设置没有对自定义源的网络请求生效的问题(#1814

### 其他

Expand Down
33 changes: 32 additions & 1 deletion src/main/modules/userApi/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof LX.AppSetting>) => {
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', () => {
Expand Down Expand Up @@ -93,14 +122,16 @@ 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'))
// global.modules.userApiWindow.webContents.openDevTools()
}

export const closeWindow = async() => {
global.lx.event_app.off('updated_config', handleUpdateProxy)
if (!browserWindow) return
await Promise.all([
browserWindow.webContents.session.clearAuthCache(),
Expand Down
31 changes: 27 additions & 4 deletions src/main/modules/userApi/renderer/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
})
2 changes: 2 additions & 0 deletions src/main/modules/userApi/rendererEvent/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const names = {
response: '',
openDevTools: '',
showUpdateAlert: '',
getProxy: '',
proxyUpdate: '',
}


Expand Down
6 changes: 5 additions & 1 deletion src/main/modules/userApi/rendererEvent/rendererEvent.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 2c1b4ac

Please sign in to comment.