From 7f2ca025f8756b6985bc368221d777c6525be53b Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 20 Sep 2021 23:37:23 +0200 Subject: [PATCH 1/5] Start using new heartbeat response format --- src/THeartbeatThread.cpp | 69 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 4a854e8..19ba0e9 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -3,8 +3,12 @@ #include "Client.h" #include "Http.h" //#include "SocketIO.h" +#include +#include #include +namespace json = rapidjson; + void THeartbeatThread::operator()() { RegisterThread("Heartbeat"); std::string Body; @@ -41,13 +45,68 @@ void THeartbeatThread::operator()() { { { "response-body", T }, { "request-body", Body } }); Sentry.SetTransaction(transaction); - trace("sending log to sentry: " + std::to_string(status) + " for " + transaction); Sentry.Log(SentryLevel::Error, "default", Http::Status::ToString(status) + " (" + std::to_string(status) + ")"); }; auto Target = "/heartbeat"; int ResponseCode = -1; - T = Http::POST(Application::GetBackendHostname(), Target, {}, Body, false, &ResponseCode); + const std::vector Urls = { + Application::GetBackendHostname(), + Application::GetBackup1Hostname(), + Application::GetBackup2Hostname(), + }; + + json::Document Doc; + bool Ok = false; + for (const auto& Url : Urls) { + T = Http::POST(Url, Target, {}, Body, false, &ResponseCode); + Doc.Parse(T.data(), T.size()); + if (Doc.HasParseError() || !Doc.IsObject()) { + error("Backend response failed to parse as valid json"); + debug("Response was: `" + T + "`"); + Sentry.SetContext("JSON Response", { { "reponse", T } }); + SentryReportError(Url + Target, ResponseCode); + } else if (ResponseCode != 200) { + SentryReportError(Url + Target, ResponseCode); + } else { + // all ok + Ok = true; + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + } + std::string Status {}; + std::string Code {}; + std::string Message {}; + const auto StatusKey = "status"; + const auto CodeKey = "code"; + const auto MessageKey = "message"; + if (!Ok) { + isAuth = false; + } else { + if (Doc.HasMember(StatusKey) && Doc[StatusKey].IsString()) { + Status = Doc[StatusKey].GetString(); + } else { + Sentry.SetContext("JSON Response", { { StatusKey, "invalid string / missing" } }); + Ok = false; + } + if (Doc.HasMember(CodeKey) && Doc[CodeKey].IsString()) { + Code = Doc[CodeKey].GetString(); + } else { + Sentry.SetContext("JSON Response", { { CodeKey, "invalid string / missing" } }); + Ok = false; + } + if (Doc.HasMember(MessageKey) && Doc[MessageKey].IsString()) { + Message = Doc[MessageKey].GetString(); + } else { + Sentry.SetContext("JSON Response", { { MessageKey, "invalid string / missing" } }); + Ok = false; + } + if (!Ok) { + error("Missing/invalid json members in backend response"); + Sentry.LogError("Missing/invalid json members in backend response", __FILE__, std::to_string(__LINE__)); + } + } if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { trace("got " + T + " from backend"); @@ -55,12 +114,10 @@ void THeartbeatThread::operator()() { std::this_thread::sleep_for(std::chrono::milliseconds(500)); T = Http::POST(Application::GetBackup1Hostname(), Target, {}, Body, false, &ResponseCode); if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { - SentryReportError(Application::GetBackup1Hostname() + Target, ResponseCode); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - T = Http::POST(Application::GetBackup2Hostname(), Target, {}, Body, false, &ResponseCode); + if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { warn("Backend system refused server! Server will not show in the public server list."); - isAuth = false; + SentryReportError(Application::GetBackup2Hostname() + Target, ResponseCode); } } From 0961f866627e11341b58bf5e4f1fd886a21f5b86 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Tue, 21 Sep 2021 13:25:26 +0200 Subject: [PATCH 2/5] Add heartbeat-api-v API version header --- src/THeartbeatThread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 19ba0e9..529f728 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -59,7 +59,7 @@ void THeartbeatThread::operator()() { json::Document Doc; bool Ok = false; for (const auto& Url : Urls) { - T = Http::POST(Url, Target, {}, Body, false, &ResponseCode); + T = Http::POST(Url, Target, {"heartbeat-api-v", "1.0.0"}, Body, false, &ResponseCode); Doc.Parse(T.data(), T.size()); if (Doc.HasParseError() || !Doc.IsObject()) { error("Backend response failed to parse as valid json"); From d0431c0b9dc75310e36d4e3c15ae836df37d8dc0 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 23 Sep 2021 00:14:38 +0200 Subject: [PATCH 3/5] Use v2 api --- src/THeartbeatThread.cpp | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 529f728..3bde422 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -59,7 +59,8 @@ void THeartbeatThread::operator()() { json::Document Doc; bool Ok = false; for (const auto& Url : Urls) { - T = Http::POST(Url, Target, {"heartbeat-api-v", "1.0.0"}, Body, false, &ResponseCode); + T = Http::POST(Url, Target, { { "api-v", "2" } }, Body, false, &ResponseCode); + trace(T); Doc.Parse(T.data(), T.size()); if (Doc.HasParseError() || !Doc.IsObject()) { error("Backend response failed to parse as valid json"); @@ -80,10 +81,9 @@ void THeartbeatThread::operator()() { std::string Message {}; const auto StatusKey = "status"; const auto CodeKey = "code"; - const auto MessageKey = "message"; - if (!Ok) { - isAuth = false; - } else { + const auto MessageKey = "msg"; + + if (Ok) { if (Doc.HasMember(StatusKey) && Doc[StatusKey].IsString()) { Status = Doc[StatusKey].GetString(); } else { @@ -108,32 +108,20 @@ void THeartbeatThread::operator()() { } } - if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { - trace("got " + T + " from backend"); - SentryReportError(Application::GetBackendHostname() + Target, ResponseCode); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - T = Http::POST(Application::GetBackup1Hostname(), Target, {}, Body, false, &ResponseCode); - if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { - - if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { - warn("Backend system refused server! Server will not show in the public server list."); - - SentryReportError(Application::GetBackup2Hostname() + Target, ResponseCode); - } - } - } - - if (!isAuth) { - if (T == "2000") { + if (Ok && !isAuth) { + if (Status == "2000") { info(("Authenticated!")); isAuth = true; - } else if (T == "200") { + } else if (Status == "200") { info(("Resumed authenticated session!")); isAuth = true; + } else { + if (Message.empty()) { + Message = "Backend didn't provide a reason"; + } + error("Backend REFUSED the auth key. " + Message); } } - - //SocketIO::Get().SetAuthenticated(isAuth); } } From 1b2ea88a717c2826b502b7e3910df12b34cfaece Mon Sep 17 00:00:00 2001 From: Lion Date: Wed, 27 Oct 2021 18:41:57 +0200 Subject: [PATCH 4/5] README: add irc and discord links --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 22464eb..ffdc1fe 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,11 @@ This is the server for the multiplayer mod **[BeamMP](https://beammp.com/)** for the game [BeamNG.drive](https://www.beamng.com/). The server is the point throug which all clients communicate. You can write lua mods for the server, detailed instructions on the [BeamMP Wiki](https://wiki.beammp.com). +## Support + Contact + +- **IRC**: `#beammp` on [irc.libera.chat](https://web.libera.chat/) +- **Discord**: [click for invite](https://discord.gg/beammp) + ## Minimum Requirements These values are guesstimated and are subject to change with each release. From a18abd6c8b05073b66aa0aa90718b1ebf64c6ce6 Mon Sep 17 00:00:00 2001 From: Lion Date: Wed, 27 Oct 2021 19:00:01 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ffdc1fe..cc53c31 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ The server is the point throug which all clients communicate. You can write lua ## Support + Contact +Feel free to ask any questions via the following channels: + - **IRC**: `#beammp` on [irc.libera.chat](https://web.libera.chat/) - **Discord**: [click for invite](https://discord.gg/beammp)