[URGENT] Add connection throttle.

This commit is contained in:
md_5 2013-08-01 13:37:32 +10:00
parent 911f08d52c
commit 1f38152530
4 changed files with 41 additions and 0 deletions

View File

@ -19,9 +19,11 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.MissingResourceException;
@ -192,6 +194,35 @@ public class BungeeCord extends ProxyServer
}
}
}
private final Map<SocketAddress, Long> throttle = new HashMap<>();
public void unThrottle(SocketAddress address)
{
if ( address != null )
{
synchronized ( throttle )
{
throttle.remove( address );
}
}
}
public boolean throttle(SocketAddress address)
{
long currentTime = System.currentTimeMillis();
synchronized ( throttle )
{
Long value = throttle.get( address );
if ( value != null && currentTime - value < config.getThrottle() )
{
throttle.put( address, currentTime );
return true;
}
throttle.put( address, currentTime );
}
return false;
}
/**
* Start this proxy instance by loading the configuration, plugins and

View File

@ -46,6 +46,7 @@ public class Configuration
private boolean onlineMode = true;
private int playerLimit = -1;
private Collection<String> disabledCommands;
private int throttle = 4000;
public void load()
{
@ -57,6 +58,7 @@ public class Configuration
uuid = adapter.getString( "stats", uuid );
onlineMode = adapter.getBoolean( "online_mode", onlineMode );
playerLimit = adapter.getInt( "player_limit", playerLimit );
throttle = adapter.getInt( "connection_throttle", throttle );
disabledCommands = new CaseInsensitiveSet( (Collection<String>) adapter.getList( "disabled_commands", Arrays.asList( "find" ) ) );

View File

@ -153,6 +153,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
+ "\00" + response.getMotd()
+ "\00" + response.getCurrentPlayers()
+ "\00" + response.getMaxPlayers();
BungeeCord.getInstance().unThrottle( getAddress() );
disconnect( kickMessage );
} catch ( Throwable t )
{

View File

@ -8,6 +8,7 @@ import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.logging.Level;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.connection.CancelSendSignal;
import net.md_5.bungee.connection.InitialHandler;
@ -34,6 +35,12 @@ public class HandlerBoss extends ChannelInboundHandlerAdapter
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception
{
if ( BungeeCord.getInstance().throttle( ctx.channel().remoteAddress() ) )
{
ctx.channel().close();
return;
}
if ( handler != null )
{
channel = new ChannelWrapper( ctx );