From 7f0143607e65f55fbd7b3d7bcbe8c803462965a4 Mon Sep 17 00:00:00 2001 From: ratkosrb Date: Sat, 8 Feb 2025 18:53:25 +0200 Subject: [PATCH] Disconnect idle connections that don't login. --- src/framework/Network/MangosSocket.h | 9 ++++++--- src/framework/Network/MangosSocketImpl.h | 10 +++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/framework/Network/MangosSocket.h b/src/framework/Network/MangosSocket.h index 11272472336..cbba90fbd2b 100644 --- a/src/framework/Network/MangosSocket.h +++ b/src/framework/Network/MangosSocket.h @@ -121,12 +121,12 @@ class MangosSocket : public WorldHandler virtual int close(int); // Get address of connected peer. - const std::string& GetRemoteAddress () const { return m_address; } + std::string const& GetRemoteAddress () const { return m_address; } // Send A packet on the socket, this function is reentrant. // @param pct packet to send // @return -1 of failure - int SendPacket (const WorldPacket& pct); + int SendPacket (WorldPacket const& pct); // Add reference to this object. long AddReference() { return static_cast(add_reference()); } @@ -179,7 +179,10 @@ class MangosSocket : public WorldHandler // to mark the socket for output ). bool iFlushPacketQueue (); - // Time in which the last ping was received + // Time at which the socket was created. + ACE_Time_Value m_createTime; + + // Time at which the last ping was received. ACE_Time_Value m_lastPingTime; // Keep track of over-speed pings ,to prevent ping flood. diff --git a/src/framework/Network/MangosSocketImpl.h b/src/framework/Network/MangosSocketImpl.h index c276f938e2c..554b388129d 100644 --- a/src/framework/Network/MangosSocketImpl.h +++ b/src/framework/Network/MangosSocketImpl.h @@ -24,6 +24,7 @@ template MangosSocket::MangosSocket() : WorldHandler(), + m_createTime(ACE_OS::gettimeofday()), m_lastPingTime(ACE_Time_Value::zero), m_overSpeedPings(0), m_session(0), @@ -77,7 +78,7 @@ void MangosSocket::CloseSocket(void) } template -int MangosSocket::SendPacket(const WorldPacket& pct) +int MangosSocket::SendPacket(WorldPacket const& pct) { GuardType lock(m_outBufferLock); @@ -283,6 +284,13 @@ int MangosSocket::Update(void) { if (closing_) return -1; + + if (!m_session && m_isServerSocket && ((ACE_OS::gettimeofday() - m_createTime) > ACE_Time_Value(10))) + { + sLog.Out(LOG_BASIC, LOG_LVL_DETAIL, "Disconnecting idle connection from %s.", m_address.c_str()); + CloseSocket(); + return -1; + } if (m_outActive || m_outBuffer->length() == 0) return 0;