mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2025-10-24 08:03:12 +00:00
refactor: 调整加密请求数据的代码结构;添加一些注释
This commit is contained in:
parent
656a69e07e
commit
8de3e9fa66
@ -85,17 +85,21 @@ const createRequest = (method, uri, data = {}, options) => {
|
|||||||
}
|
}
|
||||||
// console.log(options.cookie, headers['Cookie'])
|
// console.log(options.cookie, headers['Cookie'])
|
||||||
|
|
||||||
let url = ''
|
let url = '',
|
||||||
let encryptData = ''
|
encryptData = '',
|
||||||
// 目前任意uri都支持三种加密方式
|
crypto = options.crypto
|
||||||
if (options.crypto === 'weapi') {
|
// 根据加密方式加密请求数据;目前任意uri都支持四种加密方式
|
||||||
|
switch (crypto) {
|
||||||
|
case 'weapi':
|
||||||
headers['Referer'] = 'https://music.163.com'
|
headers['Referer'] = 'https://music.163.com'
|
||||||
headers['User-Agent'] = options.ua || chooseUserAgent('pc')
|
headers['User-Agent'] = options.ua || chooseUserAgent('pc')
|
||||||
let csrfToken = (headers['Cookie'] || '').match(/_csrf=([^(;|$)]+)/)
|
let csrfTokenList = (headers['Cookie'] || '').match(/_csrf=([^(;|$)]+)/)
|
||||||
data.csrf_token = csrfToken ? csrfToken[1] : ''
|
data.csrf_token = csrfTokenList ? csrfTokenList[1] : ''
|
||||||
encryptData = encrypt.weapi(data)
|
encryptData = encrypt.weapi(data)
|
||||||
url = APP_CONF.domain + '/weapi/' + uri.substr(5)
|
url = APP_CONF.domain + '/weapi/' + uri.substr(5)
|
||||||
} else if (options.crypto === 'linuxapi') {
|
break
|
||||||
|
|
||||||
|
case 'linuxapi':
|
||||||
headers['User-Agent'] =
|
headers['User-Agent'] =
|
||||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
|
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
|
||||||
encryptData = encrypt.linuxapi({
|
encryptData = encrypt.linuxapi({
|
||||||
@ -104,11 +108,11 @@ const createRequest = (method, uri, data = {}, options) => {
|
|||||||
params: data,
|
params: data,
|
||||||
})
|
})
|
||||||
url = 'https://music.163.com/api/linux/forward'
|
url = 'https://music.163.com/api/linux/forward'
|
||||||
} else if (
|
break
|
||||||
options.crypto === 'eapi' ||
|
|
||||||
options.crypto === 'api' ||
|
case 'eapi':
|
||||||
options.crypto === ''
|
case 'api':
|
||||||
) {
|
case '':
|
||||||
// 两种加密方式,都应生成客户端的cookie
|
// 两种加密方式,都应生成客户端的cookie
|
||||||
const cookie = options.cookie || {}
|
const cookie = options.cookie || {}
|
||||||
const csrfToken = cookie['__csrf'] || ''
|
const csrfToken = cookie['__csrf'] || ''
|
||||||
@ -136,29 +140,36 @@ const createRequest = (method, uri, data = {}, options) => {
|
|||||||
)
|
)
|
||||||
.join('; ')
|
.join('; ')
|
||||||
|
|
||||||
let eapiEncrypt = () => {
|
let eapi = () => {
|
||||||
|
// 使用eapi加密
|
||||||
data.header = header
|
data.header = header
|
||||||
encryptData = encrypt.eapi(uri, data)
|
encryptData = encrypt.eapi(uri, data)
|
||||||
url = APP_CONF.apiDomain + '/eapi/' + uri.substr(5)
|
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') {
|
let api = () => {
|
||||||
eapiEncrypt()
|
// 不使用任何加密
|
||||||
} else if (options.crypto === 'api') {
|
|
||||||
url = APP_CONF.apiDomain + uri
|
url = APP_CONF.apiDomain + uri
|
||||||
encryptData = data
|
encryptData = data
|
||||||
} else if (options.crypto === '') {
|
}
|
||||||
|
if (crypto === 'eapi') {
|
||||||
|
eapi()
|
||||||
|
} else if (crypto === 'api') {
|
||||||
|
api()
|
||||||
|
} else if (crypto === '') {
|
||||||
// 加密方式为空,以配置文件的加密方式为准
|
// 加密方式为空,以配置文件的加密方式为准
|
||||||
if (APP_CONF.encrypt) {
|
if (APP_CONF.encrypt) {
|
||||||
eapiEncrypt()
|
eapi()
|
||||||
} else {
|
} else {
|
||||||
url = APP_CONF.apiDomain + uri
|
api()
|
||||||
encryptData = data
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
break
|
||||||
|
|
||||||
|
default:
|
||||||
// 未知的加密方式
|
// 未知的加密方式
|
||||||
console.log('[ERR]', 'Unknown Crypto:', options.crypto)
|
console.log('[ERR]', 'Unknown Crypto:', crypto)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
const answer = { status: 500, body: {}, cookie: [] }
|
const answer = { status: 500, body: {}, cookie: [] }
|
||||||
// console.log(headers, 'headers')
|
// console.log(headers, 'headers')
|
||||||
@ -216,7 +227,7 @@ const createRequest = (method, uri, data = {}, options) => {
|
|||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
if (data.e_r) {
|
if (data.e_r) {
|
||||||
// eapi接口返回值被加密
|
// eapi接口返回值被加密,需要解密
|
||||||
answer.body = encrypt.eapiResDecrypt(
|
answer.body = encrypt.eapiResDecrypt(
|
||||||
body.toString('hex').toUpperCase(),
|
body.toString('hex').toUpperCase(),
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user