diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index b40ab667..616ccb71 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -41,8 +41,8 @@ export class NTQQFileApi { this.context = context; this.core = core; this.rkeyManager = new RkeyManager([ - 'https://ss.xingzhige.com/music_card/rkey', // 国内 - 'https://secret-service.bietiaop.com/rkeys',//国内 + 'https://secret-service.bietiaop.com/rkeys', + 'http://ss.xingzhige.com/music_card/rkey', ], this.context.logger ); diff --git a/src/core/helper/rkey.ts b/src/core/helper/rkey.ts index 5425279c..47f748d4 100644 --- a/src/core/helper/rkey.ts +++ b/src/core/helper/rkey.ts @@ -6,7 +6,13 @@ interface ServerRkeyData { private_rkey: string; expired_time: number; } - +interface OneBotApiRet { + status: string, + retcode: number, + data: ServerRkeyData, + message: string, + wording: string, +} interface UrlFailureInfo { count: number; lastTimestamp: number; @@ -21,7 +27,7 @@ export class RkeyManager { expired_time: 0, }; private urlFailures: Map = new Map(); - private readonly FAILURE_LIMIT: number = 8; + private readonly FAILURE_LIMIT: number = 4; private readonly ONE_DAY: number = 24 * 60 * 60 * 1000; constructor(serverUrl: string[], logger: LogWrapper) { @@ -99,7 +105,11 @@ export class RkeyManager { for (const url of availableUrls) { try { - const temp = await RequestUtil.HttpGetJson(url, 'GET'); + let temp = await RequestUtil.HttpGetJson(url, 'GET'); + if ('retcode' in temp) { + // 支持Onebot Ret风格 + temp = (temp as unknown as OneBotApiRet).data; + } this.rkeyData = { group_rkey: temp.group_rkey.slice(6), private_rkey: temp.private_rkey.slice(6), diff --git a/src/onebot/action/index.ts b/src/onebot/action/index.ts index 86f4f08a..b2d8ef9b 100644 --- a/src/onebot/action/index.ts +++ b/src/onebot/action/index.ts @@ -112,10 +112,14 @@ import SetGroupRemark from './extends/SetGroupRemark'; import { MoveGroupFile } from './extends/MoveGroupFile'; import { TransGroupFile } from './extends/TransGroupFile'; import { RenameGroupFile } from './extends/RenameGroupFile'; +import { GetRkeyServer } from './packet/GetRkeyServer'; +import { GetRkeyEx } from './packet/GetRkeyEx'; export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore) { const actionHandlers = [ + new GetRkeyEx(obContext, core), + new GetRkeyServer(obContext, core), new SetGroupRemark(obContext, core), new GetGroupInfoEx(obContext, core), new FetchEmojiLike(obContext, core), diff --git a/src/onebot/action/packet/GetRkeyEx.ts b/src/onebot/action/packet/GetRkeyEx.ts new file mode 100644 index 00000000..d330b8ee --- /dev/null +++ b/src/onebot/action/packet/GetRkeyEx.ts @@ -0,0 +1,18 @@ +import { ActionName } from '@/onebot/action/router'; +import { GetPacketStatusDepends } from '@/onebot/action/packet/GetPacketStatus'; + +export class GetRkeyEx extends GetPacketStatusDepends { + override actionName = ActionName.GetRkeyEx; + + async _handle() { + let rkeys = await this.core.apis.PacketApi.pkt.operation.FetchRkey(); + return rkeys.map(rkey => { + return { + type: rkey.type === 10 ? "private" : "group", + rkey: rkey.rkey, + created_at: rkey.time, + ttl: rkey.ttl, + }; + }); + } +} \ No newline at end of file diff --git a/src/onebot/action/packet/GetRkeyServer.ts b/src/onebot/action/packet/GetRkeyServer.ts new file mode 100644 index 00000000..96129dc0 --- /dev/null +++ b/src/onebot/action/packet/GetRkeyServer.ts @@ -0,0 +1,38 @@ +import { ActionName } from '@/onebot/action/router'; +import { GetPacketStatusDepends } from '@/onebot/action/packet/GetPacketStatus'; + +export class GetRkeyServer extends GetPacketStatusDepends { + override actionName = ActionName.GetRkeyServer; + + private rkeyCache: { + private_rkey?: string; + group_rkey?: string; + expired_time?: number; + name: string; + } | null = null; + private expiryTime: number | null = null; + + async _handle() { + // 检查缓存是否有效 + if (this.expiryTime && this.expiryTime > Math.floor(Date.now() / 1000) && this.rkeyCache) { + return this.rkeyCache; + } + + // 获取新的 Rkey + let rkeys = await this.core.apis.PacketApi.pkt.operation.FetchRkey(); + let privateRkeyItem = rkeys.filter(rkey => rkey.type === 10)[0]; + let groupRkeyItem = rkeys.filter(rkey => rkey.type === 20)[0]; + + this.expiryTime = Math.floor(Date.now() / 1000) + 3600; // 假设缓存有效期为 1 小时 + + // 更新缓存 + this.rkeyCache = { + private_rkey: privateRkeyItem ? privateRkeyItem.rkey : undefined, + group_rkey: groupRkeyItem ? groupRkeyItem.rkey : undefined, + expired_time: this.expiryTime, + name: "NapCat 4" + }; + + return this.rkeyCache; + } +} \ No newline at end of file diff --git a/src/onebot/action/router.ts b/src/onebot/action/router.ts index 2c41ffb3..d25723eb 100644 --- a/src/onebot/action/router.ts +++ b/src/onebot/action/router.ts @@ -10,6 +10,8 @@ export interface InvalidCheckResult { } export const ActionName = { + GetRkeyEx: 'get_rkey', + GetRkeyServer: 'get_rkey_server', SetGroupRemark: 'set_group_remark', NapCat_GetPrivateFileUrl: 'get_private_file_url', ClickInlineKeyboardButton: 'click_inline_keyboard_button',