Merge remote-tracking branch 'origin/main'

This commit is contained in:
Wesley F. Young 2024-08-12 02:22:50 +08:00
commit 610473b57c

View File

@ -64,7 +64,7 @@ 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.app.use((req, res) => this.handleRequest(req, res));
this.server.listen(this.port, () => {
this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Start On Port ${this.port}`);
@ -72,22 +72,21 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
}
private authorize(token: string | undefined, req: Request, res: Response, next: any) {
if (!token || token.length == 0) return;//客户端未设置密钥
if (!token || token.length == 0) return next();//客户端未设置密钥
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();
return next();
} else {
res.status(403).send(JSON.stringify({ message: 'token verify failed!' }));
return res.status(403).send(JSON.stringify({ message: 'token verify failed!' }));
}
}
private async handleRequest(req: Request, res: Response) {
if (!this.isOpen) {
this.coreContext.context.logger.log(`[OneBot] [HTTP Server Adapter] Server is closed`);
res.json(OB11Response.error('Server is closed', 200));
return;
return res.json(OB11Response.error('Server is closed', 200));
}
let payload = req.body;
@ -102,12 +101,12 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
if (action) {
try {
const result = await action.handle(payload);
res.json(result);
return res.json(result);
} catch (error: any) {
res.json(OB11Response.error(error?.stack?.toString() || error?.message || 'Error Handle', 200));
return res.json(OB11Response.error(error?.stack?.toString() || error?.message || 'Error Handle', 200));
}
} else {
res.json(OB11Response.error('不支持的api ' + actionName, 200));
return res.json(OB11Response.error('不支持的api ' + actionName, 200));
}
}
}