From 86766e08cee72b5786860dabd9ff69fa242c7032 Mon Sep 17 00:00:00 2001 From: Hpyer Date: Mon, 22 Jul 2024 16:27:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A1=A5=E5=85=85=E5=85=AC=E4=BC=97?= =?UTF-8?q?=E5=8F=B7=E5=92=8C=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=AB=AF=E7=BC=BA=E5=A4=B1=E7=9A=84=E8=8E=B7=E5=8F=96=E8=A7=A3?= =?UTF-8?q?=E5=AF=86=E6=B6=88=E6=81=AF=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E5=85=B6=E5=AE=83=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E8=AF=A5=E6=96=B9=E6=B3=95=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=9B?= =?UTF-8?q?(#86)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OfficialAccount/Server.ts | 30 ++++++++++++++++++++++++++---- src/OpenPlatform/Server.ts | 17 ++++++++++------- src/OpenWork/Server.ts | 11 ++++++++--- src/Work/Server.ts | 11 ++++++++--- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/OfficialAccount/Server.ts b/src/OfficialAccount/Server.ts index 050907c..cece5ee 100644 --- a/src/OfficialAccount/Server.ts +++ b/src/OfficialAccount/Server.ts @@ -21,13 +21,13 @@ class Server extends ServerInterface * @returns */ async serve(): Promise { - let echostr: string = this.request.getQueryParams()['echostr'] || ''; + let query = this.request.getQueryParams(); + let echostr: string = query['echostr'] || ''; if (!!echostr) { return new Response(200, { 'Content-Type': 'text/html' }, echostr); } let message = await this.getRequestMessage(this.request); - let query = this.request.getQueryParams(); if (this.encryptor && query['msg_signature']) { this.prepend(this.decryptRequestMessage(query)); @@ -80,8 +80,7 @@ class Server extends ServerInterface protected decryptRequestMessage(query: Record): ServerHandlerClosure { return async (message: Message, next: ServerHandlerClosure) => { - if (!this.encryptor) return null; - await this.decryptMessage( + message = await this.decryptMessage( message, this.encryptor, query['msg_signature'] || '', @@ -93,6 +92,29 @@ class Server extends ServerInterface }; } + /** + * 获取解密后的消息 + * @param request 未设置该参数时,则从当前服务端收到的请求中获取 + * @returns + */ + async getDecryptedMessage(request: ServerRequestInterface = null) { + request = request ?? this.request; + let message = await this.getRequestMessage(request); + let query = request.getQueryParams(); + + if (!this.encryptor || !query['msg_signature']) { + return message; + } + + return await this.decryptMessage( + message, + this.encryptor, + query['msg_signature'] ?? '', + query['timestamp'] ?? '', + query['nonce'] ?? '' + ); + } + }; interface Server { diff --git a/src/OpenPlatform/Server.ts b/src/OpenPlatform/Server.ts index c2e8be7..3c2a714 100644 --- a/src/OpenPlatform/Server.ts +++ b/src/OpenPlatform/Server.ts @@ -23,13 +23,17 @@ class Server extends ServerInterface * @returns */ async serve(): Promise { - let echostr: string = this.request.getQueryParams()['echostr'] || ''; + let query = this.request.getQueryParams(); + let echostr: string = query['echostr'] || ''; if (!!echostr) { return new Response(200, { 'Content-Type': 'text/html' }, echostr); } let message = await this.getRequestMessage(this.request); - this.prepend(this.decryptRequestMessage()); + + if (this.encryptor && query['msg_signature']) { + this.prepend(this.decryptRequestMessage(query)); + } let response = await this.handle(new Response(200, {}, 'success'), message); @@ -111,10 +115,9 @@ class Server extends ServerInterface }) } - protected decryptRequestMessage(): ServerHandlerClosure { - let query = this.request.getQueryParams(); + protected decryptRequestMessage(query: Record): ServerHandlerClosure { return async (message: Message, next: ServerHandlerClosure) => { - await this.decryptMessage( + message = await this.decryptMessage( message, this.encryptor, query['msg_signature'] || '', @@ -141,8 +144,8 @@ class Server extends ServerInterface * @returns */ async getDecryptedMessage(request: ServerRequestInterface = null): Promise { - if (!request) request = this.request; - let message = await Message.createFromRequest(request); + request = request ?? this.request; + let message = await this.getRequestMessage(request); let query = request.getQueryParams(); return this.decryptMessage( diff --git a/src/OpenWork/Server.ts b/src/OpenWork/Server.ts index cd91c85..871c434 100644 --- a/src/OpenWork/Server.ts +++ b/src/OpenWork/Server.ts @@ -37,7 +37,9 @@ class Server extends ServerInterface let message = await this.getRequestMessage(this.request); - this.prepend(this.decryptRequestMessage(query)); + if (this.encryptor && query['msg_signature']) { + this.prepend(this.decryptRequestMessage(query)); + } let response = await this.handle(new Response(200, {}, 'success'), message); @@ -210,8 +212,7 @@ class Server extends ServerInterface protected decryptRequestMessage(query: Record): ServerHandlerClosure { return async (message: Message, next: ServerHandlerClosure) => { - if (!this.encryptor) return null; - await this.decryptMessage( + message = await this.decryptMessage( message, this.encryptor, query['msg_signature'] ?? '', @@ -233,6 +234,10 @@ class Server extends ServerInterface let message = await this.getRequestMessage(request); let query = request.getQueryParams(); + if (!this.encryptor || !query['msg_signature']) { + return message; + } + return await this.decryptMessage( message, this.encryptor, diff --git a/src/Work/Server.ts b/src/Work/Server.ts index ef10556..f0ca186 100644 --- a/src/Work/Server.ts +++ b/src/Work/Server.ts @@ -34,7 +34,9 @@ class Server extends ServerInterface let message = await this.getRequestMessage(this.request); - this.prepend(this.decryptRequestMessage(query)); + if (this.encryptor && query['msg_signature']) { + this.prepend(this.decryptRequestMessage(query)); + } let response = await this.handle(new Response(200, {}, 'SUCCESS'), message); @@ -185,8 +187,7 @@ class Server extends ServerInterface protected decryptRequestMessage(query: Record): ServerHandlerClosure { return async (message: Message, next: ServerHandlerClosure) => { - if (!this.encryptor) return null; - await this.decryptMessage( + message = await this.decryptMessage( message, this.encryptor, query['msg_signature'] ?? '', @@ -208,6 +209,10 @@ class Server extends ServerInterface let message = await this.getRequestMessage(request); let query = request.getQueryParams(); + if (!this.encryptor || !query['msg_signature']) { + return message; + } + return await this.decryptMessage( message, this.encryptor,