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

pull/1940/head
lyswhut 2024-06-11 19:41:10 +08:00
parent 1fefb2bcaf
commit bd4dbb55e4
13 changed files with 94 additions and 55 deletions

View File

@ -6,6 +6,7 @@
### 变更
- 简化了应用退出行为,据测试,现在 linux 下若启用了托盘dock 右键菜单的 退出、关闭所有 之类的功能将不再退出程序,需改用托盘的退出按钮退出程序
- 现在如果在设置或者启动参数配置了代理服务,那么应用内的图片、音频加载,歌曲下载也将走代理
### 其他

View File

@ -1,14 +1,21 @@
const http = require('http')
const https = require('https')
const fs = require('fs')
const { httpOverHttp, httpsOverHttp } = require('tunnel')
const sendRequest = (url) => {
const httpsRxp = /^https:/
const getRequestAgent = (url, proxy) => {
return proxy ? (httpsRxp.test(url) ? httpsOverHttp : httpOverHttp)({ proxy }) : undefined
}
const sendRequest = (url, proxy) => {
const urlParse = new URL(url)
const httpOptions = {
method: 'get',
host: urlParse.hostname,
port: urlParse.port,
path: urlParse.pathname + urlParse.search,
agent: getRequestAgent(url, proxy),
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
},
@ -20,9 +27,9 @@ const sendRequest = (url) => {
: http.request(httpOptions)
}
module.exports = (url, filePath) => {
module.exports = (url, filePath, proxy) => {
return new Promise((resolve) => {
sendRequest(url)
sendRequest(url, proxy)
.on('response', response => {
// console.log(response.statusCode)
if (response.statusCode !== 200 && response.statusCode != 206) {

View File

@ -57,7 +57,7 @@ const writeMeta = async(filePath, meta, picPath) => {
})
}
module.exports = (filePath, meta) => {
module.exports = (filePath, meta, proxy) => {
if (!meta.APIC) return writeMeta(filePath, meta)
let picUrl = meta.APIC
delete meta.APIC
@ -68,7 +68,7 @@ module.exports = (filePath, meta) => {
let picPath = filePath.replace(/\.flac$/, '') + (ext ? ext.replace(extReg, '$1') : '.jpg')
if (picUrl.includes('music.126.net')) picUrl += `${picUrl.includes('?') ? '&' : '?'}param=500y500`
download(picUrl, picPath).then(success => {
download(picUrl, picPath, proxy).then(success => {
if (success) {
writeMeta(filePath, meta, picPath).finally(() => {
fs.unlink(picPath, err => {

View File

@ -5,4 +5,4 @@ export interface MusicMeta {
APIC: string | null
lyrics: string | null
}
export function setMeta(filePath: string, meta: MusicMeta): void
export function setMeta(filePath: string, meta: MusicMeta, proxy?: { host: string, port: number }): void

View File

@ -2,13 +2,13 @@ const path = require('path')
const mp3Meta = require('./mp3Meta')
const flacMeta = require('./flacMeta')
exports.setMeta = (filePath, meta) => {
exports.setMeta = (filePath, meta, proxy) => {
switch (path.extname(filePath)) {
case '.mp3':
mp3Meta(filePath, meta)
mp3Meta(filePath, meta, proxy)
break
case '.flac':
flacMeta(filePath, meta)
flacMeta(filePath, meta, proxy)
break
}
}

View File

@ -15,7 +15,7 @@ const handleWriteMeta = (meta, filePath) => {
NodeID3.write(meta, filePath)
}
module.exports = (filePath, meta) => {
module.exports = (filePath, meta, proxy) => {
if (!meta.APIC) return handleWriteMeta(meta, filePath)
if (!/^http/.test(meta.APIC)) {
delete meta.APIC
@ -26,7 +26,7 @@ module.exports = (filePath, meta) => {
let picUrl = meta.APIC
if (picUrl.includes('music.126.net')) picUrl += `${picUrl.includes('?') ? '&' : '?'}param=500y500`
download(picUrl, picPath).then(success => {
download(picUrl, picPath, proxy).then(success => {
if (success) {
meta.APIC = picPath
handleWriteMeta(meta, filePath)

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()
}
})
}

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)

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
}

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 {

View File

@ -15,6 +15,7 @@ import { qualityList } from '..'
import { proxyCallback } from '@renderer/worker/utils'
import { arrPush, arrUnshift, joinPath } from '@renderer/utils'
import { DOWNLOAD_STATUS } from '@common/constants'
import { proxy } from '../index'
const waitingUpdateTasks = new Map<string, LX.Download.ListItem>()
let timer: NodeJS.Timeout | null = null
@ -131,6 +132,15 @@ const setStatus = (downloadInfo: LX.Download.ListItem, status: LX.Download.Downl
// 修复 1.1.x版本 酷狗源歌词格式
const fixKgLyric = (lrc: string) => /\[00:\d\d:\d\d.\d+\]/.test(lrc) ? lrc.replace(/(?:\[00:(\d\d:\d\d.\d+\]))/gm, '[$1') : lrc
const getProxy = () => {
return proxy.enable && proxy.host ? {
host: proxy.host,
port: parseInt(proxy.port || '80'),
} : proxy.envProxy ? {
host: proxy.envProxy.host,
port: parseInt(proxy.envProxy.port || '80'),
} : undefined
}
/**
* meta
* @param downloadInfo
@ -170,7 +180,7 @@ const saveMeta = (downloadInfo: LX.Download.ListItem) => {
artist: downloadInfo.metadata.musicInfo.singer,
album: downloadInfo.metadata.musicInfo.meta.albumName,
APIC: imgUrl,
}, lrcData)
}, lrcData, getProxy())
})
}
@ -282,7 +292,7 @@ const handleStartTask = async(downloadInfo: LX.Download.ListItem) => {
default:
break
}
}))
}), getProxy())
}
const startTask = async(downloadInfo: LX.Download.ListItem) => {
setStatus(downloadInfo, DOWNLOAD_STATUS.RUN)

View File

@ -1,8 +1,8 @@
import { setMeta } from '@common/utils/musicMeta'
import { mergeLyrics } from './lrcTool'
export const writeMeta = (filePath: string, meta: Omit<LX.Music.MusicFileMeta, 'lyrics'>, lyricData: { lrc: string, tlrc: string | null, rlrc: string | null }) => {
setMeta(filePath, { ...meta, lyrics: mergeLyrics(lyricData.lrc, lyricData.tlrc, lyricData.rlrc) || null })
export const writeMeta = (filePath: string, meta: Omit<LX.Music.MusicFileMeta, 'lyrics'>, lyricData: { lrc: string, tlrc: string | null, rlrc: string | null }, proxy?: { host: string, port: number }) => {
setMeta(filePath, { ...meta, lyrics: mergeLyrics(lyricData.lrc, lyricData.tlrc, lyricData.rlrc) || null }, proxy)
}
export { saveLrc } from './utils'

View File

@ -55,7 +55,7 @@ export const createDownloadTasks = (
// }
}
const createTask = async(downloadInfo: LX.Download.ListItem, savePath: string, skipExistFile: boolean) => {
const createTask = async(downloadInfo: LX.Download.ListItem, savePath: string, skipExistFile: boolean, proxy?: { host: string, port: number }) => {
// console.log('createTask', downloadInfo, savePath)
// 开始任务
/* commit('onStart', downloadInfo)
@ -103,6 +103,7 @@ const createTask = async(downloadInfo: LX.Download.ListItem, savePath: string, s
path: savePath,
fileName: downloadInfo.metadata.fileName,
method: 'get',
proxy,
onCompleted() {
// if (downloadInfo.progress.progress != '100.00') {
// delete.get(downloadInfo.id)?
@ -241,7 +242,7 @@ export const updateUrl = (id: string, url: string) => {
})
}
export const startTask = async(downloadInfo: LX.Download.ListItem, savePath: string, skipExistFile: boolean, callback: (action: LX.Download.DownloadTaskActions) => void) => {
export const startTask = async(downloadInfo: LX.Download.ListItem, savePath: string, skipExistFile: boolean, callback: (action: LX.Download.DownloadTaskActions) => void, proxy?: { host: string, port: number }) => {
await pauseTask(downloadInfo.id)
tasks.set(downloadInfo.id, downloadInfo)
@ -276,7 +277,7 @@ export const startTask = async(downloadInfo: LX.Download.ListItem, savePath: str
// await dispatch('startTask')
}
} else {
await createTask(downloadInfo, savePath, skipExistFile)
await createTask(downloadInfo, savePath, skipExistFile, proxy)
// await dispatch('handleStartTask', downloadInfo)
}
}