switch to curl for Get and Post

This commit is contained in:
Lion Kortlepel 2024-10-04 22:20:22 +02:00
parent 7600372ca1
commit c74455e0fe
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
4 changed files with 301 additions and 324 deletions

View File

@ -17,6 +17,7 @@ add_compile_definitions(CPPHTTPLIB_OPENSSL_SUPPORT)
file(GLOB source_files "src/*.cpp" "src/*/*.cpp" "src/*/*.hpp" "include/*.h" "include/*/*.h" "include/*/*/*.h" "include/*.hpp" "include/*/*.hpp" "include/*/*/*.hpp") file(GLOB source_files "src/*.cpp" "src/*/*.cpp" "src/*/*.hpp" "include/*.h" "include/*/*.h" "include/*/*/*.h" "include/*.hpp" "include/*/*.hpp" "include/*/*/*.hpp")
find_package(httplib CONFIG REQUIRED) find_package(httplib CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED) find_package(nlohmann_json CONFIG REQUIRED)
find_package(CURL REQUIRED)
add_executable(${PROJECT_NAME} ${source_files}) add_executable(${PROJECT_NAME} ${source_files})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "BeamMP-Launcher") set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "BeamMP-Launcher")
@ -25,15 +26,15 @@ if (WIN32)
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
find_package(OpenSSL REQUIRED) find_package(OpenSSL REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE target_link_libraries(${PROJECT_NAME} PRIVATE
ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto ws2_32 httplib::httplib nlohmann_json::nlohmann_json) ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto ws2_32 httplib::httplib nlohmann_json::nlohmann_json CURL::libcurl)
elseif (LINUX) elseif (LINUX)
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
find_package(OpenSSL REQUIRED) find_package(OpenSSL REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE target_link_libraries(${PROJECT_NAME} PRIVATE
ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto) ZLIB::ZLIB OpenSSL::SSL OpenSSL::Crypto CURL::libcurl)
else(WIN32) #MINGW else(WIN32) #MINGW
add_definitions("-D_WIN32_WINNT=0x0600") add_definitions("-D_WIN32_WINNT=0x0600")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -s --static")
target_link_libraries(${PROJECT_NAME} ssl crypto ws2_32 ssp crypt32 z) target_link_libraries(${PROJECT_NAME} ssl crypto ws2_32 ssp crypt32 z CURL::libcurl)
endif(WIN32) endif(WIN32)
target_include_directories(${PROJECT_NAME} PRIVATE "include") target_include_directories(${PROJECT_NAME} PRIVATE "include")

View File

