17 Commits

Author SHA1 Message Date
Lion Kortlepel
dd3622170e add debug print and dont try new format if the packet looks invalid 2024-10-06 15:26:02 +02:00
Lion Kortlepel
885061f73d add more info to new mod list receive error 2024-10-06 15:22:45 +02:00
Lion Kortlepel
fcb51adcb8 bump version 2024-10-05 21:05:02 +02:00
Lion
768f11f6ec Add mod hashing, improve download protocol (#129) 2024-10-04 23:36:57 +02:00
Lion
7944e9dbe8 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.
2024-10-04 23:23:49 +02:00
Lion Kortlepel
0c68f91fb2 remove debug print 2024-10-04 23:22:50 +02:00
Lion
b8fdbc4ed9 Fix GetGamePath (#130)
Previously, the registry was used to get the local appdata folder for
the user folder. I've switched this over to a windows api function which
fixes some cases where the launcher wouldn't be able to find the appdata
folder in the registry.
2024-10-04 23:12:56 +02:00
Lion Kortlepel
85908e42d5 fix download code, error checking 2024-10-04 23:12:23 +02:00
Lion Kortlepel
5c77e60f29 remove mis-merged code 2024-10-04 23:04:30 +02:00
Lion Kortlepel
c74455e0fe switch to curl for Get and Post 2024-10-04 22:59:29 +02:00
Lion
dc13e4a03c remove extra return 2024-10-04 14:13:04 +02:00
Tixx
1d7eb64fe0 Get localappdata via winapi instead of registry 2024-10-03 22:42:49 +02:00
Lion Kortlepel
1676d4174e make mods not keep the hash when copying them 2024-09-29 02:36:41 +02:00
Lion Kortlepel
ad468a8971 remove debug prints 2024-09-29 02:04:39 +02:00
Lion Kortlepel
d3805f2cfd fix mod deleting misnamed mods 2024-09-29 01:57:15 +02:00
Lion Kortlepel
9f1cc15b15 fix bugs with new download 2024-09-29 01:15:57 +02:00
Lion Kortlepel
c0fb4e4ad6 implement support for new mod hashing and download 2024-09-29 00:33:15 +02:00
8 changed files with 548 additions and 339 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

@@ -8,6 +8,7 @@
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>
#include <shlobj.h>
#elif defined(__linux__) #elif defined(__linux__)
#include "vdf_parser.hpp" #include "vdf_parser.hpp"
#include <pwd.h> #include <pwd.h>
@@ -40,17 +41,17 @@ std::string GetGamePath() {
Path = QueryKey(hKey, 4); Path = QueryKey(hKey, 4);
if (Path.empty()) { if (Path.empty()) {
sk = R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders)"; Path = "";
openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey); char appDataPath[MAX_PATH];
if (openRes != ERROR_SUCCESS) { HRESULT result = SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, appDataPath);
sk = R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders)"; if (SUCCEEDED(result)) {
openRes = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS, &hKey); Path = appDataPath;
} }
if (openRes != ERROR_SUCCESS) {
if (Path.empty()) {
fatal("Cannot get Local Appdata directory"); fatal("Cannot get Local Appdata directory");
} }
Path = QueryKey(hKey, 5);
Path += "\\BeamNG.drive\\"; Path += "\\BeamNG.drive\\";
} }

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

@@ -11,6 +11,9 @@
#include <iomanip> #include <iomanip>
#include <ios> #include <ios>
#include <mutex> #include <mutex>
#include <nlohmann/json.hpp>
#include <openssl/err.h>
#include <openssl/evp.h>
#if defined(_WIN32) #if defined(_WIN32)
#include <ws2tcpip.h> #include <ws2tcpip.h>
@@ -25,6 +28,7 @@
#include "Logger.h" #include "Logger.h"
#include "Startup.h" #include "Startup.h"
#include <Utils.h>
#include <atomic> #include <atomic>
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
@@ -33,7 +37,8 @@
#include <future> #include <future>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <Utils.h>
#include "hashpp.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
@@ -214,6 +219,35 @@ SOCKET InitDSock() {
return DSock; return DSock;
} }
std::vector<char> SingleNormalDownload(SOCKET MSock, uint64_t Size, const std::string& Name) {
DownloadSpeed = 0;
uint64_t GRcv = 0;
std::thread Au([&] { AsyncUpdate(GRcv, Size, Name); });
const std::vector<char> MData = TCPRcvRaw(MSock, GRcv, Size);
if (MData.empty()) {
KillSocket(MSock);
Terminate = true;
Au.join();
return {};
}
// ensure that GRcv is good before joining the async update thread
GRcv = MData.size();
if (GRcv != Size) {
error("Something went wrong during download; didn't get enough data. Expected " + std::to_string(Size) + " bytes, got " + std::to_string(GRcv) + " bytes instead");
Terminate = true;
Au.join();
return {};
}
Au.join();
return MData;
}
std::vector<char> MultiDownload(SOCKET MSock, SOCKET DSock, uint64_t Size, const std::string& Name) { std::vector<char> MultiDownload(SOCKET MSock, SOCKET DSock, uint64_t Size, const std::string& Name) {
DownloadSpeed = 0; DownloadSpeed = 0;
@@ -253,7 +287,7 @@ std::vector<char> MultiDownload(SOCKET MSock, SOCKET DSock, uint64_t Size, const
Au.join(); Au.join();
std::vector<char> Result{}; std::vector<char> Result {};
Result.insert(Result.begin(), MData.begin(), MData.end()); Result.insert(Result.begin(), MData.begin(), MData.end());
Result.insert(Result.end(), DData.begin(), DData.end()); Result.insert(Result.end(), DData.begin(), DData.end());
return Result; return Result;
@@ -265,13 +299,234 @@ void InvalidResource(const std::string& File) {
Terminate = true; Terminate = true;
} }
std::string GetSha256HashReallyFast(const std::string& filename) {
try {
EVP_MD_CTX* mdctx;
const EVP_MD* md;
uint8_t sha256_value[EVP_MAX_MD_SIZE];
md = EVP_sha256();
if (md == nullptr) {
throw std::runtime_error("EVP_sha256() failed");
}
mdctx = EVP_MD_CTX_new();
if (mdctx == nullptr) {
throw std::runtime_error("EVP_MD_CTX_new() failed");
}
if (!EVP_DigestInit_ex2(mdctx, md, NULL)) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestInit_ex2() failed");
}
std::ifstream stream(filename, std::ios::binary);
const size_t FileSize = std::filesystem::file_size(filename);
size_t Read = 0;
std::vector<char> Data;
while (Read < FileSize) {
Data.resize(size_t(std::min<size_t>(FileSize - Read, 4096)));
size_t RealDataSize = Data.size();
stream.read(Data.data(), std::streamsize(Data.size()));
if (stream.eof() || stream.fail()) {
RealDataSize = size_t(stream.gcount());
}
Data.resize(RealDataSize);
if (RealDataSize == 0) {
break;
}
if (RealDataSize > 0 && !EVP_DigestUpdate(mdctx, Data.data(), Data.size())) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestUpdate() failed");
}
Read += RealDataSize;
}
unsigned int sha256_len = 0;
if (!EVP_DigestFinal_ex(mdctx, sha256_value, &sha256_len)) {
EVP_MD_CTX_free(mdctx);
throw std::runtime_error("EVP_DigestFinal_ex() failed");
}
EVP_MD_CTX_free(mdctx);
std::string result;
for (size_t i = 0; i < sha256_len; i++) {
char buf[3];
sprintf(buf, "%02x", sha256_value[i]);
buf[2] = 0;
result += buf;
}
return result;
} catch (const std::exception& e) {
error("Sha256 hashing of '" + filename + "' failed: " + e.what());
return "";
}
}
struct ModInfo {
static std::vector<ModInfo> ParseModInfosFromPacket(const std::string& packet) {
std::vector<ModInfo> modInfos;
try {
auto json = nlohmann::json::parse(packet);
for (const auto& entry : json) {
ModInfo modInfo {
.FileName = entry["file_name"],
.FileSize = entry["file_size"],
.Hash = entry["hash"],
.HashAlgorithm = entry["hash_algorithm"],
};
modInfos.push_back(modInfo);
}
} catch (const std::exception& e) {
debug(std::string("Failed to receive mod list: ") + e.what());
warn("Failed to receive new mod list format! This server may be outdated, but everything should still work as expected.");
return {};
}
return modInfos;
}
std::string FileName;
size_t FileSize;
std::string Hash;
std::string HashAlgorithm;
};
void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector<ModInfo> ModInfos) {
if (!SecurityWarning())
return;
info("Checking Resources...");
CheckForDir();
std::string t;
for (const auto& mod : ModInfos) {
t += mod.FileName + ";";
}
if (t.empty())
CoreSend("L");
else
CoreSend("L" + t);
t.clear();
info("Syncing...");
int ModNo = 1;
int TotalMods = ModInfos.size();
for (auto ModInfoIter = ModInfos.begin(), AlsoModInfoIter = ModInfos.begin(); ModInfoIter != ModInfos.end() && !Terminate; ++ModInfoIter, ++AlsoModInfoIter) {
if (ModInfoIter->Hash.length() < 8 || ModInfoIter->HashAlgorithm != "sha256") {
error("Unsupported hash algorithm or invalid hash for '" + ModInfoIter->FileName + "'");
Terminate = true;
return;
}
auto FileName = std::filesystem::path(ModInfoIter->FileName).stem().string() + "-" + ModInfoIter->Hash.substr(0, 8) + std::filesystem::path(ModInfoIter->FileName).extension().string();
auto PathToSaveTo = (fs::path(CachingDirectory) / FileName).string();
if (fs::exists(PathToSaveTo) && GetSha256HashReallyFast(PathToSaveTo) == ModInfoIter->Hash) {
debug("Mod '" + FileName + "' found in cache");
std::this_thread::sleep_for(std::chrono::milliseconds(50));
try {
if (!fs::exists(GetGamePath() + "mods/multiplayer")) {
fs::create_directories(GetGamePath() + "mods/multiplayer");
}
auto modname = ModInfoIter->FileName;
#if defined(__linux__)
// Linux version of the game doesnt support uppercase letters in mod names
for (char& c : modname) {
c = ::tolower(c);
}
#endif
debug("Mod name: " + modname);
auto name = std::filesystem::path(GetGamePath()) / "mods/multiplayer" / modname;
std::string tmp_name = name.string();
tmp_name += ".tmp";
fs::copy_file(PathToSaveTo, tmp_name, fs::copy_options::overwrite_existing);
fs::rename(tmp_name, name);
} catch (std::exception& e) {
error("Failed copy to the mods folder! " + std::string(e.what()));
Terminate = true;
continue;
}
WaitForConfirm();
continue;
}
CheckForDir();
std::string FName = ModInfoIter->FileName;
do {
debug("Loading file '" + FName + "' to '" + PathToSaveTo + "'");
TCPSend("f" + ModInfoIter->FileName, Sock);
std::string Data = TCPRcv(Sock);
if (Data == "CO" || Terminate) {
Terminate = true;
UUl("Server cannot find " + FName);
break;
}
std::string Name = std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + FName;
std::vector<char> DownloadedFile = SingleNormalDownload(Sock, ModInfoIter->FileSize, Name);
if (Terminate)
break;
UpdateUl(false, std::to_string(ModNo) + "/" + std::to_string(TotalMods) + ": " + FName);
// 1. write downloaded file to disk
{
std::ofstream OutFile(PathToSaveTo, std::ios::binary | std::ios::trunc);
OutFile.write(DownloadedFile.data(), DownloadedFile.size());
OutFile.flush();
}
// 2. verify size
if (std::filesystem::file_size(PathToSaveTo) != DownloadedFile.size()) {
error("Failed to write the entire file '" + PathToSaveTo + "' correctly (file size mismatch)");
Terminate = true;
}
} while (fs::file_size(PathToSaveTo) != ModInfoIter->FileSize && !Terminate);
if (!Terminate) {
if (!fs::exists(GetGamePath() + "mods/multiplayer")) {
fs::create_directories(GetGamePath() + "mods/multiplayer");
}
// Linux version of the game doesnt support uppercase letters in mod names
#if defined(__linux__)
for (char& c : FName) {
c = ::tolower(c);
}
#endif
fs::copy_file(PathToSaveTo, std::filesystem::path(GetGamePath()) / "mods/multiplayer" / FName, fs::copy_options::overwrite_existing);
}
WaitForConfirm();
++ModNo;
}
if (!Terminate) {
TCPSend("Done", Sock);
info("Done!");
} else {
UlStatus = "Ulstart";
info("Connection Terminated!");
}
}
void SyncResources(SOCKET Sock) { void SyncResources(SOCKET Sock) {
std::string Ret = Auth(Sock); std::string Ret = Auth(Sock);
if (Ret.starts_with("R")) {
debug("This server is likely outdated, not trying to parse new mod info format");
} else {
auto ModInfos = ModInfo::ParseModInfosFromPacket(Ret);
if (!ModInfos.empty()) {
NewSyncResources(Sock, Ret, ModInfos);
return;
}
}
if (Ret.empty()) if (Ret.empty())
return; return;
if (!SecurityWarning()) if (!SecurityWarning())
return; return;
info("Checking Resources..."); info("Checking Resources...");
CheckForDir(); CheckForDir();
@@ -396,6 +651,7 @@ void SyncResources(SOCKET Sock) {
} }
WaitForConfirm(); WaitForConfirm();
} }
KillSocket(DSock); KillSocket(DSock);
if (!Terminate) { if (!Terminate) {
TCPSend("Done", Sock); TCPSend("Done", Sock);

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

@@ -81,10 +81,10 @@ std::string GetEN() {
} }
std::string GetVer() { std::string GetVer() {
return "2.1"; return "2.2";
} }
std::string GetPatch() { std::string GetPatch() {
return ".4"; return ".0";
} }
std::string GetEP(char* P) { std::string GetEP(char* P) {

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