mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-07-12 17:54:04 +00:00
Refactor region handler
This commit is contained in:
@@ -25,8 +25,8 @@
|
||||
class RegionHandler final {
|
||||
public:
|
||||
RegionHandler() = delete;
|
||||
static void TopLevelDomainFailed(bool failed);
|
||||
static std::string RegionToTopLevelDomain(const std::string region);
|
||||
static void TopLevelDomainFailed();
|
||||
static std::string RegionToTopLevelDomain();
|
||||
private:
|
||||
static inline unsigned int mRegionIndex { 0 };
|
||||
const static inline std::array<std::string, 2> mValidTLDs {"beammp.com", "beammp.ru"};
|
||||
|
||||
+5
-20
@@ -35,6 +35,7 @@
|
||||
#include "Compat.h"
|
||||
#include "CustomAssert.h"
|
||||
#include "Http.h"
|
||||
#include "RegionHandler.h"
|
||||
|
||||
void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
|
||||
std::unique_lock Lock(mShutdownHandlersMutex);
|
||||
@@ -75,39 +76,23 @@ std::string Application::ServerVersionString() {
|
||||
|
||||
std::vector<std::string> Application::GetBackendUrlsInOrder() {
|
||||
return {
|
||||
"https://backend." + RegionToTopLevelDomain(Settings.getAsString(Settings::Key::General_Region)),
|
||||
"https://backend." + RegionHandler::RegionToTopLevelDomain(),
|
||||
};
|
||||
}
|
||||
|
||||
std::string Application::GetServerCheckUrl()
|
||||
{
|
||||
return "https://check." + RegionToTopLevelDomain(Settings.getAsString(Settings::Key::General_Region));
|
||||
return "https://check." + RegionHandler::RegionToTopLevelDomain();
|
||||
}
|
||||
|
||||
std::string Application::GetBackendUrlForAuth()
|
||||
{
|
||||
return "https://auth." + RegionToTopLevelDomain(Settings.getAsString(Settings::Key::General_Region));
|
||||
return "https://auth." + RegionHandler::RegionToTopLevelDomain();
|
||||
}
|
||||
|
||||
std::string Application::GetBackendUrlForSocketIO()
|
||||
{
|
||||
return "https://backend." + RegionToTopLevelDomain(Settings.getAsString(Settings::Key::General_Region));
|
||||
}
|
||||
|
||||
void Application::TopLevelDomainFailed(bool failed)
|
||||
{
|
||||
if (failed) {
|
||||
beammp_info("Top Level Domain (" + mValidTLDs[mTLDIndex % mValidTLDs.size()] + ") failed to respond correctly, switching to " + mValidTLDs[(mTLDIndex + 1) % mValidTLDs.size()]);
|
||||
mTLDIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Application::RegionToTopLevelDomain(const std::string region)
|
||||
{
|
||||
if (region == "Developer") {
|
||||
return "beammp.dev";
|
||||
}
|
||||
return mValidTLDs[mTLDIndex % mValidTLDs.size()]; // Global
|
||||
return "https://backend." + RegionHandler::RegionToTopLevelDomain();
|
||||
}
|
||||
|
||||
std::array<uint8_t, 3> Application::VersionStrToInts(const std::string& str) {
|
||||
|
||||
@@ -19,16 +19,16 @@
|
||||
#include "RegionHandler.h"
|
||||
#include "Common.h"
|
||||
|
||||
void RegionHandler::TopLevelDomainFailed(bool failed)
|
||||
void RegionHandler::TopLevelDomainFailed()
|
||||
{
|
||||
if (!failed) return;
|
||||
beammp_info("Top level domain of " + mValidTLDs[mRegionIndex % mValidTLDs.size()] + " didn't respond correctly , changing domain to " + mValidTLDs[(mRegionIndex + 1) % mValidTLDs.size()]);
|
||||
mRegionIndex++;
|
||||
}
|
||||
|
||||
std::string RegionHandler::RegionToTopLevelDomain(const std::string region)
|
||||
std::string RegionHandler::RegionToTopLevelDomain()
|
||||
{
|
||||
if (region == "Developer") {
|
||||
static bool isDeveloperRegion = Application::Settings.getAsString(Settings::Key::General_Region) == "Developer";
|
||||
if (isDeveloperRegion) {
|
||||
return "beammp.dev";
|
||||
}
|
||||
return mValidTLDs[mRegionIndex % mValidTLDs.size()]; // Global
|
||||
|
||||
+1
-1
@@ -298,7 +298,7 @@ void TConsole::Command_NetTest(const std::string& cmd, const std::vector<std::st
|
||||
std::string T = Http::GET(
|
||||
Application::GetServerCheckUrl() + "/api/v2/beammp/" + std::to_string(Application::Settings.getAsInt(Settings::Key::General_Port)), &status);
|
||||
if (T == Http::ErrorString || status != 200) {
|
||||
Application::TopLevelDomainFailed(true);
|
||||
Application::TopLevelDomainFailed();
|
||||
T = Http::GET(Application::GetServerCheckUrl() + "/api/v2/beammp/" + std::to_string(Application::Settings.getAsInt(Settings::Key::General_Port)), &status);
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ void THeartbeatThread::operator()() {
|
||||
for (const auto& Url : Application::GetBackendUrlsInOrder()) {
|
||||
T = Http::POST(Url + Target, Body, "application/json", &ResponseCode, { { "api-v", "2" } });
|
||||
if (T == Http::ErrorString || ResponseCode != 200) {
|
||||
Application::TopLevelDomainFailed(true);
|
||||
Application::TopLevelDomainFailed();
|
||||
T = Http::POST(Url + Target, Body, "application/json", &ResponseCode, { { "api-v", "2" } });
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -441,7 +441,7 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
|
||||
unsigned int ResponseCode = 0;
|
||||
AuthResStr = Http::POST(Application::GetBackendUrlForAuth() + Target, AuthReq.dump(), "application/json", &ResponseCode);
|
||||
if (AuthResStr == Http::ErrorString || ResponseCode != 200) {
|
||||
Application::TopLevelDomainFailed(true);
|
||||
Application::TopLevelDomainFailed();
|
||||
AuthResStr = Http::POST(Application::GetBackendUrlForAuth() + Target, AuthReq.dump(), "application/json", &ResponseCode);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user