From 6e6ad530e720a85f3f209bd8ca2ca40f9982211c Mon Sep 17 00:00:00 2001 From: Travis Watkins Date: Tue, 28 Jan 2014 14:52:16 -0600 Subject: [PATCH] Readd connection throttle --- .../minecraft/server/HandshakeListener.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main/java/net/minecraft/server/HandshakeListener.java b/src/main/java/net/minecraft/server/HandshakeListener.java index b7f67758ff..a02c83f709 100644 --- a/src/main/java/net/minecraft/server/HandshakeListener.java +++ b/src/main/java/net/minecraft/server/HandshakeListener.java @@ -2,8 +2,18 @@ package net.minecraft.server; import net.minecraft.util.io.netty.util.concurrent.GenericFutureListener; +// CraftBukkit start +import java.net.InetAddress; +import java.util.HashMap; +// CraftBukkit end + public class HandshakeListener implements PacketHandshakingInListener { + // CraftBukkit start + private static final HashMap throttleTracker = new HashMap(); + private static int throttleCounter = 0; + // CraftBukkit end + private final MinecraftServer a; private final NetworkManager b; @@ -18,6 +28,41 @@ public class HandshakeListener implements PacketHandshakingInListener { this.b.a(EnumProtocol.LOGIN); ChatComponentText chatcomponenttext; + // CraftBukkit start + try { + long currentTime = System.currentTimeMillis(); + long connectionThrottle = MinecraftServer.getServer().server.getConnectionThrottle(); + InetAddress address = ((java.net.InetSocketAddress) this.b.getSocketAddress()).getAddress(); + + synchronized (throttleTracker) { + if (throttleTracker.containsKey(address) && !"127.0.0.1".equals(address.getHostAddress()) && currentTime - throttleTracker.get(address) < connectionThrottle) { + throttleTracker.put(address, currentTime); + chatcomponenttext = new ChatComponentText("Connection throttled! Please wait before reconnecting."); + this.b.handle(new PacketLoginOutDisconnect(chatcomponenttext), new GenericFutureListener[0]); + this.b.a(chatcomponenttext); // Should be close + return; + } + + throttleTracker.put(address, currentTime); + throttleCounter++; + if (throttleCounter > 200) { + throttleCounter = 0; + + // Cleanup stale entries + java.util.Iterator iter = throttleTracker.entrySet().iterator(); + while (iter.hasNext()) { + java.util.Map.Entry entry = (java.util.Map.Entry) iter.next(); + if (entry.getValue() > connectionThrottle) { + iter.remove(); + } + } + } + } + } catch (Throwable t) { + org.apache.logging.log4j.LogManager.getLogger().error("Failed to check connection throttle", t); + } + // CraftBukkit end + if (packethandshakinginsetprotocol.d() > 4) { chatcomponenttext = new ChatComponentText("Outdated server! I\'m still on 1.7.2"); this.b.handle(new PacketLoginOutDisconnect(chatcomponenttext), new GenericFutureListener[0]);