feat: add new NapCat config key: packetBackend

- Acceptable values: `native`, `frida`, `auto`, `disable`
- Default value is set to `auto`
This commit is contained in:
pk5ls20 2024-11-05 14:45:02 +08:00
parent ad6f21980c
commit e46d274a75
No known key found for this signature in database
GPG Key ID: 6370ED7A169F493A
5 changed files with 25 additions and 5 deletions

View File

@ -61,7 +61,11 @@ export class NTQQPacketApi {
this.qqVersion = qqversion; this.qqVersion = qqversion;
const table = typedOffset[qqversion + '-' + os.arch()]; const table = typedOffset[qqversion + '-' + os.arch()];
if (!table) { 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; return false;
} }
this.packetSession = new PacketSession(this.core); this.packetSession = new PacketSession(this.core);

View File

@ -3,5 +3,6 @@
"consoleLog": true, "consoleLog": true,
"fileLogLevel": "debug", "fileLogLevel": "debug",
"consoleLogLevel": "info", "consoleLogLevel": "info",
"packetBackend": "auto",
"packetServer": "" "packetServer": ""
} }

View File

@ -59,7 +59,7 @@ export class PacketHighwaySession {
private async checkAvailable() { private async checkAvailable() {
if (!this.packetClient.available) { 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) { if (this.sig.sigSession === null || this.sig.sessionKey === null) {
this.logger.logWarn('[Highway] sigSession or sessionKey not available!'); this.logger.logWarn('[Highway] sigSession or sessionKey not available!');

View File

@ -23,11 +23,26 @@ export class PacketSession {
constructor(core: NapCatCore) { constructor(core: NapCatCore) {
this.logger = core.context.logger; this.logger = core.context.logger;
this.client = this.judgeClient(core); this.client = this.newClient(core);
this.packer = new PacketPacker(this.logger, this.client); this.packer = new PacketPacker(this.logger, this.client);
this.highwaySession = new PacketHighwaySession(this.logger, this.client, this.packer); 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 { private judgeClient(core: NapCatCore): PacketClient {
const sortedClients = Object.entries(clientPriority) const sortedClients = Object.entries(clientPriority)
.map(([priority, clientFactory]) => { .map(([priority, clientFactory]) => {

View File

@ -9,7 +9,7 @@ export abstract class GetPacketStatusDepends<PT, RT> extends BaseAction<PT, RT>
if (!this.core.apis.PacketApi.available) { if (!this.core.apis.PacketApi.available) {
return { return {
valid: false, 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); return await super.check(payload);