mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Rate-limit client connection attempts
This commit is contained in:
parent
050152eb90
commit
b2f0a37b18
2 changed files with 32 additions and 1 deletions
|
@ -44,6 +44,8 @@ namespace con
|
|||
// TODO: Clean this up.
|
||||
#define LOG(a) a
|
||||
|
||||
#define MAX_NEW_PEERS_PER_SEC 30
|
||||
|
||||
static inline session_t readPeerId(const u8 *packetdata)
|
||||
{
|
||||
return readU16(&packetdata[4]);
|
||||
|
@ -971,7 +973,19 @@ void ConnectionReceiveThread::receive(SharedBuffer<u8> &packetdata,
|
|||
|
||||
// Someone new is trying to talk to us. Add them.
|
||||
if (peer_id == PEER_ID_INEXISTENT) {
|
||||
peer_id = m_connection->createPeer(sender, MTP_MINETEST_RELIABLE_UDP, 0);
|
||||
auto &l = m_new_peer_ratelimit;
|
||||
l.tick();
|
||||
if (++l.counter > MAX_NEW_PEERS_PER_SEC) {
|
||||
if (!l.logged) {
|
||||
warningstream << m_connection->getDesc()
|
||||
<< "Receive(): More than " << MAX_NEW_PEERS_PER_SEC
|
||||
<< " new clients within 1s. Throttling." << std::endl;
|
||||
}
|
||||
l.logged = true;
|
||||
// We simply drop the packet, the client can try again.
|
||||
} else {
|
||||
peer_id = m_connection->createPeer(sender, MTP_MINETEST_RELIABLE_UDP, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue