Switch to curl for Get and Post (#132)

Because we can. I got a segfault while testing but then it didn't happen
again, so I'm tempted to call it done.
This commit is contained in:
Lion 2024-10-04 23:23:49 +02:00 committed by GitHub
commit 7944e9dbe8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 279 additions and 327 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,268 @@
// 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(); return Ret;
std::string verify_error; }
if (result) {
verify_error = X509_verify_cert_error_string(result); std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
} std::string Ret;
static thread_local CURL* curl = curl_easy_init();
WriteHttpDebug(cli, "GET", IP, res); if (curl) {
error("HTTP Get failed on " + to_string(res.error()) + ", ssl verify = " + verify_error); CURLcode res;
} curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteCallback);
return Ret; curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&Ret);
} curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str());
std::string HTTP::Post(const std::string& IP, const std::string& Fields) { curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size());
static std::mutex Lock; struct curl_slist* list = nullptr;
std::scoped_lock Guard(Lock); list = curl_slist_append(list, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
auto pos = IP.find('/', 10); curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // seconds
res = curl_easy_perform(curl);
httplib::Client cli(IP.substr(0, pos).c_str()); curl_slist_free_all(list);
cli.set_connection_timeout(std::chrono::seconds(10)); if (res != CURLE_OK) {
if (SkipSslVerify) { error("POST to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
debug("Skipping SSL server validation via --skip-ssl-verify"); return "";
cli.enable_server_certificate_verification(false); }
} } else {
std::string Ret; error("Curl easy init failed");
return "";
if (!Fields.empty()) { }
httplib::Result res = cli.Post(IP.substr(pos).c_str(), Fields, "application/json"); return Ret;
}
if (res) {
if (res->status != 200) { bool HTTP::Download(const std::string& IP, const std::string& Path) {
error(res->reason); static std::mutex Lock;
} std::scoped_lock Guard(Lock);
Ret = res->body;
} else { info("Downloading an update (this may take a while)");
WriteHttpDebug(cli, "POST", IP, res); std::string Ret = Get(IP);
error("HTTP Post failed on " + to_string(res.error()) + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result()));
} if (Ret.empty()) {
} else { error("Download failed");
httplib::Result res = cli.Post(IP.substr(pos).c_str()); return false;
if (res) { }
if (res->status != 200) {
error(res->reason); std::ofstream File(Path, std::ios::binary);
} if (File.is_open()) {
Ret = res->body; File << Ret;
} else { File.close();
auto result = cli.get_openssl_verify_result(); std::cout << "\n";
std::string verify_error; info("Download Complete!");
if (result) { } else {
verify_error = X509_verify_cert_error_string(result); error("Failed to open file directory: " + Path);
} return false;
WriteHttpDebug(cli, "POST", IP, res); }
error("HTTP Post failed on " + to_string(res.error()) + ", ssl verify = " + verify_error);
} return true;
} }
if (Ret.empty()) void set_headers(httplib::Response& res) {
return "-1"; res.set_header("Access-Control-Allow-Origin", "*");
else res.set_header("Access-Control-Request-Method", "POST, OPTIONS, GET");
return Ret; res.set_header("Access-Control-Request-Headers", "X-API-Version");
} }
bool HTTP::ProgressBar(size_t c, size_t t) { void HTTP::StartProxy() {
if (isDownload) { std::thread proxy([&]() {
static double last_progress, progress_bar_adv; httplib::Server HTTPProxy;
progress_bar_adv = round(c / double(t) * 25); httplib::Headers headers = {
std::cout << "\r"; { "User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch() },
std::cout << "Progress : [ "; { "Accept", "*/*" }
std::cout << round(c / double(t) * 100); };
std::cout << "% ] ["; httplib::Client backend("https://backend.beammp.com");
int i; httplib::Client forum("https://forum.beammp.com");
for (i = 0; i <= progress_bar_adv; i++)
std::cout << "#"; const std::string pattern = ".*";
for (i = 0; i < 25 - progress_bar_adv; i++)
std::cout << "."; auto handle_request = [&](const httplib::Request& req, httplib::Response& res) {
std::cout << "]"; set_headers(res);
last_progress = round(c / double(t) * 100); 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"));
bool HTTP::Download(const std::string& IP, const std::string& Path) { }
static std::mutex Lock;
std::scoped_lock Guard(Lock); const std::vector<std::string> path = Utils::Split(req.path, "/");
isDownload = true; httplib::Result cli_res;
std::string Ret = Get(IP); const std::string method = req.method;
isDownload = false; std::string host = "";
if (Ret.empty()) if (!path.empty())
return false; host = path[0];
std::ofstream File(Path, std::ios::binary); if (host == "backend") {
if (File.is_open()) { std::string remaining_path = req.path.substr(std::strlen("/backend"));
File << Ret;
File.close(); if (method == "GET")
std::cout << "\n"; cli_res = backend.Get(remaining_path, headers);
info("Download Complete!"); else if (method == "POST")
} else { cli_res = backend.Post(remaining_path, headers);
error("Failed to open file directory: " + Path);
return false; } else if (host == "avatar") {
} bool error = false;
std::string username;
return true; std::string avatar_size = "100";
}
if (path.size() > 1) {
void set_headers(httplib::Response& res) { username = path[1];
res.set_header("Access-Control-Allow-Origin", "*"); } else {
res.set_header("Access-Control-Request-Method", "POST, OPTIONS, GET"); error = true;
res.set_header("Access-Control-Request-Headers", "X-API-Version"); }
}
if (path.size() > 2) {
try {
void HTTP::StartProxy() { if (std::stoi(path[2]) > 0)
std::thread proxy([&]() { avatar_size = path[2];
httplib::Server HTTPProxy;
httplib::Headers headers = { } catch (std::exception&) { }
{ "User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch() }, }
{ "Accept", "*/*" }
}; httplib::Result summary_res;
httplib::Client backend("https://backend.beammp.com");
httplib::Client forum("https://forum.beammp.com"); if (!error) {
summary_res = forum.Get("/u/" + username + ".json", headers);
const std::string pattern = ".*";
if (!summary_res || summary_res->status != 200) {
auto handle_request = [&](const httplib::Request& req, httplib::Response& res) { error = true;
set_headers(res); }
if (req.has_header("X-BMP-Authentication")) { }
headers.emplace("X-BMP-Authentication", PrivateKey);
} if (!error) {
if (req.has_header("X-API-Version")) { try {
headers.emplace("X-API-Version", req.get_header_value("X-API-Version")); nlohmann::json d = nlohmann::json::parse(summary_res->body, nullptr, false); // can fail with parse_error
}
auto user = d.at("user"); // can fail with out_of_range
const std::vector<std::string> path = Utils::Split(req.path, "/"); auto avatar_link_json = user.at("avatar_template"); // can fail with out_of_range
httplib::Result cli_res; auto avatar_link = avatar_link_json.get<std::string>();
const std::string method = req.method; size_t start_pos = avatar_link.find("{size}");
std::string host = ""; if (start_pos != std::string::npos)
avatar_link.replace(start_pos, std::strlen("{size}"), avatar_size);
if (!path.empty())
host = path[0]; cli_res = forum.Get(avatar_link, headers);
if (host == "backend") { } catch (std::exception&) {
std::string remaining_path = req.path.substr(std::strlen("/backend")); error = true;
}
if (method == "GET") }
cli_res = backend.Get(remaining_path, headers);
else if (method == "POST") if (error) {
cli_res = backend.Post(remaining_path, headers); cli_res = forum.Get("/user_avatar/forum.beammp.com/user/0/0.png", headers);
}
} else if (host == "avatar") {
bool error = false; } else {
std::string username; res.set_content("Host not found", "text/plain");
std::string avatar_size = "100"; return;
}
if (path.size() > 1) {
username = path[1]; if (cli_res) {
} else { res.set_content(cli_res->body, cli_res->get_header_value("Content-Type"));
error = true; } else {
} res.set_content(to_string(cli_res.error()), "text/plain");
}
if (path.size() > 2) { };
try {
if (std::stoi(path[2]) > 0) HTTPProxy.Get(pattern, [&](const httplib::Request& req, httplib::Response& res) {
avatar_size = path[2]; handle_request(req, res);
});
} catch (std::exception&) {}
} HTTPProxy.Post(pattern, [&](const httplib::Request& req, httplib::Response& res) {
handle_request(req, res);
httplib::Result summary_res; });
if (!error) { ProxyPort = HTTPProxy.bind_to_any_port("0.0.0.0");
summary_res = forum.Get("/u/" + username + ".json", headers); debug("HTTP Proxy listening on port " + std::to_string(ProxyPort));
HTTPProxy.listen_after_bind();
if (!summary_res || summary_res->status != 200) { });
error = true; proxy.detach();
} }
}
if (!error) {
try {
nlohmann::json d = nlohmann::json::parse(summary_res->body, nullptr, false); // can fail with parse_error
auto user = d.at("user"); // can fail with out_of_range
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}");
if (start_pos != std::string::npos)
avatar_link.replace(start_pos, std::strlen("{size}"), avatar_size);
cli_res = forum.Get(avatar_link, headers);
} catch (std::exception&) {
error = true;
}
}
if (error) {
cli_res = forum.Get("/user_avatar/forum.beammp.com/user/0/0.png", headers);
}
} 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

@ -58,7 +58,7 @@ std::string Login(const std::string& fields) {
try { try {
std::string Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields); std::string Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields);
if (Buffer == "-1") { if (Buffer.empty()) {
return GetFail("Failed to communicate with the auth system!"); return GetFail("Failed to communicate with the auth system!");
} }
@ -119,7 +119,7 @@ void CheckLocalKey() {
nlohmann::json d = nlohmann::json::parse(Buffer, nullptr, false); nlohmann::json d = nlohmann::json::parse(Buffer, nullptr, false);
if (Buffer == "-1" || Buffer.at(0) != '{' || d.is_discarded()) { if (Buffer.empty() || Buffer.at(0) != '{' || d.is_discarded()) {
error(Buffer); error(Buffer);
info("Invalid answer from authentication servers."); info("Invalid answer from authentication servers.");
UpdateKey(nullptr); UpdateKey(nullptr);
@ -137,7 +137,6 @@ void CheckLocalKey() {
if (d.contains("id")) { if (d.contains("id")) {
UserID = d["id"].get<int>(); UserID = d["id"].get<int>();
} }
// info(Role);
} else { } else {
info("Auto-Authentication unsuccessful please re-login!"); info("Auto-Authentication unsuccessful please re-login!");
UpdateKey(nullptr); UpdateKey(nullptr);

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"
] ]
} }