Merge pull request #26 from TehRainbowGuy/dos

Implement connection throttling in netty.
This commit is contained in:
md-5 2013-03-26 23:38:33 -07:00
commit 0175fd1a17

View File

@ -1,4 +1,4 @@
From 02c3ccb6b9e24a0367962e7581015fc28fd5f053 Mon Sep 17 00:00:00 2001 From d66443f8758bf0b130a10d77060b04ddedfb2e87 Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au> From: md_5 <md_5@live.com.au>
Date: Thu, 14 Feb 2013 17:32:20 +1100 Date: Thu, 14 Feb 2013 17:32:20 +1100
Subject: [PATCH] Netty Subject: [PATCH] Netty
@ -42,14 +42,14 @@ Subject: [PATCH] Netty
.../net/minecraft/server/ThreadLoginVerifier.java | 1 + .../net/minecraft/server/ThreadLoginVerifier.java | 1 +
.../craftbukkit/scheduler/CraftScheduler.java | 2 +- .../craftbukkit/scheduler/CraftScheduler.java | 2 +-
src/main/java/org/spigotmc/netty/CipherCodec.java | 49 ++++ src/main/java/org/spigotmc/netty/CipherCodec.java | 49 ++++
.../org/spigotmc/netty/NettyNetworkManager.java | 235 +++++++++++++++++++ .../org/spigotmc/netty/NettyNetworkManager.java | 251 +++++++++++++++++++++
.../org/spigotmc/netty/NettyServerConnection.java | 109 +++++++++ .../org/spigotmc/netty/NettyServerConnection.java | 112 +++++++++
.../org/spigotmc/netty/NettySocketAdaptor.java | 248 +++++++++++++++++++++ .../org/spigotmc/netty/NettySocketAdaptor.java | 248 ++++++++++++++++++++
.../java/org/spigotmc/netty/PacketDecoder.java | 64 ++++++ .../java/org/spigotmc/netty/PacketDecoder.java | 64 ++++++
.../java/org/spigotmc/netty/PacketEncoder.java | 43 ++++ .../java/org/spigotmc/netty/PacketEncoder.java | 43 ++++
.../java/org/spigotmc/netty/PacketListener.java | 100 +++++++++ .../java/org/spigotmc/netty/PacketListener.java | 100 ++++++++
src/main/java/org/spigotmc/netty/ReadState.java | 16 ++ src/main/java/org/spigotmc/netty/ReadState.java | 16 ++
18 files changed, 917 insertions(+), 9 deletions(-) 18 files changed, 936 insertions(+), 9 deletions(-)
create mode 100644 src/main/java/net/minecraft/server/INetworkManager.java create mode 100644 src/main/java/net/minecraft/server/INetworkManager.java
create mode 100644 src/main/java/org/spigotmc/netty/CipherCodec.java create mode 100644 src/main/java/org/spigotmc/netty/CipherCodec.java
create mode 100644 src/main/java/org/spigotmc/netty/NettyNetworkManager.java create mode 100644 src/main/java/org/spigotmc/netty/NettyNetworkManager.java
@ -313,10 +313,10 @@ index 0000000..54928b4
+} +}
diff --git a/src/main/java/org/spigotmc/netty/NettyNetworkManager.java b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java diff --git a/src/main/java/org/spigotmc/netty/NettyNetworkManager.java b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java
new file mode 100644 new file mode 100644
index 0000000..f551bbe index 0000000..ee2e478
--- /dev/null --- /dev/null
+++ b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java +++ b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java
@@ -0,0 +1,235 @@ @@ -0,0 +1,251 @@
+package org.spigotmc.netty; +package org.spigotmc.netty;
+ +
+import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.google.common.util.concurrent.ThreadFactoryBuilder;
@ -388,6 +388,22 @@ index 0000000..f551bbe
+ address = channel.remoteAddress(); + address = channel.remoteAddress();
+ // Then the socket adaptor + // Then the socket adaptor
+ socketAdaptor = NettySocketAdaptor.adapt((SocketChannel) channel); + socketAdaptor = NettySocketAdaptor.adapt((SocketChannel) channel);
+ if (serverConnection.throttle == null) {
+ serverConnection.throttle = MinecraftServer.getServer().server.getConnectionThrottle();
+ }
+ if (serverConnection.throttle > 0) {
+ long time = System.currentTimeMillis();
+ String inetAddress = socketAdaptor.getInetAddress().getHostAddress();
+ if (serverConnection.throttleMap.containsKey(inetAddress) && !"127.0.0.1".equals(inetAddress)) {
+ if (time - serverConnection.throttleMap.get(inetAddress) < serverConnection.throttle) {
+ connected = false;
+ dcReason = "Too many connections.";
+ channel.close();
+ return;
+ }
+ }
+ serverConnection.throttleMap.put(inetAddress, time);
+ }
+ // Followed by their first handler + // Followed by their first handler
+ connection = new PendingConnection(server, this); + connection = new PendingConnection(server, this);
+ // Finally register the connection + // Finally register the connection
@ -554,10 +570,10 @@ index 0000000..f551bbe
+} +}
diff --git a/src/main/java/org/spigotmc/netty/NettyServerConnection.java b/src/main/java/org/spigotmc/netty/NettyServerConnection.java diff --git a/src/main/java/org/spigotmc/netty/NettyServerConnection.java b/src/main/java/org/spigotmc/netty/NettyServerConnection.java
new file mode 100644 new file mode 100644
index 0000000..9803105 index 0000000..b4cebac
--- /dev/null --- /dev/null
+++ b/src/main/java/org/spigotmc/netty/NettyServerConnection.java +++ b/src/main/java/org/spigotmc/netty/NettyServerConnection.java
@@ -0,0 +1,109 @@ @@ -0,0 +1,112 @@
+package org.spigotmc.netty; +package org.spigotmc.netty;
+ +
+import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.google.common.util.concurrent.ThreadFactoryBuilder;
@ -576,6 +592,7 @@ index 0000000..9803105
+import java.util.ArrayList; +import java.util.ArrayList;
+import java.util.Collections; +import java.util.Collections;
+import java.util.List; +import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Level; +import java.util.logging.Level;
+import javax.crypto.Cipher; +import javax.crypto.Cipher;
+import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.IvParameterSpec;
@ -594,6 +611,8 @@ index 0000000..9803105
+ +
+ private final ChannelFuture socket; + private final ChannelFuture socket;
+ final List<PendingConnection> pendingConnections = Collections.synchronizedList(new ArrayList<PendingConnection>()); + final List<PendingConnection> pendingConnections = Collections.synchronizedList(new ArrayList<PendingConnection>());
+ Long throttle;
+ final ConcurrentHashMap<String, Long> throttleMap = new ConcurrentHashMap<String, Long>();
+ +
+ public NettyServerConnection(MinecraftServer ms, InetAddress host, int port) { + public NettyServerConnection(MinecraftServer ms, InetAddress host, int port) {
+ super(ms); + super(ms);
@ -1169,5 +1188,5 @@ index 0000000..5dc3754
+ DATA; + DATA;
+} +}
-- --
1.8.1-rc2 1.8.2