Remove backend

This commit is contained in:
SaltySnail
2026-03-28 22:23:33 +01:00
parent b1c3b978ff
commit 642dc76a92
3 changed files with 158 additions and 161 deletions

View File

@@ -227,7 +227,7 @@ void Parse(std::string Data, SOCKET CSocket) {
TCPTerminate = true; TCPTerminate = true;
Data.clear(); Data.clear();
futures.push_back(std::async(std::launch::async, []() { futures.push_back(std::async(std::launch::async, []() {
CoreSend("B" + HTTP::Get("https://backend.beammp.com/servers-info")); // CoreSend("B" + HTTP::Get("https://backend.beammp.com/servers-info"));
})); }));
} }
break; break;

View File

@@ -169,118 +169,118 @@ void set_headers(httplib::Response& res) {
} }
void HTTP::StartProxy() { void HTTP::StartProxy() {
std::thread proxy([&]() { // std::thread proxy([&]() {
httplib::Server HTTPProxy; // httplib::Server HTTPProxy;
httplib::Headers headers = { // httplib::Headers headers = {
{ "User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch() }, // { "User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch() },
{ "Accept", "*/*" } // { "Accept", "*/*" }
}; // };
httplib::Client backend("https://backend.beammp.com"); // httplib::Client backend("https://backend.beammp.com");
httplib::Client forum("https://forum.beammp.com"); // httplib::Client forum("https://forum.beammp.com");
const std::string pattern = ".*"; // const std::string pattern = ".*";
auto handle_request = [&](const httplib::Request& req, httplib::Response& res) { // auto handle_request = [&](const httplib::Request& req, httplib::Response& res) {
set_headers(res); // set_headers(res);
if (req.has_header("X-BMP-Authentication")) { // if (req.has_header("X-BMP-Authentication")) {
headers.emplace("X-BMP-Authentication", PrivateKey); // headers.emplace("X-BMP-Authentication", PrivateKey);
} // }
if (req.has_header("X-API-Version")) { // if (req.has_header("X-API-Version")) {
headers.emplace("X-API-Version", req.get_header_value("X-API-Version")); // headers.emplace("X-API-Version", req.get_header_value("X-API-Version"));
} // }
const std::vector<std::string> path = Utils::Split(req.path, "/"); // const std::vector<std::string> path = Utils::Split(req.path, "/");
httplib::Result cli_res; // httplib::Result cli_res;
const std::string method = req.method; // const std::string method = req.method;
std::string host = ""; // std::string host = "";
if (!path.empty()) // if (!path.empty())
host = path[0]; // host = path[0];
if (host == "backend") { // if (host == "backend") {
std::string remaining_path = req.path.substr(std::strlen("/backend")); // std::string remaining_path = req.path.substr(std::strlen("/backend"));
if (method == "GET") // if (method == "GET")
cli_res = backend.Get(remaining_path, headers); // cli_res = backend.Get(remaining_path, headers);
else if (method == "POST") // else if (method == "POST")
cli_res = backend.Post(remaining_path, headers); // cli_res = backend.Post(remaining_path, headers);
} else if (host == "avatar") { // } else if (host == "avatar") {
bool error = false; // bool error = false;
std::string username; // std::string username;
std::string avatar_size = "100"; // std::string avatar_size = "100";
if (path.size() > 1) { // if (path.size() > 1) {
username = path[1]; // username = path[1];
} else { // } else {
error = true; // error = true;
} // }
if (path.size() > 2) { // if (path.size() > 2) {
try { // try {
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;
if (!error) { // if (!error) {
summary_res = forum.Get("/u/" + username + ".json", headers); // summary_res = forum.Get("/u/" + username + ".json", headers);
if (!summary_res || summary_res->status != 200) { // if (!summary_res || summary_res->status != 200) {
error = true; // error = true;
} // }
} // }
if (!error) { // if (!error) {
try { // try {
nlohmann::json d = nlohmann::json::parse(summary_res->body, nullptr, false); // can fail with parse_error // 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 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_json = user.at("avatar_template"); // can fail with out_of_range
auto avatar_link = avatar_link_json.get<std::string>(); // auto avatar_link = avatar_link_json.get<std::string>();
size_t start_pos = avatar_link.find("{size}"); // size_t start_pos = avatar_link.find("{size}");
if (start_pos != std::string::npos) // if (start_pos != std::string::npos)
avatar_link.replace(start_pos, std::strlen("{size}"), avatar_size); // avatar_link.replace(start_pos, std::strlen("{size}"), avatar_size);
cli_res = forum.Get(avatar_link, headers); // cli_res = forum.Get(avatar_link, headers);
} catch (std::exception&) { // } catch (std::exception&) {
error = true; // error = true;
} // }
} // }
if (error) { // if (error) {
cli_res = forum.Get("/user_avatar/forum.beammp.com/user/0/0.png", headers); // cli_res = forum.Get("/user_avatar/forum.beammp.com/user/0/0.png", headers);
} // }
} else { // } else {
res.set_content("Host not found", "text/plain"); // res.set_content("Host not found", "text/plain");
return; // return;
} // }
if (cli_res) { // if (cli_res) {
res.set_content(cli_res->body, cli_res->get_header_value("Content-Type")); // res.set_content(cli_res->body, cli_res->get_header_value("Content-Type"));
} else { // } else {
res.set_content(to_string(cli_res.error()), "text/plain"); // res.set_content(to_string(cli_res.error()), "text/plain");
} // }
}; // };
HTTPProxy.Get(pattern, [&](const httplib::Request& req, httplib::Response& res) { // HTTPProxy.Get(pattern, [&](const httplib::Request& req, httplib::Response& res) {
handle_request(req, res); // handle_request(req, res);
}); // });
HTTPProxy.Post(pattern, [&](const httplib::Request& req, httplib::Response& res) { // HTTPProxy.Post(pattern, [&](const httplib::Request& req, httplib::Response& res) {
handle_request(req, res); // handle_request(req, res);
}); // });
ProxyPort = HTTPProxy.bind_to_any_port("127.0.0.1"); // ProxyPort = HTTPProxy.bind_to_any_port("127.0.0.1");
debug("HTTP Proxy listening on port " + std::to_string(ProxyPort)); // debug("HTTP Proxy listening on port " + std::to_string(ProxyPort));
HTTPProxy.listen_after_bind(); // HTTPProxy.listen_after_bind();
}); // });
proxy.detach(); // proxy.detach();
} }

View File

@@ -195,47 +195,44 @@ void CheckName() {
} }
void CheckForUpdates(const std::string& CV) { void CheckForUpdates(const std::string& CV) {
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher?branch=" + Branch + "&pk=" + PublicKey); // std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
std::string LatestVersion = HTTP::Get( // std::string LatestVersion = HTTP::Get(
"https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey); // "https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower); // transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
beammp_fs_string BP(GetBP() / GetEN()), Back(GetBP() / beammp_wide("BeamMP-Launcher.back")); // beammp_fs_string BP(GetBP() / GetEN()), Back(GetBP() / beammp_wide("BeamMP-Launcher.back"));
std::string FileHash = Utils::GetSha256HashReallyFastFile(BP); // std::string FileHash = Utils::GetSha256HashReallyFastFile(BP);
if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion)))) { // if (FileHash != LatestHash && IsOutdated(Version(VersionStrToInts(GetVer() + GetPatch())), Version(VersionStrToInts(LatestVersion)))) {
if (!options.no_update) { // if (!options.no_update) {
info("Launcher update " + LatestVersion + " found!"); // info("Launcher update " + LatestVersion + " found!");
#if defined(__linux__) // #if defined(__linux__)
error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches."); // error("Auto update is NOT implemented for the Linux version. Please update manually ASAP as updates contain security patches.");
#else // #else
info("Downloading Launcher update " + LatestHash); // info("Downloading Launcher update " + LatestHash);
if (HTTP::Download( // HTTP::Download(
"https://backend.beammp.com/builds/launcher?download=true" // "https://backend.beammp.com/builds/launcher?download=true"
"&pk=" // "&pk="
+ PublicKey + "&branch=" + Branch, // + PublicKey + "&branch=" + Branch,
GetBP() / (beammp_wide("new_") + GetEN()), LatestHash)) { // GetBP() / (beammp_wide("new_") + GetEN()), LatestHash);
std::error_code ec; // std::error_code ec;
fs::remove(Back, ec); // fs::remove(Back, ec);
if (ec == std::errc::permission_denied) { // if (ec == std::errc::permission_denied) {
error("Failed to remove old backup file: " + ec.message() + ". Using alternative name."); // error("Failed to remove old backup file: " + ec.message() + ". Using alternative name.");
fs::rename(BP, Back + beammp_wide(".") + Utils::ToWString(FileHash.substr(0, 8))); // fs::rename(BP, Back + beammp_wide(".") + Utils::ToWString(FileHash.substr(0, 8)));
} else { // } else {
fs::rename(BP, Back); // fs::rename(BP, Back);
} // }
fs::rename(GetBP() / (beammp_wide("new_") + GetEN()), BP); // fs::rename(GetBP() / (beammp_wide("new_") + GetEN()), BP);
URelaunch(); // URelaunch();
} else { // #endif
throw std::runtime_error("Failed to download the launcher update! Please try manually updating it, https://docs.beammp.com/FAQ/Update-launcher/"); // } else {
} // warn("Launcher update was found, but not updating because --no-update or --dev was specified.");
#endif // }
} else { // } else
warn("Launcher update was found, but not updating because --no-update or --dev was specified."); // info("Launcher version is up to date. Latest version: " + LatestVersion);
} // TraceBack++;
} else
info("Launcher version is up to date. Latest version: " + LatestVersion);
TraceBack++;
} }
@@ -350,42 +347,42 @@ void PreGame(const beammp_fs_string& GamePath) {
CheckMP(GetGamePath() / beammp_wide("mods/multiplayer")); CheckMP(GetGamePath() / beammp_wide("mods/multiplayer"));
info(beammp_wide("Game user path: ") + beammp_fs_string(GetGamePath())); info(beammp_wide("Game user path: ") + beammp_fs_string(GetGamePath()));
if (!options.no_download) { // if (!options.no_download) {
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey); // std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower); // transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
LatestHash.erase(std::remove_if(LatestHash.begin(), LatestHash.end(), // LatestHash.erase(std::remove_if(LatestHash.begin(), LatestHash.end(),
[](auto const& c) -> bool { return !std::isalnum(c); }), // [](auto const& c) -> bool { return !std::isalnum(c); }),
LatestHash.end()); // LatestHash.end());
try { // try {
if (!fs::exists(GetGamePath() / beammp_wide("mods/multiplayer"))) { // if (!fs::exists(GetGamePath() / beammp_wide("mods/multiplayer"))) {
fs::create_directories(GetGamePath() / beammp_wide("mods/multiplayer")); // fs::create_directories(GetGamePath() / beammp_wide("mods/multiplayer"));
} // }
EnableMP(); // EnableMP();
} catch (std::exception& e) { // } catch (std::exception& e) {
fatal(e.what()); // fatal(e.what());
} // }
#if defined(_WIN32) // #if defined(_WIN32)
std::wstring ZipPath(GetGamePath() / LR"(mods\multiplayer\BeamMP.zip)"); // std::wstring ZipPath(GetGamePath() / LR"(mods\multiplayer\BeamMP.zip)");
#elif defined(__linux__) // #elif defined(__linux__)
// Linux version of the game cant handle mods with uppercase names // // Linux version of the game cant handle mods with uppercase names
std::string ZipPath(GetGamePath() / R"(mods/multiplayer/beammp.zip)"); // std::string ZipPath(GetGamePath() / R"(mods/multiplayer/beammp.zip)");
#endif // #endif
std::string FileHash = fs::exists(ZipPath) ? Utils::GetSha256HashReallyFastFile(ZipPath) : ""; // std::string FileHash = fs::exists(ZipPath) ? Utils::GetSha256HashReallyFastFile(ZipPath) : "";
if (FileHash != LatestHash) { // if (FileHash != LatestHash) {
info("Downloading BeamMP Update " + LatestHash); // info("Downloading BeamMP Update " + LatestHash);
HTTP::Download("https://backend.beammp.com/builds/client?download=true" // HTTP::Download("https://backend.beammp.com/builds/client?download=true"
"&pk=" // "&pk="
+ PublicKey + "&branch=" + Branch, // + PublicKey + "&branch=" + Branch,
ZipPath, LatestHash); // ZipPath, LatestHash);
} // }
beammp_fs_string Target(GetGamePath() / beammp_wide("mods/unpacked/beammp")); // beammp_fs_string Target(GetGamePath() / beammp_wide("mods/unpacked/beammp"));
if (fs::is_directory(Target) && !fs::is_directory(Target + beammp_wide("/.git"))) { // if (fs::is_directory(Target) && !fs::is_directory(Target + beammp_wide("/.git"))) {
fs::remove_all(Target); // fs::remove_all(Target);
} // }
} // }
} }