Skip to content

Commit

Permalink
Core/Auth: Moved expiring bans to background task - no longer blockin…
Browse files Browse the repository at this point in the history
…g queries during login by default running every minute (configurable)
  • Loading branch information
Shauren committed Feb 15, 2016
1 parent 7c7029c commit dfbb3be
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/server/authserver/Main.cpp
Expand Up @@ -73,6 +73,8 @@ variables_map GetConsoleArguments(int argc, char** argv, std::string& configFile
boost::asio::io_service* _ioService;
boost::asio::deadline_timer* _dbPingTimer;
uint32 _dbPingInterval;
boost::asio::deadline_timer* _banExpiryCheckTimer;
uint32 _banExpiryCheckInterval;
LoginDatabaseWorkerPool LoginDatabase;

int main(int argc, char** argv)
Expand Down Expand Up @@ -169,6 +171,11 @@ int main(int argc, char** argv)
_dbPingTimer->expires_from_now(boost::posix_time::minutes(_dbPingInterval));
_dbPingTimer->async_wait(KeepDatabaseAliveHandler);

_banExpiryCheckInterval = sConfigMgr->GetIntDefault("BanExpiryCheckInterval", 60);
_banExpiryCheckTimer = new boost::asio::deadline_timer(*_ioService);
_banExpiryCheckTimer->expires_from_now(boost::posix_time::seconds(_banExpiryCheckInterval));
_banExpiryCheckTimer->async_wait(BanExpiryHandler);

#if PLATFORM == PLATFORM_WINDOWS
if (m_ServiceStatus != -1)
{
Expand All @@ -192,6 +199,7 @@ int main(int argc, char** argv)

signals.cancel();

delete _banExpiryCheckTimer;
delete _dbPingTimer;
delete _ioService;
return 0;
Expand Down Expand Up @@ -242,6 +250,18 @@ void KeepDatabaseAliveHandler(const boost::system::error_code& error)
}
}

void BanExpiryHandler(boost::system::error_code const& error)
{
if (!error)
{
LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_DEL_EXPIRED_IP_BANS));
LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_UPD_EXPIRED_ACCOUNT_BANS));

_banExpiryCheckTimer->expires_from_now(boost::posix_time::seconds(_banExpiryCheckInterval));
_banExpiryCheckTimer->async_wait(BanExpiryHandler);
}
}

#if PLATFORM == PLATFORM_WINDOWS
void ServiceStatusWatcher(boost::system::error_code const& error)
{
Expand Down
7 changes: 0 additions & 7 deletions src/server/authserver/Server/AuthSession.cpp
Expand Up @@ -161,10 +161,6 @@ void AuthSession::Start()
std::string ip_address = GetRemoteIpAddress().to_string();
TC_LOG_TRACE("session", "Accepted connection from %s", ip_address.c_str());

// Remove expired ip ban if needed - login might fail for the first time
// but its better than allowing ourselves to be flooded by connections triggering blocking queries
LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_DEL_EXPIRED_IP_BANS));

PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_IP_INFO);
stmt->setString(0, ip_address);
stmt->setUInt32(1, inet_addr(ip_address.c_str()));
Expand Down Expand Up @@ -382,9 +378,6 @@ void AuthSession::LogonChallengeCallback(PreparedQueryResult result)
}
}

//set expired bans to inactive
LoginDatabase.DirectExecute(LoginDatabase.GetPreparedStatement(LOGIN_UPD_EXPIRED_ACCOUNT_BANS));

// If the account is banned, reject the logon attempt
if (_accountInfo.IsBanned)
{
Expand Down
7 changes: 7 additions & 0 deletions src/server/authserver/authserver.conf.dist
Expand Up @@ -130,6 +130,13 @@ WrongPass.BanType = 0

WrongPass.Logging = 0

#
# BanExpiryCheckInterval
# Description: Time (in seconds) between checks for expired bans
# Default: 60

BanExpiryCheckInterval = 60

#
###################################################################################################

Expand Down
Expand Up @@ -24,7 +24,7 @@ void LoginDatabaseConnection::DoPrepareStatements()

PrepareStatement(LOGIN_SEL_REALMLIST, "SELECT id, name, address, localAddress, localSubnetMask, port, icon, flag, timezone, allowedSecurityLevel, population, gamebuild FROM realmlist WHERE flag <> 3 ORDER BY name", CONNECTION_SYNCH);
PrepareStatement(LOGIN_DEL_EXPIRED_IP_BANS, "DELETE FROM ip_banned WHERE unbandate<>bandate AND unbandate<=UNIX_TIMESTAMP()", CONNECTION_ASYNC);
PrepareStatement(LOGIN_UPD_EXPIRED_ACCOUNT_BANS, "UPDATE account_banned SET active = 0 WHERE active = 1 AND unbandate<>bandate AND unbandate<=UNIX_TIMESTAMP()", CONNECTION_SYNCH);
PrepareStatement(LOGIN_UPD_EXPIRED_ACCOUNT_BANS, "UPDATE account_banned SET active = 0 WHERE active = 1 AND unbandate<>bandate AND unbandate<=UNIX_TIMESTAMP()", CONNECTION_ASYNC);
PrepareStatement(LOGIN_SEL_IP_INFO, "(SELECT unbandate > UNIX_TIMESTAMP() OR unbandate = bandate AS banned, NULL as country FROM ip_banned WHERE ip = ?) "
"UNION "
"(SELECT NULL AS banned, country FROM ip2nation WHERE INET_NTOA(ip) = ?)", CONNECTION_ASYNC);
Expand Down

0 comments on commit dfbb3be

Please sign in to comment.