mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-08-27 12:29:53 +00:00
Made regionhandler handled on http level
This commit is contained in:
+2
-2
@@ -39,8 +39,8 @@
|
|||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace Http {
|
namespace Http {
|
||||||
std::string GET(const std::string& url, unsigned int* status = nullptr);
|
std::string GET(std::string url, unsigned int* status = nullptr, const bool& redirect = true);
|
||||||
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 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 {
|
namespace Status {
|
||||||
std::string ToString(int code);
|
std::string ToString(int code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,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"};
|
||||||
|
|||||||
+4
-4
@@ -76,23 +76,23 @@ std::string Application::ServerVersionString() {
|
|||||||
|
|
||||||
std::vector<std::string> Application::GetBackendUrlsInOrder() {
|
std::vector<std::string> Application::GetBackendUrlsInOrder() {
|
||||||
return {
|
return {
|
||||||
"https://backend." + RegionHandler::RegionToTopLevelDomain(),
|
"https://backend.beammp.com",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Application::GetServerCheckUrl()
|
std::string Application::GetServerCheckUrl()
|
||||||
{
|
{
|
||||||
return "https://check." + RegionHandler::RegionToTopLevelDomain();
|
return "https://check.beammp.com";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Application::GetBackendUrlForAuth()
|
std::string Application::GetBackendUrlForAuth()
|
||||||
{
|
{
|
||||||
return "https://auth." + RegionHandler::RegionToTopLevelDomain();
|
return "https://auth.beammp.com";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Application::GetBackendUrlForSocketIO()
|
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) {
|
std::array<uint8_t, 3> Application::VersionStrToInts(const std::string& str) {
|
||||||
|
|||||||
+11
-2
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
#include "Http.h"
|
#include "Http.h"
|
||||||
|
|
||||||
|
#include "RegionHandler.h"
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "CustomAssert.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;
|
std::string Ret;
|
||||||
CurlLease Lease{};
|
CurlLease Lease{};
|
||||||
CURL* curl = Lease.GetHandle();
|
CURL* curl = Lease.GetHandle();
|
||||||
|
if (redirect) {
|
||||||
|
url = RegionHandler::RedirectURL(url);
|
||||||
|
}
|
||||||
if (curl) {
|
if (curl) {
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
char errbuf[CURL_ERROR_SIZE];
|
char errbuf[CURL_ERROR_SIZE];
|
||||||
@@ -142,6 +146,7 @@ std::string Http::GET(const std::string& url, unsigned int* status) {
|
|||||||
if (res != CURLE_OK) {
|
if (res != CURLE_OK) {
|
||||||
beammp_error("GET to " + url + " failed: " + std::string(curl_easy_strerror(res)));
|
beammp_error("GET to " + url + " failed: " + std::string(curl_easy_strerror(res)));
|
||||||
beammp_error("Curl error: " + std::string(errbuf));
|
beammp_error("Curl error: " + std::string(errbuf));
|
||||||
|
RegionHandler::TopLevelDomainFailed();
|
||||||
return Http::ErrorString;
|
return Http::ErrorString;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,10 +161,13 @@ std::string Http::GET(const std::string& url, unsigned int* status) {
|
|||||||
return Ret;
|
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;
|
std::string Ret;
|
||||||
CurlLease Lease{};
|
CurlLease Lease{};
|
||||||
CURL* curl = Lease.GetHandle();
|
CURL* curl = Lease.GetHandle();
|
||||||
|
if (redirect) {
|
||||||
|
url = RegionHandler::RedirectURL(url);
|
||||||
|
}
|
||||||
if (curl) {
|
if (curl) {
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
char errbuf[CURL_ERROR_SIZE];
|
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) {
|
if (res != CURLE_OK) {
|
||||||
beammp_error("POST to " + url + " failed: " + std::string(curl_easy_strerror(res)));
|
beammp_error("POST to " + url + " failed: " + std::string(curl_easy_strerror(res)));
|
||||||
beammp_error("Curl error: " + std::string(errbuf));
|
beammp_error("Curl error: " + std::string(errbuf));
|
||||||
|
RegionHandler::TopLevelDomainFailed();
|
||||||
return Http::ErrorString;
|
return Http::ErrorString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-1
@@ -19,9 +19,14 @@
|
|||||||
#include "RegionHandler.h"
|
#include "RegionHandler.h"
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
void RegionHandler::TopLevelDomainFailed()
|
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++;
|
mRegionIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,3 +38,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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user