diff --git a/lib/routes/bilibili/cache.ts b/lib/routes/bilibili/cache.ts index 0bd54c3bac..f567a8cc73 100644 --- a/lib/routes/bilibili/cache.ts +++ b/lib/routes/bilibili/cache.ts @@ -7,10 +7,8 @@ import logger from '@/utils/logger'; import puppeteer from '@/utils/puppeteer'; import { JSDOM } from 'jsdom'; -const disableConfigCookie = false; - const getCookie = () => { - if (!disableConfigCookie && Object.keys(config.bilibili.cookies).length > 0) { + if (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]; diff --git a/lib/routes/bilibili/check-cookie.ts b/lib/routes/bilibili/check-cookie.ts new file mode 100644 index 0000000000..97bd1ad3ea --- /dev/null +++ b/lib/routes/bilibili/check-cookie.ts @@ -0,0 +1,31 @@ +import { APIRoute } from '@/types'; +import cacheIn from './cache'; +import ofetch from '@/utils/ofetch'; + +export const apiRoute: APIRoute = { + path: '/check-cookie', + description: '检查 bilibili cookie 是否有效', + maintainers: ['DIYgod'], + handler, +}; + +async function handler() { + const cookie = await cacheIn.getCookie(); + + if (!cookie) { + return { + code: -1, + }; + } + + const response = await ofetch(`https://api.bilibili.com/x/web-interface/nav`, { + headers: { + Referer: `https://space.bilibili.com/1/`, + Cookie: cookie as string, + }, + }); + + return { + code: response.code === 0 && !!response.data.mid ? 0 : -1, + }; +}