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

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
+10 -3
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) {
+2 -2
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 => {
+1 -1
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
+3 -3
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
}
}
+2 -2
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)