From fe6e1e6266f9690694a79eea9ceba0c0998aad43 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Tue, 10 Aug 2021 12:51:20 +0200 Subject: [PATCH] Heartbeat: Try backup1 and backup2, refactor sentry reporting --- include/Common.h | 2 ++ include/TSentry.h | 4 +-- src/Http.cpp | 4 +-- src/THeartbeatThread.cpp | 58 +++++++++++++++++++++++++--------------- src/TNetwork.cpp | 4 +-- src/TSentry.cpp | 6 ++--- src/TServer.cpp | 8 +++--- 7 files changed, 51 insertions(+), 35 deletions(-) diff --git a/include/Common.h b/include/Common.h index 7dca4d4..483ac9b 100644 --- a/include/Common.h +++ b/include/Common.h @@ -62,6 +62,8 @@ public: 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"; } private: diff --git a/include/TSentry.h b/include/TSentry.h index 25c706a..5cb83ba 100644 --- a/include/TSentry.h +++ b/include/TSentry.h @@ -18,8 +18,8 @@ public: void SetupUser(); void Log(sentry_level_t level, const std::string& logger, const std::string& text); void LogError(const std::string& text, const std::string& file, const std::string& line); - void AddExtra(const std::string& key, const sentry_value_t& value); - void AddExtra(const std::string& key, const std::string& value); + void SetExtra(const std::string& key, const sentry_value_t& value); + void SetExtra(const std::string& key, const std::string& value); void LogException(const std::exception& e, const std::string& file, const std::string& line); void AddErrorBreadcrumb(const std::string& msg, const std::string& file, const std::string& line); // cleared when Logged diff --git a/src/Http.cpp b/src/Http.cpp index af88442..85a5ee7 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -124,14 +124,14 @@ std::string Http::POST(const std::string& host, const std::string& target, const http::read(stream, buffer, response); - Sentry.AddExtra("reponse-code", std::to_string(response.result_int())); + Sentry.SetExtra("reponse-code", std::to_string(response.result_int())); for (const auto& header : response.base()) { // need to do explicit casts to convert string_view to string // since string_view may not be null-terminated (and in fact isn't, here) std::string KeyString(header.name_string()); std::string ValueString(header.value()); - Sentry.AddExtra(KeyString, ValueString); + Sentry.SetExtra(KeyString, ValueString); } std::stringstream result; diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 575892d..c095742 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -39,32 +39,46 @@ void THeartbeatThread::operator()() { T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false); if (T.substr(0, 2) != "20") { + auto SentryReportError = [&](const std::string& transaction) { + if (T.size() > std::string("YOU_SHALL_NOT_PASS").size() + && Application::Settings.Key.size() == 36) { + auto Lock = Sentry.CreateExclusiveContext(); + Sentry.SetExtra("response-body", T); + Sentry.SetExtra("request-body", Body); + Sentry.SetTransaction(transaction); + Sentry.Log(SENTRY_LEVEL_ERROR, "default", "wrong backend response format"); + } + }; + SentryReportError(Application::GetBackendHostname() + Target); + //Backend system refused server startup! - warn("Backend system refused server! Server might not show in the public list"); - debug("server returned \"" + T + "\""); - if (T.size() > std::string("YOU_SHALL_NOT_PASS").size() - && Application::Settings.Key.size() == 36) { - auto Lock = Sentry.CreateExclusiveContext(); - Sentry.AddExtra("response-body", T); - Sentry.AddExtra("request-body", Body); - Sentry.SetTransaction(Application::GetBackendHostname() + Target); - Sentry.Log(SENTRY_LEVEL_ERROR, "default", "wrong backend response format"); - } - isAuth = false; - } - - if (!isAuth) { - if (T == "2000") { - info(("Authenticated!")); - isAuth = true; - } else if (T == "200") { - info(("Resumed authenticated session!")); - isAuth = true; + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + T = Http::POST(Application::GetBackup1Hostname(), Target, {}, Body, false); + if (T.substr(0, 2) != "20") { + SentryReportError(Application::GetBackup1Hostname() + Target); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + T = Http::POST(Application::GetBackup2Hostname(), Target, {}, Body, false); + if (T.substr(0, 2) != "20") { + warn("Backend system refused server! Server might not show in the public list"); + isAuth = false; + SentryReportError(Application::GetBackup2Hostname() + Target); + } } } - - //SocketIO::Get().SetAuthenticated(isAuth); } + + if (!isAuth) { + if (T == "2000") { + info(("Authenticated!")); + isAuth = true; + } else if (T == "200") { + info(("Resumed authenticated session!")); + isAuth = true; + } + } + + //SocketIO::Get().SetAuthenticated(isAuth); +} } std::string THeartbeatThread::GenerateCall() { std::stringstream Ret; diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index e91d3d9..9c1def8 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -298,8 +298,8 @@ void TNetwork::Authentication(SOCKET TCPSock) { ClientKick(*Client, "Backend returned invalid auth response format."); error("Backend returned invalid auth response format. This should never happen."); auto Lock = Sentry.CreateExclusiveContext(); - Sentry.AddExtra("response-body", Rc); - Sentry.AddExtra("key", RequestString); + Sentry.SetExtra("response-body", Rc); + Sentry.SetExtra("key", RequestString); Sentry.SetTransaction(Application::GetBackendUrlForAuth() + Target); Sentry.Log(SENTRY_LEVEL_ERROR, "default", "auth: wrong backend response format"); return; diff --git a/src/TSentry.cpp b/src/TSentry.cpp index 35f7486..0287c65 100644 --- a/src/TSentry.cpp +++ b/src/TSentry.cpp @@ -49,18 +49,18 @@ void TSentry::LogError(const std::string& text, const std::string& file, const s Log(SENTRY_LEVEL_ERROR, "default", file + ": " + text); } -void TSentry::AddExtra(const std::string& key, const sentry_value_t& value) { +void TSentry::SetExtra(const std::string& key, const sentry_value_t& value) { if (!mValid) { return; } sentry_set_extra(key.c_str(), value); } -void TSentry::AddExtra(const std::string& key, const std::string& value) { +void TSentry::SetExtra(const std::string& key, const std::string& value) { if (!mValid) { return; } - AddExtra(key.c_str(), sentry_value_new_string(value.c_str())); + SetExtra(key.c_str(), sentry_value_new_string(value.c_str())); } void TSentry::LogException(const std::exception& e, const std::string& file, const std::string& line) { diff --git a/src/TServer.cpp b/src/TServer.cpp index c43857d..d2e4d58 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -324,9 +324,9 @@ void TServer::Apply(TClient& c, int VID, const std::string& pckt) { if (VD.empty()) { error("Tried to apply change to vehicle that does not exist"); auto Lock = Sentry.CreateExclusiveContext(); - Sentry.AddExtra("packet", Packet); - Sentry.AddExtra("vehicle-id", std::to_string(VID)); - Sentry.AddExtra("client-car-count", std::to_string(c.GetCarCount())); + Sentry.SetExtra("packet", Packet); + Sentry.SetExtra("vehicle-id", std::to_string(VID)); + Sentry.SetExtra("client-car-count", std::to_string(c.GetCarCount())); Sentry.LogError("attempt to apply change to nonexistent vehicle", _file_basename, _line); return; } @@ -335,7 +335,7 @@ void TServer::Apply(TClient& c, int VID, const std::string& pckt) { FoundPos = VD.find('{'); if (FoundPos == std::string::npos) { auto Lock = Sentry.CreateExclusiveContext(); - Sentry.AddExtra("packet", VD); + Sentry.SetExtra("packet", VD); Sentry.LogError("malformed packet", _file_basename, _line); error("Malformed packet received, no '{' found"); return;