Refactor region handler

This commit is contained in:
SaltySnail committed 2026-06-27 20:49:12 +02:00
1 parent d7277151cc
commit f98e9a7746
8 files changed
+35 -35

No files matched your search

+2 -2
View File
@@ -13,8 +13,8 @@
class RegionHandler final { class RegionHandler final {
public: public:
RegionHandler() = delete; RegionHandler() = delete;
static void TopLevelDomainFailed(bool failed); static void TopLevelDomainFailed();
static std::string RegionToTopLevelDomain(const std::string region); static std::string RegionToTopLevelDomain();
private: private:
static inline unsigned int mRegionIndex { 0 }; static inline unsigned int mRegionIndex { 0 };
const static inline std::array<std::string, 2> mValidTLDs {"beammp.com", "beammp.ru"}; const static inline std::array<std::string, 2> mValidTLDs {"beammp.com", "beammp.ru"};
+1 -1
View File
@@ -336,4 +336,4 @@ namespace Utils {
throw std::runtime_error("Game disconnected"); throw std::runtime_error("Game disconnected");
} }
} }
}; };
+3 -3
View File
@@ -227,10 +227,10 @@ 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, []() {
std::string resp = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/servers-info"); std::string resp = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/servers-info");
if (resp == "") { if (resp == "") {
RegionHandler::TopLevelDomainFailed(true); RegionHandler::TopLevelDomainFailed();
resp = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/servers-info"); resp = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/servers-info");
} }
CoreSend("B" + resp); CoreSend("B" + resp);
})); }));
+3 -3
View File
@@ -138,8 +138,8 @@ void HTTP::StartProxy() {
{ "User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch() }, { "User-Agent", "BeamMP-Launcher/" + GetVer() + GetPatch() },
{ "Accept", "*/*" } { "Accept", "*/*" }
}; };
httplib::Client backend("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region)); httplib::Client backend("https://backend." + RegionHandler::RegionToTopLevelDomain());
httplib::Client forum("https://forum." + RegionHandler::RegionToTopLevelDomain(options.region)); httplib::Client forum("https://forum." + RegionHandler::RegionToTopLevelDomain());
const std::string pattern = ".*"; const std::string pattern = ".*";
@@ -218,7 +218,7 @@ void HTTP::StartProxy() {
} }
if (error) { if (error) {
cli_res = forum.Get("/user_avatar/forum." + RegionHandler::RegionToTopLevelDomain(options.region) + "/user/0/0.png", headers); cli_res = forum.Get("/user_avatar/forum." + RegionHandler::RegionToTopLevelDomain() + "/user/0/0.png", headers);
} }
} else { } else {
+5 -4
View File
@@ -6,17 +6,18 @@
#include "RegionHandler.h" #include "RegionHandler.h"
#include "Logger.h" #include "Logger.h"
#include "Options.h"
void RegionHandler::TopLevelDomainFailed(bool failed) void RegionHandler::TopLevelDomainFailed()
{ {
if (!failed) return;
info("Top level domain of " + mValidTLDs[mRegionIndex % mValidTLDs.size()] + " didn't respond correctly , changing domain to " + mValidTLDs[(mRegionIndex + 1) % mValidTLDs.size()]); info("Top level domain of " + mValidTLDs[mRegionIndex % mValidTLDs.size()] + " didn't respond correctly , changing domain to " + mValidTLDs[(mRegionIndex + 1) % mValidTLDs.size()]);
mRegionIndex++; mRegionIndex++;
} }
std::string RegionHandler::RegionToTopLevelDomain(const std::string region) std::string RegionHandler::RegionToTopLevelDomain()
{ {
if (region == "Developer") { static bool isDeveloperRegion = options.region == "Developer";
if (isDeveloperRegion) {
return "beammp.dev"; return "beammp.dev";
} }
return mValidTLDs[mRegionIndex % mValidTLDs.size()]; // Global return mValidTLDs[mRegionIndex % mValidTLDs.size()]; // Global
-1
View File
@@ -115,7 +115,6 @@ void InitOptions(int argc, const char *argv[], Options &options) {
"\t--no-launch Skip launching the game (you must launch the game manually)\n" "\t--no-launch Skip launching the game (you must launch the game manually)\n"
"\t--dev Developer mode, same as --verbose --no-download --no-launch --no-update\n" "\t--dev Developer mode, same as --verbose --no-download --no-launch --no-update\n"
"\t--user-path <path> Path to BeamNG's User Path\n" "\t--user-path <path> Path to BeamNG's User Path\n"
"\t--region <region> Sets a custom region, options are 'Global', and 'Restricted'\n"
"\t--game <args...> Passes ALL following arguments to the game, see also `--`\n" "\t--game <args...> Passes ALL following arguments to the game, see also `--`\n"
<< std::flush; << std::flush;
exit(0); exit(0);
+6 -6
View File
@@ -57,10 +57,10 @@ std::string Login(const std::string& fields) {
} }
info("Attempting to authenticate..."); info("Attempting to authenticate...");
try { try {
std::string Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain(options.region) + "/userlogin", fields); std::string Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", fields);
if (Buffer == "") { if (Buffer == "") {
RegionHandler::TopLevelDomainFailed(true); RegionHandler::TopLevelDomainFailed();
Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain(options.region) + "/userlogin", fields); Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", fields);
} }
if (Buffer.empty()) { if (Buffer.empty()) {
@@ -121,10 +121,10 @@ void CheckLocalKey() {
} }
} }
Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain(options.region) + "/userlogin", R"({"pk":")" + Buffer + "\"}"); Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", R"({"pk":")" + Buffer + "\"}");
if (Buffer == "") { if (Buffer == "") {
RegionHandler::TopLevelDomainFailed(true); RegionHandler::TopLevelDomainFailed();
Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain(options.region) + "/userlogin", R"({"pk":")" + Buffer + "\"}"); Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", R"({"pk":")" + Buffer + "\"}");
} }
nlohmann::json d = nlohmann::json::parse(Buffer, nullptr, false); nlohmann::json d = nlohmann::json::parse(Buffer, nullptr, false);
+15 -15
View File
@@ -332,15 +332,15 @@ bool VerifySignature(const std::filesystem::path& filePath)
#endif #endif
void CheckForUpdates(const std::string& CV) { void CheckForUpdates(const std::string& CV) {
std::string LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/sha/launcher?branch=" + Branch + "&pk=" + PublicKey); std::string LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
if (LatestHash == "") { if (LatestHash == "") {
RegionHandler::TopLevelDomainFailed(true); RegionHandler::TopLevelDomainFailed();
LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/sha/launcher?branch=" + Branch + "&pk=" + PublicKey); LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
} }
std::string LatestVersion = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/version/launcher?branch=" + Branch + "&pk=" + PublicKey); std::string LatestVersion = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
if (LatestVersion == "") { if (LatestVersion == "") {
RegionHandler::TopLevelDomainFailed(true); RegionHandler::TopLevelDomainFailed();
LatestVersion = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/version/launcher?branch=" + Branch + "&pk=" + PublicKey); LatestVersion = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
} }
std::regex sha256_pattern(R"(^[a-fA-F0-9]{64}$)"); std::regex sha256_pattern(R"(^[a-fA-F0-9]{64}$)");
@@ -367,14 +367,14 @@ void CheckForUpdates(const std::string& CV) {
std::wstring DownloadLocation = GetBP() / (beammp_wide("new_") + GetEN()); std::wstring DownloadLocation = GetBP() / (beammp_wide("new_") + GetEN());
bool downloadSuccess = false; bool downloadSuccess = false;
downloadSuccess = HTTP::Download( downloadSuccess = HTTP::Download(
"https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/builds/launcher?download=true" "https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/launcher?download=true"
"&pk=" "&pk="
+ PublicKey + "&branch=" + Branch, + PublicKey + "&branch=" + Branch,
DownloadLocation, LatestHash); DownloadLocation, LatestHash);
if (!downloadSuccess) { if (!downloadSuccess) {
RegionHandler::TopLevelDomainFailed(true); RegionHandler::TopLevelDomainFailed();
downloadSuccess = HTTP::Download( downloadSuccess = HTTP::Download(
"https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/builds/launcher?download=true" "https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/launcher?download=true"
"&pk=" "&pk="
+ PublicKey + "&branch=" + Branch, + PublicKey + "&branch=" + Branch,
DownloadLocation, LatestHash); DownloadLocation, LatestHash);
@@ -532,10 +532,10 @@ void PreGame(const beammp_fs_string& GamePath) {
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." + RegionHandler::RegionToTopLevelDomain(options.region) + "/sha/mod?branch=" + Branch + "&pk=" + PublicKey); std::string LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
if (LatestHash == "") { if (LatestHash == "") {
RegionHandler::TopLevelDomainFailed(true); RegionHandler::TopLevelDomainFailed();
LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/sha/mod?branch=" + Branch + "&pk=" + PublicKey); LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/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(),
@@ -570,12 +570,12 @@ void PreGame(const beammp_fs_string& GamePath) {
if (FileHash != LatestHash) { if (FileHash != LatestHash) {
info("Downloading BeamMP Update " + LatestHash); info("Downloading BeamMP Update " + LatestHash);
if (!HTTP::Download("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/builds/client?download=true" if (!HTTP::Download("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/client?download=true"
"&pk=" "&pk="
+ PublicKey + "&branch=" + Branch, + PublicKey + "&branch=" + Branch,
ZipPath, LatestHash)) { ZipPath, LatestHash)) {
RegionHandler::TopLevelDomainFailed(true); RegionHandler::TopLevelDomainFailed();
HTTP::Download("https://backend." + RegionHandler::RegionToTopLevelDomain(options.region) + "/builds/client?download=true" HTTP::Download("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/client?download=true"
"&pk=" "&pk="
+ PublicKey + "&branch=" + Branch, + PublicKey + "&branch=" + Branch,
ZipPath, LatestHash); ZipPath, LatestHash);