mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-10-25 21:50:02 +00:00
Made regionhandler trigger in http requests
This commit is contained in:
1 parent
f98e9a7746
commit
968c5d318e
8 files changed
+51
-24
No files matched your search
+3
-3
@@ -11,9 +11,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
class HTTP {
|
class HTTP {
|
||||||
public:
|
public:
|
||||||
static bool Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash);
|
static bool Download(const std::string& IP, const beammp_fs_string& Path, const std::string& Hash, const bool& redirect = true);
|
||||||
static std::string Post(const std::string& IP, const std::string& Fields);
|
static std::string Post(std::string IP, const std::string& Fields, const bool& redirect = true);
|
||||||
static std::string Get(const std::string& IP);
|
static std::string Get(std::string IP, const bool& redirect = true);
|
||||||
static bool ProgressBar(size_t c, size_t t);
|
static bool ProgressBar(size_t c, size_t t);
|
||||||
static void StartProxy();
|
static void StartProxy();
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public:
|
|||||||
RegionHandler() = delete;
|
RegionHandler() = delete;
|
||||||
static void TopLevelDomainFailed();
|
static void TopLevelDomainFailed();
|
||||||
static std::string RegionToTopLevelDomain();
|
static std::string RegionToTopLevelDomain();
|
||||||
|
static std::string RedirectURL(const std::string& URL);
|
||||||
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"};
|
||||||
|
|||||||
@@ -49,6 +49,9 @@ void ParseConfig(const nlohmann::json& d) {
|
|||||||
deleteDuplicateMods = d["DeleteDuplicateMods"].get<bool>();
|
deleteDuplicateMods = d["DeleteDuplicateMods"].get<bool>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (d.contains("Region") && d["Region"].is_string()) {
|
||||||
|
options.region = d["Region"].get<std::string>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigInit() {
|
void ConfigInit() {
|
||||||
|
|||||||
@@ -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() + "/servers-info");
|
std::string resp = HTTP::Get("https://backend.beammp.com/servers-info", true);
|
||||||
if (resp == "") {
|
if (resp == "") {
|
||||||
RegionHandler::TopLevelDomainFailed();
|
RegionHandler::TopLevelDomainFailed();
|
||||||
resp = HTTP::Get("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/servers-info");
|
resp = HTTP::Get("https://backend.beammp.com/servers-info", true);
|
||||||
}
|
}
|
||||||
CoreSend("B" + resp);
|
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;
|
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;
|
std::string Ret;
|
||||||
static thread_local CURL* curl = curl_easy_init();
|
static thread_local CURL* curl = curl_easy_init();
|
||||||
|
if (redirect) {
|
||||||
|
IP = RegionHandler::RedirectURL(IP);
|
||||||
|
}
|
||||||
if (curl) {
|
if (curl) {
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
char errbuf[CURL_ERROR_SIZE];
|
char errbuf[CURL_ERROR_SIZE];
|
||||||
@@ -48,6 +51,7 @@ std::string HTTP::Get(const std::string& IP) {
|
|||||||
if (res != CURLE_OK) {
|
if (res != CURLE_OK) {
|
||||||
error("GET to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
|
error("GET to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
|
||||||
error("Curl error: " + std::string(errbuf));
|
error("Curl error: " + std::string(errbuf));
|
||||||
|
RegionHandler::TopLevelDomainFailed();
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -57,9 +61,12 @@ std::string HTTP::Get(const std::string& IP) {
|
|||||||
return Ret;
|
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;
|
std::string Ret;
|
||||||
static thread_local CURL* curl = curl_easy_init();
|
static thread_local CURL* curl = curl_easy_init();
|
||||||
|
if (redirect) {
|
||||||
|
IP = RegionHandler::RedirectURL(IP);
|
||||||
|
}
|
||||||
if (curl) {
|
if (curl) {
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
char errbuf[CURL_ERROR_SIZE];
|
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) {
|
if (res != CURLE_OK) {
|
||||||
error("POST to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
|
error("POST to " + IP + " failed: " + std::string(curl_easy_strerror(res)));
|
||||||
error("Curl error: " + std::string(errbuf));
|
error("Curl error: " + std::string(errbuf));
|
||||||
|
RegionHandler::TopLevelDomainFailed();
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -90,12 +98,12 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
|
|||||||
return Ret;
|
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;
|
static std::mutex Lock;
|
||||||
std::scoped_lock Guard(Lock);
|
std::scoped_lock Guard(Lock);
|
||||||
|
|
||||||
info("Downloading an update (this may take a while)");
|
info("Downloading an update (this may take a while)");
|
||||||
std::string Ret = Get(IP);
|
std::string Ret = Get(IP, redirect);
|
||||||
|
|
||||||
if (Ret.empty()) {
|
if (Ret.empty()) {
|
||||||
error("Download failed");
|
error("Download failed");
|
||||||
@@ -218,7 +226,7 @@ void HTTP::StartProxy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (error) {
|
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 {
|
} else {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include "Options.h"
|
#include "Options.h"
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
void RegionHandler::TopLevelDomainFailed()
|
void RegionHandler::TopLevelDomainFailed()
|
||||||
{
|
{
|
||||||
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()]);
|
||||||
@@ -22,3 +24,16 @@ std::string RegionHandler::RegionToTopLevelDomain()
|
|||||||
}
|
}
|
||||||
return mValidTLDs[mRegionIndex % mValidTLDs.size()]; // Global
|
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...");
|
info("Attempting to authenticate...");
|
||||||
try {
|
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 == "") {
|
if (Buffer == "") {
|
||||||
RegionHandler::TopLevelDomainFailed();
|
RegionHandler::TopLevelDomainFailed();
|
||||||
Buffer = HTTP::Post("https://auth." + RegionHandler::RegionToTopLevelDomain() + "/userlogin", fields);
|
Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Buffer.empty()) {
|
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 == "") {
|
if (Buffer == "") {
|
||||||
RegionHandler::TopLevelDomainFailed();
|
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);
|
nlohmann::json d = nlohmann::json::parse(Buffer, nullptr, false);
|
||||||
|
|||||||
+10
-10
@@ -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() + "/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/launcher?branch=" + Branch + "&pk=" + PublicKey);
|
||||||
if (LatestHash == "") {
|
if (LatestHash == "") {
|
||||||
RegionHandler::TopLevelDomainFailed();
|
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 == "") {
|
if (LatestVersion == "") {
|
||||||
RegionHandler::TopLevelDomainFailed();
|
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}$)");
|
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() + "/builds/launcher?download=true"
|
"https://backend.beammp.com/builds/launcher?download=true"
|
||||||
"&pk="
|
"&pk="
|
||||||
+ PublicKey + "&branch=" + Branch,
|
+ PublicKey + "&branch=" + Branch,
|
||||||
DownloadLocation, LatestHash);
|
DownloadLocation, LatestHash);
|
||||||
if (!downloadSuccess) {
|
if (!downloadSuccess) {
|
||||||
RegionHandler::TopLevelDomainFailed();
|
RegionHandler::TopLevelDomainFailed();
|
||||||
downloadSuccess = HTTP::Download(
|
downloadSuccess = HTTP::Download(
|
||||||
"https://backend." + RegionHandler::RegionToTopLevelDomain() + "/builds/launcher?download=true"
|
"https://backend.beammp.com/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() + "/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
|
std::string LatestHash = HTTP::Get("https://backend.beammp.com/sha/mod?branch=" + Branch + "&pk=" + PublicKey);
|
||||||
if (LatestHash == "") {
|
if (LatestHash == "") {
|
||||||
RegionHandler::TopLevelDomainFailed();
|
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);
|
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() + "/builds/client?download=true"
|
if (!HTTP::Download("https://backend.beammp.com/builds/client?download=true"
|
||||||
"&pk="
|
"&pk="
|
||||||
+ PublicKey + "&branch=" + Branch,
|
+ PublicKey + "&branch=" + Branch,
|
||||||
ZipPath, LatestHash)) {
|
ZipPath, LatestHash)) {
|
||||||
RegionHandler::TopLevelDomainFailed();
|
RegionHandler::TopLevelDomainFailed();
|
||||||
HTTP::Download("https://backend." + RegionHandler::RegionToTopLevelDomain() + "/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);
|
||||||
|
|||||||
Reference in new issue
Block a user