mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
7b1b503703
@ -9,6 +9,8 @@ import { LegacyNTEventWrapper } from '@/common/framework/event-legacy';
|
||||
import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from './apis';
|
||||
import os from 'node:os';
|
||||
import { NTQQCollectionApi } from './apis/collection';
|
||||
import { OB11Config } from '@/onebot/helper/config';
|
||||
import { NapCatConfig } from './helper/config';
|
||||
|
||||
export enum NapCatCoreWorkingEnv {
|
||||
Unknown = 0,
|
||||
@ -36,6 +38,7 @@ export class NapCatCore {
|
||||
// runtime info, not readonly
|
||||
selfInfo: SelfInfo;
|
||||
util: NodeQQNTWrapperUtil;
|
||||
config: any;
|
||||
|
||||
// 通过构造器递过去的 runtime info 应该尽量少
|
||||
constructor(context: InstanceContext, selfInfo: SelfInfo) {
|
||||
@ -53,6 +56,7 @@ export class NapCatCore {
|
||||
UserApi: new NTQQUserApi(this.context, this),
|
||||
GroupApi: new NTQQGroupApi(this.context, this),
|
||||
};
|
||||
this.config = new NapCatConfig(this,this.context.pathWrapper.cachePath);
|
||||
this.NapCatDataPath = path.join(this.dataPath, 'NapCat');
|
||||
fs.mkdirSync(this.NapCatDataPath, { recursive: true });
|
||||
this.NapCatTempPath = path.join(this.NapCatDataPath, 'temp');
|
||||
|
16
src/core/helper/config.ts
Normal file
16
src/core/helper/config.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { ConfigBase } from "@/common/utils/ConfigBase";
|
||||
import { LogLevel } from "@/common/utils/log";
|
||||
|
||||
export interface NapCatConfig {
|
||||
fileLog: boolean,
|
||||
consoleLog: boolean,
|
||||
fileLogLevel: LogLevel,
|
||||
consoleLogLevel: LogLevel,
|
||||
}
|
||||
|
||||
export class NapCatConfig extends ConfigBase<NapCatConfig> {
|
||||
getConfigName() {
|
||||
return 'onebot11';
|
||||
}
|
||||
|
||||
}
|
@ -55,7 +55,9 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||
|
||||
close() {
|
||||
if (this.isClosed) {
|
||||
throw new Error('Cannot close a closed WebSocket connection');
|
||||
this.logger.logDebug('Cannot close a closed WebSocket connection');
|
||||
return;
|
||||
//throw new Error('Cannot close a closed WebSocket connection');
|
||||
}
|
||||
this.isClosed = true;
|
||||
if (this.connection) {
|
||||
|
@ -4,6 +4,7 @@ import express, { Express, Request, Response } from 'express';
|
||||
import http from 'http';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { NapCatOneBot11Adapter } from '../main';
|
||||
import { OB11Response } from '../action/OB11Response';
|
||||
|
||||
export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
||||
token: string;
|
||||
@ -12,7 +13,6 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
||||
private app: Express | undefined;
|
||||
private server: http.Server | undefined;
|
||||
private isOpen: boolean = false;
|
||||
private hasBeenClosed: boolean = false;
|
||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||
private port: number;
|
||||
|
||||
@ -36,18 +36,23 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
||||
}
|
||||
|
||||
open() {
|
||||
if (this.hasBeenClosed) {
|
||||
this.coreContext.context.logger.logError('Cannot open a closed HTTP server');
|
||||
}
|
||||
if (!this.isOpen) {
|
||||
this.initializeServer();
|
||||
this.isOpen = true;
|
||||
try {
|
||||
if (this.isOpen) {
|
||||
this.coreContext.context.logger.logError('Cannot open a closed HTTP server');
|
||||
return;
|
||||
}
|
||||
if (!this.isOpen) {
|
||||
this.initializeServer();
|
||||
this.isOpen = true;
|
||||
}
|
||||
} catch (e) {
|
||||
this.coreContext.context.logger.logError(`[OneBot] [HTTP Server Adapter] Boot Error: ${e}`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async close() {
|
||||
this.isOpen = false;
|
||||
this.hasBeenClosed = true;
|
||||
this.server?.close();
|
||||
this.app = undefined;
|
||||
}
|
||||
@ -58,30 +63,51 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
||||
|
||||
this.app.use(express.json());
|
||||
this.app.use(express.urlencoded({ extended: false }));
|
||||
this.app.use((req, res, next) => this.authorize(this.token, req, res, next));
|
||||
this.app.use('/', (req, res) => this.handleRequest(req, res));
|
||||
|
||||
this.server.listen(this.port, () => {
|
||||
this.coreContext.context.logger.log(`HTTP server listening on port ${this.port}`);
|
||||
this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Start On Port ${this.port}`);
|
||||
});
|
||||
}
|
||||
|
||||
private authorize(token: string | undefined, req: Request, res: Response, next: any) {
|
||||
if (!token || token.length == 0) return;//客户端未设置密钥
|
||||
const HeaderClientToken = req.headers.authorization?.split('Bearer ').pop() || '';
|
||||
const QueryClientToken = req.query.access_token;
|
||||
const ClientToken = typeof (QueryClientToken) === 'string' && QueryClientToken !== '' ? QueryClientToken : HeaderClientToken;
|
||||
if (ClientToken === token) {
|
||||
next();
|
||||
} else {
|
||||
res.status(403).send(JSON.stringify({ message: 'token verify failed!' }));
|
||||
}
|
||||
}
|
||||
|
||||
private async handleRequest(req: Request, res: Response) {
|
||||
if (!this.isOpen) {
|
||||
res.status(503).send('Server is closed');
|
||||
this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Server is closed`);
|
||||
res.json(OB11Response.error('Server is closed', 200));
|
||||
return;
|
||||
}
|
||||
|
||||
let payload = req.body;
|
||||
if (req.method == 'get') {
|
||||
payload = req.query;
|
||||
} else if (req.query) {
|
||||
payload = { ...req.query, ...req.body };
|
||||
}
|
||||
|
||||
const actionName = req.path.split('/')[1];
|
||||
const action = this.actionMap.get(actionName);
|
||||
if (action) {
|
||||
try {
|
||||
const result = await action.handle(req.body);
|
||||
const result = await action.handle(payload);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
res.status(500).send('Internal Server Error');
|
||||
} catch (error: any) {
|
||||
res.json(OB11Response.error(error?.stack?.toString() || error?.message || 'Error Handle', 200));
|
||||
}
|
||||
} else {
|
||||
res.status(404).send('Action not found');
|
||||
res.json(OB11Response.error('不支持的api ' + actionName, 200));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,6 +39,8 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||
//鉴权
|
||||
this.authorize(token, wsClient, wsReq);
|
||||
|
||||
wsClient.on('error', (err) => this.logger.log('[OneBot] [WebSocket Server] Client Error:', err.message));
|
||||
|
||||
wsClient.on('message', (message) => {
|
||||
this.handleMessage(wsClient, message).then().catch(this.logger.logError);
|
||||
});
|
||||
@ -53,7 +55,7 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||
await this.wsClientsMutex.runExclusive(async () => {
|
||||
this.wsClients.push(wsClient);
|
||||
});
|
||||
});
|
||||
}).on('error', (err) => this.logger.log('[OneBot] [WebSocket Server] Server Error:', err.message));
|
||||
}
|
||||
|
||||
registerActionMap(actionMap: Map<string, BaseAction<any, any>>) {
|
||||
@ -67,25 +69,25 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||
this.wsClientsMutex.runExclusive(async () => {
|
||||
this.wsClients.forEach((wsClient) => {
|
||||
const wrappedEvent = this.wrapEvent(event);
|
||||
wsClient.send(JSON.stringify(wrappedEvent));
|
||||
wsClient.send(JSON.stringify(event));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
open() {
|
||||
if (this.hasBeenClosed) {
|
||||
this.logger.logError('Cannot open a closed WebSocket server');
|
||||
if (this.isOpen) {
|
||||
this.logger.logError('[OneBot] [WebSocket Server] Cannot open a opened WebSocket server');
|
||||
return;
|
||||
}
|
||||
this.logger.log('WebSocket server started', this.wsServer.address());
|
||||
let addressInfo = this.wsServer.address();
|
||||
this.logger.log('[OneBot] [WebSocket Server] Server Started', typeof (addressInfo) === 'string' ? addressInfo : addressInfo?.address + ':' + addressInfo?.port);
|
||||
|
||||
this.isOpen = true;
|
||||
this.registerHeartBeat();
|
||||
}
|
||||
|
||||
async close() {
|
||||
this.isOpen = false;
|
||||
this.hasBeenClosed = true;
|
||||
this.wsServer.close();
|
||||
if (this.heartbeatIntervalId) {
|
||||
clearInterval(this.heartbeatIntervalId);
|
||||
@ -117,14 +119,6 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||
wsClient.close();
|
||||
}
|
||||
|
||||
private async checkStateAndAnnounce<T>(data: T) {
|
||||
await this.wsClientsMutex.runExclusive(async () => {
|
||||
this.wsClients.forEach((wsClient) => {
|
||||
this.checkStateAndReply(data, wsClient);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private checkStateAndReply<T>(data: T, wsClient: WebSocket) {
|
||||
if (wsClient.readyState === WebSocket.OPEN) {
|
||||
wsClient.send(JSON.stringify(data));
|
||||
@ -138,7 +132,7 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||
try {
|
||||
receiveData = JSON.parse(message.toString());
|
||||
echo = receiveData.echo;
|
||||
this.logger.logDebug('收到正向Websocket消息', receiveData);
|
||||
//this.logger.logDebug('收到正向Websocket消息', receiveData);
|
||||
} catch (e) {
|
||||
this.checkStateAndReply<any>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo), wsClient);
|
||||
}
|
||||
@ -151,11 +145,5 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private wrapEvent<T extends OB11EmitEventContent>(event: T) {
|
||||
return {
|
||||
type: 'event',
|
||||
data: event,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user