Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add packet limiter for incoming packets #3733

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/conf/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class Configuration implements ProxyConfig
private boolean preventProxyConnections;
private boolean forgeSupport;
private boolean rejectTransfers;
private int maxPacketsPerSecond = 2000;

public void load()
{
Expand Down Expand Up @@ -105,6 +106,7 @@ public void load()
preventProxyConnections = adapter.getBoolean( "prevent_proxy_connections", preventProxyConnections );
forgeSupport = adapter.getBoolean( "forge_support", forgeSupport );
rejectTransfers = adapter.getBoolean( "reject_transfers", rejectTransfers );
maxPacketsPerSecond = adapter.getInt( "max_packets_per_second", maxPacketsPerSecond );

disabledCommands = new CaseInsensitiveSet( (Collection<String>) adapter.getList( "disabled_commands", Arrays.asList( "disabledcommandhere" ) ) );

Expand Down
9 changes: 9 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.logging.Level;
import lombok.Setter;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.connection.CancelSendSignal;
import net.md_5.bungee.connection.InitialHandler;
Expand All @@ -18,6 +19,7 @@
import net.md_5.bungee.protocol.OverflowPacketException;
import net.md_5.bungee.protocol.PacketWrapper;
import net.md_5.bungee.protocol.Protocol;
import net.md_5.bungee.util.PacketLimiter;
import net.md_5.bungee.util.QuietException;

/**
Expand All @@ -28,6 +30,8 @@
public class HandlerBoss extends ChannelInboundHandlerAdapter
{

@Setter
private PacketLimiter limiter;
private ChannelWrapper channel;
private PacketHandler handler;
private boolean healthCheck;
Expand Down Expand Up @@ -80,6 +84,11 @@ public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exceptio
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
{
if ( limiter != null )
{
limiter.received();
}

if ( msg instanceof HAProxyMessage )
{
HAProxyMessage proxy = (HAProxyMessage) msg;
Expand Down
10 changes: 9 additions & 1 deletion proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import net.md_5.bungee.protocol.Varint21FrameDecoder;
import net.md_5.bungee.protocol.Varint21LengthFieldExtraBufPrepender;
import net.md_5.bungee.protocol.Varint21LengthFieldPrepender;
import net.md_5.bungee.util.PacketLimiter;

public class PipelineUtils
{
Expand Down Expand Up @@ -82,7 +83,14 @@ protected void initChannel(Channel ch) throws Exception
ch.pipeline().addAfter( FRAME_DECODER, PACKET_DECODER, new MinecraftDecoder( Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion() ) );
ch.pipeline().addAfter( FRAME_PREPENDER, PACKET_ENCODER, new MinecraftEncoder( Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion() ) );
ch.pipeline().addBefore( FRAME_PREPENDER, LEGACY_KICKER, legacyKicker );
ch.pipeline().get( HandlerBoss.class ).setHandler( new InitialHandler( BungeeCord.getInstance(), listener ) );

HandlerBoss handlerBoss = ch.pipeline().get( HandlerBoss.class );
handlerBoss.setHandler( new InitialHandler( BungeeCord.getInstance(), listener ) );
int packetLimit = BungeeCord.getInstance().getConfig().getMaxPacketsPerSecond();
if ( packetLimit > 0 )
{
handlerBoss.setLimiter( new PacketLimiter( packetLimit ) );
}

if ( listener.isProxyProtocol() )
{
Expand Down
26 changes: 26 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.md_5.bungee.util;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class PacketLimiter
{
// max amount of packets allowed per second
private final int limit;
private int counter;
private long nextSecond;

public void received()
{
if ( ++counter == limit )
{
long now = System.currentTimeMillis();
if ( nextSecond > now )
{
throw new QuietException( "exceeded packet limit" );
}
nextSecond = now + 1000;
counter = 0;
}
}
}