Skip to content

Commit 7b438d3

Browse files
committed
Core/Authserver: Add auth session timeout - socket is closed after 10s of inactivity if not authenticated or after 1 minute if authenticated
1 parent 4fb3bbe commit 7b438d3

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/server/authserver/Server/AuthSession.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "CryptoRandom.h"
2727
#include "DatabaseEnv.h"
2828
#include "IPLocation.h"
29+
#include "IoContext.h"
2930
#include "Log.h"
3031
#include "RealmList.h"
3132
#include "SecretMgr.h"
@@ -199,6 +200,7 @@ void AccountInfo::LoadResult(Field* fields)
199200
}
200201

201202
AuthSession::AuthSession(tcp::socket&& socket) : Socket(std::move(socket)),
203+
_timeout(*underlying_stream().get_executor().target<boost::asio::io_context::executor_type>()),
202204
_status(STATUS_CHALLENGE), _locale(LOCALE_enUS), _os(0), _build(0), _expversion(0), _timezoneOffset(0min)
203205
{
204206
}
@@ -250,6 +252,7 @@ void AuthSession::CheckIpCallback(PreparedQueryResult result)
250252
}
251253

252254
AsyncRead();
255+
SetTimeout();
253256
}
254257

255258
void AuthSession::ReadHandler()
@@ -290,6 +293,7 @@ void AuthSession::ReadHandler()
290293
}
291294

292295
packet.ReadCompleted(size);
296+
SetTimeout();
293297
}
294298

295299
AsyncRead();
@@ -898,3 +902,34 @@ bool AuthSession::VerifyVersion(std::span<uint8 const> a, Trinity::Crypto::SHA1:
898902

899903
return versionProof == version.GetDigest();
900904
}
905+
906+
void AuthSession::SetTimeout()
907+
{
908+
_timeout.cancel();
909+
910+
switch (_status)
911+
{
912+
case STATUS_AUTHED:
913+
case STATUS_WAITING_FOR_REALM_LIST:
914+
_timeout.expires_after(1min);
915+
break;
916+
case STATUS_XFER:
917+
return;
918+
default:
919+
_timeout.expires_after(10s);
920+
break;
921+
}
922+
923+
_timeout.async_wait([selfRef = weak_from_this()](boost::system::error_code const& error)
924+
{
925+
std::shared_ptr<AuthSession> self = selfRef.lock();
926+
if (!self)
927+
return;
928+
929+
if (error == boost::asio::error::operation_aborted)
930+
return;
931+
932+
TC_LOG_DEBUG("server.authserver", "{}:{} session timed out.", self->GetRemoteIpAddress().to_string(), self->GetRemotePort());
933+
self->CloseSocket();
934+
});
935+
}

src/server/authserver/Server/AuthSession.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Common.h"
2323
#include "CryptoHash.h"
2424
#include "DatabaseEnvFwd.h"
25+
#include "DeadlineTimer.h"
2526
#include "Duration.h"
2627
#include "Optional.h"
2728
#include "Socket.h"
@@ -92,11 +93,13 @@ class AuthSession : public Socket<AuthSession>
9293
void RealmListCallback(PreparedQueryResult result);
9394

9495
bool VerifyVersion(std::span<uint8 const> a, Trinity::Crypto::SHA1::Digest const& versionProof, bool isReconnect);
96+
void SetTimeout();
9597

9698
Optional<Trinity::Crypto::SRP6> _srp6;
9799
SessionKey _sessionKey = {};
98100
std::array<uint8, 16> _reconnectProof = {};
99101

102+
Trinity::Asio::DeadlineTimer _timeout;
100103
AuthStatus _status;
101104
AccountInfo _accountInfo;
102105
Optional<std::vector<uint8>> _totpSecret;

src/server/shared/Networking/Socket.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ class Socket : public std::enable_shared_from_this<T>
138138

139139
MessageBuffer& GetReadBuffer() { return _readBuffer; }
140140

141+
tcp::socket& underlying_stream()
142+
{
143+
return _socket;
144+
}
145+
141146
protected:
142147
virtual void OnClose() { }
143148

0 commit comments

Comments
 (0)