chore: 鉴权和回复

This commit is contained in:
手瓜一十雪 2024-08-11 23:59:58 +08:00
parent e9e517533a
commit 9afc0f6667

View File

@ -4,6 +4,7 @@ import express, { Express, Request, Response } from 'express';
import http from 'http'; import http from 'http';
import { NapCatCore } from '@/core'; import { NapCatCore } from '@/core';
import { NapCatOneBot11Adapter } from '../main'; import { NapCatOneBot11Adapter } from '../main';
import { OB11Response } from '../action/OB11Response';
export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter { export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
token: string; token: string;
@ -35,14 +36,19 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
} }
open() { open() {
if (this.isOpen) { try {
this.coreContext.context.logger.logError('Cannot open a closed HTTP server'); if (this.isOpen) {
return; this.coreContext.context.logger.logError('Cannot open a closed HTTP server');
} return;
if (!this.isOpen) { }
this.initializeServer(); if (!this.isOpen) {
this.isOpen = true; this.initializeServer();
this.isOpen = true;
}
} catch (e) {
this.coreContext.context.logger.logError(`[OneBot] [HTTP Server Adapter] Boot Error: ${e}`);
} }
} }
async close() { async close() {
@ -61,7 +67,7 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
this.app.use('/', (req, res) => this.handleRequest(req, res)); this.app.use('/', (req, res) => this.handleRequest(req, res));
this.server.listen(this.port, () => { this.server.listen(this.port, () => {
this.coreContext.context.logger.log(`[OneBot] [HTTP Adapter] Start On Port ${this.port}`); this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Start On Port ${this.port}`);
}); });
} }
@ -79,21 +85,29 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
private async handleRequest(req: Request, res: Response) { private async handleRequest(req: Request, res: Response) {
if (!this.isOpen) { 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; 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 actionName = req.path.split('/')[1];
const action = this.actionMap.get(actionName); const action = this.actionMap.get(actionName);
if (action) { if (action) {
try { try {
const result = await action.handle(req.body); const result = await action.handle(payload);
res.json(result); res.json(result);
} catch (error) { } catch (error: any) {
res.status(500).send('Internal Server Error'); res.json(OB11Response.error(error?.stack?.toString() || error?.message || 'Error Handle', 200));
} }
} else { } else {
res.status(404).send('Action not found'); res.json(OB11Response.error('不支持的api ' + actionName, 200));
} }
} }
} }