--- a/net/minecraft/server/HandshakeListener.java +++ b/net/minecraft/server/HandshakeListener.java @@ -1,7 +1,16 @@ package net.minecraft.server; +// CraftBukkit start +import java.net.InetAddress; +import java.util.HashMap; +// CraftBukkit end + public class HandshakeListener implements PacketHandshakingInListener { + // CraftBukkit start - add fields + private static final HashMap throttleTracker = new HashMap(); + private static int throttleCounter = 0; + // CraftBukkit end private static final IChatBaseComponent a = new ChatComponentText("Ignoring status request"); private final MinecraftServer b; private final NetworkManager c; @@ -16,6 +25,40 @@ switch (packethandshakinginsetprotocol.b()) { case LOGIN: this.c.setProtocol(EnumProtocol.LOGIN); + // CraftBukkit start - Connection throttle + try { + long currentTime = System.currentTimeMillis(); + long connectionThrottle = this.b.server.getConnectionThrottle(); + InetAddress address = ((java.net.InetSocketAddress) this.c.getSocketAddress()).getAddress(); + + synchronized (throttleTracker) { + if (throttleTracker.containsKey(address) && !"127.0.0.1".equals(address.getHostAddress()) && currentTime - throttleTracker.get(address) < connectionThrottle) { + throttleTracker.put(address, currentTime); + ChatMessage chatmessage = new ChatMessage("Connection throttled! Please wait before reconnecting."); + this.c.sendPacket(new PacketLoginOutDisconnect(chatmessage)); + this.c.close(chatmessage); + 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().debug("Failed to check connection throttle", t); + } + // CraftBukkit end if (packethandshakinginsetprotocol.c() != SharedConstants.getGameVersion().getProtocolVersion()) { ChatMessage chatmessage; @@ -29,6 +72,7 @@ this.c.close(chatmessage); } else { this.c.setPacketListener(new LoginListener(this.b, this.c)); + ((LoginListener) this.c.j()).hostname = packethandshakinginsetprotocol.hostname + ":" + packethandshakinginsetprotocol.port; // CraftBukkit - set hostname } break; case STATUS: