From 982dd67815bfd43cb68008356aac5166b70e4448 Mon Sep 17 00:00:00 2001 From: Night Space <63776734+NightSpaceC@users.noreply.github.com> Date: Sat, 23 Sep 2023 13:18:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E9=80=9A=E8=BF=87=E5=9C=A8?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=AD=E9=A2=84=E6=B7=BB=E5=8A=A0=E5=A4=A7?= =?UTF-8?q?=E9=87=8F=20Cookie=20=E6=9D=A5=E7=BB=95=E8=BF=87=20Bilibili=20?= =?UTF-8?q?=E5=8F=8D=E7=88=AC=20(#13365)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(route): allow to use the cookies which are in config * fix(route): get full cookie --- lib/v2/bilibili/cache.js | 43 +++++++++++++++++++----- lib/v2/bilibili/utils.js | 64 +++++++++++++++++++++++++++++++++++- lib/v2/bilibili/video-all.js | 6 +--- 3 files changed, 98 insertions(+), 15 deletions(-) diff --git a/lib/v2/bilibili/cache.js b/lib/v2/bilibili/cache.js index 4ff2227ea..8a8cc525f 100644 --- a/lib/v2/bilibili/cache.js +++ b/lib/v2/bilibili/cache.js @@ -1,24 +1,49 @@ const got = require('@/utils/got'); const utils = require('./utils'); const cheerio = require('cheerio'); +const config = require('@/config').value; module.exports = { getCookie: (ctx) => { + if (Object.keys(config.bilibili.cookies).length > 0) { + return config.bilibili.cookies[Object.keys(config.bilibili.cookies)[Math.floor(Math.random() * Object.keys(config.bilibili.cookies).length)]]; + } const key = 'bili-cookie'; 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, { - headers: { - Referer: 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 5038b8775..1ebd2bb49 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, }; diff --git a/lib/v2/bilibili/video-all.js b/lib/v2/bilibili/video-all.js index 95fdf6bb0..11a6c21d3 100644 --- a/lib/v2/bilibili/video-all.js +++ b/lib/v2/bilibili/video-all.js @@ -2,7 +2,6 @@ const got = require('@/utils/got'); const cache = require('./cache'); const utils = require('./utils'); const { parseDate } = require('@/utils/parse-date'); -const config = require('@/config'); module.exports = async (ctx) => { const uid = ctx.params.uid; @@ -29,13 +28,11 @@ module.exports = async (ctx) => { const pageTotal = Math.ceil(response.data.data.page.count / response.data.data.page.ps); const getPage = async (pageId) => { - // A hack way to skip limit - const userAgent = config.ua + `.${pageId}`; + const cookie = await cache.getCookie(ctx); await got(`https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`, { headers: { Referer: `https://space.bilibili.com/${uid}/`, Cookie: cookie, - 'User-Agent': userAgent, }, }); const params = utils.addVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=${pageId}&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true`, verifyString); @@ -43,7 +40,6 @@ module.exports = async (ctx) => { headers: { Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=${pageId}&keyword=&order=pubdate`, Cookie: cookie, - 'User-Agent': userAgent, }, }); };