From e6b6947d49bc1e43941d9c9763043403347cef15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Tue, 22 Oct 2024 11:04:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=A0=87=E5=87=86=E5=8C=96=E5=87=AD?= =?UTF-8?q?=E6=8D=AE=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/apis/webapi.ts | 8 ++++++ src/onebot/action/index.ts | 2 ++ src/onebot/action/system/GetCSRF.ts | 6 ++++- src/onebot/action/system/GetCredentials.ts | 31 ++++++++++++++++++++++ src/onebot/action/types.ts | 4 +-- 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/onebot/action/system/GetCredentials.ts 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',