Skip to content

Commit

Permalink
feat(route/bilibili): generate and replace b_lsid in cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Oct 17, 2024
1 parent e4eedac commit 578e2f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion lib/routes/bilibili/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ import puppeteer from '@/utils/puppeteer';
import { JSDOM } from 'jsdom';

let disableConfigCookie = false;

const getCookie = () => {
if (!disableConfigCookie && Object.keys(config.bilibili.cookies).length > 0) {
// Update b_lsid in cookies
for (const key of Object.keys(config.bilibili.cookies)) {
const cookie = config.bilibili.cookies[key];
if (cookie) {
const updatedCookie = cookie.replace(/b_lsid=[0-9A-F]+_[0-9A-F]+/, `b_lsid=${generateBLsid()}`);
config.bilibili.cookies[key] = updatedCookie;
}
}

return config.bilibili.cookies[Object.keys(config.bilibili.cookies)[Math.floor(Math.random() * Object.keys(config.bilibili.cookies).length)]];
}
const key = 'bili-cookie';
Expand All @@ -22,7 +32,9 @@ const getCookie = () => {
page.on('requestfinished', async (request) => {
if (request.url() === 'https://api.bilibili.com/x/internal/gaia-gateway/ExClimbWuzhi') {
const cookies = await page.cookies();
const cookieString = cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join('; ');
let cookieString = cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join('; ');

cookieString = cookieString.replace(/b_lsid=[0-9A-F]+_[0-9A-F]+/, `b_lsid=${generateBLsid()}`);
resolve(cookieString);
}
});
Expand All @@ -35,6 +47,12 @@ const getCookie = () => {
});
};

const generateBLsid = () => {

This comment has been minimized.

Copy link
@TonyRL

TonyRL Oct 17, 2024

Collaborator

@pseudoyu You can reuse the same function from

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

This comment has been minimized.

Copy link
@pseudoyu

pseudoyu Oct 17, 2024

Author Collaborator

cool! thanks~

const randomString = Array.from({ length: 8 }, () => '0123456789ABCDEF'[Math.floor(Math.random() * 16)]).join('');
const timestamp = Date.now();
return `${randomString}_${timestamp.toString(16).toUpperCase()}`;
};

const clearCookie = () => {
cache.set('bili-cookie');
disableConfigCookie = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/bilibili/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const route: Route = {
},
],
name: 'UP 主投稿',
maintainers: ['DIYgod', 'Konano'],
maintainers: ['DIYgod', 'Konano', 'pseudoyu'],
handler,
};

Expand Down

0 comments on commit 578e2f1

Please sign in to comment.