add http debug error logging

This commit is contained in:
Lion Kortlepel 2024-03-22 12:19:18 +01:00
parent cc42a5e0ab
commit adba66afb9
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
3 changed files with 146 additions and 85 deletions

View File

@ -15,3 +15,4 @@ void debug(const std::string& toPrint);
void error(const std::string& toPrint);
void info(const std::string& toPrint);
void warn(const std::string& toPrint);
std::string getDate();

View File

@ -13,7 +13,51 @@
#include "Http.h"
#include <mutex>
#include <cmath>
#include <filesystem>
#include <httplib.h>
#include <nlohmann/json.hpp>
void WriteHttpDebug(const httplib::Client& client, const std::string& method, const std::string& target, const httplib::Result& result) try {
const std::filesystem::path folder = ".https_debug";
std::filesystem::create_directories(folder);
if (!std::filesystem::exists(folder / "WHAT IS THIS FOLDER.txt")) {
std::ofstream ignore { folder / "WHAT IS THIS FOLDER.txt" };
ignore << "This folder exists to help debug current issues with the backend. Do not share this folder with anyone but BeamMP staff. It contains detailed logs of any failed http requests." << std::endl;
}
const auto file = folder / (method + ".json");
// 1 MB limit
if (std::filesystem::exists(file) && std::filesystem::file_size(file) > 1'000'000) {
std::filesystem::rename(file, file.generic_string() + ".bak");
}
std::ofstream of { file, std::ios::app };
nlohmann::json js {
{ "utc", std::chrono::system_clock::now().time_since_epoch().count() },
{ "target", target },
{ "client_info", {
{ "openssl_verify_result", client.get_openssl_verify_result() },
{ "host", client.host()},
{ "port", client.port()},
{ "socket_open", client.is_socket_open()},
{"valid", client.is_valid()},
}
},
};
if (result) {
auto value = result.value();
js["result"] = {};
js["result"]["body"] = value.body;
js["result"]["status"] = value.status;
js["result"]["headers"] = value.headers;
js["result"]["version"] = value.version;
js["result"]["location"] = value.location;
js["result"]["reason"] = value.reason;
}
of << js.dump();
}
catch (const std::exception& e) {
error(e.what());
}
bool HTTP::isDownload = false;
std::string HTTP::Get(const std::string& IP) {
@ -33,13 +77,18 @@ std::string HTTP::Get(const std::string &IP) {
Ret = res->body;
}
else {
WriteHttpDebug(cli, "GET", IP, res);
error("Failed to GET '" + IP + "': " + res->reason + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result()));
}
}else{
}
else {
if (isDownload) {
std::cout << "\n";
}
WriteHttpDebug(cli, "GET", IP, res);
error("HTTP Get failed on " + to_string(res.error()) + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result()));
}
return Ret;
@ -63,18 +112,25 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
error(res->reason);
}
Ret = res->body;
}else{
error("HTTP Post failed on " + to_string(res.error()) + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result()));
}
}else{
else {
WriteHttpDebug(cli, "POST", IP, res);
error("HTTP Post failed on " + to_string(res.error()) + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result()));
}
}
else {
httplib::Result res = cli.Post(IP.substr(pos).c_str());
if (res) {
if (res->status != 200) {
error(res->reason);
}
Ret = res->body;
}else{
}
else {
WriteHttpDebug(cli, "POST", IP, res);
error("HTTP Post failed on " + to_string(res.error()) + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result()));
}
}
@ -115,7 +171,8 @@ bool HTTP::Download(const std::string &IP, const std::string &Path) {
File.close();
std::cout << "\n";
info("Download Complete!");
}else{
}
else {
error("Failed to open file directory: " + Path);
return false;
}

View File

@ -25,6 +25,9 @@ int main(int argc, char* argv[]) {
std::thread th(flush);
th.detach();
#endif
HTTP::Get("https://backend.beammp.com/fucker");
GetEP(argv[0]);
InitLauncher(argc, argv);