diff --git a/package.json b/package.json index e783ef2..da5281d 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "dotenv": "^16.4.5", "feed": "^4.2.2", "hono": "^4.2.2", + "md5": "^2.3.0", "node-cache": "^5.1.2", "winston": "^3.13.0" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c25c2a8..57c97bf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ dependencies: hono: specifier: ^4.2.2 version: 4.2.2 + md5: + specifier: ^2.3.0 + version: 2.3.0 node-cache: specifier: ^5.1.2 version: 5.1.2 @@ -615,6 +618,10 @@ packages: supports-color: 7.2.0 dev: true + /charenc@0.0.2: + resolution: {integrity: sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==} + dev: false + /cheerio-select@2.1.0: resolution: {integrity: sha512-9v9kG0LvzrlcungtnJtpGNxY+fzECQKhK4EGJX2vByejiMX84MFNQw4UxPJl3bFbTMw+Dfs37XaIkCwTZfLh4g==} dependencies: @@ -705,6 +712,10 @@ packages: which: 2.0.2 dev: true + /crypt@0.0.2: + resolution: {integrity: sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==} + dev: false + /css-select@5.1.0: resolution: {integrity: sha512-nwoRF1rvRRnnCqqY7updORDsuqKzqYJ28+oSMaJMMgOauh3fvwHqMS7EZpIPqK8GL+g9mKxF1vP/ZjSeNjEVHg==} dependencies: @@ -1139,6 +1150,10 @@ packages: resolution: {integrity: sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==} dev: false + /is-buffer@1.1.6: + resolution: {integrity: sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==} + dev: false + /is-extglob@2.1.1: resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==} engines: {node: '>=0.10.0'} @@ -1237,6 +1252,14 @@ packages: yallist: 4.0.0 dev: true + /md5@2.3.0: + resolution: {integrity: sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==} + dependencies: + charenc: 0.0.2 + crypt: 0.0.2 + is-buffer: 1.1.6 + dev: false + /merge2@1.4.1: resolution: {integrity: sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==} engines: {node: '>= 8'} diff --git a/src/routes/bilibili.ts b/src/routes/bilibili.ts index 3bed74a..1aaaad0 100644 --- a/src/routes/bilibili.ts +++ b/src/routes/bilibili.ts @@ -1,6 +1,7 @@ import type { RouterData, ListContext, Options } from "../types.js"; import type { RouterType } from "../router.types.js"; import { get } from "../utils/getData.js"; +import getBiliWbi from "../utils/getBiliWbi.js"; export const handleRoute = async (c: ListContext, noCache: boolean) => { const type = c.req.query("type") || "0"; @@ -41,11 +42,14 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => { const getList = async (options: Options, noCache: boolean) => { const { type } = options; - const url = `https://api.bilibili.com/x/web-interface/ranking/v2?tid=${type}`; + const wbiData = await getBiliWbi(); + const url = `https://api.bilibili.com/x/web-interface/ranking/v2?tid=${type}&type=all&${wbiData}`; const result = await get({ url, headers: { Referer: `https://www.bilibili.com/ranking/all`, + "User-Agent": + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36", }, noCache, }); diff --git a/src/utils/getBiliWbi.ts b/src/utils/getBiliWbi.ts new file mode 100644 index 0000000..ceae8db --- /dev/null +++ b/src/utils/getBiliWbi.ts @@ -0,0 +1,85 @@ +// 获取 Bilibili Web 端 WBI 签名鉴权 +import { getCache, setCache } from "./cache.js"; +import { get } from "./getData.js"; +import md5 from "md5"; + +type EncodedKeys = { + img_key: string; + sub_key: string; +}; + +interface WbiParams { + [key: string]: string | number; +} + +const mixinKeyEncTab = [ + 46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42, 19, 29, 28, + 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, + 21, 56, 59, 6, 63, 57, 62, 11, 36, 20, 34, 44, 52, +]; + +// 对 imgKey 和 subKey 进行字符顺序打乱编码 +const getMixinKey = (orig: string): string => + mixinKeyEncTab + .map((n) => orig[n]) + .join("") + .slice(0, 32); + +// 为请求参数进行 wbi 签名 +const encWbi = (params: WbiParams, img_key: string, sub_key: string): string => { + const mixin_key = getMixinKey(img_key + sub_key); + const curr_time = Math.round(Date.now() / 1000); + const chr_filter = /[!'()*]/g; + // 添加 wts 字段 + Object.assign(params, { wts: curr_time }); + // 按照 key 重排参数 + const query = Object.keys(params) + .sort() + .map((key) => { + // 过滤 value 中的 "!'()*" 字符 + const value = params[key].toString().replace(chr_filter, ""); + return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`; + }) + .join("&"); + // 计算 w_rid + const wbi_sign = md5(query + mixin_key); + return query + "&w_rid=" + wbi_sign; +}; + +// 获取最新的 img_key 和 sub_key +const getWbiKeys = async (): Promise => { + const result = await get({ + url: "https://api.bilibili.com/x/web-interface/nav", + headers: { + // SESSDATA 字段 + Cookie: "SESSDATA=xxxxxx", + "User-Agent": + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3", + Referer: "https://www.bilibili.com/", + }, + }); + const img_url: string = result.data.wbi_img?.img_url ?? ""; + const sub_url: string = result.data.wbi_img?.sub_url ?? ""; + return { + img_key: img_url.slice(img_url.lastIndexOf("/") + 1, img_url.lastIndexOf(".")), + sub_key: sub_url.slice(sub_url.lastIndexOf("/") + 1, sub_url.lastIndexOf(".")), + }; +}; + +const getBiliWbi = async (): Promise => { + const cachedData = getCache("bilibili-wbi"); + console.log(cachedData); + if (cachedData && typeof cachedData === "object" && "wbi" in cachedData) { + const { wbi } = cachedData as { wbi: string }; + return wbi; + } + const web_keys = await getWbiKeys(); + const params = { foo: "114", bar: "514", baz: 1919810 }; + const img_key = web_keys.img_key; + const sub_key = web_keys.sub_key; + const query = encWbi(params, img_key, sub_key); + setCache("bilibili-wbi", { wbi: query }); + return query; +}; + +export default getBiliWbi;