Made regionhandler handled on http level

This commit is contained in:
SaltySnail
2026-07-19 02:53:27 +02:00
parent 8239a31e4b
commit 47949514af
5 changed files with 37 additions and 9 deletions
+2 -2
View File
@@ -39,8 +39,8 @@
namespace fs = std::filesystem;
namespace Http {
std::string GET(const std::string& url, unsigned int* status = nullptr);
std::string POST(const std::string& url, const std::string& body, const std::string& ContentType, unsigned int* status = nullptr, const std::map<std::string, std::string>& headers = {});
std::string GET(std::string url, unsigned int* status = nullptr, const bool& redirect = true);
std::string POST(std::string url, const std::string& body, const std::string& ContentType, unsigned int* status = nullptr, const std::map<std::string, std::string>& headers = {}, const bool& redirect = true);
namespace Status {
std::string ToString(int code);
}
+1
View File
@@ -27,6 +27,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"};
+4 -4
View File
@@ -76,23 +76,23 @@ std::string Application::ServerVersionString() {
std::vector<std::string> Application::GetBackendUrlsInOrder() {
return {
"https://backend." + RegionHandler::RegionToTopLevelDomain(),
"https://backend.beammp.com",
};
}
std::string Application::GetServerCheckUrl()
{
return "https://check." + RegionHandler::RegionToTopLevelDomain();
return "https://check.beammp.com";
}
std::string Application::GetBackendUrlForAuth()
{
return "https://auth." + RegionHandler::RegionToTopLevelDomain();
return "https://auth.beammp.com";
}
std::string Application::GetBackendUrlForSocketIO()
{
return "https://backend." + RegionHandler::RegionToTopLevelDomain();
return "https://backend.beammp.com";
}
std::array<uint8_t, 3> Application::VersionStrToInts(const std::string& str) {
+11 -2
View File
@@ -18,6 +18,7 @@
#include "Http.h"
#include "RegionHandler.h"
#include "Common.h"
#include "CustomAssert.h"
@@ -115,10 +116,13 @@ public:
}
};
std::string Http::GET(const std::string& url, unsigned int* status) {
std::string Http::GET(std::string url, unsigned int* status, const bool& redirect) {
std::string Ret;
CurlLease Lease{};
CURL* curl = Lease.GetHandle();
if (redirect) {
url = RegionHandler::RedirectURL(url);
}
if (curl) {
CURLcode res;
char errbuf[CURL_ERROR_SIZE];
@@ -142,6 +146,7 @@ std::string Http::GET(const std::string& url, unsigned int* status) {
if (res != CURLE_OK) {
beammp_error("GET to " + url + " failed: " + std::string(curl_easy_strerror(res)));
beammp_error("Curl error: " + std::string(errbuf));
RegionHandler::TopLevelDomainFailed();
return Http::ErrorString;
}
@@ -156,10 +161,13 @@ std::string Http::GET(const std::string& url, unsigned int* status) {
return Ret;
}
std::string Http::POST(const std::string& url, const std::string& body, const std::string& ContentType, unsigned int* status, const std::map<std::string, std::string>& headers) {
std::string Http::POST(std::string url, const std::string& body, const std::string& ContentType, unsigned int* status, const std::map<std::string, std::string>& headers, const bool& redirect) {
std::string Ret;
CurlLease Lease{};
CURL* curl = Lease.GetHandle();
if (redirect) {
url = RegionHandler::RedirectURL(url);
}
if (curl) {
CURLcode res;
char errbuf[CURL_ERROR_SIZE];
@@ -195,6 +203,7 @@ std::string Http::POST(const std::string& url, const std::string& body, const st
if (res != CURLE_OK) {
beammp_error("POST to " + url + " failed: " + std::string(curl_easy_strerror(res)));
beammp_error("Curl error: " + std::string(errbuf));
RegionHandler::TopLevelDomainFailed();
return Http::ErrorString;
}
+19 -1
View File
@@ -19,9 +19,14 @@
#include "RegionHandler.h"
#include "Common.h"
#include <regex>
void RegionHandler::TopLevelDomainFailed()
{
beammp_info("Top level domain of " + mValidTLDs[mRegionIndex % mValidTLDs.size()] + " didn't respond correctly , changing domain to " + mValidTLDs[(mRegionIndex + 1) % mValidTLDs.size()]);
static bool isDeveloperRegion = Application::Settings.getAsString(Settings::Key::General_Region) == "Developer";
if (!isDeveloperRegion) {
beammp_info("Top level domain of " + mValidTLDs[mRegionIndex % mValidTLDs.size()] + " didn't respond correctly , changing domain to " + mValidTLDs[(mRegionIndex + 1) % mValidTLDs.size()]);
}
mRegionIndex++;
}
@@ -33,3 +38,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
}