refactor: 调整加密请求数据的代码结构;添加一些注释

This commit is contained in:
overwriter 2024-07-13 08:46:45 +08:00
parent 656a69e07e
commit 8de3e9fa66

View File

@ -85,17 +85,21 @@ const createRequest = (method, uri, data = {}, options) => {
}
// console.log(options.cookie, headers['Cookie'])
let url = ''
let encryptData = ''
// 目前任意uri都支持三种加密方式
if (options.crypto === 'weapi') {
let url = '',
encryptData = '',
crypto = options.crypto
// 根据加密方式加密请求数据目前任意uri都支持四种加密方式
switch (crypto) {
case 'weapi':
headers['Referer'] = 'https://music.163.com'
headers['User-Agent'] = options.ua || chooseUserAgent('pc')
let csrfToken = (headers['Cookie'] || '').match(/_csrf=([^(;|$)]+)/)
data.csrf_token = csrfToken ? csrfToken[1] : ''
let csrfTokenList = (headers['Cookie'] || '').match(/_csrf=([^(;|$)]+)/)
data.csrf_token = csrfTokenList ? csrfTokenList[1] : ''
encryptData = encrypt.weapi(data)
url = APP_CONF.domain + '/weapi/' + uri.substr(5)
} else if (options.crypto === 'linuxapi') {
break
case 'linuxapi':
headers['User-Agent'] =
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
encryptData = encrypt.linuxapi({
@ -104,11 +108,11 @@ const createRequest = (method, uri, data = {}, options) => {
params: data,
})
url = 'https://music.163.com/api/linux/forward'
} else if (
options.crypto === 'eapi' ||
options.crypto === 'api' ||
options.crypto === ''
) {
break
case 'eapi':
case 'api':
case '':
// 两种加密方式都应生成客户端的cookie
const cookie = options.cookie || {}
const csrfToken = cookie['__csrf'] || ''
@ -136,29 +140,36 @@ const createRequest = (method, uri, data = {}, options) => {
)
.join('; ')
let eapiEncrypt = () => {
let eapi = () => {
// 使用eapi加密
data.header = header
encryptData = encrypt.eapi(uri, data)
url = APP_CONF.apiDomain + '/eapi/' + uri.substr(5)
data.e_r = data.e_r != undefined ? data.e_r : APP_CONF.encryptResponse // 用于加密eapi接口返回值
data.e_r = data.e_r != undefined ? data.e_r : APP_CONF.encryptResponse // 用于加密接口返回值
}
if (options.crypto === 'eapi') {
eapiEncrypt()
} else if (options.crypto === 'api') {
let api = () => {
// 不使用任何加密
url = APP_CONF.apiDomain + uri
encryptData = data
} else if (options.crypto === '') {
}
if (crypto === 'eapi') {
eapi()
} else if (crypto === 'api') {
api()
} else if (crypto === '') {
// 加密方式为空,以配置文件的加密方式为准
if (APP_CONF.encrypt) {
eapiEncrypt()
eapi()
} else {
url = APP_CONF.apiDomain + uri
encryptData = data
api()
}
}
} else {
break
default:
// 未知的加密方式
console.log('[ERR]', 'Unknown Crypto:', options.crypto)
console.log('[ERR]', 'Unknown Crypto:', crypto)
break
}
const answer = { status: 500, body: {}, cookie: [] }
// console.log(headers, 'headers')
@ -216,7 +227,7 @@ const createRequest = (method, uri, data = {}, options) => {
)
try {
if (data.e_r) {
// eapi接口返回值被加密
// eapi接口返回值被加密,需要解密
answer.body = encrypt.eapiResDecrypt(
body.toString('hex').toUpperCase(),
)