新增eapi算法

This commit is contained in:
lizhenyuls
2019-05-16 23:01:27 +08:00
parent 4163cbbe3d
commit 99e60a5fc4
3 changed files with 99 additions and 8 deletions

View File

@ -2,6 +2,7 @@ const encrypt = require('./crypto')
const request = require('request')
const queryString = require('querystring')
const PacProxyAgent = require('pac-proxy-agent')
const zlib = require('zlib')
// request.debug = true // 开启可看到更详细信息
@ -25,8 +26,8 @@ const chooseUserAgent = ua => {
let index = 0
if (typeof ua == 'undefined')
index = Math.floor(Math.random() * userAgentList.length)
else if (ua == 'mobile') index = Math.floor(Math.random() * 7)
else if (ua == 'pc') index = Math.floor(Math.random() * 5) + 8
else if (ua === 'mobile') index = Math.floor(Math.random() * 7)
else if (ua === 'pc') index = Math.floor(Math.random() * 5) + 8
else return ua
return userAgentList[index]
}
@ -34,7 +35,7 @@ const chooseUserAgent = ua => {
const createRequest = (method, url, data, options) => {
return new Promise((resolve, reject) => {
let headers = { 'User-Agent': chooseUserAgent(options.ua) }
if (method.toUpperCase() == 'POST')
if (method.toUpperCase() === 'POST')
headers['Content-Type'] = 'application/x-www-form-urlencoded'
if (url.includes('music.163.com'))
headers['Referer'] = 'https://music.163.com'
@ -51,12 +52,12 @@ const createRequest = (method, url, data, options) => {
.join('; ')
else if (options.cookie) headers['Cookie'] = options.cookie
if (options.crypto == 'weapi') {
if (options.crypto === 'weapi') {
let csrfToken = (headers['Cookie'] || '').match(/_csrf=([^(;|$)]+)/)
data.csrf_token = csrfToken ? csrfToken[1] : ''
data = encrypt.weapi(data)
url = url.replace(/\w*api/, 'weapi')
} else if (options.crypto == 'linuxapi') {
} else if (options.crypto === 'linuxapi') {
data = encrypt.linuxapi({
method: method,
url: url.replace(/\w*api/, 'api'),
@ -65,6 +66,35 @@ const createRequest = (method, url, data, options) => {
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') {
const cookie = options.cookie || {};
const csrfToken = cookie['__csrf'] || ''
const header = {
"osver": cookie.osver, //系统版本
"deviceId": cookie.deviceId, //encrypt.base64.encode(imei + '\t02:00:00:00:00:00\t5106025eb79a5247\t70ffbaac7')
"appver": cookie.appver || "6.1.1", // app版本
"versioncode": cookie.versioncode || "140", //版本号
"mobilename": cookie.mobilename, //设备model
"buildver": cookie.buildver || Date.now().toString().substr(0, 10),
"resolution": cookie.resolution || "1920x1080", //设备分辨率
"__csrf": csrfToken,
"os": cookie.os || 'android',
"channel": cookie.channel,
"requestId":`${Date.now()}_${Math.floor(Math.random() * 1000).toString().padStart(4, '0')}`
}
if (cookie.MUSIC_U) header["MUSIC_U"] = cookie.MUSIC_U
if (cookie.MUSIC_A) header["MUSIC_A"] = cookie.MUSIC_A
headers['Cookie'] = Object.keys(header)
.map(
key =>
encodeURIComponent(key) +
'=' +
encodeURIComponent(header[key])
)
.join('; ')
data.header = header
data = encrypt.eapi(options.url, data)
url = url.replace(/\w*api/,'eapi')
}
const answer = { status: 500, body: {}, cookie: [] }
@ -75,6 +105,8 @@ const createRequest = (method, url, data, options) => {
body: queryString.stringify(data)
}
if (options.crypto === 'eapi') settings.encoding = null
if (/\.pac$/i.test(options.proxy)) {
settings.agent = new PacProxyAgent(options.proxy)
} else {
@ -93,12 +125,41 @@ const createRequest = (method, url, data, options) => {
x.replace(/\s*Domain=[^(;|$)]+;*/, '')
)
try {
answer.body = JSON.parse(body)
answer.status = answer.body.code || res.statusCode
if (options.crypto === 'eapi') {
zlib.unzip(body, function (err, buffer) {
const _buffer = err ? body : buffer
try {
try{
answer.body = JSON.parse(encrypt.decrypt(_buffer).toString())
answer.status = answer.body.code || res.statusCode
} catch(e){
answer.body = JSON.parse(_buffer.toString())
answer.status = res.statusCode
}
} catch (e) {
answer.body = _buffer.toString()
answer.status = res.statusCode
}
answer.status =
100 < answer.status && answer.status < 600 ? answer.status : 400
if (answer.status === 200) resolve(answer)
else reject(answer)
});
return false
} else {
answer.body = JSON.parse(body)
answer.status = answer.body.code || res.statusCode
}
} catch (e) {
answer.body = body
answer.status = res.statusCode
}
answer.status =
100 < answer.status && answer.status < 600 ? answer.status : 400
if (answer.status == 200) resolve(answer)