From 157a058f74b45b4da70b3ade9285edbd3e622005 Mon Sep 17 00:00:00 2001 From: overwriter <9856mmm@gmail.com> Date: Thu, 27 Jun 2024 19:53:42 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20eapi=E5=92=8Capi=E9=83=BD=E7=94=9F?= =?UTF-8?q?=E6=88=90cookie=EF=BC=9B=E7=94=9A=E8=87=B3=E9=80=82=E9=85=8D?= =?UTF-8?q?=E4=BA=86linuxapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/request.js | 67 +++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/util/request.js b/util/request.js index 3dc9f62..12108bd 100644 --- a/util/request.js +++ b/util/request.js @@ -44,8 +44,6 @@ const createRequest = (method, uri, data = {}, options) => { } if (method.toUpperCase() === 'POST') 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 || '' // console.log(ip) if (ip) { @@ -87,8 +85,28 @@ const createRequest = (method, uri, data = {}, options) => { } // console.log(options.cookie, headers['Cookie']) let url = '' - let eapiEncrypt = () => { - options['url'] = uri + if (options.crypto === '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] : '' + 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 csrfToken = cookie['__csrf'] || '' const header = { @@ -114,33 +132,22 @@ const createRequest = (method, uri, data = {}, options) => { encodeURIComponent(key) + '=' + encodeURIComponent(header[key]), ) .join('; ') - data.header = header - data = encrypt.eapi(options.url, data) - url = APP_CONF.apiDomain + '/eapi/' + uri.substr(5) - } - if (options.crypto === 'weapi') { - 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() - } else if (options.crypto === 'api') { - url = APP_CONF.apiDomain + uri - } else if (options.crypto === '') { - if (APP_CONF.encrypt) { + + let eapiEncrypt = () => { + data.header = header + data = encrypt.eapi(uri, data) + url = APP_CONF.apiDomain + '/eapi/' + uri.substr(5) + } + if (options.crypto === 'eapi') { eapiEncrypt() - } else url = APP_CONF.apiDomain + uri + } else if (options.crypto === 'api') { + url = APP_CONF.apiDomain + uri + } else if (options.crypto === '') { + // 加密方式为空,以配置文件的加密方式为准 + if (APP_CONF.encrypt) { + eapiEncrypt() + } else url = APP_CONF.apiDomain + uri + } } const answer = { status: 500, body: {}, cookie: [] } // console.log(headers, 'headers') From 9006fdc938768a029feffa295639d0a23df58969 Mon Sep 17 00:00:00 2001 From: overwriter <9856mmm@gmail.com> Date: Thu, 27 Jun 2024 20:18:01 +0800 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20=E9=BB=98=E8=AE=A4=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=80=BC=E9=83=BD=E6=98=AF=E6=9C=AA=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/request.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/util/request.js b/util/request.js index 12108bd..7316732 100644 --- a/util/request.js +++ b/util/request.js @@ -204,11 +204,7 @@ const createRequest = (method, uri, data = {}, options) => { x.replace(/\s*Domain=[^(;|$)]+;*/, ''), ) try { - if (options.crypto === 'eapi') { - answer.body = JSON.parse(encrypt.decrypt(body)) - } else { - answer.body = body - } + answer.body = JSON.parse(body.toString()) if (answer.body.code) { answer.body.code = Number(answer.body.code) } @@ -224,7 +220,7 @@ const createRequest = (method, uri, data = {}, options) => { } catch (e) { // console.log(e) try { - answer.body = JSON.parse(body.toString()) + answer.body = JSON.parse(encrypt.decrypt(body)) } catch (err) { // console.log(err) // can't decrypt and can't parse directly From c38a8a0816d199974131667d783102a89f96578b Mon Sep 17 00:00:00 2001 From: overwriter <9856mmm@gmail.com> Date: Fri, 28 Jun 2024 05:55:14 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20api=E6=B5=8B=E8=AF=95=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3data=E4=BC=A0=E9=80=92=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/api.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/module/api.js b/module/api.js index d1df38e..7c79439 100644 --- a/module/api.js +++ b/module/api.js @@ -2,7 +2,8 @@ const createOption = require('../util/option.js') module.exports = (query, request) => { const method = query.method || 'POST' const uri = query.uri - const data = query.data || {} + const data = + typeof query.data === 'string' ? JSON.parse(query.data) : query.data || {} const crypto = query.crypto || '' let res = request(method, uri, data, createOption(query, crypto)) From bd8c04957610277f60012aa47bc5d0d5fdf13daa Mon Sep 17 00:00:00 2001 From: overwriter <9856mmm@gmail.com> Date: Sat, 29 Jun 2024 11:36:16 +0800 Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20=E8=A1=A5=E5=85=85=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/api.js | 2 +- util/request.js | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/module/api.js b/module/api.js index 7c79439..f4d5eac 100644 --- a/module/api.js +++ b/module/api.js @@ -6,6 +6,6 @@ module.exports = (query, request) => { typeof query.data === 'string' ? JSON.parse(query.data) : query.data || {} const crypto = query.crypto || '' - let res = request(method, uri, data, createOption(query, crypto)) + const res = request(method, uri, data, createOption(query, crypto)) return res } diff --git a/util/request.js b/util/request.js index 7316732..9f51510 100644 --- a/util/request.js +++ b/util/request.js @@ -84,7 +84,9 @@ const createRequest = (method, uri, data = {}, options) => { headers['Cookie'] = cookieObjToString(cookie) } // console.log(options.cookie, headers['Cookie']) + let url = '' + // 目前任意uri都支持三种加密方式 if (options.crypto === 'weapi') { headers['Referer'] = 'https://music.163.com' headers['User-Agent'] = options.ua || chooseUserAgent('pc') @@ -93,13 +95,13 @@ const createRequest = (method, uri, data = {}, options) => { data = encrypt.weapi(data) url = APP_CONF.domain + '/weapi/' + uri.substr(5) } else if (options.crypto === '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' 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' || @@ -148,6 +150,9 @@ const createRequest = (method, uri, data = {}, options) => { eapiEncrypt() } else url = APP_CONF.apiDomain + uri } + } else { + // 未知的加密方式 + console.log('[ERR]', 'Unknown Crypto:', options.crypto) } const answer = { status: 500, body: {}, cookie: [] } // console.log(headers, 'headers') @@ -160,8 +165,6 @@ const createRequest = (method, uri, data = {}, options) => { httpsAgent: new https.Agent({ keepAlive: true }), } - if (options.crypto === 'eapi') settings.encoding = null - if (options.proxy) { if (options.proxy.indexOf('pac') > -1) { settings.httpAgent = new PacProxyAgent(options.proxy) @@ -191,12 +194,6 @@ const createRequest = (method, uri, data = {}, options) => { } else { settings.proxy = false } - if (options.crypto === 'eapi') { - settings = { - ...settings, - responseType: 'arraybuffer', - } - } axios(settings) .then((res) => { const body = res.data From 58e9865b70e41197c2ab75c46a775fc45d6efa6e Mon Sep 17 00:00:00 2001 From: overwriter <9856mmm@gmail.com> Date: Sat, 29 Jun 2024 16:50:30 +0800 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dbatch=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=EF=BC=9Beapi?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=EF=BC=8C=E4=BC=A0=E5=85=A5e=5Fr:=20true?= =?UTF-8?q?=EF=BC=8C=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E4=BC=9A=E8=A2=AB?= =?UTF-8?q?=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/batch.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/module/batch.js b/module/batch.js index 576dde5..669c6fc 100644 --- a/module/batch.js +++ b/module/batch.js @@ -2,9 +2,7 @@ const createOption = require('../util/option.js') module.exports = (query, request) => { - const data = { - e_r: true, - } + const data = {} Object.keys(query).forEach((i) => { if (/^\/api\//.test(i)) { data[i] = query[i]