optimize base cookie for multiple users

This commit is contained in:
Nzix 2018-09-24 16:45:05 +08:00
parent fbd969e36b
commit ac66ae339f
3 changed files with 44 additions and 23 deletions

View File

@ -13,7 +13,7 @@ module.exports = (req, res, createWebAPIRequest, request) => {
var bindings = (/var GBinds=([^;]+);/g).exec(music_req)[1]; var bindings = (/var GBinds=([^;]+);/g).exec(music_req)[1];
profile = eval(`(${profile})`); profile = eval(`(${profile})`);
bindings = eval(`(${bindings})`); bindings = eval(`(${bindings})`);
res.send({code: 200,profile: profile, bindings: bindings}); res.send({code: 200, profile: profile, bindings: bindings});
} catch (error) { } catch (error) {
res.status(502).send("fetch error"); res.status(502).send("fetch error");
} }

View File

@ -2,7 +2,15 @@ function randomString(pattern, length){
return Array.apply(null, {length: length}).map(() => (pattern[Math.floor(Math.random() * pattern.length)])).join(''); return Array.apply(null, {length: length}).map(() => (pattern[Math.floor(Math.random() * pattern.length)])).join('');
} }
const jsessionid = randomString('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ\\/+',176) + ':' + (new Date).getTime(); function generateCookie(){
const nuid = randomString('0123456789abcdefghijklmnopqrstuvwxyz',32); const jsessionid = randomString('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ\\/+',176) + ':' + (new Date).getTime();
const nuid = randomString('0123456789abcdefghijklmnopqrstuvwxyz',32);
return {
'JSESSIONID-WYYY': jsessionid,
'_iuqxldmzr_': 32,
'_ntes_nnid': nuid + ',' + (new Date).getTime(),
'_ntes_nuid': nuid
}
}
module.exports = `JSESSIONID-WYYY=${jsessionid}; _iuqxldmzr_=32; _ntes_nnid=${nuid},${(new Date).getTime()}; _ntes_nuid=${nuid}`; module.exports = generateCookie

View File

@ -1,7 +1,7 @@
const Encrypt = require("./crypto.js"); const Encrypt = require("./crypto.js");
const request = require("request"); const request = require("request");
const querystring = require("querystring"); const queryString = require("querystring");
const baseCookie = require("./init.js"); const randomCookie = require("./init.js");
request.debug = true; request.debug = true;
@ -38,31 +38,43 @@ function createWebAPIRequest(
data, data,
cookie, cookie,
callback, callback,
errorcallback errorCallback
) { ) {
// console.log(cookie); const csrfToken = cookie.match(/_csrf=([^(;|$)]+)/);
if (cookie.match(/_csrf=[^(;|$)]+/g)) if (csrfToken)
data.csrf_token = cookie.match(/_csrf=[^(;|$)]+/g)[0].slice(6); data.csrf_token = csrfToken[1];
else data.csrf_token = ""; else
data.csrf_token = "";
const proxy = cookie.split("__proxy__")[1]; const proxy = cookie.split("__proxy__")[1];
cookie = cookie.split("__proxy__")[0]; cookie = cookie.split("__proxy__")[0];
const cryptoreq = Encrypt(data);
const jsCookie = randomCookie();
const missingCookie = [];
for (let key in jsCookie){
if (cookie.indexOf(key) == -1){
missingCookie.push(`${key}=${jsCookie[key]}`);
}
}
cookie = cookie.split(/;\s*/).concat(missingCookie).join("; ");
const encryptedData = Encrypt(data);
const options = { const options = {
url: `http://${host}${path}`, url: `http://${host}${path}`,
method: method, method: method,
headers: { headers: {
Accept: "*/*", "Accept": "*/*",
"Accept-Language": "zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4", "Accept-Language": "zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4",
Connection: "keep-alive", "Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "application/x-www-form-urlencoded",
Referer: "http://music.163.com", "Referer": "http://music.163.com",
Host: "music.163.com", "Host": "music.163.com",
Cookie: baseCookie + (cookie ? "; " : "") + cookie, "Cookie": cookie,
"User-Agent": randomUserAgent() "User-Agent": randomUserAgent()
}, },
body: querystring.stringify({ body: queryString.stringify({
params: cryptoreq.params, params: encryptedData.params,
encSecKey: cryptoreq.encSecKey encSecKey: encryptedData.encSecKey
}), }),
proxy: proxy proxy: proxy
}; };
@ -73,7 +85,7 @@ function createWebAPIRequest(
request(options, function(error, res, body) { request(options, function(error, res, body) {
if (error) { if (error) {
console.error(error); console.error(error);
errorcallback(error); errorCallback(error);
} else { } else {
//解决 网易云 cookie 添加 .music.163.com 域设置。 //解决 网易云 cookie 添加 .music.163.com 域设置。
//如: Domain=.music.163.com //如: Domain=.music.163.com
@ -82,6 +94,7 @@ function createWebAPIRequest(
cookie = cookie cookie = cookie
.map(x => x.replace(/.music.163.com/g, "")) .map(x => x.replace(/.music.163.com/g, ""))
.sort((a, b) => a.length - b.length); .sort((a, b) => a.length - b.length);
cookie = cookie.concat(missingCookie.map(x => x + '; Expires=' + (new Date((new Date).getTime() + 157680000000)).toGMTString()));
} }
callback(body, cookie); callback(body, cookie);
} }
@ -94,8 +107,8 @@ function createRequest(path, method, data) {
url: `http://music.163.com${path}`, url: `http://music.163.com${path}`,
method: method, method: method,
headers: { headers: {
Referer: "http://music.163.com", "Referer": "http://music.163.com",
Cookie: "appver=1.5.2", "Cookie": "appver=1.5.2",
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "application/x-www-form-urlencoded",
"User-Agent": randomUserAgent() "User-Agent": randomUserAgent()
} }