add api-v header to heartbeat post

This commit is contained in:
Lion Kortlepel 2022-01-20 16:09:08 +01:00
parent c42c748b37
commit cd4129e05d
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 55 additions and 16 deletions

View File

@ -36,10 +36,11 @@ 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, const httplib::Headers& headers) { 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); httplib::SSLClient client(host, port);
client.set_read_timeout(std::chrono::seconds(10));
beammp_assert(client.is_valid()); beammp_assert(client.is_valid());
client.enable_server_certificate_verification(false); client.enable_server_certificate_verification(false);
client.set_address_family(AF_INET); client.set_address_family(AF_INET);
auto res = client.Post(target.c_str(), body.c_str(), body.size(), ContentType.c_str()); auto res = client.Post(target.c_str(), headers, body.c_str(), body.size(), ContentType.c_str());
if (res) { if (res) {
if (status) { if (status) {
*status = res->status; *status = res->status;

View File

@ -55,32 +55,70 @@ void THeartbeatThread::operator()() {
auto Target = "/heartbeat"; auto Target = "/heartbeat";
unsigned int ResponseCode = 0; unsigned int ResponseCode = 0;
bool Ok = true;
json::Document Doc; json::Document Doc;
for (const auto& Hostname : Application::GetBackendUrlsInOrder()) { bool Ok = false;
T = Http::POST(Hostname, 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode, { { "api-v", "2" } }); for (const auto& Url : Application::GetBackendUrlsInOrder()) {
if ((T.substr(0, 2) != "20" && ResponseCode != 200) || ResponseCode != 200) { T = Http::POST(Url, 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode, { { "api-v", "2" } });
beammp_trace("heartbeat to " + Hostname + " returned: " + T); beammp_trace(T);
Application::SetSubsystemStatus("Heartbeat", Application::Status::Bad); Doc.Parse(T.data(), T.size());
isAuth = false; if (Doc.HasParseError() || !Doc.IsObject()) {
SentryReportError(Hostname + Target, ResponseCode); beammp_error("Backend response failed to parse as valid json");
Ok = false; beammp_debug("Response was: `" + T + "`");
Sentry.SetContext("JSON Response", { { "reponse", T } });
SentryReportError(Url + Target, ResponseCode);
} else if (ResponseCode != 200) {
SentryReportError(Url + Target, ResponseCode);
} else { } else {
Application::SetSubsystemStatus("Heartbeat", Application::Status::Good); // all ok
Ok = true; Ok = true;
break; 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 = "msg";
if (Ok) {
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) { if (!Ok) {
beammp_warn("Backend system refused server! Server will not show in the public server list."); beammp_error("Missing/invalid json members in backend response");
Sentry.LogError("Missing/invalid json members in backend response", __FILE__, std::to_string(__LINE__));
} }
if (!isAuth) { }
if (T == "2000") {
if (Ok && !isAuth) {
if (Status == "2000") {
beammp_info(("Authenticated!")); beammp_info(("Authenticated!"));
isAuth = true; isAuth = true;
} else if (T == "200") { } else if (Status == "200") {
beammp_info(("Resumed authenticated session!")); beammp_info(("Resumed authenticated session!"));
isAuth = true; isAuth = true;
} else {
if (Message.empty()) {
Message = "Backend didn't provide a reason";
}
beammp_error("Backend REFUSED the auth key. " + Message);
} }
} }
} }