allocate js cookie in middleware, update res cookie distribution

This commit is contained in:
Nzix
2018-09-25 19:31:47 +08:00
parent f8660cdca6
commit 50adbfd804
6 changed files with 40 additions and 46 deletions
+24 -10
View File
@@ -1,16 +1,30 @@
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('')
}
function generateCookie(){
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
function completeCookie(cookie){
let origin = (cookie || '').split(/;\s*/).map(element => (element.split('=')[0])), extra = []
let now = (new Date).getTime()
if(!origin.includes('JSESSIONID-WYYY')){
let expire = new Date(now + 1800000) //30 minutes
let jessionid = randomString('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ\\/+',176) + ':' + expire.getTime()
extra.push(['JSESSIONID-WYYY=' + jessionid, 'Expires=' + expire.toGMTString()])
}
if(!origin.includes('_iuqxldmzr_')){
let expire = new Date(now + 157680000000) //5 years
extra.push(['_iuqxldmzr_=32', 'Expires=' + expire.toGMTString()])
}
if((!origin.includes('_ntes_nnid'))||(!origin.includes('_ntes_nuid'))){
let expire = new Date(now + 3153600000000) //100 years
let nnid = randomString('0123456789abcdefghijklmnopqrstuvwxyz',32) + ',' + now
extra.push(['_ntes_nnid=' + nnid, 'Expires=' + expire.toGMTString()])
extra.push(['_ntes_nuid=' + nnid.slice(0,32), 'Expires=' + expire.toGMTString()])
}
return extra
}
module.exports = generateCookie
module.exports = {
completeCookie
}
+2 -19
View File
@@ -1,7 +1,6 @@
const Encrypt = require("./crypto.js");
const request = require("request");
const queryString = require("querystring");
const randomCookie = require("./init.js");
request.debug = true;
@@ -48,15 +47,6 @@ function createWebAPIRequest(
const proxy = cookie.split("__proxy__")[1];
cookie = cookie.split("__proxy__")[0];
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 = {
@@ -87,15 +77,8 @@ function createWebAPIRequest(
console.error(error);
errorCallback(error);
} else {
//解决 网易云 cookie 添加 .music.163.com 域设置。
//如: Domain=.music.163.com
let cookie = res.headers["set-cookie"];
if (Array.isArray(cookie)) {
cookie = cookie
.map(x => x.replace(/.music.163.com/g, ""))
.sort((a, b) => a.length - b.length);
cookie = cookie.concat(missingCookie.map(x => x + '; Expires=' + (new Date((new Date).getTime() + 157680000000)).toGMTString()));
}
let cookie = res.headers["set-cookie"] || [];
cookie = cookie.map(x => x.replace(/\s*Domain=[^(;|$)]+;*/, ""));
callback(body, cookie);
}
});