koishi-plugin-bilibili-notify 3.2.6-alpha.0 → 3.2.7-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -94412,6 +94412,11 @@ var BiliAPI = class extends koishi.Service {
94412
94412
  loginNotifier;
94413
94413
  refreshCookieTimer;
94414
94414
  loginInfoIsLoaded = false;
94415
+ wbiSign = {
94416
+ img_key: "",
94417
+ sub_key: ""
94418
+ };
94419
+ updateJob;
94415
94420
  constructor(ctx, config) {
94416
94421
  super(ctx, "ba");
94417
94422
  this.apiConfig = config;
@@ -94422,11 +94427,25 @@ var BiliAPI = class extends koishi.Service {
94422
94427
  this.cacheable.install(node_http.default.globalAgent);
94423
94428
  this.cacheable.install(node_https.default.globalAgent);
94424
94429
  await this.createNewClient();
94425
- this.loadCookiesFromDatabase();
94430
+ await this.loadCookiesFromDatabase();
94431
+ await this.updateBiliTicket();
94432
+ this.updateJob = new cron.CronJob("0 0 * * *", async () => {
94433
+ await this.updateBiliTicket();
94434
+ });
94435
+ this.updateJob.start();
94426
94436
  }
94427
94437
  stop() {
94428
94438
  this.cacheable.uninstall(node_http.default.globalAgent);
94429
94439
  this.cacheable.uninstall(node_https.default.globalAgent);
94440
+ this.updateJob.stop();
94441
+ }
94442
+ async updateBiliTicket() {
94443
+ const csrf = this.getCSRF();
94444
+ const ticket = await this.getBiliTicket(csrf);
94445
+ if (ticket.code !== 0) throw new Error(`获取BiliTicket失败: ${ticket.message}`);
94446
+ this.jar.setCookieSync(`bili_ticket=${ticket.data.ticket}; path=/; domain=.bilibili.com`, "https://www.bilibili.com");
94447
+ this.wbiSign.img_key = ticket.data.nav.img.slice(ticket.data.nav.img.lastIndexOf("/") + 1, ticket.data.nav.img.lastIndexOf("."));
94448
+ this.wbiSign.sub_key = ticket.data.nav.sub.slice(ticket.data.nav.sub.lastIndexOf("/") + 1, ticket.data.nav.sub.lastIndexOf("."));
94430
94449
  }
94431
94450
  getMixinKey = (orig) => mixinKeyEncTab.map((n$2) => orig[n$2]).join("").slice(0, 32);
94432
94451
  encWbi(params, img_key, sub_key) {
@@ -94442,7 +94461,7 @@ var BiliAPI = class extends koishi.Service {
94442
94461
  return `${query}&w_rid=${wbi_sign}`;
94443
94462
  }
94444
94463
  async getWbi(params) {
94445
- const web_keys = await this.getWbiKeys();
94464
+ const web_keys = this.wbiSign || await this.getWbiKeys();
94446
94465
  const img_key = web_keys.img_key;
94447
94466
  const sub_key = web_keys.sub_key;
94448
94467
  const query = this.encWbi(params, img_key, sub_key);
@@ -94587,16 +94606,39 @@ var BiliAPI = class extends koishi.Service {
94587
94606
  disposeNotifier() {
94588
94607
  if (this.loginNotifier) this.loginNotifier.dispose();
94589
94608
  }
94590
- getRandomUserAgent() {
94591
- const userAgents = [
94592
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
94593
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
94594
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
94595
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0",
94596
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
94597
- ];
94598
- const index = Math.floor(Math.random() * userAgents.length);
94599
- return userAgents[index];
94609
+ /**
94610
+ * Generate HMAC-SHA256 signature
94611
+ * @param {string} key The key string to use for the HMAC-SHA256 hash
94612
+ * @param {string} message The message string to hash
94613
+ * @returns {string} The HMAC-SHA256 signature as a hex string
94614
+ */
94615
+ hmacSha256(key, message$1) {
94616
+ const hmac = node_crypto.default.createHmac("sha256", key);
94617
+ hmac.update(message$1);
94618
+ return hmac.digest("hex");
94619
+ }
94620
+ /**
94621
+ * Get Bilibili web ticket
94622
+ * @param {string} csrf CSRF token, can be empty or null
94623
+ * @returns {Promise<any>} Promise of the ticket response in JSON format
94624
+ */
94625
+ async getBiliTicket(csrf) {
94626
+ const ts = Math.floor(Date.now() / 1e3);
94627
+ const hexSign = this.hmacSha256("XgwSnGZ1p", `ts${ts}`);
94628
+ const params = new URLSearchParams({
94629
+ key_id: "ec02",
94630
+ hexsign: hexSign,
94631
+ "context[ts]": ts.toString(),
94632
+ csrf: csrf || ""
94633
+ });
94634
+ const url = "https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket";
94635
+ const resp = await this.client.post(`${url}?${params.toString()}`, {}, { headers: {
94636
+ "Content-Type": "application/x-www-form-urlencoded",
94637
+ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
94638
+ } }).catch((e$1) => {
94639
+ throw e$1;
94640
+ });
94641
+ return resp.data;
94600
94642
  }
94601
94643
  async createNewClient() {
94602
94644
  const wrapper = (await import("axios-cookiejar-support")).wrapper;
@@ -94605,7 +94647,7 @@ var BiliAPI = class extends koishi.Service {
94605
94647
  jar: this.jar,
94606
94648
  headers: {
94607
94649
  "Content-Type": "application/json",
94608
- "User-Agent": this.apiConfig.userAgent !== "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" ? this.apiConfig.userAgent : this.getRandomUserAgent(),
94650
+ "User-Agent": this.apiConfig.userAgent || "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
94609
94651
  Origin: "https://www.bilibili.com",
94610
94652
  Referer: "https://www.bilibili.com/"
94611
94653
  }
@@ -95113,7 +95155,7 @@ const Config = koishi.Schema.object({
95113
95155
  masterAccountGuildId: koishi.Schema.string().role("secret").description("主人账号所在的群组ID,只有在QQ频道、Discord这样的环境才需要填写,请使用inspect插件获取群组ID")
95114
95156
  }), koishi.Schema.object({})])]),
95115
95157
  basicSettings: koishi.Schema.object({}).description("基本设置"),
95116
- userAgent: koishi.Schema.string().required().description("设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111"),
95158
+ userAgent: koishi.Schema.string().description("设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111"),
95117
95159
  subTitle: koishi.Schema.object({}).description("订阅配置"),
95118
95160
  sub: koishi.Schema.array(koishi.Schema.object({
95119
95161
  name: koishi.Schema.string().description("订阅用户昵称,只是给你自己看的(相当于备注),可填可不填"),
package/lib/index.mjs CHANGED
@@ -94414,6 +94414,11 @@ var BiliAPI = class extends Service {
94414
94414
  loginNotifier;
94415
94415
  refreshCookieTimer;
94416
94416
  loginInfoIsLoaded = false;
94417
+ wbiSign = {
94418
+ img_key: "",
94419
+ sub_key: ""
94420
+ };
94421
+ updateJob;
94417
94422
  constructor(ctx, config) {
94418
94423
  super(ctx, "ba");
94419
94424
  this.apiConfig = config;
@@ -94424,11 +94429,25 @@ var BiliAPI = class extends Service {
94424
94429
  this.cacheable.install(http.globalAgent);
94425
94430
  this.cacheable.install(https.globalAgent);
94426
94431
  await this.createNewClient();
94427
- this.loadCookiesFromDatabase();
94432
+ await this.loadCookiesFromDatabase();
94433
+ await this.updateBiliTicket();
94434
+ this.updateJob = new CronJob("0 0 * * *", async () => {
94435
+ await this.updateBiliTicket();
94436
+ });
94437
+ this.updateJob.start();
94428
94438
  }
94429
94439
  stop() {
94430
94440
  this.cacheable.uninstall(http.globalAgent);
94431
94441
  this.cacheable.uninstall(https.globalAgent);
94442
+ this.updateJob.stop();
94443
+ }
94444
+ async updateBiliTicket() {
94445
+ const csrf = this.getCSRF();
94446
+ const ticket = await this.getBiliTicket(csrf);
94447
+ if (ticket.code !== 0) throw new Error(`获取BiliTicket失败: ${ticket.message}`);
94448
+ this.jar.setCookieSync(`bili_ticket=${ticket.data.ticket}; path=/; domain=.bilibili.com`, "https://www.bilibili.com");
94449
+ this.wbiSign.img_key = ticket.data.nav.img.slice(ticket.data.nav.img.lastIndexOf("/") + 1, ticket.data.nav.img.lastIndexOf("."));
94450
+ this.wbiSign.sub_key = ticket.data.nav.sub.slice(ticket.data.nav.sub.lastIndexOf("/") + 1, ticket.data.nav.sub.lastIndexOf("."));
94432
94451
  }
94433
94452
  getMixinKey = (orig) => mixinKeyEncTab.map((n$2) => orig[n$2]).join("").slice(0, 32);
94434
94453
  encWbi(params, img_key, sub_key) {
@@ -94444,7 +94463,7 @@ var BiliAPI = class extends Service {
94444
94463
  return `${query}&w_rid=${wbi_sign}`;
94445
94464
  }
94446
94465
  async getWbi(params) {
94447
- const web_keys = await this.getWbiKeys();
94466
+ const web_keys = this.wbiSign || await this.getWbiKeys();
94448
94467
  const img_key = web_keys.img_key;
94449
94468
  const sub_key = web_keys.sub_key;
94450
94469
  const query = this.encWbi(params, img_key, sub_key);
@@ -94589,16 +94608,39 @@ var BiliAPI = class extends Service {
94589
94608
  disposeNotifier() {
94590
94609
  if (this.loginNotifier) this.loginNotifier.dispose();
94591
94610
  }
94592
- getRandomUserAgent() {
94593
- const userAgents = [
94594
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
94595
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
94596
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
94597
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0",
94598
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
94599
- ];
94600
- const index = Math.floor(Math.random() * userAgents.length);
94601
- return userAgents[index];
94611
+ /**
94612
+ * Generate HMAC-SHA256 signature
94613
+ * @param {string} key The key string to use for the HMAC-SHA256 hash
94614
+ * @param {string} message The message string to hash
94615
+ * @returns {string} The HMAC-SHA256 signature as a hex string
94616
+ */
94617
+ hmacSha256(key, message$1) {
94618
+ const hmac = crypto.createHmac("sha256", key);
94619
+ hmac.update(message$1);
94620
+ return hmac.digest("hex");
94621
+ }
94622
+ /**
94623
+ * Get Bilibili web ticket
94624
+ * @param {string} csrf CSRF token, can be empty or null
94625
+ * @returns {Promise<any>} Promise of the ticket response in JSON format
94626
+ */
94627
+ async getBiliTicket(csrf) {
94628
+ const ts = Math.floor(Date.now() / 1e3);
94629
+ const hexSign = this.hmacSha256("XgwSnGZ1p", `ts${ts}`);
94630
+ const params = new URLSearchParams({
94631
+ key_id: "ec02",
94632
+ hexsign: hexSign,
94633
+ "context[ts]": ts.toString(),
94634
+ csrf: csrf || ""
94635
+ });
94636
+ const url = "https://api.bilibili.com/bapis/bilibili.api.ticket.v1.Ticket/GenWebTicket";
94637
+ const resp = await this.client.post(`${url}?${params.toString()}`, {}, { headers: {
94638
+ "Content-Type": "application/x-www-form-urlencoded",
94639
+ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
94640
+ } }).catch((e$1) => {
94641
+ throw e$1;
94642
+ });
94643
+ return resp.data;
94602
94644
  }
94603
94645
  async createNewClient() {
94604
94646
  const wrapper = (await import("axios-cookiejar-support")).wrapper;
@@ -94607,7 +94649,7 @@ var BiliAPI = class extends Service {
94607
94649
  jar: this.jar,
94608
94650
  headers: {
94609
94651
  "Content-Type": "application/json",
94610
- "User-Agent": this.apiConfig.userAgent !== "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" ? this.apiConfig.userAgent : this.getRandomUserAgent(),
94652
+ "User-Agent": this.apiConfig.userAgent || "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0",
94611
94653
  Origin: "https://www.bilibili.com",
94612
94654
  Referer: "https://www.bilibili.com/"
94613
94655
  }
@@ -95115,7 +95157,7 @@ const Config = Schema.object({
95115
95157
  masterAccountGuildId: Schema.string().role("secret").description("主人账号所在的群组ID,只有在QQ频道、Discord这样的环境才需要填写,请使用inspect插件获取群组ID")
95116
95158
  }), Schema.object({})])]),
95117
95159
  basicSettings: Schema.object({}).description("基本设置"),
95118
- userAgent: Schema.string().required().description("设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111"),
95160
+ userAgent: Schema.string().description("设置请求头User-Agen,请求出现-352时可以尝试修改,UA获取方法可参考:https://blog.csdn.net/qq_44503987/article/details/104929111"),
95119
95161
  subTitle: Schema.object({}).description("订阅配置"),
95120
95162
  sub: Schema.array(Schema.object({
95121
95163
  name: Schema.string().description("订阅用户昵称,只是给你自己看的(相当于备注),可填可不填"),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-bilibili-notify",
3
3
  "description": "Koishi bilibili notify plugin",
4
- "version": "3.2.6-alpha.0",
4
+ "version": "3.2.7-alpha.0",
5
5
  "contributors": [
6
6
  "Akokko <admin@akokko.com>"
7
7
  ],
package/readme.md CHANGED
@@ -277,26 +277,23 @@ uid为必填参数,为要推送的UP主的UID,index为可选参数,为要
277
277
  > - ver 3.2.3 优化:移除不必要的代码;
278
278
  > - ver 3.2.4-alpha.0 优化:选项 `pushImgsInDynamic` 发送多图会以转发消息的格式发> 送; 新增:选项 `dynamicVideoUrlToBV` 开启后将链接转换为bv号以用作特殊用途;
279
279
  > - ver 3.2.4 修复:第一次使用插件时,使用登录指令报错; 插件配置页新增和删除提示信息;
280
- >
281
- > > [!WARNING]
282
- > > `3.2.5-alpha.x` 目前均为测试版本,请不要更新🙅
283
- > > - ver 3.2.5-alpha.0 优化:新增 `DNS` 缓存,以减少DNS错误;
284
- > > - ver 3.2.5-alpha.1 测试版本
285
- > > - ver 3.2.5-alpha.2 测试版本
286
- > > - ver 3.2.5-alpha.3 更新依赖版本
287
- > > - ver 3.2.5-alpha.4 测试版本
288
- > > - ver 3.2.5-alpha.5 测试版本
289
- > > - ver 3.2.5-alpha.6 测试版本
290
- > > - ver 3.2.5-alpha.7 测试版本
291
- > > - ver 3.2.5-alpha.8 测试版本
292
- > > - ver 3.2.5-alpha.9 测试版本
293
- > > - ver 3.2.5-alpha.10 测试版本
294
- > > - ver 3.2.5-alpha.11 测试版本
295
- > > - ver 3.2.5-alpha.12 测试版本
296
- > > - ver 3.2.5-alpha.13 测试版本
297
- >
280
+ > - ver 3.2.5-alpha.0 优化:新增 `DNS` 缓存,以减少DNS错误;
281
+ > - ver 3.2.5-alpha.1 测试版本
282
+ > - ver 3.2.5-alpha.2 测试版本
283
+ > - ver 3.2.5-alpha.3 更新依赖版本
284
+ > - ver 3.2.5-alpha.4 测试版本
285
+ > - ver 3.2.5-alpha.5 测试版本
286
+ > - ver 3.2.5-alpha.6 测试版本
287
+ > - ver 3.2.5-alpha.7 测试版本
288
+ > - ver 3.2.5-alpha.8 测试版本
289
+ > - ver 3.2.5-alpha.9 测试版本
290
+ > - ver 3.2.5-alpha.10 测试版本
291
+ > - ver 3.2.5-alpha.11 测试版本
292
+ > - ver 3.2.5-alpha.12 测试版本
293
+ > - ver 3.2.5-alpha.13 测试版本
298
294
  > - ver 3.2.5 重构:消息推送逻辑; 新增:选项 `dynamicCron`;
299
295
  > - ver 3.2.6-alpha.0 优化:更新依赖; 修复:长图动态向下箭头无法显示的bug;
296
+ > - ver 3.2.7-alpha.0 优化:将选项 `userAgent` 更改为可选项,在 `cookies` 中添加 `bili_ticket` 降低风控概率;
300
297
 
301
298
  ## 交流群
302
299