Skip to content

Commit

Permalink
tweak(server): apply rate limiter to reassembled events as well
Browse files Browse the repository at this point in the history
  • Loading branch information
blattersturm committed Dec 13, 2020
1 parent 152b5d6 commit 1f30060
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Expand Up @@ -13,10 +13,15 @@ namespace fx
class Resource;
class ResourceManager;

class EventReassemblySink
class CRC_EXPORT EventReassemblySink
{
public:
virtual void SendPacket(int target, std::string_view data) = 0;

virtual bool LimitEvent(int source)
{
return false;
}
};

class CRC_EXPORT EventReassemblyComponent : public fwRefCountable, public IAttached<ResourceManager>
Expand Down
Expand Up @@ -310,6 +310,11 @@ void EventReassemblyComponentImpl::HandleReceivedPacket(int source, const std::s
// get the resource manager and eventing component
fwRefContainer<fx::ResourceEventManagerComponent> eventManager = m_resourceManager->GetComponent<fx::ResourceEventManagerComponent>();

if (m_sink->LimitEvent(source))
{
return;
}

// and queue the event
eventManager->QueueEvent(
std::string{ eventName },
Expand Down
27 changes: 27 additions & 0 deletions code/components/citizen-server-impl/src/ServerResources.cpp
Expand Up @@ -313,6 +313,33 @@ static class : public fx::EventReassemblySink
}
}

virtual bool LimitEvent(int source) override
{
static fx::RateLimiterStore<uint32_t, false> netEventRateLimiterStore{ instance->GetComponent<console::Context>().GetRef() };
static auto netEventRateLimiter = netEventRateLimiterStore.GetRateLimiter("netEvent", fx::RateLimiterDefaults{ 50.f, 200.f });
static auto netFloodRateLimiter = netEventRateLimiterStore.GetRateLimiter("netEventFlood", fx::RateLimiterDefaults{ 75.f, 300.f });

if (!netEventRateLimiter->Consume(source))
{
if (!netFloodRateLimiter->Consume(source))
{
gscomms_execute_callback_on_main_thread([this, source]()
{
auto client = instance->GetComponent<fx::ClientRegistry>()->GetClientByNetID(source);

if (client)
{
instance->GetComponent<fx::GameServer>()->DropClient(client, "Unreliable network event overflow.");
}
});
}

return true;
}

return false;
}

public:
fx::ServerInstanceBase* instance;
} g_reassemblySink;
Expand Down

0 comments on commit 1f30060

Please sign in to comment.