mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-07-19 21:24:02 +00:00
Made regionhandler trigger in http requests
This commit is contained in:
+3
-3
@@ -11,9 +11,9 @@
|
||||
#include <string>
|
||||
class HTTP {
|
||||
public:
|
||||
static bool Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash);
|
||||
static std::string Post(const std::string& IP, const std::string& Fields);
|
||||
static std::string Get(const std::string& IP);
|
||||
static bool Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash, const bool& redirect = true);
|
||||
static std::string Post(std::string IP, const std::string& Fields, const bool& redirect = true);
|
||||
static std::string Get(std::string IP, const bool& redirect = true);
|
||||
static bool ProgressBar(size_t c, size_t t);
|
||||
static void StartProxy();
|
||||
public:
|
||||
|
||||
@@ -15,6 +15,7 @@ public:
|
||||
RegionHandler() = delete;
|
||||
static void TopLevelDomainFailed();
|
||||
static std::string RegionToTopLevelDomain();
|
||||
static std::string RedirectURL(const std::string& URL);
|
||||
private:
|
||||
static inline unsigned int mRegionIndex { 0 };
|
||||
const static inline std::array<std::string, 2> mValidTLDs {"beammp.com", "beammp.ru"};
|
||||
|
||||
@@ -49,6 +49,9 @@ void ParseConfig(const nlohmann::json& d) {
|
||||
deleteDuplicateMods = d["DeleteDuplicateMods"].get<bool>();
|
||||
}
|
||||
|
||||
if (d.contains("Region") && d["Region"].is_string()) {
|
||||
options.region = d["Region"].get<std::string>();
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigInit() {
|
||||
|
||||
@@ -227,10 +227,10 @@ void Parse(std::string Data, SOCKET CSocket) {
|
||||
TCPTerminate = true;
|
||||
Data.clear();
|
||||
futures.push_back(std::async(std::launch::async, []() {
|
||||
std::string resp = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/servers-info");
|
||||
std::string resp = HTTP::Get("https://backend.beammp.com/servers-info", true);
|
||||
if (resp == "") {
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
resp = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/servers-info");
|
||||
resp = HTTP::Get("https://backend.beammp.com/servers-info", true);
|
||||
}
|
||||
CoreSend("B" + resp);
|
||||
}));
|
||||
|
||||
+13
-5
@@ -31,9 +31,12 @@ static size_t CurlWriteCallback(void* contents, size_t size, size_t nmemb, void*
|
||||
}
|
||||
|
||||
bool HTTP::isDownload = false;
|
||||
std::string HTTP::Get(const std::string& IP) {
|
||||
std::string HTTP::Get(std::string IP, const bool& redirect) {
|
||||
std::string Ret;
|
||||
static thread_local CURL* curl = curl_easy_init();
|
||||
if (redirect) {
|
||||
IP = RegionHandler::RedirectURL(IP);
|
||||
}
|
||||
if (curl) {
|
||||
CURLcode res;
|
||||
char errbuf[CURL_ERROR_SIZE];
|
||||
@@ -48,6 +51,7 @@ std::string HTTP::Get(const std::string& IP) {
|
||||
if (res != CURLE_OK) {
|
||||
error("GET to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
|
||||
error("Curl error: " + std::string(errbuf));
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
return "";
|
||||
}
|
||||
} else {
|
||||
@@ -57,9 +61,12 @@ std::string HTTP::Get(const std::string& IP) {
|
||||
return Ret;
|
||||
}
|
||||
|
||||
std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
||||
std::string HTTP::Post(std::string IP, const std::string& Fields, const bool& redirect) {
|
||||
std::string Ret;
|
||||
static thread_local CURL* curl = curl_easy_init();
|
||||
if (redirect) {
|
||||
IP = RegionHandler::RedirectURL(IP);
|
||||
}
|
||||
if (curl) {
|
||||
CURLcode res;
|
||||
char errbuf[CURL_ERROR_SIZE];
|
||||
@@ -81,6 +88,7 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
||||
if (res != CURLE_OK) {
|
||||
error("POST to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
|
||||
error("Curl error: " + std::string(errbuf));
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
return "";
|
||||
}
|
||||
} else {
|
||||
@@ -90,12 +98,12 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
||||
return Ret;
|
||||
}
|
||||
|
||||
bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash) {
|
||||
bool HTTP::Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash, const bool& redirect) {
|
||||
static std::mutex Lock;
|
||||
std::scoped_lock Guard(Lock);
|
||||
|
||||
info("Downloading an update (this may take a while)");
|
||||
std::string Ret = Get(IP);
|
||||
std::string Ret = Get(IP, redirect);
|
||||
|
||||
if (Ret.empty()) {
|
||||
error("Download failed");
|
||||
@@ -218,7 +226,7 @@ void HTTP::StartProxy() {
|
||||
}
|
||||
|
||||
if (error) {
|
||||
cli_res = forum.Get("/user_avatar/forum." + RegionHandler::RegionToTopLevelDomain() + "/user/0/0.png", headers);
|
||||
cli_res = forum.Get("/user_avatar/forum./user/0/0.png", headers);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "Logger.h"
|
||||
#include "Options.h"
|
||||
|
||||
#include <regex>
|
||||
|
||||
void RegionHandler::TopLevelDomainFailed()
|
||||
{
|
||||
info("Top level domain of " + mValidTLDs[mRegionIndex % mValidTLDs.size()] + " didn't respond correctly , changing domain to " + mValidTLDs[(mRegionIndex + 1) % mValidTLDs.size()]);
|
||||
@@ -22,3 +24,16 @@ std::string RegionHandler::RegionToTopLevelDomain()
|
||||
}
|
||||
return mValidTLDs[mRegionIndex % mValidTLDs.size()]; // Global
|
||||
}
|
||||
|
||||
std::string RegionHandler::RedirectURL(const std::string &URL)
|
||||
{
|
||||
std::regex link_pattern(R"(^(https:\/\/.*)beammp\.com(\/.*)?$)");
|
||||
std::smatch link_match;
|
||||
if (std::regex_search(URL, link_match, link_pattern) && link_match.position() == 0) {
|
||||
//TLD matched beammp.com
|
||||
std::string before = link_match[1].str(); // "https://..." up to beammp
|
||||
std::string after = link_match[2].matched ? link_match[2].str() : ""; // "/path" or ""
|
||||
return before + RegionToTopLevelDomain() + after;
|
||||
}
|
||||
return URL; //if it didn't match, just return the unmodified URL
|
||||
}
|
||||
|
||||
@@ -57,10 +57,10 @@ std::string Login(const std::string& fields) {
|
||||
}
|
||||
info("Attempting to authenticate...");
|
||||
try {
|
||||
std::string Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", fields);
|
||||
std::string Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields);
|
||||
if (Buffer == "") {
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", fields);
|
||||
Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields);
|
||||
}
|
||||
|
||||
if (Buffer.empty()) {
|
||||
@@ -121,10 +121,10 @@ void CheckLocalKey() {
|
||||
}
|
||||
}
|
||||
|
||||
Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", R"({"pk":")" + Buffer + "\"}");
|
||||
Buffer = HTTP::Post("https://auth.beammp.com/userlogin", R"({"pk":")" + Buffer + "\"}");
|
||||
if (Buffer == "") {
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", R"({"pk":")" + Buffer + "\"}");
|
||||
Buffer = HTTP::Post("https://auth.beammp.com/userlogin", R"({"pk":")" + Buffer + "\"}");
|
||||
}
|
||||
|
||||
nlohmann::json d = nlohmann::json::parse(Buffer, nullptr, false);
|
||||
|
||||
+10
-10
@@ -332,15 +332,15 @@ bool VerifySignature(const std::filesystem::path& filePath)
|
||||
#endif
|
||||
|
||||
void CheckForUpdates(const std::string& CV) {
|
||||
std::string LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
if (LatestHash == "") {
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
}
|
||||
std::string LatestVersion = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
std::string LatestVersion = HTTP::Get("https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
if (LatestVersion == "") {
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
LatestVersion = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
LatestVersion = HTTP::Get("https://backend.beammp.com/version/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||
}
|
||||
|
||||
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());
|
||||
bool downloadSuccess = false;
|
||||
downloadSuccess = HTTP::Download(
|
||||
"https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/launcher?download=true"
|
||||
"https://backend.beammp.com/builds/launcher?download=true"
|
||||
"&pk="
|
||||
+ PublicKey + "&branch=" + Branch,
|
||||
DownloadLocation, LatestHash);
|
||||
if (!downloadSuccess) {
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
downloadSuccess = HTTP::Download(
|
||||
"https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/launcher?download=true"
|
||||
"https://backend.beammp.com/builds/launcher?download=true"
|
||||
"&pk="
|
||||
+ PublicKey + "&branch=" + Branch,
|
||||
DownloadLocation, LatestHash);
|
||||
@@ -532,10 +532,10 @@ void PreGame(const beammp_fs_string& GamePath) {
|
||||
info(beammp_wide("Game user path: ") + beammp_fs_string(GetGamePath()));
|
||||
|
||||
if (!options.no_download) {
|
||||
std::string LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
|
||||
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
|
||||
if (LatestHash == "") {
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
LatestHash = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
|
||||
LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
|
||||
}
|
||||
transform(LatestHash.begin(), LatestHash.end(), LatestHash.begin(), ::tolower);
|
||||
LatestHash.erase(std::remove_if(LatestHash.begin(), LatestHash.end(),
|
||||
@@ -570,12 +570,12 @@ void PreGame(const beammp_fs_string& GamePath) {
|
||||
|
||||
if (FileHash != LatestHash) {
|
||||
info("Downloading BeamMP Update " + LatestHash);
|
||||
if (!HTTP::Download("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/client?download=true"
|
||||
if (!HTTP::Download("https://backend.beammp.com/builds/client?download=true"
|
||||
"&pk="
|
||||
+ PublicKey + "&branch=" + Branch,
|
||||
ZipPath, LatestHash)) {
|
||||
RegionHandler::TopLevelDomainFailed();
|
||||
HTTP::Download("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/client?download=true"
|
||||
HTTP::Download("https://backend.beammp.com/builds/client?download=true"
|
||||
"&pk="
|
||||
+ PublicKey + "&branch=" + Branch,
|
||||
ZipPath, LatestHash);
|
||||
|
||||
Reference in New Issue
Block a user