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