From 5562a3251deff9b14fabd878a2a1653b3a46c6cf Mon Sep 17 00:00:00 2001 From: linyuchen Date: Tue, 16 Apr 2024 23:55:21 +0800 Subject: [PATCH] feat: get cookies --- manifest.json | 4 +-- src/ntqqapi/api/user.ts | 39 ++++++++++++++++++++++++--- src/ntqqapi/api/webapi.ts | 12 ++------- src/onebot11/action/index.ts | 3 ++- src/onebot11/action/types.ts | 1 + src/onebot11/action/user/GetCookie.ts | 12 +++++++++ src/version.ts | 2 +- 7 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 src/onebot11/action/user/GetCookie.ts diff --git a/manifest.json b/manifest.json index b6ea390..b337eeb 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,10 @@ { "manifest_version": 4, "type": "extension", - "name": "LLOneBot v3.22.1", + "name": "LLOneBot v3.23.0", "slug": "LLOneBot", "description": "使你的NTQQ支持OneBot11协议进行QQ机器人开发, 不支持商店在线更新", - "version": "3.22.1", + "version": "3.23.0", "icon": "./icon.jpg", "authors": [ { diff --git a/src/ntqqapi/api/user.ts b/src/ntqqapi/api/user.ts index 26b6255..60e778e 100644 --- a/src/ntqqapi/api/user.ts +++ b/src/ntqqapi/api/user.ts @@ -1,9 +1,9 @@ import {callNTQQApi, GeneralCallResult, NTQQApiClass, NTQQApiMethod} from "../ntcall"; -import {SelfInfo, User} from "../types"; +import {Group, SelfInfo, User} from "../types"; import {ReceiveCmdS} from "../hook"; -import {uidMaps} from "../../common/data"; +import {selfInfo, uidMaps} from "../../common/data"; import {NTQQWindowApi, NTQQWindows} from "./window"; -import {isQQ998, sleep} from "../../common/utils"; +import {isQQ998, log, sleep} from "../../common/utils"; let userInfoCache: Record = {}; // uid: User @@ -68,7 +68,8 @@ export class NTQQUserApi{ return userInfo } - static async getPSkey() { + // return 'p_uin=o0xxx; p_skey=orXDssiGF8axxxxxxxxxxxxxx_; skey=' + static async getCookieWithoutSkey() { return await callNTQQApi({ className: NTQQApiClass.GROUP_HOME_WORK, methodName: NTQQApiMethod.UPDATE_SKEY, @@ -114,4 +115,34 @@ export class NTQQUserApi{ // }) } + static async getCookie(group: Group){ + let cookies = await this.getCookieWithoutSkey(); + let skey = "" + for (let i = 0; i < 2; i++) { + skey = (await this.getSkey(group.groupName, group.groupCode)).data; + skey = skey.trim(); + if (skey) { + break; + } + await sleep(1000); + } + if (!skey) { + throw new Error("获取skey失败"); + } + const bkn = NTQQUserApi.genBkn(skey); + cookies = cookies.replace("skey=;", `skey=${skey};`); + return {cookies, bkn}; + } + + static genBkn(sKey: string){ + sKey = sKey || ""; + 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(); + } } \ No newline at end of file diff --git a/src/ntqqapi/api/webapi.ts b/src/ntqqapi/api/webapi.ts index 74d397e..8fcfe9d 100644 --- a/src/ntqqapi/api/webapi.ts +++ b/src/ntqqapi/api/webapi.ts @@ -29,22 +29,14 @@ export class WebApi{ } private genBkn(sKey: string){ - sKey = sKey || ""; - 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(); + return NTQQUserApi.genBkn(sKey); } private async init(){ if (!WebApi.bkn) { const group = groups[0]; WebApi.skey = (await NTQQUserApi.getSkey(group.groupName, group.groupCode)).data; WebApi.bkn = this.genBkn(WebApi.skey); - let cookie = await NTQQUserApi.getPSkey(); + let cookie = await NTQQUserApi.getCookieWithoutSkey(); const pskeyRegex = /p_skey=([^;]+)/; const match = cookie.match(pskeyRegex); const pskeyValue = match ? match[1] : null; diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index 3e760a7..938bcef 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -40,6 +40,7 @@ import GoCQHTTPDownloadFile from "./go-cqhttp/DownloadFile"; import GoCQHTTPGetGroupMsgHistory from "./go-cqhttp/GetGroupMsgHistory"; import GetFile from "./file/GetFile"; import {GoCQHTTGetForwardMsgAction} from "./go-cqhttp/GetForwardMsg"; +import {GetCookies} from "./user/GetCookie"; export const actionHandlers = [ new GetFile(), @@ -72,7 +73,7 @@ export const actionHandlers = [ new GetImage(), new GetRecord(), new CleanCache(), - + new GetCookies(), //以下为go-cqhttp api new GoCQHTTPSendForwardMsg(), new GoCQHTTPSendGroupForwardMsg(), diff --git a/src/onebot11/action/types.ts b/src/onebot11/action/types.ts index c3e2f8b..789c772 100644 --- a/src/onebot11/action/types.ts +++ b/src/onebot11/action/types.ts @@ -50,6 +50,7 @@ export enum ActionName { GetImage = "get_image", GetRecord = "get_record", CleanCache = "clean_cache", + GetCookies = "get_cookies", // 以下为go-cqhttp api GoCQHTTP_SendForwardMsg = "send_forward_msg", GoCQHTTP_SendGroupForwardMsg = "send_group_forward_msg", diff --git a/src/onebot11/action/user/GetCookie.ts b/src/onebot11/action/user/GetCookie.ts new file mode 100644 index 0000000..9383543 --- /dev/null +++ b/src/onebot11/action/user/GetCookie.ts @@ -0,0 +1,12 @@ +import BaseAction from "../BaseAction"; +import {NTQQUserApi} from "../../../ntqqapi/api"; +import {groups} from "../../../common/data"; +import {ActionName} from "../types"; + +export class GetCookies extends BaseAction{ + actionName = ActionName.GetCookies; + + protected async _handle() { + return NTQQUserApi.getCookie(groups[0]) + } +} \ No newline at end of file diff --git a/src/version.ts b/src/version.ts index cfc57ab..e9b9090 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export const version = "3.22.1" \ No newline at end of file +export const version = "3.23.0" \ No newline at end of file