From be7b04004bf0e9b284f22b38328ce96ef35d6708 Mon Sep 17 00:00:00 2001 From: Johannes Donath Date: Sat, 4 Jul 2015 06:31:33 +0200 Subject: [PATCH] Add basic support for configurable tab-complete throttling diff --git a/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java b/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java index 293ec4e3..66d0b8a1 100644 --- a/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java +++ b/api/src/main/java/net/md_5/bungee/api/ProxyConfig.java @@ -88,4 +88,13 @@ public interface ProxyConfig * If metrics is enabled */ boolean isMetrics(); + + // Throttling options + + /** + * How often tab-complete packets can be sent. + *

+ * Values in milliseconds. + */ + int getTabThrottle(); } diff --git a/proxy/src/main/java/io/github/waterfallmc/waterfall/conf/WaterfallConfiguration.java b/proxy/src/main/java/io/github/waterfallmc/waterfall/conf/WaterfallConfiguration.java index 056b8040..e56d3591 100644 --- a/proxy/src/main/java/io/github/waterfallmc/waterfall/conf/WaterfallConfiguration.java +++ b/proxy/src/main/java/io/github/waterfallmc/waterfall/conf/WaterfallConfiguration.java @@ -16,16 +16,36 @@ public class WaterfallConfiguration extends Configuration { */ private boolean metrics = true; + /* + * Throttling options + * Helps prevent players from overloading the servers behind us + */ + + /** + * How often players are allowed to send tab throttle. + * Value in milliseconds. + *

+ * Default is one packet per second. + */ + private int tabThrottle = 1000; + @Override public void load() { super.load(); YamlConfig config = new YamlConfig(new File("waterfall.yml")); config.load(false); // Load, but no permissions metrics = config.getBoolean("metrics", metrics); + // Throttling options + tabThrottle = config.getInt("throttling.tab_complete", tabThrottle); } @Override public boolean isMetrics() { return metrics; } + + @Override + public int getTabThrottle() { + return tabThrottle; + } } diff --git a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java index bb43810b..5f5f00ca 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/UpstreamBridge.java @@ -32,6 +32,8 @@ public class UpstreamBridge extends PacketHandler private final ProxyServer bungee; private final UserConnection con; + private long lastTabCompletion = -1; + public UpstreamBridge(ProxyServer bungee, UserConnection con) { this.bungee = bungee; @@ -148,6 +150,16 @@ public class UpstreamBridge extends PacketHandler @Override public void handle(TabCompleteRequest tabComplete) throws Exception { + if ( bungee.getConfig().getTabThrottle() > 0 ) + { + long now = System.currentTimeMillis(); + if ( lastTabCompletion > 0 && (now - lastTabCompletion) <= bungee.getConfig().getTabThrottle() ) + { + throw CancelSendSignal.INSTANCE; + } + lastTabCompletion = now; + } + List suggestions = new ArrayList<>(); if ( tabComplete.getCursor().startsWith( "/" ) ) -- 2.14.1