修复网络代理设置没有对自定义源的网络请求生效的问题(#1814)
parent
b14883c9af
commit
2c1b4acfcc
|
@ -18,6 +18,7 @@
|
|||
- 修复mg歌单搜索(@helloplhm-qwq)
|
||||
- 修复kg最新评论无法获取的问题(@helloplhm-qwq)
|
||||
- 修复更新超时弹窗在非更新阶段意外弹出的问题(#1797)
|
||||
- 修复网络代理设置没有对自定义源的网络请求生效的问题(#1814)
|
||||
|
||||
### 其他
|
||||
|
||||
|
|
|
@ -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', () => {
|
||||
|
@ -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(),
|
||||
|
|
|
@ -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
|
||||
})
|
||||
|
|
|
@ -5,6 +5,8 @@ const names = {
|
|||
response: '',
|
||||
openDevTools: '',
|
||||
showUpdateAlert: '',
|
||||
getProxy: '',
|
||||
proxyUpdate: '',
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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) => {
|
||||
|
|
Loading…
Reference in New Issue