From 72b37ecd21f2983e454318b95d8b8df0c92a8e52 Mon Sep 17 00:00:00 2001 From: NightSpaceC Date: Sat, 23 Sep 2023 10:51:10 +0800 Subject: [PATCH] fix(route): get full cookie --- lib/v2/bilibili/cache.js | 35 ++++++++++++++++++---- lib/v2/bilibili/utils.js | 64 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 6 deletions(-) diff --git a/lib/v2/bilibili/cache.js b/lib/v2/bilibili/cache.js index 1ac72a87239ac3..8a8cc525f7ad5d 100644 --- a/lib/v2/bilibili/cache.js +++ b/lib/v2/bilibili/cache.js @@ -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) => { diff --git a/lib/v2/bilibili/utils.js b/lib/v2/bilibili/utils.js index 5038b8775214a0..1ebd2bb49a3656 100644 --- a/lib/v2/bilibili/utils.js +++ b/lib/v2/bilibili/utils.js @@ -1,4 +1,5 @@ const md5 = require('@/utils/md5'); +const CryptoJS = require('crypto-js'); function iframe(aid, page, bvid) { return ``; } -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(); @@ -17,6 +76,9 @@ const addVerifyInfo = (params, verifyString) => { module.exports = { iframe, + lsid, + _uuid, + hexsign, addVerifyInfo, bvidTime: 1589990400, };