From 60865b7dbf14f4918e9697ad253ea46cdc61dfe5 Mon Sep 17 00:00:00 2001 From: def Date: Sun, 4 Aug 2013 17:50:12 +0200 Subject: [PATCH] More advanced netlimit --- src/engine/server/server.cpp | 19 +++++++++---------- src/engine/server/server.h | 2 +- src/engine/shared/config_variables.h | 3 ++- src/engine/shared/network_server.cpp | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 6c2d97c74..ad2362ad7 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -848,20 +848,19 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket) if(g_Config.m_SvNetlimit && Msg != NETMSG_REQUEST_MAP_DATA) { int64 Now = time_get(); + int64 Diff = Now - m_aClients[ClientID].m_TrafficSince; + float Alpha = g_Config.m_SvNetlimitAlpha / 100.0; + float Limit = (float) g_Config.m_SvNetlimit * 1024 / time_freq(); - if (Now - m_aClients[ClientID].m_TrafficSince > time_freq() * 5) + if (m_aClients[ClientID].m_Traffic > Limit) { - m_aClients[ClientID].m_Traffic = 0; - m_aClients[ClientID].m_TrafficSince = Now; + m_NetServer.NetBan()->BanAddr(&pPacket->m_Address, 60, "Stressing network"); + return; } - else + if (Diff > 100) { - if ((Now - m_aClients[ClientID].m_TrafficSince) / time_freq() > 0 && m_aClients[ClientID].m_Traffic / ((Now - m_aClients[ClientID].m_TrafficSince) / time_freq()) > g_Config.m_SvNetlimit * 1024) - { - m_NetServer.NetBan()->BanAddr(&pPacket->m_Address, 60, "Stressing network"); - return; - } - m_aClients[ClientID].m_Traffic += pPacket->m_DataSize; + m_aClients[ClientID].m_Traffic = (Alpha * ((float) pPacket->m_DataSize / Diff)) + (1.0 - Alpha) * m_aClients[ClientID].m_Traffic; + m_aClients[ClientID].m_TrafficSince = Now; } } diff --git a/src/engine/server/server.h b/src/engine/server/server.h index 1b78ed263..68172cc93 100644 --- a/src/engine/server/server.h +++ b/src/engine/server/server.h @@ -118,7 +118,7 @@ public: int m_Latency; int m_SnapRate; - int64 m_Traffic; + float m_Traffic; int64 m_TrafficSince; int m_LastAckedSnapshot; diff --git a/src/engine/shared/config_variables.h b/src/engine/shared/config_variables.h index 56b29b8d0..6b515bdcc 100644 --- a/src/engine/shared/config_variables.h +++ b/src/engine/shared/config_variables.h @@ -222,6 +222,7 @@ MACRO_CONFIG_INT(SvGlobalBantime, sv_global_ban_time, 60, 0, 1440, CFGFLAG_SERVE MACRO_CONFIG_INT(SvEvents, sv_events, 1, 0, 1, CFGFLAG_SERVER, "Enable triggering of server events, like the happy eyeemotes on some holidays.") // netlimit -MACRO_CONFIG_INT(SvNetlimit, sv_netlimit, 10, 0, 1000, CFGFLAG_SERVER, "Netlimit: Maximum amount of traffic a client is allowed to use (in kb/s)") +MACRO_CONFIG_INT(SvNetlimit, sv_netlimit, 500, 0, 10000, CFGFLAG_SERVER, "Netlimit: Maximum amount of traffic a client is allowed to use (in kb/s)") +MACRO_CONFIG_INT(SvNetlimitAlpha, sv_netlimit_alpha, 50, 1, 100, CFGFLAG_SERVER, "Netlimit: Alpha of Exponention moving average") #endif diff --git a/src/engine/shared/network_server.cpp b/src/engine/shared/network_server.cpp index 0e0f5eda0..9050cfc39 100644 --- a/src/engine/shared/network_server.cpp +++ b/src/engine/shared/network_server.cpp @@ -76,7 +76,7 @@ int CNetServer::Update() if(m_aSlots[i].m_Connection.State() == NET_CONNSTATE_ERROR) { if (Now - m_aSlots[i].m_Connection.ConnectTime() < time_freq() && NetBan()) - NetBan()->BanAddr(ClientAddr(i), 60, "Stressing network"); + NetBan()->BanAddr(ClientAddr(i), 60, "Too many connections"); else Drop(i, m_aSlots[i].m_Connection.ErrorString()); }