mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
chore: 挂载逻辑
This commit is contained in:
parent
4a531ccea1
commit
e9e517533a
@ -55,7 +55,9 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
|
|
||||||
close() {
|
close() {
|
||||||
if (this.isClosed) {
|
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;
|
this.isClosed = true;
|
||||||
if (this.connection) {
|
if (this.connection) {
|
||||||
|
@ -12,7 +12,6 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
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 actionMap: Map<string, BaseAction<any, any>> = new Map();
|
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||||
private port: number;
|
private port: number;
|
||||||
|
|
||||||
@ -36,8 +35,9 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
open() {
|
open() {
|
||||||
if (this.hasBeenClosed) {
|
if (this.isOpen) {
|
||||||
this.coreContext.context.logger.logError('Cannot open a closed HTTP server');
|
this.coreContext.context.logger.logError('Cannot open a closed HTTP server');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (!this.isOpen) {
|
if (!this.isOpen) {
|
||||||
this.initializeServer();
|
this.initializeServer();
|
||||||
@ -47,7 +47,6 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
this.isOpen = false;
|
this.isOpen = false;
|
||||||
this.hasBeenClosed = true;
|
|
||||||
this.server?.close();
|
this.server?.close();
|
||||||
this.app = undefined;
|
this.app = undefined;
|
||||||
}
|
}
|
||||||
@ -58,13 +57,26 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
|
|
||||||
this.app.use(express.json());
|
this.app.use(express.json());
|
||||||
this.app.use(express.urlencoded({ extended: false }));
|
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.server.listen(this.port, () => {
|
||||||
this.coreContext.context.logger.log(`HTTP server listening on port ${this.port}`);
|
this.coreContext.context.logger.log(`[OneBot] [HTTP 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) {
|
private async handleRequest(req: Request, res: Response) {
|
||||||
if (!this.isOpen) {
|
if (!this.isOpen) {
|
||||||
res.status(503).send('Server is closed');
|
res.status(503).send('Server is closed');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user