From 4415c819cd95ffdb530187967624daf2fcec2851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=80=D0=B5=D0=BF=D0=BE=D0=B4=D0=BE=D0=B1=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BD?= Date: Fri, 7 Oct 2022 17:15:52 +0300 Subject: [PATCH] Committing updates. --- src/app/Common.cpp | 4 +- src/app/Common.hpp | 1 + src/modules/Workers/WebService/WebService.cpp | 69 +++++++++++++++++++ src/modules/Workers/WebService/WebService.hpp | 1 + src/modules/Workers/WebSocket/WebSocket.cpp | 54 +++++++++++++++ src/modules/Workers/WebSocket/WebSocket.hpp | 1 + 6 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/app/Common.cpp b/src/app/Common.cpp index a2c3313..a014f51 100644 --- a/src/app/Common.cpp +++ b/src/app/Common.cpp @@ -521,7 +521,7 @@ namespace Apostol { } else if (caCommand == "signature") { DoSignature(AConnection); } else { - AConnection->SendStockReply(CHTTPReply::not_found); + DoProxy(AConnection, "POST", sRoute); } } catch (std::exception &e) { ReplyError(AConnection, CHTTPReply::bad_request, e.what()); @@ -647,7 +647,7 @@ namespace Apostol { } else { - AConnection->SendStockReply(CHTTPReply::not_found); + DoProxy(AConnection, "GET", sRoute); } diff --git a/src/app/Common.hpp b/src/app/Common.hpp index 88f0a5e..5cc898c 100644 --- a/src/app/Common.hpp +++ b/src/app/Common.hpp @@ -112,6 +112,7 @@ namespace Apostol { virtual void DoAccount(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) abstract; virtual void DoDeal(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI, const CString &Action) abstract; + virtual void DoProxy(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) abstract; virtual void DoSignature(CHTTPServerConnection *AConnection) abstract; public: diff --git a/src/modules/Workers/WebService/WebService.cpp b/src/modules/Workers/WebService/WebService.cpp index dc87387..ad634ad 100644 --- a/src/modules/Workers/WebService/WebService.cpp +++ b/src/modules/Workers/WebService/WebService.cpp @@ -436,6 +436,8 @@ namespace Apostol { pProxyRequest->AddHeader("Origin", caOrigin); AConnection->CloseConnection(false); + + pProxy->AutoFree(true); pProxy->Active(true); } //-------------------------------------------------------------------------------------------------------------- @@ -852,6 +854,73 @@ namespace Apostol { AConnection->CloseConnection(false); + pProxy->AutoFree(true); + pProxy->Active(true); + } + //-------------------------------------------------------------------------------------------------------------- + + void CWebService::DoProxy(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) { + + auto pProxy = GetProxy(AConnection); + auto pServerRequest = AConnection->Request(); + auto pProxyRequest = pProxy->Request(); + + const auto &caModuleAddress = m_Module["address"]; + const auto &caModuleFee = m_Module["fee"]; + + const auto checkFee = CheckFee(caModuleFee); + if (checkFee == -1) + throw ExceptionFrm("Invalid module fee value: %s", caModuleFee.c_str()); + + const auto &caHost = pServerRequest->Headers["host"]; + const auto &caOrigin = pServerRequest->Headers["origin"]; + + const auto &address = pServerRequest->Params["address"]; + const auto &code = pServerRequest->Params["code"]; + + const auto &caUserAddress = address.length() == 40 ? CString() : address; + const auto &caDealCode = !code.empty() ? code : address.length() == 40 ? address : CString(); + + const auto &pgpValue = pServerRequest->Params["pgp"]; + const auto &caServerParam = pServerRequest->Params["server"]; + + const auto &caServer = caServerParam.IsEmpty() ? m_CurrentServer.URL().Origin() : caServerParam; + + CLocation Location(caServer); + + pProxy->Host() = Location.hostname; + pProxy->Port(Location.port == 0 ? BPS_SERVER_PORT : Location.port); + + pProxyRequest->Clear(); + + pProxyRequest->Location = pServerRequest->Location; + pProxyRequest->ContentType = pServerRequest->ContentType; + + if (Method == "POST") { + pProxyRequest->Content = pServerRequest->Content; + } + + pProxyRequest->CloseConnection = true; + + CHTTPRequest::Prepare(pProxyRequest, Method.c_str(), URI.c_str()); + + pProxyRequest->AddHeader("Authorization", "Bearer " + m_CurrentServer.Tokens()["access_token"]); + + if (!caModuleAddress.IsEmpty()) + pProxyRequest->AddHeader("Module-Address", caModuleAddress); + + if (!caModuleFee.IsEmpty()) + pProxyRequest->AddHeader("Module-Fee", caModuleFee); + + if (!caOrigin.IsEmpty()) + pProxyRequest->AddHeader("Origin", caOrigin); + + AConnection->TimeOutInterval(15 * 1000); + AConnection->UpdateTimeOut(Now()); + + AConnection->CloseConnection(false); + + pProxy->AutoFree(true); pProxy->Active(true); } //-------------------------------------------------------------------------------------------------------------- diff --git a/src/modules/Workers/WebService/WebService.hpp b/src/modules/Workers/WebService/WebService.hpp index 8f80678..317eb9b 100644 --- a/src/modules/Workers/WebService/WebService.hpp +++ b/src/modules/Workers/WebService/WebService.hpp @@ -64,6 +64,7 @@ namespace Apostol { void DoAccount(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) override; void DoDeal(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI, const CString &Action) override; + void DoProxy(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) override; void DoSignature(CHTTPServerConnection *AConnection) override; bool DoProxyExecute(CTCPConnection *AConnection); diff --git a/src/modules/Workers/WebSocket/WebSocket.cpp b/src/modules/Workers/WebSocket/WebSocket.cpp index a565e28..dda105d 100644 --- a/src/modules/Workers/WebSocket/WebSocket.cpp +++ b/src/modules/Workers/WebSocket/WebSocket.cpp @@ -964,6 +964,60 @@ namespace Apostol { } //-------------------------------------------------------------------------------------------------------------- + void CWebSocketModule::DoProxy(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) { + + auto OnRequest = [AConnection](CWebSocketMessageHandler *AHandler, CWebSocketConnection *AWSConnection) { + auto pReply = AConnection->Reply(); + const auto &wsMessage = CCustomWebSocketClient::RequestToMessage(AWSConnection); + pReply->ContentType = CHTTPReply::json; + if (wsMessage.MessageTypeId == mtCallResult) { + pReply->Content = wsMessage.Payload.ToString(); + AConnection->SendReply(CHTTPReply::ok, nullptr, true); + } else { + ReplyError(AConnection, CHTTPReply::bad_request, wsMessage.ErrorMessage); + } + }; + + auto pServerRequest = AConnection->Request(); + + const auto &caModuleAddress = m_Module["address"]; + const auto &caModuleFee = m_Module["fee"]; + + const auto checkFee = CheckFee(caModuleFee); + if (checkFee == -1) + throw ExceptionFrm("Invalid module fee value: %s", caModuleFee.c_str()); + + const auto &caHost = pServerRequest->Headers["host"]; + const auto &caOrigin = pServerRequest->Headers["origin"]; + + const auto &address = pServerRequest->Params["address"]; + const auto &code = pServerRequest->Params["code"]; + + const auto &caUserAddress = address.length() == 40 ? CString() : address; + const auto &caDealCode = !code.empty() ? code : address.length() == 40 ? address : CString(); + + const auto &pgpValue = pServerRequest->Params["pgp"]; + const auto &caServerParam = pServerRequest->Params["server"]; + + const auto index = CurrentContextIndex(caServerParam); + if (index == -1) { + throw Delphi::Exception::Exception(NOT_FOUND_ACTIVE_CONNECTION); + } + + const auto &caContext = m_Servers[index].Value(); + + auto pClient = GetConnectedClient(caContext); + + if (pClient == nullptr) { + throw Delphi::Exception::Exception(NOT_FOUND_ACTIVE_CONNECTION); + } + + const CJSON Json(pServerRequest->Content); + + pClient->Send(URI, Json, OnRequest); + } + //-------------------------------------------------------------------------------------------------------------- + void CWebSocketModule::DoSignature(CHTTPServerConnection *AConnection) { auto pRequest = AConnection->Request(); auto pReply = AConnection->Reply(); diff --git a/src/modules/Workers/WebSocket/WebSocket.hpp b/src/modules/Workers/WebSocket/WebSocket.hpp index 838ab9b..b44a9f3 100644 --- a/src/modules/Workers/WebSocket/WebSocket.hpp +++ b/src/modules/Workers/WebSocket/WebSocket.hpp @@ -88,6 +88,7 @@ namespace Apostol { void DoAccount(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) override; void DoDeal(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI, const CString &Action) override; + void DoProxy(CHTTPServerConnection *AConnection, const CString &Method, const CString &URI) override; void DoSignature(CHTTPServerConnection *AConnection) override; void DoWebSocketError(CTCPConnection *AConnection);