Core/Network: Refactor local/remote ip address selection code and allow using hostnames in LoginREST bnetserver config options

This commit is contained in:
Shauren
2023-07-15 00:45:16 +02:00
parent cdfaecda52
commit 6be536a73b
13 changed files with 384 additions and 91 deletions

View File

@@ -31,31 +31,24 @@ void Realm::SetName(std::string name)
boost::asio::ip::address Realm::GetAddressForClient(boost::asio::ip::address const& clientAddr) const
{
boost::asio::ip::address realmIp;
// Attempt to send best address for client
if (clientAddr.is_loopback())
std::array<boost::asio::ip::address, 2> addresses = std::array{ *ExternalAddress, * LocalAddress };
if (auto addressIndex = Trinity::Net::SelectAddressForClient(clientAddr, addresses))
{
// Try guessing if realm is also connected locally
if (LocalAddress->is_loopback() || ExternalAddress->is_loopback())
realmIp = clientAddr;
else
switch (*addressIndex)
{
// Assume that user connecting from the machine that bnetserver is located on
// has all realms available in his local network
realmIp = *LocalAddress;
case 0:
return *ExternalAddress;
case 1:
return *LocalAddress;
default:
break;
}
}
else
{
if (clientAddr.is_v4() && Trinity::Net::IsInNetwork(LocalAddress->to_v4(), LocalSubnetMask->to_v4(), clientAddr.to_v4()))
realmIp = *LocalAddress;
else
realmIp = *ExternalAddress;
}
// Return external IP
return realmIp;
if (clientAddr.is_loopback())
return *LocalAddress;
return *ExternalAddress;
}
uint32 Realm::GetConfigId() const

View File

@@ -94,7 +94,7 @@ void RealmList::LoadBuildInfo()
}
void RealmList::UpdateRealm(Realm& realm, Battlenet::RealmHandle const& id, uint32 build, std::string const& name,
boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr, boost::asio::ip::address&& localSubmask,
boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr,
uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel,
float population)
{
@@ -111,8 +111,6 @@ void RealmList::UpdateRealm(Realm& realm, Battlenet::RealmHandle const& id, uint
realm.ExternalAddress = std::make_unique<boost::asio::ip::address>(std::move(address));
if (!realm.LocalAddress || *realm.LocalAddress != localAddr)
realm.LocalAddress = std::make_unique<boost::asio::ip::address>(std::move(localAddr));
if (!realm.LocalSubnetMask || *realm.LocalSubnetMask != localSubmask)
realm.LocalSubnetMask = std::make_unique<boost::asio::ip::address>(std::move(localSubmask));
realm.Port = port;
}
@@ -145,7 +143,6 @@ void RealmList::UpdateRealms(boost::system::error_code const& error)
std::string name = fields[1].GetString();
std::string externalAddressString = fields[2].GetString();
std::string localAddressString = fields[3].GetString();
std::string localSubmaskString = fields[4].GetString();
Optional<boost::asio::ip::tcp::endpoint> externalAddress = _resolver->Resolve(boost::asio::ip::tcp::v4(), externalAddressString, "");
if (!externalAddress)
@@ -161,30 +158,23 @@ void RealmList::UpdateRealms(boost::system::error_code const& error)
continue;
}
Optional<boost::asio::ip::tcp::endpoint> localSubmask = _resolver->Resolve(boost::asio::ip::tcp::v4(), localSubmaskString, "");
if (!localSubmask)
{
TC_LOG_ERROR("realmlist", "Could not resolve localSubnetMask {} for realm \"{}\" id {}", localSubmaskString, name, realmId);
continue;
}
uint16 port = fields[5].GetUInt16();
uint8 icon = fields[6].GetUInt8();
uint16 port = fields[4].GetUInt16();
uint8 icon = fields[5].GetUInt8();
if (icon == REALM_TYPE_FFA_PVP)
icon = REALM_TYPE_PVP;
if (icon >= MAX_CLIENT_REALM_TYPE)
icon = REALM_TYPE_NORMAL;
RealmFlags flag = RealmFlags(fields[7].GetUInt8());
uint8 timezone = fields[8].GetUInt8();
uint8 allowedSecurityLevel = fields[9].GetUInt8();
float pop = fields[10].GetFloat();
uint32 build = fields[11].GetUInt32();
uint8 region = fields[12].GetUInt8();
uint8 battlegroup = fields[13].GetUInt8();
RealmFlags flag = RealmFlags(fields[6].GetUInt8());
uint8 timezone = fields[7].GetUInt8();
uint8 allowedSecurityLevel = fields[8].GetUInt8();
float pop = fields[9].GetFloat();
uint32 build = fields[10].GetUInt32();
uint8 region = fields[11].GetUInt8();
uint8 battlegroup = fields[12].GetUInt8();
Battlenet::RealmHandle id{ region, battlegroup, realmId };
UpdateRealm(newRealms[id], id, build, name, externalAddress->address(), localAddress->address(), localSubmask->address(), port, icon,
UpdateRealm(newRealms[id], id, build, name, externalAddress->address(), localAddress->address(), port, icon,
flag, timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop);
newSubRegions.insert(Battlenet::RealmHandle{ region, battlegroup, 0 }.GetAddressString());

View File

@@ -98,7 +98,7 @@ private:
void LoadBuildInfo();
void UpdateRealms(boost::system::error_code const& error);
void UpdateRealm(Realm& realm, Battlenet::RealmHandle const& id, uint32 build, std::string const& name,
boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr, boost::asio::ip::address&& localSubmask,
boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr,
uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population);
std::vector<RealmBuildInfo> _builds;