From e46d274a7522f855f2510d14fbb7fbf33a9d20d7 Mon Sep 17 00:00:00 2001 From: pk5ls20 Date: Tue, 5 Nov 2024 14:45:02 +0800 Subject: [PATCH] feat: add new NapCat config key: `packetBackend` - Acceptable values: `native`, `frida`, `auto`, `disable` - Default value is set to `auto` --- src/core/apis/packet.ts | 6 +++++- src/core/external/napcat.json | 3 ++- src/core/packet/highway/session.ts | 2 +- src/core/packet/session.ts | 17 ++++++++++++++++- src/onebot/action/packet/GetPacketStatus.ts | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/core/apis/packet.ts b/src/core/apis/packet.ts index 0bbb9cef..beb47362 100644 --- a/src/core/apis/packet.ts +++ b/src/core/apis/packet.ts @@ -61,7 +61,11 @@ export class NTQQPacketApi { this.qqVersion = qqversion; const table = typedOffset[qqversion + '-' + os.arch()]; if (!table) { - this.logger.logError('PacketServer Offset table not found for QQVersion: ', qqversion + '-' + os.arch()); + this.logger.logError('[Core] [Packet] PacketServer Offset table not found for QQVersion: ', qqversion + '-' + os.arch()); + return false; + } + if (this.core.configLoader.configData.packetBackend === 'disable') { + this.logger.logWarn('[Core] [Packet] 已禁用Packet后端,NapCat.Packet将不会加载!'); return false; } this.packetSession = new PacketSession(this.core); diff --git a/src/core/external/napcat.json b/src/core/external/napcat.json index 92432677..6abfe1e6 100644 --- a/src/core/external/napcat.json +++ b/src/core/external/napcat.json @@ -3,5 +3,6 @@ "consoleLog": true, "fileLogLevel": "debug", "consoleLogLevel": "info", + "packetBackend": "auto", "packetServer": "" -} \ No newline at end of file +} diff --git a/src/core/packet/highway/session.ts b/src/core/packet/highway/session.ts index c1c5b1d1..8cc534d3 100644 --- a/src/core/packet/highway/session.ts +++ b/src/core/packet/highway/session.ts @@ -59,7 +59,7 @@ export class PacketHighwaySession { private async checkAvailable() { if (!this.packetClient.available) { - throw new Error('packetServer不可用,请参照文档 https://napneko.github.io/config/advanced 检查packetServer状态或进行配置'); + throw new Error('packetBackend不可用,请参照文档 https://napneko.github.io/config/advanced 和启动日志检查packetBackend状态或进行配置!'); } if (this.sig.sigSession === null || this.sig.sessionKey === null) { this.logger.logWarn('[Highway] sigSession or sessionKey not available!'); diff --git a/src/core/packet/session.ts b/src/core/packet/session.ts index 993822ae..3413bc3b 100644 --- a/src/core/packet/session.ts +++ b/src/core/packet/session.ts @@ -23,11 +23,26 @@ export class PacketSession { constructor(core: NapCatCore) { this.logger = core.context.logger; - this.client = this.judgeClient(core); + this.client = this.newClient(core); this.packer = new PacketPacker(this.logger, this.client); this.highwaySession = new PacketHighwaySession(this.logger, this.client, this.packer); } + private newClient(core: NapCatCore): PacketClient { + const prefer = core.configLoader.configData.packetBackend; + switch (prefer) { + case "native": + return new NativePacketClient(core); + case "frida": + return new wsPacketClient(core); + case "auto": + case undefined: + return this.judgeClient(core); + default: + throw new Error(`[Core] [Packet] 未知的Packet后端类型 ${prefer},请检查配置文件!`); + } + } + private judgeClient(core: NapCatCore): PacketClient { const sortedClients = Object.entries(clientPriority) .map(([priority, clientFactory]) => { diff --git a/src/onebot/action/packet/GetPacketStatus.ts b/src/onebot/action/packet/GetPacketStatus.ts index 265e7126..2c611135 100644 --- a/src/onebot/action/packet/GetPacketStatus.ts +++ b/src/onebot/action/packet/GetPacketStatus.ts @@ -9,7 +9,7 @@ export abstract class GetPacketStatusDepends extends BaseAction if (!this.core.apis.PacketApi.available) { return { valid: false, - message: "packetServer不可用,请参照文档 https://napneko.github.io/config/advanced 检查packetServer状态或进行配置!", + message: "packetBackend不可用,请参照文档 https://napneko.github.io/config/advanced 和启动日志检查packetBackend状态或进行配置!", }; } return await super.check(payload);