@ -1,319 +1,292 @@
// Copyright (c) 2019-present Anonymous275. // Copyright (c) 2019-present Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software. // BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries. // One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository. // Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
/// ///
/// Created by Anonymous275 on 7/18/2020 /// Created by Anonymous275 on 7/18/2020
/// ///
#include "Http.h" #include "Http.h"
#include <Logger.h> #include <Logger.h>
#include <cmath> #include <Network/network.hpp>
#include <filesystem> #include <Startup.h>
#include <fstream> #include <Utils.h>
#include <httplib.h> #include <cmath>
#include <iostream> #include <curl/curl.h>
#include <mutex> #include <curl/easy.h>
#include <nlohmann/json.hpp> #include <filesystem>
#include <Startup.h> #include <fstream>
#include <Network/network.hpp> #include <httplib.h>
#include <Utils.h> #include <iostream>
#include <mutex>
void WriteHttpDebug(const httplib::Client& client, const std::string& method, const std::string& target, const httplib::Result& result) try { #include <nlohmann/json.hpp>
const std::filesystem::path folder = ".https_debug";
std::filesystem::create_directories(folder); void WriteHttpDebug(const httplib::Client& client, const std::string& method, const std::string& target, const httplib::Result& result) try {
if (!std::filesystem::exists(folder / "WHAT IS THIS FOLDER.txt")) { const std::filesystem::path folder = ".https_debug";
std::ofstream ignore { folder / "WHAT IS THIS FOLDER.txt" }; std::filesystem::create_directories(folder);
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; if (!std::filesystem::exists(folder / "WHAT IS THIS FOLDER.txt")) {
} std::ofstream ignore { folder / "WHAT IS THIS FOLDER.txt" };
const auto file = folder / (method + ".json"); 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;
// 1 MB limit }
if (std::filesystem::exists(file) && std::filesystem::file_size(file) > 1'000'000) { const auto file = folder / (method + ".json");
std::filesystem::rename(file, file.generic_string() + ".bak"); // 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() }, std::ofstream of { file, std::ios::app };
{ "target", target }, nlohmann::json js {
{ "client_info", { { "utc", std::chrono::system_clock::now().time_since_epoch().count() },
{ "openssl_verify_result", client.get_openssl_verify_result() }, { "target", target },
{ "host", client.host() }, { "client_info", {
{ "port", client.port() }, { "openssl_verify_result", client.get_openssl_verify_result() },
{ "socket_open", client.is_socket_open() }, { "host", client.host() },
{ "valid", client.is_valid() }, { "port", client.port() },
} }, { "socket_open", client.is_socket_open() },
}; { "valid", client.is_valid() },
if (result) { } },
auto value = result.value(); };
js["result"] = {}; if (result) {
js["result"]["body"] = value.body; auto value = result.value();
js["result"]["status"] = value.status; js["result"] = {};
js["result"]["headers"] = value.headers; js["result"]["body"] = value.body;
js["result"]["version"] = value.version; js["result"]["status"] = value.status;
js["result"]["location"] = value.location; js["result"]["headers"] = value.headers;
js["result"]["reason"] = value.reason; js["result"]["version"] = value.version;
} js["result"]["location"] = value.location;
of << js.dump(); js["result"]["reason"] = value.reason;
} catch (const std::exception& e) { }
error(e.what()); of << js.dump();
} } catch (const std::exception& e) {
error(e.what());
bool HTTP::isDownload = false; }
std::string HTTP::Get(const std::string& IP) {
static std::mutex Lock; static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
std::scoped_lock Guard(Lock); std::string* Result = reinterpret_cast<std::string*>(userp);
std::string NewContents(reinterpret_cast<char*>(contents), size * nmemb);
auto pos = IP.find('/', 10); *Result += NewContents;
return size * nmemb;
httplib::Client cli(IP.substr(0, pos).c_str()); }
cli.set_connection_timeout(std::chrono::seconds(10));
cli.set_follow_location(true); bool HTTP::isDownload = false;
if (SkipSslVerify) { std::string HTTP::Get(const std::string& IP) {
debug("Skipping SSL server validation via --skip-ssl-verify"); std::string Ret;
cli.enable_server_certificate_verification(false); static thread_local CURL* curl = curl_easy_init();
} if (curl) {
auto res = cli.Get(IP.substr(pos).c_str(), ProgressBar); CURLcode res;
std::string Ret; curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteCallback);
if (res) { curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&Ret);
if (res->status == 200) { curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // seconds
Ret = res->body; res = curl_easy_perform(curl);
} else { if (res != CURLE_OK) {
WriteHttpDebug(cli, "GET", IP, res); error("GET to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
error("Failed to GET (status " + std::to_string(res->status) + ") '" + IP + "': " + res->reason + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result())); return "";
} }
} else { } else {
if (isDownload) { error("Curl easy init failed");
std::cout << "\n"; return "";
} }
auto result = cli.get_openssl_verify_result(); auto result = cli.get_openssl_verify_result();
std::string verify_error; std::string verify_error;
if (result) { if (result) {
verify_error = X509_verify_cert_error_string(result); verify_error = X509_verify_cert_error_string(result);
} }
WriteHttpDebug(cli, "GET", IP, res); return Ret;
error("HTTP Get failed on " + to_string(res.error()) + ", ssl verify = " + verify_error); }
}
std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
return Ret; std::string Ret;
} static thread_local CURL* curl = curl_easy_init();
if (curl) {
std::string HTTP::Post(const std::string& IP, const std::string& Fields) { CURLcode res;
static std::mutex Lock; curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
std::scoped_lock Guard(Lock); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&Ret);
auto pos = IP.find('/', 10); curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str());
httplib::Client cli(IP.substr(0, pos).c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size());
cli.set_connection_timeout(std::chrono::seconds(10)); struct curl_slist* list = nullptr;
if (SkipSslVerify) { list = curl_slist_append(list, "Content-Type: application/json");
debug("Skipping SSL server validation via --skip-ssl-verify"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
cli.enable_server_certificate_verification(false); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // seconds
} res = curl_easy_perform(curl);
std::string Ret; curl_slist_free_all(list);
if (res != CURLE_OK) {
if (!Fields.empty()) { error("POST to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
httplib::Result res = cli.Post(IP.substr(pos).c_str(), Fields, "application/json"); return "";
}
if (res) { } else {
if (res->status != 200) { error("Curl easy init failed");
error(res->reason); return "";
} }
Ret = res->body; return Ret;
} 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())); bool HTTP::ProgressBar(size_t c, size_t t) {
} if (isDownload) {
} else { static double last_progress, progress_bar_adv;
httplib::Result res = cli.Post(IP.substr(pos).c_str()); progress_bar_adv = round(c / double(t) * 25);
if (res) { std::cout << "\r";
if (res->status != 200) { std::cout << "Progress : [ ";
error(res->reason); std::cout << round(c / double(t) * 100);
} std::cout << "% ] [";
Ret = res->body; int i;
} else { for (i = 0; i <= progress_bar_adv; i++)
auto result = cli.get_openssl_verify_result(); std::cout << "#";
std::string verify_error; for (i = 0; i < 25 - progress_bar_adv; i++)
if (result) { std::cout << ".";
verify_error = X509_verify_cert_error_string(result); std::cout << "]";
} last_progress = round(c / double(t) * 100);
WriteHttpDebug(cli, "POST", IP, res); }
error("HTTP Post failed on " + to_string(res.error()) + ", ssl verify = " + verify_error); return true;
} }
}
bool HTTP::Download(const std::string& IP, const std::string& Path) {
if (Ret.empty()) static std::mutex Lock;
return "-1"; std::scoped_lock Guard(Lock);
else
return Ret; isDownload = true;
} std::string Ret = Get(IP);
isDownload = false;
bool HTTP::ProgressBar(size_t c, size_t t) {
if (isDownload) { if (Ret.empty())
static double last_progress, progress_bar_adv; return false;
progress_bar_adv = round(c / double(t) * 25);
std::cout << "\r"; std::ofstream File(Path, std::ios::binary);
std::cout << "Progress : [ "; if (File.is_open()) {
std::cout << round(c / double(t) * 100); File << Ret;
std::cout << "% ] ["; File.close();
int i; std::cout << "\n";
for (i = 0; i <= progress_bar_adv; i++) info("Download Complete!");
std::cout << "#"; } else {
for (i = 0; i < 25 - progress_bar_adv; i++) error("Failed to open file directory: " + Path);
std::cout << "."; return false;
std::cout << "]"; }
last_progress = round(c / double(t) * 100);
} return true;
return true; }
}
void set_headers(httplib::Response& res) {
bool HTTP::Download(const std::string& IP, const std::string& Path) { res.set_header("Access-Control-Allow-Origin", "*");
static std::mutex Lock; res.set_header("Access-Control-Request-Method", "POST, OPTIONS, GET");
std::scoped_lock Guard(Lock); res.set_header("Access-Control-Request-Headers", "X-API-Version");
}
isDownload = true;
std::string Ret = Get(IP); void HTTP::StartProxy() {
isDownload = false; std::thread proxy([&]() {
httplib::Server HTTPProxy;
if (Ret.empty()) httplib::Headers headers = {
return false; { "User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch() },
{ "Accept", "*/*" }
std::ofstream File(Path, std::ios::binary); };
if (File.is_open()) { httplib::Client backend("https://backend.beammp.com");
File << Ret; httplib::Client forum("https://forum.beammp.com");
File.close();
std::cout << "\n"; const std::string pattern = ".*";
info("Download Complete!");
} else { auto handle_request = [&](const httplib::Request& req, httplib::Response& res) {
error("Failed to open file directory: " + Path); set_headers(res);
return false; if (req.has_header("X-BMP-Authentication")) {
} headers.emplace("X-BMP-Authentication", PrivateKey);
}
return true; if (req.has_header("X-API-Version")) {
} headers.emplace("X-API-Version", req.get_header_value("X-API-Version"));
}
void set_headers(httplib::Response& res) {
res.set_header("Access-Control-Allow-Origin", "*"); const std::vector<std::string> path = Utils::Split(req.path, "/");
res.set_header("Access-Control-Request-Method", "POST, OPTIONS, GET");
res.set_header("Access-Control-Request-Headers", "X-API-Version"); httplib::Result cli_res;
} const std::string method = req.method;
std::string host = "";
void HTTP::StartProxy() { if (!path.empty())
std::thread proxy([&]() { host = path[0];
httplib::Server HTTPProxy;
httplib::Headers headers = { if (host == "backend") {
{ "User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch() }, std::string remaining_path = req.path.substr(std::strlen("/backend"));
{ "Accept", "*/*" }
}; if (method == "GET")
httplib::Client backend("https://backend.beammp.com"); cli_res = backend.Get(remaining_path, headers);
httplib::Client forum("https://forum.beammp.com"); else if (method == "POST")
cli_res = backend.Post(remaining_path, headers);
const std::string pattern = ".*";
} else if (host == "avatar") {
auto handle_request = [&](const httplib::Request& req, httplib::Response& res) { bool error = false;
set_headers(res); std::string username;
if (req.has_header("X-BMP-Authentication")) { std::string avatar_size = "100";
headers.emplace("X-BMP-Authentication", PrivateKey);
} if (path.size() > 1) {
if (req.has_header("X-API-Version")) { username = path[1];
headers.emplace("X-API-Version", req.get_header_value("X-API-Version")); } else {
} error = true;
}
const std::vector<std::string> path = Utils::Split(req.path, "/");
if (path.size() > 2) {
httplib::Result cli_res; try {
const std::string method = req.method; if (std::stoi(path[2]) > 0)
std::string host = ""; avatar_size = path[2];
if (!path.empty()) } catch (std::exception&) { }
host = path[0]; }
if (host == "backend") { httplib::Result summary_res;
std::string remaining_path = req.path.substr(std::strlen("/backend"));
if (!error) {
if (method == "GET") summary_res = forum.Get("/u/" + username + ".json", headers);
cli_res = backend.Get(remaining_path, headers);
else if (method == "POST") if (!summary_res || summary_res->status != 200) {
cli_res = backend.Post(remaining_path, headers); error = true;
}
} else if (host == "avatar") { }
bool error = false;
std::string username; if (!error) {
std::string avatar_size = "100"; try {
nlohmann::json d = nlohmann::json::parse(summary_res->body, nullptr, false); // can fail with parse_error
if (path.size() > 1) {
username = path[1]; auto user = d.at("user"); // can fail with out_of_range
} else { auto avatar_link_json = user.at("avatar_template"); // can fail with out_of_range
error = true;
} auto avatar_link = avatar_link_json.get<std::string>();
size_t start_pos = avatar_link.find("{size}");
if (path.size() > 2) { if (start_pos != std::string::npos)
try { avatar_link.replace(start_pos, std::strlen("{size}"), avatar_size);
if (std::stoi(path[2]) > 0)
avatar_size = path[2]; cli_res = forum.Get(avatar_link, headers);
} catch (std::exception&) {} } catch (std::exception&) {
} error = true;
}
httplib::Result summary_res; }
if (!error) { if (error) {
summary_res = forum.Get("/u/" + username + ".json", headers); cli_res = forum.Get("/user_avatar/forum.beammp.com/user/0/0.png", headers);
}
if (!summary_res || summary_res->status != 200) {
error = true; } else {
} res.set_content("Host not found", "text/plain");
} return;
}
if (!error) {
try { if (cli_res) {
nlohmann::json d = nlohmann::json::parse(summary_res->body, nullptr, false); // can fail with parse_error res.set_content(cli_res->body, cli_res->get_header_value("Content-Type"));
} else {
auto user = d.at("user"); // can fail with out_of_range res.set_content(to_string(cli_res.error()), "text/plain");
auto avatar_link_json = user.at("avatar_template"); // can fail with out_of_range }
};
auto avatar_link = avatar_link_json.get<std::string>();
size_t start_pos = avatar_link.find("{size}"); HTTPProxy.Get(pattern, [&](const httplib::Request& req, httplib::Response& res) {
if (start_pos != std::string::npos) handle_request(req, res);
avatar_link.replace(start_pos, std::strlen("{size}"), avatar_size); });
cli_res = forum.Get(avatar_link, headers); HTTPProxy.Post(pattern, [&](const httplib::Request& req, httplib::Response& res) {
handle_request(req, res);
} catch (std::exception&) { });
error = true;
} ProxyPort = HTTPProxy.bind_to_any_port("0.0.0.0");
} debug("HTTP Proxy listening on port " + std::to_string(ProxyPort));
HTTPProxy.listen_after_bind();
if (error) { });
cli_res = forum.Get("/user_avatar/forum.beammp.com/user/0/0.png", headers); proxy.detach();
} }
} else {
res.set_content("Host not found", "text/plain");
return;
}
if (cli_res) {
res.set_content(cli_res->body, cli_res->get_header_value("Content-Type"));
} else {
res.set_content(to_string(cli_res.error()), "text/plain");
}
};
HTTPProxy.Get(pattern, [&](const httplib::Request& req, httplib::Response& res) {
handle_request(req, res);
});
HTTPProxy.Post(pattern, [&](const httplib::Request& req, httplib::Response& res) {
handle_request(req, res);
});
ProxyPort = HTTPProxy.bind_to_any_port("0.0.0.0");
debug("HTTP Proxy listening on port " + std::to_string(ProxyPort));
HTTPProxy.listen_after_bind();
});
proxy.detach();
}

View File

@ -10,6 +10,7 @@
#include "Network/network.hpp" #include "Network/network.hpp"
#include "Security/Init.h" #include "Security/Init.h"
#include "Startup.h" #include "Startup.h"
#include <curl/curl.h>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
@ -26,6 +27,7 @@ int main(int argc, char** argv) try {
th.detach(); th.detach();
#endif #endif
curl_global_init(CURL_GLOBAL_ALL);
#if defined(_WIN32) #if defined(_WIN32)
system("cls"); system("cls");

View File

@ -3,6 +3,7 @@
"cpp-httplib", "cpp-httplib",
"nlohmann-json", "nlohmann-json",
"zlib", "zlib",
"openssl" "openssl",
"curl"
] ]
} }