diff options
author | Shauren <shauren.trinity@gmail.com> | 2023-07-15 00:45:16 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2023-07-15 00:45:16 +0200 |
commit | 6be536a73bc8a6e331ce20e7d19e2ea56b99b4d0 (patch) | |
tree | 2ad2ae6d9f4096c97235063eff60e7d69641a2b9 /src/server/bnetserver | |
parent | cdfaecda52e165bb04381e1677108ff87b8bdf13 (diff) |
Core/Network: Refactor local/remote ip address selection code and allow using hostnames in LoginREST bnetserver config options
Diffstat (limited to 'src/server/bnetserver')
-rw-r--r-- | src/server/bnetserver/Main.cpp | 3 | ||||
-rw-r--r-- | src/server/bnetserver/REST/LoginRESTService.cpp | 54 | ||||
-rw-r--r-- | src/server/bnetserver/REST/LoginRESTService.h | 10 | ||||
-rw-r--r-- | src/server/bnetserver/Server/Session.cpp | 4 |
4 files changed, 54 insertions, 17 deletions
diff --git a/src/server/bnetserver/Main.cpp b/src/server/bnetserver/Main.cpp index a1b93920489..6b2e058262e 100644 --- a/src/server/bnetserver/Main.cpp +++ b/src/server/bnetserver/Main.cpp @@ -32,6 +32,7 @@ #include "DeadlineTimer.h" #include "GitRevision.h" #include "IPLocation.h" +#include "IpNetwork.h" #include "LoginRESTService.h" #include "MySQLThreading.h" #include "OpenSSLCrypto.h" @@ -192,6 +193,8 @@ int main(int argc, char** argv) std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>(); + Trinity::Net::ScanLocalNetworks(); + // Start the listening port (acceptor) for auth connections int32 bnport = sConfigMgr->GetIntDefault("BattlenetPort", 1119); if (bnport < 0 || bnport > 0xFFFF) diff --git a/src/server/bnetserver/REST/LoginRESTService.cpp b/src/server/bnetserver/REST/LoginRESTService.cpp index 5267b2bd761..6e260b4df56 100644 --- a/src/server/bnetserver/REST/LoginRESTService.cpp +++ b/src/server/bnetserver/REST/LoginRESTService.cpp @@ -90,7 +90,8 @@ bool LoginRESTService::Start(Trinity::Asio::IoContext* ioContext) return false; } - _externalAddress = *externalAddress; + _externalEndpoint = *externalAddress; + _externalHostname = Trinity::StringFormat("{}:{}", configuredAddress, _port); configuredAddress = sConfigMgr->GetStringDefault("LoginREST.LocalAddress", "127.0.0.1"); Optional<boost::asio::ip::tcp::endpoint> localAddress = resolver.Resolve(boost::asio::ip::tcp::v4(), configuredAddress, std::to_string(_port)); @@ -100,8 +101,8 @@ bool LoginRESTService::Start(Trinity::Asio::IoContext* ioContext) return false; } - _localAddress = *localAddress; - _localNetmask = Trinity::Net::GetDefaultNetmaskV4(_localAddress.address().to_v4()); + _localEndpoint = *localAddress; + _localHostname = Trinity::StringFormat("{}:{}", configuredAddress, _port); // set up form inputs Battlenet::JSON::Login::FormInput* input; @@ -135,17 +136,48 @@ void LoginRESTService::Stop() _thread.join(); } -boost::asio::ip::tcp::endpoint const& LoginRESTService::GetAddressForClient(boost::asio::ip::address const& address) const +boost::asio::ip::tcp::endpoint const& LoginRESTService::GetEndpointForClient(boost::asio::ip::address const& address) const { + std::array<boost::asio::ip::address, 2> addresses = std::array{ _externalEndpoint.address(), _localEndpoint.address() }; + if (auto addressIndex = Trinity::Net::SelectAddressForClient(address, addresses)) + { + switch (*addressIndex) + { + case 0: + return _externalEndpoint; + case 1: + return _localEndpoint; + default: + break; + } + } + if (address.is_loopback()) - return _localAddress; - else if (_localAddress.address().is_loopback()) - return _externalAddress; + return _localEndpoint; + + return _externalEndpoint; +} - if (Trinity::Net::IsInNetwork(_localAddress.address().to_v4(), _localNetmask, address.to_v4())) - return _localAddress; +std::string const& LoginRESTService::GetHostnameForClient(boost::asio::ip::address const& address) const +{ + std::array<boost::asio::ip::address, 2> addresses = std::array{ _externalEndpoint.address(), _localEndpoint.address() }; + if (auto addressIndex = Trinity::Net::SelectAddressForClient(address, addresses)) + { + switch (*addressIndex) + { + case 0: + return _externalHostname; + case 1: + return _localHostname; + default: + break; + } + } + + if (address.is_loopback()) + return _localHostname; - return _externalAddress; + return _externalHostname; } void LoginRESTService::Run() @@ -298,7 +330,7 @@ int32 LoginRESTService::HandleGetGameAccounts(std::shared_ptr<AsyncRequest> requ int32 LoginRESTService::HandleGetPortal(std::shared_ptr<AsyncRequest> request) { - boost::asio::ip::tcp::endpoint const& endpoint = GetAddressForClient(boost::asio::ip::address_v4(request->GetClient()->ip)); + boost::asio::ip::tcp::endpoint const& endpoint = GetEndpointForClient(boost::asio::ip::address_v4(request->GetClient()->ip)); std::string response = Trinity::StringFormat("{}:{}", endpoint.address().to_string(), sConfigMgr->GetIntDefault("BattlenetPort", 1119)); soap_response(request->GetClient(), SOAP_FILE); diff --git a/src/server/bnetserver/REST/LoginRESTService.h b/src/server/bnetserver/REST/LoginRESTService.h index e4d22dd9661..5c549fbf589 100644 --- a/src/server/bnetserver/REST/LoginRESTService.h +++ b/src/server/bnetserver/REST/LoginRESTService.h @@ -46,7 +46,8 @@ public: bool Start(Trinity::Asio::IoContext* ioContext); void Stop(); - boost::asio::ip::tcp::endpoint const& GetAddressForClient(boost::asio::ip::address const& address) const; + boost::asio::ip::tcp::endpoint const& GetEndpointForClient(boost::asio::ip::address const& address) const; + std::string const& GetHostnameForClient(boost::asio::ip::address const& address) const; private: void Run(); @@ -101,9 +102,10 @@ private: Battlenet::JSON::Login::FormInputs _formInputs; std::string _bindIP; int32 _port; - boost::asio::ip::tcp::endpoint _externalAddress; - boost::asio::ip::tcp::endpoint _localAddress; - boost::asio::ip::address_v4 _localNetmask; + std::string _externalHostname; + boost::asio::ip::tcp::endpoint _externalEndpoint; + std::string _localHostname; + boost::asio::ip::tcp::endpoint _localEndpoint; uint32 _loginTicketDuration; HttpMethodHandlerMap _getHandlers; diff --git a/src/server/bnetserver/Server/Session.cpp b/src/server/bnetserver/Server/Session.cpp index 806b371f79d..f98c429acf8 100644 --- a/src/server/bnetserver/Server/Session.cpp +++ b/src/server/bnetserver/Server/Session.cpp @@ -232,11 +232,11 @@ uint32 Battlenet::Session::HandleLogon(authentication::v1::LogonRequest const* l if (logonRequest->has_cached_web_credentials()) return VerifyWebCredentials(logonRequest->cached_web_credentials(), continuation); - boost::asio::ip::tcp::endpoint const& endpoint = sLoginService.GetAddressForClient(GetRemoteIpAddress()); + std::string const& hostname = sLoginService.GetHostnameForClient(GetRemoteIpAddress()); challenge::v1::ChallengeExternalRequest externalChallenge; externalChallenge.set_payload_type("web_auth_url"); - externalChallenge.set_payload(Trinity::StringFormat("https://{}:{}/bnetserver/login/", endpoint.address().to_string(), endpoint.port())); + externalChallenge.set_payload(Trinity::StringFormat("https://{}/bnetserver/login/", hostname)); Service<challenge::v1::ChallengeListener>(this).OnExternalChallenge(&externalChallenge); return ERROR_OK; } |