[URGENT] Add connection throttle.
This commit is contained in:
parent
911f08d52c
commit
1f38152530
@ -19,9 +19,11 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.SocketAddress;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.MissingResourceException;
|
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
|
* Start this proxy instance by loading the configuration, plugins and
|
||||||
|
@ -46,6 +46,7 @@ public class Configuration
|
|||||||
private boolean onlineMode = true;
|
private boolean onlineMode = true;
|
||||||
private int playerLimit = -1;
|
private int playerLimit = -1;
|
||||||
private Collection<String> disabledCommands;
|
private Collection<String> disabledCommands;
|
||||||
|
private int throttle = 4000;
|
||||||
|
|
||||||
public void load()
|
public void load()
|
||||||
{
|
{
|
||||||
@ -57,6 +58,7 @@ public class Configuration
|
|||||||
uuid = adapter.getString( "stats", uuid );
|
uuid = adapter.getString( "stats", uuid );
|
||||||
onlineMode = adapter.getBoolean( "online_mode", onlineMode );
|
onlineMode = adapter.getBoolean( "online_mode", onlineMode );
|
||||||
playerLimit = adapter.getInt( "player_limit", playerLimit );
|
playerLimit = adapter.getInt( "player_limit", playerLimit );
|
||||||
|
throttle = adapter.getInt( "connection_throttle", throttle );
|
||||||
|
|
||||||
disabledCommands = new CaseInsensitiveSet( (Collection<String>) adapter.getList( "disabled_commands", Arrays.asList( "find" ) ) );
|
disabledCommands = new CaseInsensitiveSet( (Collection<String>) adapter.getList( "disabled_commands", Arrays.asList( "find" ) ) );
|
||||||
|
|
||||||
|
@ -153,6 +153,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
|
|||||||
+ "\00" + response.getMotd()
|
+ "\00" + response.getMotd()
|
||||||
+ "\00" + response.getCurrentPlayers()
|
+ "\00" + response.getCurrentPlayers()
|
||||||
+ "\00" + response.getMaxPlayers();
|
+ "\00" + response.getMaxPlayers();
|
||||||
|
BungeeCord.getInstance().unThrottle( getAddress() );
|
||||||
disconnect( kickMessage );
|
disconnect( kickMessage );
|
||||||
} catch ( Throwable t )
|
} catch ( Throwable t )
|
||||||
{
|
{
|
||||||
|
@ -8,6 +8,7 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayDeque;
|
import java.util.ArrayDeque;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import net.md_5.bungee.BungeeCord;
|
||||||
import net.md_5.bungee.api.ProxyServer;
|
import net.md_5.bungee.api.ProxyServer;
|
||||||
import net.md_5.bungee.connection.CancelSendSignal;
|
import net.md_5.bungee.connection.CancelSendSignal;
|
||||||
import net.md_5.bungee.connection.InitialHandler;
|
import net.md_5.bungee.connection.InitialHandler;
|
||||||
@ -34,6 +35,12 @@ public class HandlerBoss extends ChannelInboundHandlerAdapter
|
|||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception
|
public void channelActive(ChannelHandlerContext ctx) throws Exception
|
||||||
{
|
{
|
||||||
|
if ( BungeeCord.getInstance().throttle( ctx.channel().remoteAddress() ) )
|
||||||
|
{
|
||||||
|
ctx.channel().close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ( handler != null )
|
if ( handler != null )
|
||||||
{
|
{
|
||||||
channel = new ChannelWrapper( ctx );
|
channel = new ChannelWrapper( ctx );
|
||||||
|
Loading…
Reference in New Issue
Block a user