How to count and limit (packets/second)/connection? I want to prevent players from spamming packets with hacked clients. I know that viaversion has packet-limit feature but it is unuseable on bungeecord (it laggs CPU as hell when player joins to the server due to all injections). Do someone know good solution how to limit packets per player/ip? Is it possible to do it with iptable rule?
You can hook into the player's channel pipeline with an extended ChannelDuplexHandler and increment the counter when the void channelRead(ChannelHandlerContext, Object) is fired.
Do this when a player joins your server Code (Text): ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler() { @Override public void channelRead(ChannelHandlerContext context, Object packet) throws Exception { someCounterForThePlayer++; super.channelRead(context, packet); } }; ChannelPipeline pipeline; pipeline = ((CraftPlayer) playerInstance).getHandle().playerConnection.networkManager.channel.pipeline(); if (pipeline == null) return; String handlerName = "my_handler_name"; if (pipeline.get(handlerName) != null) pipeline.remove(handlerName); pipeline.addBefore("packet_handler", handlerName, channelDuplexHandler);
The provided solution is for Spigot. He is asking about BungeeCord. About BungeeCord you can get the Player's ChannelWrapper by some huuuge casting, then inject the ChannelDuplexHandler before "inbound-boss" and call the whole into the ConnectionInitEvent.
Can you give example too, please? I have never used netty classes and it looks strange to me. I will be very grateful.