Core/DBLayer: Sprinkle some async magic on worldserver auth/session load process
(cherry picked from commit 0e49eefe85790ac365c90202a2e22ce6b266ac31)
This commit is contained in:
1 parent
5e28a8e375
commit
517a30d7c1
10 files changed
+330
-171
No files matched your search
@@ -35,10 +35,62 @@ WorldSocket::WorldSocket(tcp::socket&& socket)
|
||||
|
||||
void WorldSocket::Start()
|
||||
{
|
||||
std::string ip_address = GetRemoteIpAddress().to_string();
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_IP_INFO);
|
||||
stmt->setString(0, ip_address);
|
||||
stmt->setUInt32(1, inet_addr(ip_address.c_str()));
|
||||
|
||||
_queryCallback = std::bind(&WorldSocket::CheckIpCallback, this, std::placeholders::_1);
|
||||
_queryFuture = LoginDatabase.AsyncQuery(stmt);
|
||||
}
|
||||
|
||||
void WorldSocket::CheckIpCallback(PreparedQueryResult result)
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
bool banned = false;
|
||||
do
|
||||
{
|
||||
Field* fields = result->Fetch();
|
||||
if (fields[0].GetUInt64() != 0)
|
||||
banned = true;
|
||||
|
||||
if (!fields[1].GetString().empty())
|
||||
_ipCountry = fields[1].GetString();
|
||||
|
||||
} while (result->NextRow());
|
||||
|
||||
if (banned)
|
||||
{
|
||||
SendAuthResponseError(AUTH_REJECT);
|
||||
TC_LOG_ERROR("network", "WorldSocket::CheckIpCallback: Sent Auth Response (IP %s banned).", GetRemoteIpAddress().to_string().c_str());
|
||||
DelayedCloseSocket();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
AsyncRead();
|
||||
HandleSendAuthSession();
|
||||
}
|
||||
|
||||
bool WorldSocket::Update()
|
||||
{
|
||||
if (!BaseSocket::Update())
|
||||
return false;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queryLock);
|
||||
if (_queryFuture.valid() && _queryFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
|
||||
{
|
||||
auto callback = std::move(_queryCallback);
|
||||
_queryCallback = nullptr;
|
||||
callback(_queryFuture.get());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void WorldSocket::HandleSendAuthSession()
|
||||
{
|
||||
WorldPacket packet(SMSG_AUTH_CHALLENGE, 37);
|
||||
@@ -145,6 +197,71 @@ bool WorldSocket::ReadHeaderHandler()
|
||||
return true;
|
||||
}
|
||||
|
||||
struct AuthSession
|
||||
{
|
||||
uint32 BattlegroupID = 0;
|
||||
uint32 LoginServerType = 0;
|
||||
uint32 RealmID = 0;
|
||||
uint32 Build = 0;
|
||||
uint32 LocalChallenge = 0;
|
||||
uint32 LoginServerID = 0;
|
||||
uint32 RegionID = 0;
|
||||
uint64 DosResponse = 0;
|
||||
uint8 Digest[SHA_DIGEST_LENGTH] = {};
|
||||
std::string Account;
|
||||
ByteBuffer AddonInfo;
|
||||
};
|
||||
|
||||
struct AccountInfo
|
||||
{
|
||||
uint32 Id;
|
||||
BigNumber SessionKey;
|
||||
std::string LastIP;
|
||||
bool IsLockedToIP;
|
||||
std::string LockCountry;
|
||||
uint8 Expansion;
|
||||
int64 MuteTime;
|
||||
LocaleConstant Locale;
|
||||
uint32 Recruiter;
|
||||
std::string OS;
|
||||
bool IsRectuiter;
|
||||
AccountTypes Security;
|
||||
bool IsBanned;
|
||||
|
||||
explicit AccountInfo(Field* fields)
|
||||
{
|
||||
// 0 1 2 3 4 5 6 7 8 9 10
|
||||
// SELECT a.id, a.sessionkey, a.last_ip, a.locked, a.lock_country, a.expansion, a.mutetime, a.locale, a.recruiter, a.os, aa.gmLevel,
|
||||
// 11 12
|
||||
// ab.unbandate > UNIX_TIMESTAMP() OR ab.unbandate = ab.bandate, r.id
|
||||
// FROM account a
|
||||
// LEFT JOIN account_access aa ON a.id = aa.id AND aa.RealmID IN (-1, ?)
|
||||
// LEFT JOIN account_banned ab ON a.id = ab.id
|
||||
// LEFT JOIN account r ON a.id = r.recruiter
|
||||
// WHERE a.username = ? ORDER BY aa.RealmID DESC LIMIT 1
|
||||
Id = fields[0].GetUInt32();
|
||||
SessionKey.SetHexStr(fields[1].GetCString());
|
||||
LastIP = fields[2].GetString();
|
||||
IsLockedToIP = fields[3].GetBool();
|
||||
LockCountry = fields[4].GetString();
|
||||
Expansion = fields[5].GetUInt8();
|
||||
MuteTime = fields[6].GetInt64();
|
||||
Locale = LocaleConstant(fields[7].GetUInt8());
|
||||
Recruiter = fields[8].GetUInt32();
|
||||
OS = fields[9].GetString();
|
||||
Security = AccountTypes(fields[10].GetUInt8());
|
||||
IsBanned = fields[11].GetUInt64() != 0;
|
||||
IsRectuiter = fields[12].GetUInt32() != 0;
|
||||
|
||||
uint32 world_expansion = sWorld->getIntConfig(CONFIG_EXPANSION);
|
||||
if (Expansion > world_expansion)
|
||||
Expansion = world_expansion;
|
||||
|
||||
if (Locale >= TOTAL_LOCALES)
|
||||
Locale = LOCALE_enUS;
|
||||
}
|
||||
};
|
||||
|
||||
bool WorldSocket::ReadDataHandler()
|
||||
{
|
||||
ClientPktHeader* header = reinterpret_cast<ClientPktHeader*>(_headerBuffer.GetReadPointer());
|
||||
@@ -256,48 +373,35 @@ void WorldSocket::SendPacket(WorldPacket const& packet)
|
||||
|
||||
void WorldSocket::HandleAuthSession(WorldPacket& recvPacket)
|
||||
{
|
||||
uint8 digest[SHA_DIGEST_LENGTH];
|
||||
uint32 clientSeed;
|
||||
uint8 security;
|
||||
uint32 id;
|
||||
LocaleConstant locale;
|
||||
std::string account;
|
||||
SHA1Hash sha;
|
||||
uint32 clientBuild;
|
||||
uint32 serverId, loginServerType, region, battlegroup, realmIndex;
|
||||
uint64 unk4;
|
||||
WorldPacket packet, SendAddonPacked;
|
||||
BigNumber k;
|
||||
bool wardenActive = sWorld->getBoolConfig(CONFIG_WARDEN_ENABLED);
|
||||
std::shared_ptr<AuthSession> authSession = std::make_shared<AuthSession>();
|
||||
|
||||
// Read the content of the packet
|
||||
recvPacket >> clientBuild;
|
||||
recvPacket >> serverId; // Used for GRUNT only
|
||||
recvPacket >> account;
|
||||
recvPacket >> loginServerType; // 0 GRUNT, 1 Battle.net
|
||||
recvPacket >> clientSeed;
|
||||
recvPacket >> region >> battlegroup; // Used for Battle.net only
|
||||
recvPacket >> realmIndex; // realmId from auth_database.realmlist table
|
||||
recvPacket >> unk4;
|
||||
recvPacket.read(digest, 20);
|
||||
|
||||
TC_LOG_DEBUG("network", "WorldSocket::HandleAuthSession: client %u, serverId %u, account %s, loginServerType %u, clientseed %u, realmIndex %u",
|
||||
clientBuild,
|
||||
serverId,
|
||||
account.c_str(),
|
||||
loginServerType,
|
||||
clientSeed,
|
||||
realmIndex);
|
||||
recvPacket >> authSession->Build;
|
||||
recvPacket >> authSession->LoginServerID;
|
||||
recvPacket >> authSession->Account;
|
||||
recvPacket >> authSession->LoginServerType;
|
||||
recvPacket >> authSession->LocalChallenge;
|
||||
recvPacket >> authSession->RegionID;
|
||||
recvPacket >> authSession->BattlegroupID;
|
||||
recvPacket >> authSession->RealmID; // realmId from auth_database.realmlist table
|
||||
recvPacket >> authSession->DosResponse;
|
||||
recvPacket.read(authSession->Digest, 20);
|
||||
authSession->AddonInfo.append(recvPacket.contents() + recvPacket.rpos(), recvPacket.size() - recvPacket.rpos());
|
||||
|
||||
// Get the account information from the auth database
|
||||
// 0 1 2 3 4 5 6 7 8
|
||||
// SELECT id, sessionkey, last_ip, locked, expansion, mutetime, locale, recruiter, os FROM account WHERE username = ?
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_INFO_BY_NAME);
|
||||
stmt->setInt32(0, int32(realmID));
|
||||
stmt->setString(1, authSession->Account);
|
||||
|
||||
stmt->setString(0, account);
|
||||
|
||||
PreparedQueryResult result = LoginDatabase.Query(stmt);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_queryLock);
|
||||
_queryCallback = std::bind(&WorldSocket::HandleAuthSessionCallback, this, authSession, std::placeholders::_1);
|
||||
_queryFuture = LoginDatabase.AsyncQuery(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
void WorldSocket::HandleAuthSessionCallback(std::shared_ptr<AuthSession> authSession, PreparedQueryResult result)
|
||||
{
|
||||
// Stop if the account is not found
|
||||
if (!result)
|
||||
{
|
||||
@@ -308,32 +412,20 @@ void WorldSocket::HandleAuthSession(WorldPacket& recvPacket)
|
||||
return;
|
||||
}
|
||||
|
||||
Field* fields = result->Fetch();
|
||||
|
||||
uint8 expansion = fields[4].GetUInt8();
|
||||
uint32 world_expansion = sWorld->getIntConfig(CONFIG_EXPANSION);
|
||||
if (expansion > world_expansion)
|
||||
expansion = world_expansion;
|
||||
AccountInfo account(result->Fetch());
|
||||
|
||||
// For hook purposes, we get Remoteaddress at this point.
|
||||
std::string address = GetRemoteIpAddress().to_string();
|
||||
|
||||
// As we don't know if attempted login process by ip works, we update last_attempt_ip right away
|
||||
stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LAST_ATTEMPT_IP);
|
||||
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LAST_ATTEMPT_IP);
|
||||
stmt->setString(0, address);
|
||||
stmt->setString(1, account);
|
||||
|
||||
stmt->setString(1, authSession->Account);
|
||||
LoginDatabase.Execute(stmt);
|
||||
// This also allows to check for possible "hack" attempts on account
|
||||
|
||||
// id has to be fetched at this point, so that first actual account response that fails can be logged
|
||||
id = fields[0].GetUInt32();
|
||||
|
||||
k.SetHexStr(fields[1].GetCString());
|
||||
|
||||
// even if auth credentials are bad, try using the session key we have - client cannot read auth response error without it
|
||||
_authCrypt.Init(&k);
|
||||
_authCrypt.Init(&account.SessionKey);
|
||||
|
||||
// First reject the connection if packet contains invalid data or realm state doesn't allow logging in
|
||||
if (sWorld->IsClosed())
|
||||
@@ -344,7 +436,7 @@ void WorldSocket::HandleAuthSession(WorldPacket& recvPacket)
|
||||
return;
|
||||
}
|
||||
|
||||
if (realmIndex != realmID)
|
||||
if (authSession->RealmID != realmID)
|
||||
{
|
||||
SendAuthResponseError(REALM_LIST_REALM_NOT_FOUND);
|
||||
TC_LOG_ERROR("network", "WorldSocket::HandleAuthSession: Sent Auth Response (bad realm).");
|
||||
@@ -352,13 +444,12 @@ void WorldSocket::HandleAuthSession(WorldPacket& recvPacket)
|
||||
return;
|
||||
}
|
||||
|
||||
std::string os = fields[8].GetString();
|
||||
|
||||
// Must be done before WorldSession is created
|
||||
if (wardenActive && os != "Win" && os != "OSX")
|
||||
bool wardenActive = sWorld->getBoolConfig(CONFIG_WARDEN_ENABLED);
|
||||
if (wardenActive && account.OS != "Win" && account.OS != "Wn64" && account.OS != "Mc64")
|
||||
{
|
||||
SendAuthResponseError(AUTH_REJECT);
|
||||
TC_LOG_ERROR("network", "WorldSocket::HandleAuthSession: Client %s attempted to log in using invalid client OS (%s).", address.c_str(), os.c_str());
|
||||
TC_LOG_ERROR("network", "WorldSocket::HandleAuthSession: Client %s attempted to log in using invalid client OS (%s).", address.c_str(), account.OS.c_str());
|
||||
DelayedCloseSocket();
|
||||
return;
|
||||
}
|
||||
@@ -366,135 +457,111 @@ void WorldSocket::HandleAuthSession(WorldPacket& recvPacket)
|
||||
// Check that Key and account name are the same on client and server
|
||||
uint32 t = 0;
|
||||
|
||||
sha.UpdateData(account);
|
||||
SHA1Hash sha;
|
||||
sha.UpdateData(authSession->Account);
|
||||
sha.UpdateData((uint8*)&t, 4);
|
||||
sha.UpdateData((uint8*)&clientSeed, 4);
|
||||
sha.UpdateData((uint8*)&authSession->LocalChallenge, 4);
|
||||
sha.UpdateData((uint8*)&_authSeed, 4);
|
||||
sha.UpdateBigNumbers(&k, NULL);
|
||||
sha.UpdateBigNumbers(&account.SessionKey, NULL);
|
||||
sha.Finalize();
|
||||
|
||||
if (memcmp(sha.GetDigest(), digest, SHA_DIGEST_LENGTH) != 0)
|
||||
if (memcmp(sha.GetDigest(), authSession->Digest, SHA_DIGEST_LENGTH) != 0)
|
||||
{
|
||||
SendAuthResponseError(AUTH_FAILED);
|
||||
TC_LOG_ERROR("network", "WorldSocket::HandleAuthSession: Authentication failed for account: %u ('%s') address: %s", id, account.c_str(), address.c_str());
|
||||
TC_LOG_ERROR("network", "WorldSocket::HandleAuthSession: Authentication failed for account: %u ('%s') address: %s", account.Id, authSession->Account.c_str(), address.c_str());
|
||||
DelayedCloseSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
///- Re-check ip locking (same check as in auth).
|
||||
if (fields[3].GetUInt8() == 1) // if ip is locked
|
||||
if (account.IsLockedToIP)
|
||||
{
|
||||
if (strcmp(fields[2].GetCString(), address.c_str()) != 0)
|
||||
if (account.LastIP != address)
|
||||
{
|
||||
SendAuthResponseError(AUTH_FAILED);
|
||||
TC_LOG_DEBUG("network", "WorldSocket::HandleAuthSession: Sent Auth Response (Account IP differs. Original IP: %s, new IP: %s).", fields[2].GetCString(), address.c_str());
|
||||
TC_LOG_DEBUG("network", "WorldSocket::HandleAuthSession: Sent Auth Response (Account IP differs. Original IP: %s, new IP: %s).", account.LastIP.c_str(), address.c_str());
|
||||
// We could log on hook only instead of an additional db log, however action logger is config based. Better keep DB logging as well
|
||||
sScriptMgr->OnFailedAccountLogin(id);
|
||||
sScriptMgr->OnFailedAccountLogin(account.Id);
|
||||
DelayedCloseSocket();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (!account.LockCountry.empty() && account.LockCountry != "00" && !_ipCountry.empty())
|
||||
{
|
||||
if (account.LockCountry != _ipCountry)
|
||||
{
|
||||
SendAuthResponseError(AUTH_FAILED);
|
||||
TC_LOG_DEBUG("network", "WorldSocket::HandleAuthSession: Sent Auth Response (Account country differs. Original country: %s, new country: %s).", account.LockCountry.c_str(), _ipCountry.c_str());
|
||||
// We could log on hook only instead of an additional db log, however action logger is config based. Better keep DB logging as well
|
||||
sScriptMgr->OnFailedAccountLogin(account.Id);
|
||||
DelayedCloseSocket();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int64 mutetime = fields[5].GetInt64();
|
||||
int64 mutetime = account.MuteTime;
|
||||
//! Negative mutetime indicates amount of seconds to be muted effective on next login - which is now.
|
||||
if (mutetime < 0)
|
||||
{
|
||||
mutetime = time(NULL) + llabs(mutetime);
|
||||
|
||||
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_MUTE_TIME_LOGIN);
|
||||
|
||||
stmt->setInt64(0, mutetime);
|
||||
stmt->setUInt32(1, id);
|
||||
|
||||
stmt->setUInt32(1, account.Id);
|
||||
LoginDatabase.Execute(stmt);
|
||||
}
|
||||
|
||||
locale = LocaleConstant(fields[6].GetUInt8());
|
||||
if (locale >= TOTAL_LOCALES)
|
||||
locale = LOCALE_enUS;
|
||||
|
||||
uint32 recruiter = fields[7].GetUInt32();
|
||||
// Checks gmlevel per Realm
|
||||
stmt = LoginDatabase.GetPreparedStatement(LOGIN_GET_GMLEVEL_BY_REALMID);
|
||||
|
||||
stmt->setUInt32(0, id);
|
||||
stmt->setInt32(1, int32(realmID));
|
||||
|
||||
result = LoginDatabase.Query(stmt);
|
||||
|
||||
if (!result)
|
||||
security = 0;
|
||||
else
|
||||
{
|
||||
fields = result->Fetch();
|
||||
security = fields[0].GetUInt8();
|
||||
}
|
||||
|
||||
// Re-check account ban (same check as in auth)
|
||||
stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BANS);
|
||||
|
||||
stmt->setUInt32(0, id);
|
||||
stmt->setString(1, address);
|
||||
|
||||
PreparedQueryResult banresult = LoginDatabase.Query(stmt);
|
||||
|
||||
if (banresult) // if account banned
|
||||
if (account.IsBanned)
|
||||
{
|
||||
SendAuthResponseError(AUTH_BANNED);
|
||||
TC_LOG_ERROR("network", "WorldSocket::HandleAuthSession: Sent Auth Response (Account banned).");
|
||||
sScriptMgr->OnFailedAccountLogin(id);
|
||||
sScriptMgr->OnFailedAccountLogin(account.Id);
|
||||
DelayedCloseSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check locked state for server
|
||||
AccountTypes allowedAccountType = sWorld->GetPlayerSecurityLimit();
|
||||
TC_LOG_DEBUG("network", "Allowed Level: %u Player Level %u", allowedAccountType, AccountTypes(security));
|
||||
if (allowedAccountType > SEC_PLAYER && AccountTypes(security) < allowedAccountType)
|
||||
TC_LOG_DEBUG("network", "Allowed Level: %u Player Level %u", allowedAccountType, account.Security);
|
||||
if (allowedAccountType > SEC_PLAYER && account.Security < allowedAccountType)
|
||||
{
|
||||
SendAuthResponseError(AUTH_UNAVAILABLE);
|
||||
TC_LOG_DEBUG("network", "WorldSocket::HandleAuthSession: User tries to login but his security level is not enough");
|
||||
sScriptMgr->OnFailedAccountLogin(id);
|
||||
sScriptMgr->OnFailedAccountLogin(account.Id);
|
||||
DelayedCloseSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
TC_LOG_DEBUG("network", "WorldSocket::HandleAuthSession: Client '%s' authenticated successfully from %s.",
|
||||
account.c_str(),
|
||||
address.c_str());
|
||||
|
||||
// Check if this user is by any chance a recruiter
|
||||
stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_RECRUITER);
|
||||
|
||||
stmt->setUInt32(0, id);
|
||||
|
||||
result = LoginDatabase.Query(stmt);
|
||||
|
||||
bool isRecruiter = false;
|
||||
if (result)
|
||||
isRecruiter = true;
|
||||
TC_LOG_DEBUG("network", "WorldSocket::HandleAuthSession: Client '%s' authenticated successfully from %s.", authSession->Account.c_str(), address.c_str());
|
||||
|
||||
// Update the last_ip in the database as it was successful for login
|
||||
stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LAST_IP);
|
||||
|
||||
stmt->setString(0, address);
|
||||
stmt->setString(1, account);
|
||||
stmt->setString(1, authSession->Account);
|
||||
|
||||
LoginDatabase.Execute(stmt);
|
||||
|
||||
// At this point, we can safely hook a successful login
|
||||
sScriptMgr->OnAccountLogin(id);
|
||||
sScriptMgr->OnAccountLogin(account.Id);
|
||||
|
||||
_authed = true;
|
||||
_worldSession = new WorldSession(id, shared_from_this(), AccountTypes(security), expansion, mutetime, locale, recruiter, isRecruiter);
|
||||
_worldSession->LoadGlobalAccountData();
|
||||
_worldSession->LoadTutorialsData();
|
||||
_worldSession->ReadAddonsInfo(recvPacket);
|
||||
_worldSession->LoadPermissions();
|
||||
_worldSession = new WorldSession(account.Id, std::move(authSession->Account), shared_from_this(), account.Security,
|
||||
account.Expansion, mutetime, account.Locale, account.Recruiter, account.IsRectuiter);
|
||||
_worldSession->ReadAddonsInfo(authSession->AddonInfo);
|
||||
|
||||
// Initialize Warden system only if it is enabled by config
|
||||
if (wardenActive)
|
||||
_worldSession->InitWarden(&k, os);
|
||||
_worldSession->InitWarden(&account.SessionKey, account.OS);
|
||||
|
||||
_queryCallback = std::bind(&WorldSocket::LoadSessionPermissionsCallback, this, std::placeholders::_1);
|
||||
_queryFuture = _worldSession->LoadPermissionsAsync();
|
||||
}
|
||||
|
||||
void WorldSocket::LoadSessionPermissionsCallback(PreparedQueryResult result)
|
||||
{
|
||||
// RBAC must be loaded before adding session to check for skip queue permission
|
||||
_worldSession->GetRBACData()->LoadFromDBCallback(result);
|
||||
|
||||
sWorld->AddSession(_worldSession);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user