diff --git a/src/core/apis/webapi.ts b/src/core/apis/webapi.ts index 6e3c1218..63cf2247 100644 --- a/src/core/apis/webapi.ts +++ b/src/core/apis/webapi.ts @@ -338,4 +338,12 @@ export class NTQQWebApi { } return (hash & 0x7FFFFFFF).toString(); } + public getBknFromSKey(sKey: string) { + let hash = 5381; + for (let i = 0; i < sKey.length; i++) { + const code = sKey.charCodeAt(i); + hash = hash + (hash << 5) + code; + } + return (hash & 0x7FFFFFFF).toString(); + } } diff --git a/src/onebot/action/index.ts b/src/onebot/action/index.ts index 1584d1c9..331de2c3 100644 --- a/src/onebot/action/index.ts +++ b/src/onebot/action/index.ts @@ -92,6 +92,7 @@ import { GetGroupMemberList } from './group/GetGroupMemberList'; import { GetGroupFileUrl } from "@/onebot/action/file/GetGroupFileUrl"; import { GetPacketStatus } from "@/onebot/action/packet/GetPacketStatus"; import { FriendPoke } from "@/onebot/action/user/FriendPoke"; +import { GetCredentials } from './system/GetCredentials'; export type ActionMap = Map>; @@ -180,6 +181,7 @@ export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCo new SetModelShow(obContext, core), new SetInputStatus(obContext, core), new GetCSRF(obContext, core), + new GetCredentials(obContext, core), new DelGroupNotice(obContext, core), new DeleteGroupFile(obContext, core), new CreateGroupFileFolder(obContext, core), diff --git a/src/onebot/action/system/GetCSRF.ts b/src/onebot/action/system/GetCSRF.ts index 9cab912e..68945adf 100644 --- a/src/onebot/action/system/GetCSRF.ts +++ b/src/onebot/action/system/GetCSRF.ts @@ -5,8 +5,12 @@ export class GetCSRF extends BaseAction { actionName = ActionName.GetCSRF; async _handle(payload: any) { + const sKey = await this.core.apis.UserApi.getSKey(); + if (!sKey) { + throw new Error('SKey is undefined'); + } return { - token: '', + token: +this.core.apis.WebApi.getBknFromSKey(sKey), }; } } diff --git a/src/onebot/action/system/GetCredentials.ts b/src/onebot/action/system/GetCredentials.ts new file mode 100644 index 00000000..96293905 --- /dev/null +++ b/src/onebot/action/system/GetCredentials.ts @@ -0,0 +1,31 @@ +import BaseAction from '../BaseAction'; +import { ActionName } from '../types'; +import { FromSchema, JSONSchema } from 'json-schema-to-ts'; + +interface Response { + cookies: string, + token: number +} + +const SchemaData = { + type: 'object', + properties: { + domain: { type: 'string' }, + }, + required: ['domain'], +} as const satisfies JSONSchema; + +type Payload = FromSchema; + +export class GetCredentials extends BaseAction { + actionName = ActionName.GetCredentials; + payloadSchema = SchemaData; + + async _handle(payload: Payload) { + const cookiesObject = await this.core.apis.UserApi.getCookies(payload.domain); + //把获取到的cookiesObject转换成 k=v; 格式字符串拼接在一起 + const cookies = Object.entries(cookiesObject).map(([key, value]) => `${key}=${value}`).join('; '); + const bkn = cookiesObject?.skey ? this.core.apis.WebApi.getBknFromCookie(cookiesObject) : ''; + return { cookies: cookies, token: +bkn }; + } +} diff --git a/src/onebot/action/types.ts b/src/onebot/action/types.ts index deda296f..736fbe51 100644 --- a/src/onebot/action/types.ts +++ b/src/onebot/action/types.ts @@ -43,8 +43,8 @@ export enum ActionName { GetGroupMemberList = 'get_group_member_list', GetGroupHonorInfo = 'get_group_honor_info', GetCookies = 'get_cookies', - // GetCSRF = 'get_csrf_token', - // GetCredentials = 'get_credentials', + GetCSRF = 'get_csrf_token', + GetCredentials = 'get_credentials', GetRecord = 'get_record', GetImage = 'get_image', CanSendImage = 'can_send_image',