diff options
-rw-r--r-- | src/common/Asio/IpNetwork.cpp | 2 | ||||
-rw-r--r-- | src/common/Asio/IpNetwork.h | 2 | ||||
-rw-r--r-- | src/server/bnetserver/REST/LoginRESTService.cpp | 63 | ||||
-rw-r--r-- | src/server/bnetserver/REST/LoginRESTService.h | 8 | ||||
-rw-r--r-- | src/server/bnetserver/Server/Session.cpp | 4 |
5 files changed, 20 insertions, 59 deletions
diff --git a/src/common/Asio/IpNetwork.cpp b/src/common/Asio/IpNetwork.cpp index 9e5ac1f03e5..5d7948e847c 100644 --- a/src/common/Asio/IpNetwork.cpp +++ b/src/common/Asio/IpNetwork.cpp @@ -67,7 +67,7 @@ bool IsInNetwork(boost::asio::ip::network_v6 const& network, boost::asio::ip::ad return endpointAsNetwork.is_subnet_of(network); } -Optional<std::size_t> SelectAddressForClient(boost::asio::ip::address const& clientAddress, std::span<boost::asio::ip::address> const& addresses) +Optional<std::size_t> SelectAddressForClient(boost::asio::ip::address const& clientAddress, std::span<boost::asio::ip::address const> const& addresses) { Optional<std::size_t> localIpv6Index; Optional<std::size_t> externalIpv6Index; diff --git a/src/common/Asio/IpNetwork.h b/src/common/Asio/IpNetwork.h index 242c344fc15..08db7d49f84 100644 --- a/src/common/Asio/IpNetwork.h +++ b/src/common/Asio/IpNetwork.h @@ -31,7 +31,7 @@ TC_COMMON_API bool IsInNetwork(boost::asio::ip::network_v4 const& network, boost TC_COMMON_API bool IsInNetwork(boost::asio::ip::network_v6 const& network, boost::asio::ip::address_v6 const& clientAddress); -TC_COMMON_API Optional<std::size_t> SelectAddressForClient(boost::asio::ip::address const& clientAddress, std::span<boost::asio::ip::address> const& addresses); +TC_COMMON_API Optional<std::size_t> SelectAddressForClient(boost::asio::ip::address const& clientAddress, std::span<boost::asio::ip::address const> const& addresses); TC_COMMON_API void ScanLocalNetworks(); } diff --git a/src/server/bnetserver/REST/LoginRESTService.cpp b/src/server/bnetserver/REST/LoginRESTService.cpp index 6e260b4df56..6cfdcb1324f 100644 --- a/src/server/bnetserver/REST/LoginRESTService.cpp +++ b/src/server/bnetserver/REST/LoginRESTService.cpp @@ -82,27 +82,25 @@ bool LoginRESTService::Start(Trinity::Asio::IoContext* ioContext) Trinity::Asio::Resolver resolver(*ioContext); - std::string configuredAddress = sConfigMgr->GetStringDefault("LoginREST.ExternalAddress", "127.0.0.1"); - Optional<boost::asio::ip::tcp::endpoint> externalAddress = resolver.Resolve(boost::asio::ip::tcp::v4(), configuredAddress, std::to_string(_port)); + _hostnames[0] = sConfigMgr->GetStringDefault("LoginREST.ExternalAddress", "127.0.0.1"); + Optional<boost::asio::ip::tcp::endpoint> externalAddress = resolver.Resolve(boost::asio::ip::tcp::v4(), _hostnames[0], std::to_string(_port)); if (!externalAddress) { - TC_LOG_ERROR("server.rest", "Could not resolve LoginREST.ExternalAddress {}", configuredAddress); + TC_LOG_ERROR("server.rest", "Could not resolve LoginREST.ExternalAddress {}", _hostnames[0]); return false; } - _externalEndpoint = *externalAddress; - _externalHostname = Trinity::StringFormat("{}:{}", configuredAddress, _port); + _addresses[0] = externalAddress->address(); - 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)); + _hostnames[1] = sConfigMgr->GetStringDefault("LoginREST.LocalAddress", "127.0.0.1"); + Optional<boost::asio::ip::tcp::endpoint> localAddress = resolver.Resolve(boost::asio::ip::tcp::v4(), _hostnames[1], std::to_string(_port)); if (!localAddress) { - TC_LOG_ERROR("server.rest", "Could not resolve LoginREST.LocalAddress {}", configuredAddress); + TC_LOG_ERROR("server.rest", "Could not resolve LoginREST.LocalAddress {}", _hostnames[1]); return false; } - _localEndpoint = *localAddress; - _localHostname = Trinity::StringFormat("{}:{}", configuredAddress, _port); + _addresses[1] = localAddress->address(); // set up form inputs Battlenet::JSON::Login::FormInput* input; @@ -136,48 +134,15 @@ void LoginRESTService::Stop() _thread.join(); } -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 _localEndpoint; - - return _externalEndpoint; -} - 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 (auto addressIndex = Trinity::Net::SelectAddressForClient(address, _addresses)) + return _hostnames[*addressIndex]; if (address.is_loopback()) - return _localHostname; + return _hostnames[0]; - return _externalHostname; + return _hostnames[1]; } void LoginRESTService::Run() @@ -330,8 +295,8 @@ int32 LoginRESTService::HandleGetGameAccounts(std::shared_ptr<AsyncRequest> requ int32 LoginRESTService::HandleGetPortal(std::shared_ptr<AsyncRequest> request) { - 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)); + std::string const& hostname = GetHostnameForClient(boost::asio::ip::address_v4(request->GetClient()->ip)); + std::string response = Trinity::StringFormat("{}:{}", hostname, sConfigMgr->GetIntDefault("BattlenetPort", 1119)); soap_response(request->GetClient(), SOAP_FILE); soap_send_raw(request->GetClient(), response.c_str(), response.length()); diff --git a/src/server/bnetserver/REST/LoginRESTService.h b/src/server/bnetserver/REST/LoginRESTService.h index 5c549fbf589..f783fea243a 100644 --- a/src/server/bnetserver/REST/LoginRESTService.h +++ b/src/server/bnetserver/REST/LoginRESTService.h @@ -46,8 +46,8 @@ public: bool Start(Trinity::Asio::IoContext* ioContext); void Stop(); - 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; + int32 GetPort() const { return _port; } private: void Run(); @@ -102,10 +102,8 @@ private: Battlenet::JSON::Login::FormInputs _formInputs; std::string _bindIP; int32 _port; - std::string _externalHostname; - boost::asio::ip::tcp::endpoint _externalEndpoint; - std::string _localHostname; - boost::asio::ip::tcp::endpoint _localEndpoint; + std::array<std::string, 2> _hostnames; + std::array<boost::asio::ip::address, 2> _addresses; uint32 _loginTicketDuration; HttpMethodHandlerMap _getHandlers; diff --git a/src/server/bnetserver/Server/Session.cpp b/src/server/bnetserver/Server/Session.cpp index f98c429acf8..6e5b16cd477 100644 --- a/src/server/bnetserver/Server/Session.cpp +++ b/src/server/bnetserver/Server/Session.cpp @@ -232,11 +232,9 @@ uint32 Battlenet::Session::HandleLogon(authentication::v1::LogonRequest const* l if (logonRequest->has_cached_web_credentials()) return VerifyWebCredentials(logonRequest->cached_web_credentials(), continuation); - 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/", hostname)); + externalChallenge.set_payload(Trinity::StringFormat("https://{}:{}/bnetserver/login/", sLoginService.GetHostnameForClient(GetRemoteIpAddress()), sLoginService.GetPort())); Service<challenge::v1::ChallengeListener>(this).OnExternalChallenge(&externalChallenge); return ERROR_OK; } |