From 9f44e77237eb46c54eabe6a06e8059430c1eb9c2 Mon Sep 17 00:00:00 2001 From: Otstar Lin Date: Fri, 18 Oct 2024 09:47:17 +0800 Subject: [PATCH] fix(route/bilibili): add addDmVerifyInfoWithInter to bypass the risk control mechanism (#17147) --- lib/config.ts | 2 + lib/routes/bilibili/cache.ts | 10 ++-- lib/routes/bilibili/utils.ts | 88 ++++++++++++++++++++++++++++++++++-- lib/routes/bilibili/video.ts | 14 +++--- 4 files changed, 96 insertions(+), 18 deletions(-) diff --git a/lib/config.ts b/lib/config.ts index eaf103ddd..87d28699d 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -81,6 +81,7 @@ export type Config = { bilibili: { cookies: Record; dmImgList?: string; + dmImgInter?: string; }; bitbucket: { username?: string; @@ -472,6 +473,7 @@ const calculateValue = () => { bilibili: { cookies: bilibili_cookies, dmImgList: envs.BILIBILI_DM_IMG_LIST, + dmImgInter: envs.BILIBILI_DM_IMG_INTER, }, bitbucket: { username: envs.BITBUCKET_USERNAME, diff --git a/lib/routes/bilibili/cache.ts b/lib/routes/bilibili/cache.ts index a4a43e83e..102b776f1 100644 --- a/lib/routes/bilibili/cache.ts +++ b/lib/routes/bilibili/cache.ts @@ -141,13 +141,9 @@ const getUsernameAndFaceFromUID = async (uid) => { if (!name || !face) { const cookie = await getCookie(); const wbiVerifyString = await getWbiVerifyString(); - // await got(`https://space.bilibili.com/${uid}/`, { - // headers: { - // Referer: `https://www.bilibili.com/`, - // Cookie: cookie, - // }, - // }); - const params = utils.addWbiVerifyInfo(`mid=${uid}&token=&platform=web&web_location=1550101`, wbiVerifyString); + const dmImgList = utils.getDmImgList(); + const renderData = await getRenderData(uid); + const params = utils.addWbiVerifyInfo(utils.addRenderData(utils.addDmVerifyInfo(`mid=${uid}&token=&platform=web&web_location=1550101`, dmImgList), renderData), wbiVerifyString); const { data: nameResponse } = await got(`https://api.bilibili.com/x/space/wbi/acc/info?${params}`, { headers: { Referer: `https://space.bilibili.com/${uid}/`, diff --git a/lib/routes/bilibili/utils.ts b/lib/routes/bilibili/utils.ts index 2f2f974d2..7a776e724 100644 --- a/lib/routes/bilibili/utils.ts +++ b/lib/routes/bilibili/utils.ts @@ -95,8 +95,8 @@ function getDmImgList() { const dmImgList = JSON.parse(config.bilibili.dmImgList); return JSON.stringify([dmImgList[Math.floor(Math.random() * dmImgList.length)]]); } - const x = Math.max(generateGaussianInteger(650, 5), 0); - const y = Math.max(generateGaussianInteger(400, 5), 0); + const x = Math.max(generateGaussianInteger(1245, 5), 0); + const y = Math.max(generateGaussianInteger(1285, 5), 0); const path = [ { x: 3 * x + 2 * y, @@ -109,12 +109,92 @@ function getDmImgList() { return JSON.stringify(path); } -function addDmVerifyInfo(params, dmImgList) { +function getDmImgInter() { + if (config.bilibili.dmImgInter !== undefined) { + const dmImgInter = JSON.parse(config.bilibili.dmImgInter); + return JSON.stringify([dmImgInter[Math.floor(Math.random() * dmImgInter.length)]]); + } + const p1 = getDmImgInterWh(274, 601); + const s1 = getDmImgInterOf(134, 30); + const p2 = getDmImgInterWh(332, 64); + const s2 = getDmImgInterOf(1101, 338); + const of = getDmImgInterOf(0, 0); + const wh = getDmImgInterWh(1245, 1285); + const ds = [ + { + t: getDmImgInterT('div'), + c: getDmImgInterC('clearfix g-search search-container'), + p: [p1[0], p1[2], p1[1]], + s: [s1[2], s1[0], s1[1]], + }, + { + t: getDmImgInterT('div'), + c: getDmImgInterC('wrapper'), + p: [p2[0], p2[2], p2[1]], + s: [s2[2], s2[0], s2[1]], + }, + ]; + return JSON.stringify({ ds, wh, of }); +} + +function getDmImgInterT(tag: string) { + return { + a: 4, + article: 29, + button: 7, + div: 2, + em: 27, + form: 17, + h1: 11, + h2: 12, + h3: 13, + h4: 14, + h5: 15, + h6: 16, + img: 5, + input: 6, + label: 25, + li: 10, + ol: 9, + option: 20, + p: 3, + section: 28, + select: 19, + span: 1, + strong: 26, + table: 21, + td: 23, + textarea: 18, + th: 24, + tr: 22, + ul: 8, + }[tag]; +} + +function getDmImgInterC(className: string) { + return Buffer.from(className).toString('base64').slice(0, -2); +} + +function getDmImgInterOf(top: number, left: number) { + const seed = Math.floor(514 * Math.random()); + return [3 * top + 2 * left + seed, 4 * top - 4 * left + 2 * seed, seed]; +} + +function getDmImgInterWh(width: number, height: number) { + const seed = Math.floor(114 * Math.random()); + return [2 * width + 2 * height + 3 * seed, 4 * width - height + seed, seed]; +} + +function addDmVerifyInfo(params: string, dmImgList: string) { const dmImgStr = Buffer.from('no webgl').toString('base64').slice(0, -2); const dmCoverImgStr = Buffer.from('no webgl').toString('base64').slice(0, -2); return `${params}&dm_img_list=${dmImgList}&dm_img_str=${dmImgStr}&dm_cover_img_str=${dmCoverImgStr}`; } +function addDmVerifyInfoWithInter(params: string, dmImgList: string, dmImgInter: string) { + return `${addDmVerifyInfo(params, dmImgList)}&dm_img_inter=${dmImgInter}`; +} + const bvidTime = 1_589_990_400; export default { @@ -124,7 +204,9 @@ export default { hexsign, addWbiVerifyInfo, getDmImgList, + getDmImgInter, addDmVerifyInfo, + addDmVerifyInfoWithInter, bvidTime, addRenderData, }; diff --git a/lib/routes/bilibili/video.ts b/lib/routes/bilibili/video.ts index 7e4ea66ad..457b1fd55 100644 --- a/lib/routes/bilibili/video.ts +++ b/lib/routes/bilibili/video.ts @@ -35,19 +35,17 @@ async function handler(ctx) { const cookie = await cache.getCookie(); const wbiVerifyString = await cache.getWbiVerifyString(); const dmImgList = utils.getDmImgList(); + const dmImgInter = utils.getDmImgInter(); const renderData = await cache.getRenderData(uid); const [name, face] = await cache.getUsernameAndFaceFromUID(uid); - // await got(`https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, { - // headers: { - // Referer: `https://space.bilibili.com/${uid}/`, - // Cookie: cookie, - // }, - // }); - const params = utils.addWbiVerifyInfo(utils.addRenderData(utils.addDmVerifyInfo(`mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true`, dmImgList), renderData), wbiVerifyString); + const params = utils.addWbiVerifyInfo( + utils.addRenderData(utils.addDmVerifyInfoWithInter(`mid=${uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&platform=web&web_location=1550101&order_avoided=true`, dmImgList, dmImgInter), renderData), + wbiVerifyString + ); const response = await got(`https://api.bilibili.com/x/space/wbi/arc/search?${params}`, { headers: { - Referer: `https://space.bilibili.com/${uid}/video?tid=0&page=1&keyword=&order=pubdate`, + Referer: `https://space.bilibili.com/${uid}/video?tid=0&pn=1&keyword=&order=pubdate`, Cookie: cookie, }, });