feat: eapi和api都生成cookie;甚至适配了linuxapi

This commit is contained in:
overwriter
2024-06-27 19:53:42 +08:00
parent 9754f341ea
commit 157a058f74

View File

@ -44,8 +44,6 @@ const createRequest = (method, uri, data = {}, options) => {
} }
if (method.toUpperCase() === 'POST') if (method.toUpperCase() === 'POST')
headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['Content-Type'] = 'application/x-www-form-urlencoded'
if (uri.includes('music.163.com'))
headers['Referer'] = 'https://music.163.com'
let ip = options.realIP || options.ip || '' let ip = options.realIP || options.ip || ''
// console.log(ip) // console.log(ip)
if (ip) { if (ip) {
@ -87,8 +85,28 @@ const createRequest = (method, uri, data = {}, options) => {
} }
// console.log(options.cookie, headers['Cookie']) // console.log(options.cookie, headers['Cookie'])
let url = '' let url = ''
let eapiEncrypt = () => { if (options.crypto === 'weapi') {
options['url'] = uri 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] : ''
data = encrypt.weapi(data)
url = APP_CONF.domain + '/weapi/' + uri.substr(5)
} else if (options.crypto === 'linuxapi') {
data = encrypt.linuxapi({
method: method,
url: APP_CONF.apiDomain + uri,
params: data,
})
headers['User-Agent'] =
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
url = 'https://music.163.com/api/linux/forward'
} else if (
options.crypto === 'eapi' ||
options.crypto === 'api' ||
options.crypto === ''
) {
// 两种加密方式都应生成客户端的cookie
const cookie = options.cookie || {} const cookie = options.cookie || {}
const csrfToken = cookie['__csrf'] || '' const csrfToken = cookie['__csrf'] || ''
const header = { const header = {
@ -114,34 +132,23 @@ const createRequest = (method, uri, data = {}, options) => {
encodeURIComponent(key) + '=' + encodeURIComponent(header[key]), encodeURIComponent(key) + '=' + encodeURIComponent(header[key]),
) )
.join('; ') .join('; ')
let eapiEncrypt = () => {
data.header = header data.header = header
data = encrypt.eapi(options.url, data) data = encrypt.eapi(uri, data)
url = APP_CONF.apiDomain + '/eapi/' + uri.substr(5) url = APP_CONF.apiDomain + '/eapi/' + uri.substr(5)
} }
if (options.crypto === 'weapi') { if (options.crypto === 'eapi') {
headers['User-Agent'] = options.ua || chooseUserAgent('pc')
let csrfToken = (headers['Cookie'] || '').match(/_csrf=([^(;|$)]+)/)
data.csrf_token = csrfToken ? csrfToken[1] : ''
data = encrypt.weapi(data)
url = APP_CONF.domain + '/weapi/' + uri.substr(5)
} else if (options.crypto === 'linuxapi') {
data = encrypt.linuxapi({
method: method,
url: uri.replace(/\w*api/, 'api'),
params: data,
})
headers['User-Agent'] =
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
url = 'https://music.163.com/api/linux/forward'
} else if (options.crypto === 'eapi') {
eapiEncrypt() eapiEncrypt()
} else if (options.crypto === 'api') { } else if (options.crypto === 'api') {
url = APP_CONF.apiDomain + uri url = APP_CONF.apiDomain + uri
} else if (options.crypto === '') { } else if (options.crypto === '') {
// 加密方式为空,以配置文件的加密方式为准
if (APP_CONF.encrypt) { if (APP_CONF.encrypt) {
eapiEncrypt() eapiEncrypt()
} else url = APP_CONF.apiDomain + uri } else url = APP_CONF.apiDomain + uri
} }
}
const answer = { status: 500, body: {}, cookie: [] } const answer = { status: 500, body: {}, cookie: [] }
// console.log(headers, 'headers') // console.log(headers, 'headers')
let settings = { let settings = {