mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-03 22:36:14 +00:00
Compare commits
32 Commits
v2.1.2
...
fix-resour
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd3622170e | ||
|
|
885061f73d | ||
|
|
fcb51adcb8 | ||
|
|
768f11f6ec | ||
|
|
7944e9dbe8 | ||
|
|
0c68f91fb2 | ||
|
|
b8fdbc4ed9 | ||
|
|
85908e42d5 | ||
|
|
5c77e60f29 | ||
|
|
c74455e0fe | ||
|
|
dc13e4a03c | ||
|
|
1d7eb64fe0 | ||
|
|
1676d4174e | ||
|
|
ad468a8971 | ||
|
|
d3805f2cfd | ||
|
|
9f1cc15b15 | ||
|
|
c0fb4e4ad6 | ||
|
|
7600372ca1 | ||
|
|
54cd5b5e0e | ||
|
|
ede6fcd7dd | ||
|
|
eaeacbd8de | ||
|
|
0ffed00bcb | ||
|
|
c0c3d6b30e | ||
|
|
9c59a83f04 | ||
|
|
95436cb073 | ||
|
|
cbb5502a40 | ||
|
|
d6dfe85f69 | ||
|
|
ae9af1470c | ||
|
|
9255c70b0b | ||
|
|
53c514ecc6 | ||
|
|
e348d59a7e | ||
|
|
244d27341f |
@@ -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")
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ void ConfigInit() {
|
|||||||
R"({
|
R"({
|
||||||
"Port": 4444,
|
"Port": 4444,
|
||||||
"Build": "Default",
|
"Build": "Default",
|
||||||
"CachingDirectory": "./Resources",
|
"CachingDirectory": "./Resources"
|
||||||
})";
|
})";
|
||||||
cfg.close();
|
cfg.close();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -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\\";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +101,11 @@ void StartGame(std::string Dir) {
|
|||||||
std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64");
|
std::string filename = (Dir + "/BinLinux/BeamNG.drive.x64");
|
||||||
char* argv[] = { filename.data(), NULL };
|
char* argv[] = { filename.data(), NULL };
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int result = posix_spawn(&pid, filename.c_str(), NULL, NULL, argv, environ);
|
posix_spawn_file_actions_t spawn_actions;
|
||||||
|
posix_spawn_file_actions_init(&spawn_actions);
|
||||||
|
posix_spawn_file_actions_addclose(&spawn_actions, STDOUT_FILENO);
|
||||||
|
posix_spawn_file_actions_addclose(&spawn_actions, STDERR_FILENO);
|
||||||
|
int result = posix_spawn(&pid, filename.c_str(), &spawn_actions, nullptr, argv, environ);
|
||||||
|
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
error("Failed to Launch the game! launcher closing soon");
|
error("Failed to Launch the game! launcher closing soon");
|
||||||
|
|||||||
@@ -8,16 +8,18 @@
|
|||||||
|
|
||||||
#include "Http.h"
|
#include "Http.h"
|
||||||
#include <Logger.h>
|
#include <Logger.h>
|
||||||
|
#include <Network/network.hpp>
|
||||||
|
#include <Startup.h>
|
||||||
|
#include <Utils.h>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <curl/curl.h>
|
||||||
|
#include <curl/easy.h>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <Startup.h>
|
|
||||||
#include <Network/network.hpp>
|
|
||||||
#include <Utils.h>
|
|
||||||
|
|
||||||
void WriteHttpDebug(const httplib::Client& client, const std::string& method, const std::string& target, const httplib::Result& result) try {
|
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";
|
const std::filesystem::path folder = ".https_debug";
|
||||||
@@ -59,115 +61,74 @@ void WriteHttpDebug(const httplib::Client& client, const std::string& method, co
|
|||||||
error(e.what());
|
error(e.what());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
|
||||||
|
std::string* Result = reinterpret_cast<std::string*>(userp);
|
||||||
|
std::string NewContents(reinterpret_cast<char*>(contents), size * nmemb);
|
||||||
|
*Result += NewContents;
|
||||||
|
return size * nmemb;
|
||||||
|
}
|
||||||
|
|
||||||
bool HTTP::isDownload = false;
|
bool HTTP::isDownload = false;
|
||||||
std::string HTTP::Get(const std::string& IP) {
|
std::string HTTP::Get(const std::string& IP) {
|
||||||
static std::mutex Lock;
|
|
||||||
std::scoped_lock Guard(Lock);
|
|
||||||
|
|
||||||
auto pos = IP.find('/', 10);
|
|
||||||
|
|
||||||
httplib::Client cli(IP.substr(0, pos).c_str());
|
|
||||||
cli.set_connection_timeout(std::chrono::seconds(10));
|
|
||||||
cli.set_follow_location(true);
|
|
||||||
if (SkipSslVerify) {
|
|
||||||
debug("Skipping SSL server validation via --skip-ssl-verify");
|
|
||||||
cli.enable_server_certificate_verification(false);
|
|
||||||
}
|
|
||||||
auto res = cli.Get(IP.substr(pos).c_str(), ProgressBar);
|
|
||||||
std::string Ret;
|
std::string Ret;
|
||||||
|
static thread_local CURL* curl = curl_easy_init();
|
||||||
if (res) {
|
if (curl) {
|
||||||
if (res->status == 200) {
|
CURLcode res;
|
||||||
Ret = res->body;
|
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
|
||||||
} else {
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteCallback);
|
||||||
WriteHttpDebug(cli, "GET", IP, res);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&Ret);
|
||||||
error("Failed to GET '" + IP + "': " + res->reason + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result()));
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // seconds
|
||||||
|
res = curl_easy_perform(curl);
|
||||||
|
if (res != CURLE_OK) {
|
||||||
|
error("GET to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (isDownload) {
|
error("Curl easy init failed");
|
||||||
std::cout << "\n";
|
return "";
|
||||||
}
|
|
||||||
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;
|
return Ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
||||||
static std::mutex Lock;
|
|
||||||
std::scoped_lock Guard(Lock);
|
|
||||||
|
|
||||||
auto pos = IP.find('/', 10);
|
|
||||||
|
|
||||||
httplib::Client cli(IP.substr(0, pos).c_str());
|
|
||||||
cli.set_connection_timeout(std::chrono::seconds(10));
|
|
||||||
if (SkipSslVerify) {
|
|
||||||
debug("Skipping SSL server validation via --skip-ssl-verify");
|
|
||||||
cli.enable_server_certificate_verification(false);
|
|
||||||
}
|
|
||||||
std::string Ret;
|
std::string Ret;
|
||||||
|
static thread_local CURL* curl = curl_easy_init();
|
||||||
if (!Fields.empty()) {
|
if (curl) {
|
||||||
httplib::Result res = cli.Post(IP.substr(pos).c_str(), Fields, "application/json");
|
CURLcode res;
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
|
||||||
if (res) {
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteCallback);
|
||||||
if (res->status != 200) {
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&Ret);
|
||||||
error(res->reason);
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||||
}
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str());
|
||||||
Ret = res->body;
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size());
|
||||||
} else {
|
struct curl_slist* list = nullptr;
|
||||||
WriteHttpDebug(cli, "POST", IP, res);
|
list = curl_slist_append(list, "Content-Type: application/json");
|
||||||
error("HTTP Post failed on " + to_string(res.error()) + ", ssl verify = " + std::to_string(cli.get_openssl_verify_result()));
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // seconds
|
||||||
|
res = curl_easy_perform(curl);
|
||||||
|
curl_slist_free_all(list);
|
||||||
|
if (res != CURLE_OK) {
|
||||||
|
error("POST to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
httplib::Result res = cli.Post(IP.substr(pos).c_str());
|
error("Curl easy init failed");
|
||||||
if (res) {
|
return "";
|
||||||
if (res->status != 200) {
|
|
||||||
error(res->reason);
|
|
||||||
}
|
|
||||||
Ret = res->body;
|
|
||||||
} 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()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return Ret;
|
||||||
if (Ret.empty())
|
|
||||||
return "-1";
|
|
||||||
else
|
|
||||||
return Ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HTTP::ProgressBar(size_t c, size_t t) {
|
|
||||||
if (isDownload) {
|
|
||||||
static double last_progress, progress_bar_adv;
|
|
||||||
progress_bar_adv = round(c / double(t) * 25);
|
|
||||||
std::cout << "\r";
|
|
||||||
std::cout << "Progress : [ ";
|
|
||||||
std::cout << round(c / double(t) * 100);
|
|
||||||
std::cout << "% ] [";
|
|
||||||
int i;
|
|
||||||
for (i = 0; i <= progress_bar_adv; i++)
|
|
||||||
std::cout << "#";
|
|
||||||
for (i = 0; i < 25 - progress_bar_adv; i++)
|
|
||||||
std::cout << ".";
|
|
||||||
std::cout << "]";
|
|
||||||
last_progress = round(c / double(t) * 100);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HTTP::Download(const std::string& IP, const std::string& Path) {
|
bool HTTP::Download(const std::string& IP, const std::string& Path) {
|
||||||
static std::mutex Lock;
|
static std::mutex Lock;
|
||||||
std::scoped_lock Guard(Lock);
|
std::scoped_lock Guard(Lock);
|
||||||
|
|
||||||
isDownload = true;
|
info("Downloading an update (this may take a while)");
|
||||||
std::string Ret = Get(IP);
|
std::string Ret = Get(IP);
|
||||||
isDownload = false;
|
|
||||||
|
|
||||||
if (Ret.empty())
|
if (Ret.empty()) {
|
||||||
|
error("Download failed");
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
std::ofstream File(Path, std::ios::binary);
|
std::ofstream File(Path, std::ios::binary);
|
||||||
if (File.is_open()) {
|
if (File.is_open()) {
|
||||||
@@ -189,7 +150,6 @@ void set_headers(httplib::Response& res) {
|
|||||||
res.set_header("Access-Control-Request-Headers", "X-API-Version");
|
res.set_header("Access-Control-Request-Headers", "X-API-Version");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void HTTP::StartProxy() {
|
void HTTP::StartProxy() {
|
||||||
std::thread proxy([&]() {
|
std::thread proxy([&]() {
|
||||||
httplib::Server HTTPProxy;
|
httplib::Server HTTPProxy;
|
||||||
@@ -244,7 +204,7 @@ void HTTP::StartProxy() {
|
|||||||
if (std::stoi(path[2]) > 0)
|
if (std::stoi(path[2]) > 0)
|
||||||
avatar_size = path[2];
|
avatar_size = path[2];
|
||||||
|
|
||||||
} catch (std::exception&) {}
|
} catch (std::exception&) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
httplib::Result summary_res;
|
httplib::Result summary_res;
|
||||||
|
|||||||
@@ -8,7 +8,12 @@
|
|||||||
|
|
||||||
#include "Network/network.hpp"
|
#include "Network/network.hpp"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <iomanip>
|
||||||
|
#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>
|
||||||
@@ -23,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>
|
||||||
@@ -31,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;
|
||||||
|
|
||||||
@@ -123,11 +130,19 @@ void UpdateUl(bool D, const std::string& msg) {
|
|||||||
UlStatus = "UlLoading Resource " + msg;
|
UlStatus = "UlLoading Resource " + msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float DownloadSpeed = 0;
|
||||||
|
|
||||||
void AsyncUpdate(uint64_t& Rcv, uint64_t Size, const std::string& Name) {
|
void AsyncUpdate(uint64_t& Rcv, uint64_t Size, const std::string& Name) {
|
||||||
do {
|
do {
|
||||||
double pr = double(Rcv) / double(Size) * 100;
|
double pr = double(Rcv) / double(Size) * 100;
|
||||||
std::string Per = std::to_string(trunc(pr * 10) / 10);
|
std::string Per = std::to_string(trunc(pr * 10) / 10);
|
||||||
UpdateUl(true, Name + " (" + Per.substr(0, Per.find('.') + 2) + "%)");
|
std::string SpeedString = "";
|
||||||
|
if (DownloadSpeed > 0.01) {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << " at " << std::setprecision(1) << std::fixed << DownloadSpeed << " Mbit/s";
|
||||||
|
SpeedString = ss.str();
|
||||||
|
}
|
||||||
|
UpdateUl(true, Name + " (" + Per.substr(0, Per.find('.') + 2) + "%)" + SpeedString);
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
} while (!Terminate && Rcv < Size);
|
} while (!Terminate && Rcv < Size);
|
||||||
}
|
}
|
||||||
@@ -144,12 +159,12 @@ std::vector<char> TCPRcvRaw(SOCKET Sock, uint64_t& GRcv, uint64_t Size) {
|
|||||||
std::vector<char> File(Size);
|
std::vector<char> File(Size);
|
||||||
uint64_t Rcv = 0;
|
uint64_t Rcv = 0;
|
||||||
|
|
||||||
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
do {
|
do {
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
|
||||||
|
|
||||||
// receive at most some MB at a time
|
// receive at most some MB at a time
|
||||||
int Len = std::min(int(Size - Rcv), 2 * 1024 * 1024);
|
int Len = std::min(int(Size - Rcv), 1 * 1024 * 1024);
|
||||||
int32_t Temp = recv(Sock, &File[Rcv], Len, MSG_WAITALL);
|
int32_t Temp = recv(Sock, &File[Rcv], Len, MSG_WAITALL);
|
||||||
if (Temp < 1) {
|
if (Temp < 1) {
|
||||||
info(std::to_string(Temp));
|
info(std::to_string(Temp));
|
||||||
@@ -161,12 +176,13 @@ std::vector<char> TCPRcvRaw(SOCKET Sock, uint64_t& GRcv, uint64_t Size) {
|
|||||||
Rcv += Temp;
|
Rcv += Temp;
|
||||||
GRcv += Temp;
|
GRcv += Temp;
|
||||||
|
|
||||||
// every 8th iteration calculate download speed for that iteration
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
auto difference = end - start;
|
||||||
|
float bits_per_s = float(Rcv * 8) / float(std::chrono::duration_cast<std::chrono::milliseconds>(difference).count());
|
||||||
|
float megabits_per_s = bits_per_s / 1000;
|
||||||
|
DownloadSpeed = megabits_per_s;
|
||||||
|
// every 8th iteration print the speed
|
||||||
if (i % 8 == 0) {
|
if (i % 8 == 0) {
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
|
||||||
auto difference = end - start;
|
|
||||||
float bits_per_s = float(Temp * 8) / float(std::chrono::duration_cast<std::chrono::milliseconds>(difference).count());
|
|
||||||
float megabits_per_s = bits_per_s / 1000;
|
|
||||||
debug("Download speed: " + std::to_string(uint32_t(megabits_per_s)) + "Mbit/s");
|
debug("Download speed: " + std::to_string(uint32_t(megabits_per_s)) + "Mbit/s");
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
@@ -203,7 +219,38 @@ 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;
|
||||||
|
|
||||||
uint64_t GRcv = 0;
|
uint64_t GRcv = 0;
|
||||||
|
|
||||||
uint64_t MSize = Size / 2;
|
uint64_t MSize = Size / 2;
|
||||||
@@ -240,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;
|
||||||
@@ -252,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();
|
||||||
@@ -383,6 +651,7 @@ void SyncResources(SOCKET Sock) {
|
|||||||
}
|
}
|
||||||
WaitForConfirm();
|
WaitForConfirm();
|
||||||
}
|
}
|
||||||
|
|
||||||
KillSocket(DSock);
|
KillSocket(DSock);
|
||||||
if (!Terminate) {
|
if (!Terminate) {
|
||||||
TCPSend("Done", Sock);
|
TCPSend("Done", Sock);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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 ".2";
|
return ".0";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GetEP(char* P) {
|
std::string GetEP(char* P) {
|
||||||
|
|||||||
@@ -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");
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
"cpp-httplib",
|
"cpp-httplib",
|
||||||
"nlohmann-json",
|
"nlohmann-json",
|
||||||
"zlib",
|
"zlib",
|
||||||
"openssl"
|
"openssl",
|
||||||
|
"curl"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user