mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
chore: clean-up
This commit is contained in:
parent
a1badcd9a1
commit
3c42cc17c8
@ -12,7 +12,7 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
heartbeatInterval: number;
|
heartbeatInterval: number;
|
||||||
secret: string | undefined;
|
secret: string | undefined;
|
||||||
coreContext: NapCatCore;
|
coreContext: NapCatCore;
|
||||||
onebotContext: NapCatOneBot11Adapter;
|
obContext: NapCatOneBot11Adapter;
|
||||||
logger: LogWrapper;
|
logger: LogWrapper;
|
||||||
|
|
||||||
constructor(url: string, heartbeatInterval: number, secret: string | undefined, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
|
constructor(url: string, heartbeatInterval: number, secret: string | undefined, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
|
||||||
@ -20,15 +20,13 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
this.url = url;
|
this.url = url;
|
||||||
this.secret = secret;
|
this.secret = secret;
|
||||||
this.coreContext = coreContext;
|
this.coreContext = coreContext;
|
||||||
this.onebotContext = onebotContext;
|
this.obContext = onebotContext;
|
||||||
this.logger = coreContext.context.logger;
|
this.logger = coreContext.context.logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
registerHeartBeat() {
|
|
||||||
// HttpPost 心跳
|
|
||||||
}
|
|
||||||
registerActionMap(actionMap: Map<string, BaseAction<any, any>>) {
|
registerActionMap(actionMap: Map<string, BaseAction<any, any>>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
||||||
// Passive http adapter does not need to register actions
|
// Passive http adapter does not need to register actions
|
||||||
}
|
}
|
||||||
|
@ -6,33 +6,30 @@ import { NapCatCore } from '@/core';
|
|||||||
import { NapCatOneBot11Adapter } from '../main';
|
import { NapCatOneBot11Adapter } from '../main';
|
||||||
|
|
||||||
export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
||||||
|
token: string;
|
||||||
|
coreContext: NapCatCore;
|
||||||
|
obContext: NapCatOneBot11Adapter;
|
||||||
private app: Express | undefined;
|
private app: Express | undefined;
|
||||||
private server: http.Server | undefined;
|
private server: http.Server | undefined;
|
||||||
private isOpen: boolean = false;
|
private isOpen: boolean = false;
|
||||||
private hasBeenClosed: boolean = false;
|
private hasBeenClosed: boolean = false;
|
||||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||||
private port: number;
|
private port: number;
|
||||||
token: string;
|
|
||||||
coreContext: NapCatCore;
|
|
||||||
onebotContext: NapCatOneBot11Adapter;
|
|
||||||
|
|
||||||
constructor(port: number, token: string, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
|
constructor(port: number, token: string, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.coreContext = coreContext;
|
this.coreContext = coreContext;
|
||||||
this.onebotContext = onebotContext;
|
this.obContext = onebotContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
||||||
this.actionMap.set(action.actionName, action);
|
this.actionMap.set(action.actionName, action);
|
||||||
}
|
}
|
||||||
registerActionMap(actionMap: Map<string, BaseAction<any, any>>) {
|
|
||||||
|
|
||||||
|
registerActionMap(actionMap: Map<string, BaseAction<any, any>>) {
|
||||||
this.actionMap = actionMap;
|
this.actionMap = actionMap;
|
||||||
}
|
}
|
||||||
registerHeartBeat() {
|
|
||||||
//空心跳
|
|
||||||
}
|
|
||||||
|
|
||||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
// 事件处理逻辑可以在这里实现
|
// 事件处理逻辑可以在这里实现
|
||||||
@ -48,6 +45,13 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async close() {
|
||||||
|
this.isOpen = false;
|
||||||
|
this.hasBeenClosed = true;
|
||||||
|
this.server?.close();
|
||||||
|
this.app = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
private initializeServer() {
|
private initializeServer() {
|
||||||
this.app = express();
|
this.app = express();
|
||||||
this.server = http.createServer(this.app);
|
this.server = http.createServer(this.app);
|
||||||
@ -80,11 +84,4 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
res.status(404).send('Action not found');
|
res.status(404).send('Action not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
|
||||||
this.isOpen = false;
|
|
||||||
this.hasBeenClosed = true;
|
|
||||||
this.server?.close();
|
|
||||||
this.app = undefined;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -18,27 +18,15 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
isOpen: boolean = false;
|
isOpen: boolean = false;
|
||||||
hasBeenClosed: boolean = false;
|
hasBeenClosed: boolean = false;
|
||||||
heartbeatInterval: number = 0;
|
heartbeatInterval: number = 0;
|
||||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
obContext: NapCatOneBot11Adapter;
|
||||||
onebotContext: NapCatOneBot11Adapter;
|
|
||||||
coreContext: NapCatCore;
|
coreContext: NapCatCore;
|
||||||
logger: LogWrapper;
|
logger: LogWrapper;
|
||||||
|
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||||
private heartbeatIntervalId: NodeJS.Timeout | null = null;
|
private heartbeatIntervalId: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
authorize(token: string | undefined, wsClient: WebSocket, wsReq: IncomingMessage) {
|
|
||||||
if (!token || token.length == 0) return;//客户端未设置密钥
|
|
||||||
let QueryClientToken = urlParse.parse(wsReq?.url || "", true).query.access_token;
|
|
||||||
let HeaderClientToken = wsReq.headers.authorization?.split('Bearer ').pop() || '';
|
|
||||||
let ClientToken = typeof (QueryClientToken) === 'string' && QueryClientToken !== "" ? QueryClientToken : HeaderClientToken;
|
|
||||||
if (ClientToken === token) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
wsClient.send(JSON.stringify(OB11Response.res(null, 'failed', 1403, 'token验证失败')));
|
|
||||||
wsClient.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(ip: string, port: number, heartbeatInterval: number, token: string, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
|
constructor(ip: string, port: number, heartbeatInterval: number, token: string, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
|
||||||
this.coreContext = coreContext;
|
this.coreContext = coreContext;
|
||||||
this.onebotContext = onebotContext;
|
this.obContext = onebotContext;
|
||||||
this.logger = coreContext.context.logger;
|
this.logger = coreContext.context.logger;
|
||||||
|
|
||||||
this.heartbeatInterval = heartbeatInterval;
|
this.heartbeatInterval = heartbeatInterval;
|
||||||
@ -76,18 +64,6 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
this.actionMap.set(action.actionName, action);
|
this.actionMap.set(action.actionName, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
registerHeartBeat() {
|
|
||||||
this.heartbeatIntervalId = setInterval(() => {
|
|
||||||
this.wsClientsMutex.runExclusive(async () => {
|
|
||||||
this.wsClients.forEach((wsClient) => {
|
|
||||||
if (wsClient.readyState === WebSocket.OPEN) {
|
|
||||||
wsClient.send(JSON.stringify(new OB11HeartbeatEvent(this.coreContext, this.heartbeatInterval, this.coreContext.selfInfo.online, true)));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}, this.heartbeatInterval);
|
|
||||||
}
|
|
||||||
|
|
||||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
this.wsClientsMutex.runExclusive(async () => {
|
this.wsClientsMutex.runExclusive(async () => {
|
||||||
this.wsClients.forEach((wsClient) => {
|
this.wsClients.forEach((wsClient) => {
|
||||||
@ -117,17 +93,39 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async WsReplyAll<T>(data: T) {
|
private registerHeartBeat() {
|
||||||
this.wsClientsMutex.runExclusive(async () => {
|
this.heartbeatIntervalId = setInterval(() => {
|
||||||
|
this.wsClientsMutex.runExclusive(async () => {
|
||||||
|
this.wsClients.forEach((wsClient) => {
|
||||||
|
if (wsClient.readyState === WebSocket.OPEN) {
|
||||||
|
wsClient.send(JSON.stringify(new OB11HeartbeatEvent(this.coreContext, this.heartbeatInterval, this.coreContext.selfInfo.online, true)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, this.heartbeatInterval);
|
||||||
|
}
|
||||||
|
|
||||||
|
private authorize(token: string | undefined, wsClient: WebSocket, wsReq: IncomingMessage) {
|
||||||
|
if (!token || token.length == 0) return;//客户端未设置密钥
|
||||||
|
const QueryClientToken = urlParse.parse(wsReq?.url || '', true).query.access_token;
|
||||||
|
const HeaderClientToken = wsReq.headers.authorization?.split('Bearer ').pop() || '';
|
||||||
|
const ClientToken = typeof (QueryClientToken) === 'string' && QueryClientToken !== '' ? QueryClientToken : HeaderClientToken;
|
||||||
|
if (ClientToken === token) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
wsClient.send(JSON.stringify(OB11Response.res(null, 'failed', 1403, 'token验证失败')));
|
||||||
|
wsClient.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkStateAndAnnounce<T>(data: T) {
|
||||||
|
await this.wsClientsMutex.runExclusive(async () => {
|
||||||
this.wsClients.forEach((wsClient) => {
|
this.wsClients.forEach((wsClient) => {
|
||||||
if (wsClient.readyState === WebSocket.OPEN) {
|
this.checkStateAndReply(data, wsClient);
|
||||||
wsClient.send(JSON.stringify(data));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async WsReply<T>(data: T, wsClient: WebSocket) {
|
private checkStateAndReply<T>(data: T, wsClient: WebSocket) {
|
||||||
if (wsClient.readyState === WebSocket.OPEN) {
|
if (wsClient.readyState === WebSocket.OPEN) {
|
||||||
wsClient.send(JSON.stringify(data));
|
wsClient.send(JSON.stringify(data));
|
||||||
}
|
}
|
||||||
@ -142,21 +140,21 @@ export class OB11PassiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
echo = receiveData.echo;
|
echo = receiveData.echo;
|
||||||
this.logger.logDebug('收到正向Websocket消息', receiveData);
|
this.logger.logDebug('收到正向Websocket消息', receiveData);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.WsReply<any>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo), wsClient);
|
this.checkStateAndReply<any>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo), wsClient);
|
||||||
}
|
}
|
||||||
receiveData.params = (receiveData?.params) ? receiveData.params : {};//兼容类型验证
|
receiveData.params = (receiveData?.params) ? receiveData.params : {};//兼容类型验证
|
||||||
let retdata = await this.actionMap.get(receiveData.action)?.websocketHandle(receiveData.params, echo || "");
|
const retdata = await this.actionMap.get(receiveData.action)?.websocketHandle(receiveData.params, echo || '');
|
||||||
const packet = Object.assign({}, retdata);
|
const packet = Object.assign({}, retdata);
|
||||||
this.WsReply<any>(packet, wsClient);
|
this.checkStateAndReply<any>(packet, wsClient);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.WsReply<any>(OB11Response.error('不支持的api ' + receiveData.action, 1404, echo), wsClient);
|
this.checkStateAndReply<any>(OB11Response.error('不支持的api ' + receiveData.action, 1404, echo), wsClient);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private wrapEvent<T extends OB11EmitEventContent>(event: T) {
|
private wrapEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
return {
|
return {
|
||||||
type: 'event',
|
type: 'event',
|
||||||
data: event
|
data: event,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user