-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize: risk control, add bili_ticket
- Loading branch information
Showing
5 changed files
with
111 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# 2.0.4 | ||
* 优化B站风控相关,新增bili_tiket参数 | ||
* fix Repeated Instantiation puppeteer | ||
* 优化获取B站登录ck | ||
* 添加截图列队,优化配置文件注释 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { createHmac } from 'crypto'; | ||
import { BiliApi } from '@/models/bilibili/bilibili.api'; | ||
|
||
/** | ||
* Generate HMAC-SHA256 signature | ||
* @param {string} key The key string to use for the HMAC-SHA256 hash | ||
* @param {string} message The message string to hash | ||
* @returns {string} The HMAC-SHA256 signature as a hex string | ||
*/ | ||
function hmacSha256(key: string, message: string): string { | ||
return createHmac('sha256', key).update(message).digest('hex'); | ||
} | ||
|
||
/** | ||
* Get Bilibili web ticket | ||
* @param {string | null} csrf CSRF token, can be empty or null, or the cookie's bili_jct value | ||
* @returns {Promise<{ code: number, ticket: string, created_at: number, ttl: number }>} | ||
* Promise that resolves to an object containing code, ticket, created_at, and ttl values | ||
*/ | ||
export async function getBiliTicket(csrf: string | null): Promise<{ code?: number; ticket?: string; created_at?: number; ttl?: number }> { | ||
const ts = Math.floor(Date.now() / 1000); | ||
const hexSign = hmacSha256('XgwSnGZ1p', `ts${ts}`); | ||
const url = 'https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket'; | ||
|
||
const params = new URLSearchParams({ | ||
key_id: 'ec02', | ||
hexsign: hexSign, | ||
'context[ts]': String(ts), | ||
csrf: csrf ?? '' // 使用空字符串代替null | ||
}); | ||
|
||
try { | ||
const response = await fetch(`${url}?${params}`, { | ||
method: 'POST', | ||
headers: { | ||
'User-Agent': BiliApi.BILIBILI_HEADERS['User-Agent'] | ||
} | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`get bili_jct HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
if (data.code !== 0) { | ||
if (data.code === 400) { | ||
throw new Error(`get bili_jct Parameter error! ${data.message}`); | ||
} | ||
throw new Error(`Failed to retrieve bili ticket: ${data.message}`); | ||
} | ||
|
||
// 返回所需的对象结构 | ||
return { | ||
code: data.code, | ||
ticket: data.data?.ticket, | ||
created_at: data.data?.created_at, | ||
ttl: data.data?.ttl | ||
}; | ||
|
||
} catch (error) { | ||
throw new Error(`Failed to fetch Bilibili ticket: ${error instanceof Error ? error.message : String(error)}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters