Skip to content

Commit

Permalink
chore: 挂载逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed Aug 11, 2024
1 parent 4a531cc commit e9e5175
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/onebot/network/active-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 16 additions & 4 deletions src/onebot/network/passive-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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;

Expand All @@ -36,8 +35,9 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {
}

open() {
if (this.hasBeenClosed) {
if (this.isOpen) {
this.coreContext.context.logger.logError('Cannot open a closed HTTP server');
return;
}
if (!this.isOpen) {
this.initializeServer();
Expand All @@ -47,7 +47,6 @@ export class OB11PassiveHttpAdapter implements IOB11NetworkAdapter {

async close() {
this.isOpen = false;
this.hasBeenClosed = true;
this.server?.close();
this.app = undefined;
}
Expand All @@ -58,13 +57,26 @@ 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 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');
Expand Down

0 comments on commit e9e5175

Please sign in to comment.