Skip to content

Commit

Permalink
fix(route): get full cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
NightSpaceC committed Sep 23, 2023
1 parent 0653caa commit 72b37ec
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
35 changes: 30 additions & 5 deletions lib/v2/bilibili/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,38 @@ module.exports = {
return ctx.cache.tryGet(key, async () => {
// default Referer: https://www.bilibili.com is limited
// Bilibili return cookies with multiple set-cookie
const url = 'https://www.bilibili.com/';
const response = await got(url);
const setCookies = response.headers['set-cookie'];
if (typeof setCookies === 'undefined') {
let response = await got('https://www.bilibili.com/');
const setCookie = response.headers['set-cookie'];
if (typeof setCookie === 'undefined') {
return '';
}
return setCookies.map((cookie) => cookie.split(';')[0]).join('; ');
const cookie = setCookie.map((cookie) => cookie.split(';')[0]);
cookie.push(['b_lsid', utils.lsid()].join('='));
cookie.push(['_uuid', utils._uuid()].join('='));
response = await got('https://api.bilibili.com/x/frontend/finger/spi', {
headers: {
Referer: 'https://www.bilibili.com/',
Cookie: cookie.join('; '),
}
});
cookie.push(['bvuid4', encodeURIComponent(response.data.data.b_4)].join('='));
const e = Math.floor(Date.now() / 1000);;
const hexsign = utils.hexsign(e);
await got('https://space.bilibili.com/1', {
headers: {
Referer: 'https://www.bilibili.com/',
Cookie: cookie.join('; '),
}
});
response = await got.post(`https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket?key_id=ec02&hexsign=${hexsign}&context[ts]=${e}&csrf=`, {
headers: {
Referer: 'https://space.bilibili.com/1',
Cookie: cookie.join('; '),
}
});
cookie.push(['bili_ticket', response.data.data.ticket].join('='));
cookie.push(['bili_ticket_expires', (parseInt(response.data.data.created_at) + parseInt(response.data.data.ttl)).toString()].join('='));
return cookie.join('; ');
});
},
getVerifyString: (ctx) => {
Expand Down
64 changes: 63 additions & 1 deletion lib/v2/bilibili/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,71 @@
const md5 = require('@/utils/md5');
const CryptoJS = require('crypto-js');

function iframe(aid, page, bvid) {
return `<iframe src="https://player.bilibili.com/player.html?${bvid ? `bvid=${bvid}` : `aid=${aid}`}${
page ? `&page=${page}` : ''
}&high_quality=1&autoplay=0" width="650" height="477" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"></iframe>`;
}

const addVerifyInfo = (params, verifyString) => {
// a
function randomHexStr(length) {
let string = '';
for (let r = 0; r < length; r++) {
string += dec2HexUpper(16 * Math.random());
}
return padStringWithZeros(string, length);
}

// o
function dec2HexUpper(e) {
return Math.ceil(e).toString(16).toUpperCase();
}

// s
function padStringWithZeros(string, length) {
let padding = '';
if (string.length < length) {
for (let n = 0; n < length - string.length; n++) {
padding += '0';
}
}
return padding + string;
}

function lsid() {
const e = Date.now().toString(16).toUpperCase();
const lsid = randomHexStr(8) + '_' + e;
return lsid;
}

function _uuid() {
const e = randomHexStr(8);
const t = randomHexStr(4);
const r = randomHexStr(4);
const n = randomHexStr(4);
const o = randomHexStr(12);
const i = Date.now();
return e + '-' + t + '-' + r + '-' + n + '-' + o + padStringWithZeros((i % 100000).toString(), 5) + 'infoc';
}

// P
function shiftCharByOne(string) {
let shiftedStr = '';
for (let n = 0; n < string.length; n++) {
shiftedStr += String.fromCharCode(string.charCodeAt(n) - 1);
}
return shiftedStr;
}

// o
function hexsign(e) {
const n = 'YhxToH[2q';
const r = CryptoJS.HmacSHA256('ts'.concat(e), shiftCharByOne(n));
const o = CryptoJS.enc.Hex.stringify(r);
return o;
}

function addVerifyInfo(params, verifyString) {
const searchParams = new URLSearchParams(params);
searchParams.sort();
const verifyParam = searchParams.toString();
Expand All @@ -17,6 +76,9 @@ const addVerifyInfo = (params, verifyString) => {

module.exports = {
iframe,
lsid,
_uuid,
hexsign,
addVerifyInfo,
bvidTime: 1589990400,
};

0 comments on commit 72b37ec

Please sign in to comment.