From bbd58bba7567f463e2581559df522d67f3d0b026 Mon Sep 17 00:00:00 2001 From: IcePixelx <41352111+PixieCore@users.noreply.github.com> Date: Tue, 17 Aug 2021 23:18:35 +0200 Subject: [PATCH] Fixed potential crashes in r5net and added debug prints. * Potential crashes were when in certain post functions the returned status wasn't 200. --- r5net/include/netpch.h | 2 + r5net/include/r5/r5net.h | 2 +- r5net/src/r5net.cpp | 148 +++++++++++++++++++++++++++------------ 3 files changed, 107 insertions(+), 45 deletions(-) diff --git a/r5net/include/netpch.h b/r5net/include/netpch.h index c41f119c..78f633cf 100644 --- a/r5net/include/netpch.h +++ b/r5net/include/netpch.h @@ -1,6 +1,8 @@ #pragma once #pragma message("Precompiling r5net headers.\n") +//#define DebugR5Net + #define WIN32_LEAN_AND_MEAN #include #include diff --git a/r5net/include/r5/r5net.h b/r5net/include/r5/r5net.h index dd77c6ad..1293c5f1 100644 --- a/r5net/include/r5/r5net.h +++ b/r5net/include/r5/r5net.h @@ -22,7 +22,7 @@ namespace R5Net std::vector GetServersList(std::string& outMessage); bool PostServerHost(std::string& outMessage, std::string& outToken, const ServerListing& serverListing); - bool GetServerByToken(ServerListing& outServer, std::string& outError, const std::string& token, const std::string& password = ""); + bool GetServerByToken(ServerListing& outServer, std::string& outMessage, const std::string token, const std::string password = ""); std::string GetVersionString(); }; } \ No newline at end of file diff --git a/r5net/src/r5net.cpp b/r5net/src/r5net.cpp index 852ca936..5a901949 100644 --- a/r5net/src/r5net.cpp +++ b/r5net/src/r5net.cpp @@ -16,11 +16,19 @@ std::vector R5Net::Client::GetServersList(std::string& outMessage nlohmann::json reqBody = nlohmann::json::object(); reqBody["version"] = GetVersionString(); - std::string reqBodyStr = reqBody.dump();; + std::string reqBodyStr = reqBody.dump(); + +#ifdef DebugR5Net + std::cout << " [+R5Net+] Sending GetServerList post now..\n"; +#endif httplib::Result res = m_HttpClient.Post("/servers", reqBody.dump().c_str(), reqBody.dump().length(), "application/json"); - if (res) +#ifdef DebugR5Net + std::cout << " [+R5Net+] GetServerList replied with " << res->status << "\n"; +#endif + + if (res && res->status == 200) // STATUS_OK { nlohmann::json resBody = nlohmann::json::parse(res->body); if (resBody["success"].is_boolean() && resBody["success"].get()) @@ -42,7 +50,10 @@ std::vector R5Net::Client::GetServersList(std::string& outMessage } else { - outMessage = "Failed to reach comp-server"; + if (res) + outMessage = std::string("Failed to reach comp-server ") + std::to_string(res->status); + else + outMessage = "Failed to reach comp-server unknown error code."; } return list; @@ -61,70 +72,119 @@ bool R5Net::Client::PostServerHost(std::string& outMessage, std::string& outToke std::string reqBodyStr = reqBody.dump(); - auto res = m_HttpClient.Post("/servers/add", reqBodyStr.c_str(), reqBodyStr.length(), "application/json"); +#ifdef DebugR5Net + std::cout << " [+R5Net+] Sending PostServerHost post now..\n"; +#endif - if (!res) + httplib::Result res = m_HttpClient.Post("/servers/add", reqBodyStr.c_str(), reqBodyStr.length(), "application/json"); + +#ifdef DebugR5Net + std::cout << " [+R5Net+] PostServerHost replied with " << res->status << "\n"; +#endif + + if (res && res->status == 200) // STATUS_OK { - outMessage = "Failed to reach comp-server"; - outToken = ""; - return false; - } - - nlohmann::json resBody = nlohmann::json::parse(res->body); - if (resBody["success"].is_boolean() && resBody["success"].get()) - { - if (resBody["token"].is_string()) - outToken = resBody["token"].get(); + nlohmann::json resBody = nlohmann::json::parse(res->body); + if (resBody["success"].is_boolean() && resBody["success"].get()) + { + if (resBody["token"].is_string()) + outToken = resBody["token"].get(); + else + outToken = ""; + + return true; + } else - outToken = ""; - return true; + { + if (resBody["err"].is_string()) + outMessage = resBody["err"].get(); + else + outMessage = "An unknown error occured!"; + + return false; + } } else { - if (resBody["err"].is_string()) - outMessage = resBody["err"].get(); - else - outMessage = "An unknown error occured!"; + if (res) + outMessage = std::string("Failed to reach comp-server ") + std::to_string(res->status); + else + outMessage = "Failed to reach comp-server unknown error code."; + + outToken = ""; return false; } + + return false; } -bool R5Net::Client::GetServerByToken(ServerListing& outServer, std::string& outError, const std::string& token, const std::string& password) +bool R5Net::Client::GetServerByToken(ServerListing& outServer, std::string& outMessage, const std::string token, const std::string password) { nlohmann::json reqBody = nlohmann::json::object(); reqBody["token"] = token; reqBody["password"] = password; +#ifdef DebugR5Net + std::cout << " [+R5Net+] Sending GetServerByToken post now...\n"; +#endif + httplib::Result res = m_HttpClient.Post("/server/byToken", reqBody.dump().c_str(), reqBody.dump().length(), "application/json"); - if (!res) +#ifdef DebugR5Net + std::cout << " [+R5Net+] GetServerByToken replied with " << res->status << "\n"; +#endif + + if (res && res->status == 200) // STATUS_OK { - outError = "Failed to reach comp-server"; - outServer = ServerListing{}; - return false; + if (!res->body.empty()) + { + nlohmann::json resBody = nlohmann::json::parse(res->body); + + if (res && resBody["success"].is_boolean() && resBody["success"]) + { + outServer = ServerListing{ + resBody["server"]["name"].get(), + resBody["server"]["map"].get(), + resBody["server"]["ip"].get(), + resBody["server"]["port"].get() + }; + return true; + } + else + { + if (resBody["err"].is_string()) + outMessage = resBody["err"].get(); + else + outMessage = ""; + + outServer = ServerListing{}; + return false; + } + } } - - nlohmann::json resBody = nlohmann::json::parse(res->body); - - if (res && resBody["success"].is_boolean() && resBody["success"]) + else { - outServer = ServerListing{ - resBody["server"]["name"].get(), - resBody["server"]["map"].get(), - resBody["server"]["ip"].get(), - resBody["server"]["port"].get() - }; - return true; - } - else - { - if (resBody["err"].is_string()) - outError = resBody["err"].get(); - else - outError = ""; + if (res) + { + if (!res->body.empty()) + { + nlohmann::json resBody = nlohmann::json::parse(res->body); + if (resBody["err"].is_string()) + outMessage = resBody["err"].get(); + else + outMessage = "Failed to reach comp-server unknown error code."; + + return false; + } + + outMessage = std::string("Failed to reach comp-server ") + std::to_string(res->status); + return false; + } + + outMessage = "failed to reach comp-server unknown error code."; outServer = ServerListing{}; return false; }