From 943ddd52e2950af8c5893ea4e1d47528d009ecfd Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:06:36 +0200 Subject: [PATCH 01/11] add packet limiter impl --- .../net/md_5/bungee/util/PacketLimiter.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java diff --git a/proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java b/proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java new file mode 100644 index 0000000000..1d46696841 --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java @@ -0,0 +1,28 @@ +package net.md_5.bungee.util; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class PacketLimiter +{ + // max amount of packets allowed per second + @Getter + 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; + } + } +} From b6f9e36d0e3d260e69f388c46067e3c9b6dbea9a Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:08:35 +0200 Subject: [PATCH 02/11] add max packets per second to config --- proxy/src/main/java/net/md_5/bungee/conf/Configuration.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java b/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java index 6967d4fcd2..b26669bec6 100644 --- a/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java +++ b/proxy/src/main/java/net/md_5/bungee/conf/Configuration.java @@ -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() { @@ -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) adapter.getList( "disabled_commands", Arrays.asList( "disabledcommandhere" ) ) ); From b3af0d8c88bd9155ba5554b288dac24223db6e58 Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:10:55 +0200 Subject: [PATCH 03/11] add packetlimiter to HandlerBoss --- .../src/main/java/net/md_5/bungee/netty/HandlerBoss.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index d82173b1ea..819ff0abb3 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -18,6 +18,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; /** @@ -28,6 +29,7 @@ public class HandlerBoss extends ChannelInboundHandlerAdapter { + private PacketLimiter limiter = new PacketLimiter( BungeeCord.getInstance().getConfig().getMaxPacketsPerSecond() ); private ChannelWrapper channel; private PacketHandler handler; private boolean healthCheck; @@ -118,6 +120,12 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception if ( handler != null ) { + + if ( limiter.getLimit() > 0 && ( handler instanceof InitialHandler || handler instanceof UpstreamBridge ) ) + { + limiter.received(); + } + boolean sendPacket = handler.shouldHandle( packet ); try { From 70def7e62ac66a15d7a543583da90c8752f2e7ad Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:25:08 +0200 Subject: [PATCH 04/11] import --- proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java | 1 + 1 file changed, 1 insertion(+) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index 819ff0abb3..f2de99bcd4 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.util.logging.Level; +import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.connection.CancelSendSignal; import net.md_5.bungee.connection.InitialHandler; From ad183d181172f0d3ac1a21eac06afcbd6441d204 Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Mon, 26 Aug 2024 15:27:39 +0200 Subject: [PATCH 05/11] import 2 --- proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java | 1 + 1 file changed, 1 insertion(+) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index f2de99bcd4..df26eff0de 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -15,6 +15,7 @@ import net.md_5.bungee.connection.CancelSendSignal; import net.md_5.bungee.connection.InitialHandler; import net.md_5.bungee.connection.PingHandler; +import net.md_5.bungee.connection.UpstreamBridge; import net.md_5.bungee.protocol.BadPacketException; import net.md_5.bungee.protocol.OverflowPacketException; import net.md_5.bungee.protocol.PacketWrapper; From b3f2389570369323ac2734f786c80a5816a1b22d Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:54:55 +0200 Subject: [PATCH 06/11] improve the logic 1 --- .../java/net/md_5/bungee/netty/HandlerBoss.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index df26eff0de..c7f97f5467 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -31,7 +31,8 @@ public class HandlerBoss extends ChannelInboundHandlerAdapter { - private PacketLimiter limiter = new PacketLimiter( BungeeCord.getInstance().getConfig().getMaxPacketsPerSecond() ); + @Setter + private PacketLimiter limiter; private ChannelWrapper channel; private PacketHandler handler; private boolean healthCheck; @@ -84,6 +85,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; @@ -122,12 +128,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception if ( handler != null ) { - - if ( limiter.getLimit() > 0 && ( handler instanceof InitialHandler || handler instanceof UpstreamBridge ) ) - { - limiter.received(); - } - boolean sendPacket = handler.shouldHandle( packet ); try { From c286901d463a2c09281721781ae4d5f14ab0b391 Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:55:45 +0200 Subject: [PATCH 07/11] import --- proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index c7f97f5467..272b297580 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -10,12 +10,10 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.util.logging.Level; -import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.connection.CancelSendSignal; import net.md_5.bungee.connection.InitialHandler; import net.md_5.bungee.connection.PingHandler; -import net.md_5.bungee.connection.UpstreamBridge; import net.md_5.bungee.protocol.BadPacketException; import net.md_5.bungee.protocol.OverflowPacketException; import net.md_5.bungee.protocol.PacketWrapper; From 4a60a17457ac243203275c63ca810d5eb01c7cb5 Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:57:09 +0200 Subject: [PATCH 08/11] set the limiter in the channel init --- .../main/java/net/md_5/bungee/netty/PipelineUtils.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java index 25f045bebe..b21c70b078 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java @@ -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 { @@ -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() ) { From 0d743b9d28b2631ec2a890181fb644c9be0cfd6e Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:59:11 +0200 Subject: [PATCH 09/11] import --- proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java | 1 + 1 file changed, 1 insertion(+) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java index 272b297580..30b157d5c8 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/HandlerBoss.java @@ -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; From 4988053da6350f2d56e44736682944d76a31cf23 Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:59:59 +0200 Subject: [PATCH 10/11] no need for the getter anymore --- proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java b/proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java index 1d46696841..7440a2b508 100644 --- a/proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java +++ b/proxy/src/main/java/net/md_5/bungee/util/PacketLimiter.java @@ -1,13 +1,11 @@ package net.md_5.bungee.util; -import lombok.Getter; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor public class PacketLimiter { // max amount of packets allowed per second - @Getter private final int limit; private int counter; private long nextSecond; From 91fc21250c35ad9cea6eb2b939ec5e61c470556e Mon Sep 17 00:00:00 2001 From: Outfluencer <48880402+Outfluencer@users.noreply.github.com> Date: Tue, 27 Aug 2024 01:02:12 +0200 Subject: [PATCH 11/11] space --- proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java index b21c70b078..af79c50028 100644 --- a/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java +++ b/proxy/src/main/java/net/md_5/bungee/netty/PipelineUtils.java @@ -87,7 +87,7 @@ protected void initChannel(Channel ch) throws Exception HandlerBoss handlerBoss = ch.pipeline().get( HandlerBoss.class ); handlerBoss.setHandler( new InitialHandler( BungeeCord.getInstance(), listener ) ); int packetLimit = BungeeCord.getInstance().getConfig().getMaxPacketsPerSecond(); - if( packetLimit > 0 ) + if ( packetLimit > 0 ) { handlerBoss.setLimiter( new PacketLimiter( packetLimit ) ); }