2022-06-09 08:51:45 +00:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
2022-09-26 08:02:51 +00:00
|
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
2022-06-09 08:51:45 +00:00
|
|
|
Date: Fri, 30 Oct 2020 22:37:16 -0700
|
|
|
|
Subject: [PATCH] Add packet limiter config
|
|
|
|
|
|
|
|
Example config:
|
|
|
|
packet-limiter:
|
|
|
|
kick-message: '&cSent too many packets'
|
|
|
|
limits:
|
|
|
|
all:
|
|
|
|
interval: 7.0
|
|
|
|
max-packet-rate: 500.0
|
2022-06-09 12:27:48 +00:00
|
|
|
ServerboundPlaceRecipePacket:
|
2022-06-09 08:51:45 +00:00
|
|
|
interval: 4.0
|
|
|
|
max-packet-rate: 5.0
|
|
|
|
action: DROP
|
|
|
|
|
|
|
|
all section refers to all incoming packets, the action for all is
|
|
|
|
hard coded to KICK.
|
|
|
|
|
|
|
|
For specific limits, the section name is the class's name,
|
|
|
|
and an action can be defined: DROP or KICK
|
|
|
|
|
|
|
|
If interval or rate are less-than 0, the limit is ignored
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java
|
2022-10-31 02:44:25 +00:00
|
|
|
index 489ab7c7a66969501e60fbd44c16ba4cdc180d46..28e91d015cf0034cd7ca952440fd4f915c34d489 100644
|
2022-06-09 08:51:45 +00:00
|
|
|
--- a/src/main/java/net/minecraft/network/Connection.java
|
|
|
|
+++ b/src/main/java/net/minecraft/network/Connection.java
|
2022-10-31 02:44:25 +00:00
|
|
|
@@ -154,6 +154,22 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
|
2022-06-09 08:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Paper end - allow controlled flushing
|
|
|
|
+ // Paper start - packet limiter
|
|
|
|
+ protected final Object PACKET_LIMIT_LOCK = new Object();
|
|
|
|
+ protected final @Nullable io.papermc.paper.util.IntervalledCounter allPacketCounts = io.papermc.paper.configuration.GlobalConfiguration.get().packetLimiter.allPackets.isEnabled() ? new io.papermc.paper.util.IntervalledCounter(
|
|
|
|
+ (long)(io.papermc.paper.configuration.GlobalConfiguration.get().packetLimiter.allPackets.interval() * 1.0e9)
|
|
|
|
+ ) : null;
|
|
|
|
+ protected final java.util.Map<Class<? extends net.minecraft.network.protocol.Packet<?>>, io.papermc.paper.util.IntervalledCounter> packetSpecificLimits = new java.util.HashMap<>();
|
|
|
|
+
|
|
|
|
+ private boolean stopReadingPackets;
|
|
|
|
+ private void killForPacketSpam() {
|
2022-07-27 22:00:14 +00:00
|
|
|
+ this.sendPacket(new ClientboundDisconnectPacket(io.papermc.paper.adventure.PaperAdventure.asVanilla(io.papermc.paper.configuration.GlobalConfiguration.get().packetLimiter.kickMessage)), PacketSendListener.thenRun(() -> {
|
2022-06-09 21:11:43 +00:00
|
|
|
+ this.disconnect(io.papermc.paper.adventure.PaperAdventure.asVanilla(io.papermc.paper.configuration.GlobalConfiguration.get().packetLimiter.kickMessage));
|
2022-07-27 22:00:14 +00:00
|
|
|
+ }));
|
2022-06-09 08:51:45 +00:00
|
|
|
+ this.setReadOnly();
|
|
|
|
+ this.stopReadingPackets = true;
|
|
|
|
+ }
|
|
|
|
+ // Paper end - packet limiter
|
|
|
|
|
|
|
|
public Connection(PacketFlow side) {
|
|
|
|
this.receiving = side;
|
2022-10-31 02:44:25 +00:00
|
|
|
@@ -234,6 +250,45 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
|
2022-06-09 08:51:45 +00:00
|
|
|
|
|
|
|
protected void channelRead0(ChannelHandlerContext channelhandlercontext, Packet<?> packet) {
|
|
|
|
if (this.channel.isOpen()) {
|
|
|
|
+ // Paper start - packet limiter
|
|
|
|
+ if (this.stopReadingPackets) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (this.allPacketCounts != null ||
|
|
|
|
+ io.papermc.paper.configuration.GlobalConfiguration.get().packetLimiter.overrides.containsKey(packet.getClass())) {
|
|
|
|
+ long time = System.nanoTime();
|
|
|
|
+ synchronized (PACKET_LIMIT_LOCK) {
|
|
|
|
+ if (this.allPacketCounts != null) {
|
|
|
|
+ this.allPacketCounts.updateAndAdd(1, time);
|
|
|
|
+ if (this.allPacketCounts.getRate() >= io.papermc.paper.configuration.GlobalConfiguration.get().packetLimiter.allPackets.maxPacketRate()) {
|
|
|
|
+ this.killForPacketSpam();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (Class<?> check = packet.getClass(); check != Object.class; check = check.getSuperclass()) {
|
|
|
|
+ io.papermc.paper.configuration.GlobalConfiguration.PacketLimiter.PacketLimit packetSpecificLimit =
|
|
|
|
+ io.papermc.paper.configuration.GlobalConfiguration.get().packetLimiter.overrides.get(check);
|
2022-06-13 22:29:44 +00:00
|
|
|
+ if (packetSpecificLimit == null || !packetSpecificLimit.isEnabled()) {
|
2022-06-09 08:51:45 +00:00
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ io.papermc.paper.util.IntervalledCounter counter = this.packetSpecificLimits.computeIfAbsent((Class)check, (clazz) -> {
|
|
|
|
+ return new io.papermc.paper.util.IntervalledCounter((long)(packetSpecificLimit.interval() * 1.0e9));
|
|
|
|
+ });
|
|
|
|
+ counter.updateAndAdd(1, time);
|
|
|
|
+ if (counter.getRate() >= packetSpecificLimit.maxPacketRate()) {
|
|
|
|
+ switch (packetSpecificLimit.action()) {
|
|
|
|
+ case DROP:
|
|
|
|
+ return;
|
|
|
|
+ case KICK:
|
|
|
|
+ this.killForPacketSpam();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Paper end - packet limiter
|
|
|
|
try {
|
|
|
|
Connection.genericsFtw(packet, this.packetListener);
|
|
|
|
} catch (RunningOnDifferentThreadException cancelledpackethandleexception) {
|