From c42c748b37a96b7b628dd30e82d8f554d6f134d3 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 20 Jan 2022 15:46:13 +0100 Subject: [PATCH] start fixing backend heartbeat --- CMakeLists.txt | 2 ++ include/Common.h | 11 ++++++++--- include/Http.h | 2 +- src/Common.cpp | 2 +- src/Http.cpp | 5 ++++- src/THeartbeatThread.cpp | 34 +++++++++++++--------------------- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8f37ca..4f818d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,8 @@ project(BeamMP-Server HOMEPAGE_URL https://beammp.com LANGUAGES CXX C) +set(HTTPLIB_REQUIRE_OPENSSL ON) + include_directories("${PROJECT_SOURCE_DIR}/deps/asio/asio/include") include_directories("${PROJECT_SOURCE_DIR}/deps/rapidjson/include") include_directories("${PROJECT_SOURCE_DIR}/deps/websocketpp") diff --git a/include/Common.h b/include/Common.h index c61f98b..4b8a1e0 100644 --- a/include/Common.h +++ b/include/Common.h @@ -74,10 +74,15 @@ public: static TSettings Settings; + static std::vector GetBackendUrlsInOrder() { + return { + "backend.beammp.com", + "backup1.beammp.com", + "backup2.beammp.com" + }; + } + static std::string GetBackendUrlForAuth() { return "auth.beammp.com"; } - static std::string GetBackendHostname() { return "backend.beammp.com"; } - static std::string GetBackup1Hostname() { return "backup1.beammp.com"; } - static std::string GetBackup2Hostname() { return "backup2.beammp.com"; } static std::string GetBackendUrlForSocketIO() { return "https://backend.beammp.com"; } static void CheckForUpdates(); static std::array VersionStrToInts(const std::string& str); diff --git a/include/Http.h b/include/Http.h index 62ded67..3ff2cd5 100644 --- a/include/Http.h +++ b/include/Http.h @@ -25,7 +25,7 @@ constexpr size_t RSA_DEFAULT_KEYLENGTH { 2048 }; namespace Http { std::string GET(const std::string& host, int port, const std::string& target, unsigned int* status = nullptr); -std::string POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status = nullptr); +std::string POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status = nullptr, const httplib::Headers& headers = {}); namespace Status { std::string ToString(int code); } diff --git a/src/Common.cpp b/src/Common.cpp index 4ed5814..a6ef230 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -99,7 +99,7 @@ void Application::CheckForUpdates() { Application::SetSubsystemStatus("UpdateCheck", Application::Status::Starting); // checks current version against latest version std::regex VersionRegex { R"(\d+\.\d+\.\d+\n*)" }; - auto Response = Http::GET(GetBackendHostname(), 443, "/v/s"); + auto Response = Http::GET(GetBackendUrlsInOrder().at(0), 443, "/v/s"); bool Matches = std::regex_match(Response, VersionRegex); if (Matches) { auto MyVersion = ServerVersion(); diff --git a/src/Http.cpp b/src/Http.cpp index 11bea18..cc91e04 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -4,6 +4,7 @@ #include "Common.h" #include "CustomAssert.h" #include "LuaAPI.h" +#include "httplib.h" #include #include @@ -33,8 +34,9 @@ std::string Http::GET(const std::string& host, int port, const std::string& targ } } -std::string Http::POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status) { +std::string Http::POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status, const httplib::Headers& headers) { httplib::SSLClient client(host, port); + beammp_assert(client.is_valid()); client.enable_server_certificate_verification(false); client.set_address_family(AF_INET); auto res = client.Post(target.c_str(), body.c_str(), body.size(), ContentType.c_str()); @@ -44,6 +46,7 @@ std::string Http::POST(const std::string& host, int port, const std::string& tar } return res->body; } else { + beammp_debug("POST failed: " + httplib::to_string(res.error())); return Http::ErrorString; } } diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 27ef253..4311cf5 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -54,34 +54,26 @@ void THeartbeatThread::operator()() { auto Target = "/heartbeat"; unsigned int ResponseCode = 0; - T = Http::POST(Application::GetBackendHostname(), 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode); - if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { - beammp_trace("got " + T + " from backend"); - Application::SetSubsystemStatus("Heartbeat", Application::Status::Bad); - SentryReportError(Application::GetBackendHostname() + Target, ResponseCode); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - T = Http::POST(Application::GetBackup1Hostname(), 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode); + bool Ok = true; + json::Document Doc; + for (const auto& Hostname : Application::GetBackendUrlsInOrder()) { + T = Http::POST(Hostname, 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode, { { "api-v", "2" } }); if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { - SentryReportError(Application::GetBackup1Hostname() + Target, ResponseCode); + beammp_trace("heartbeat to " + Hostname + " returned: " + T); Application::SetSubsystemStatus("Heartbeat", Application::Status::Bad); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - T = Http::POST(Application::GetBackup2Hostname(), 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode); - if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { - beammp_warn("Backend system refused server! Server will not show in the public server list."); - Application::SetSubsystemStatus("Heartbeat", Application::Status::Bad); - isAuth = false; - SentryReportError(Application::GetBackup2Hostname() + Target, ResponseCode); - } else { - Application::SetSubsystemStatus("Heartbeat", Application::Status::Good); - } + isAuth = false; + SentryReportError(Hostname + Target, ResponseCode); + Ok = false; } else { Application::SetSubsystemStatus("Heartbeat", Application::Status::Good); + Ok = true; + break; } - } else { - Application::SetSubsystemStatus("Heartbeat", Application::Status::Good); } - + if (!Ok) { + beammp_warn("Backend system refused server! Server will not show in the public server list."); + } if (!isAuth) { if (T == "2000") { beammp_info(("Authenticated!"));