From 248450373a82e8d75ff1268c9cd1907169dd3e64 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sun, 25 Aug 2019 14:30:14 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=9A=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=8B=20AbstractLimiter=20-=20gc,clear,addExclude,rmExcl?= =?UTF-8?q?ude,=20clearExclude?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pro/gravit/launchserver/LaunchServer.java | 1 - .../components/AbstractLimiter.java | 52 ++++++++++++++++++- .../components/AuthLimiterComponent.java | 2 +- .../launchserver/components/IPLimiter.java | 8 +++ .../components/RegLimiterComponent.java | 2 +- 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/components/IPLimiter.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index 1b6d8e03..81247f3b 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -745,7 +745,6 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException LogHelper.debug("Init components"); config.components.forEach((k, v) -> { LogHelper.subDebug("Init component %s", k); - registerObject("component.".concat(k), v); v.init(this); }); LogHelper.debug("Init components successful"); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AbstractLimiter.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AbstractLimiter.java index 0d629039..1087e359 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AbstractLimiter.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AbstractLimiter.java @@ -6,12 +6,62 @@ import java.util.Map; import pro.gravit.launcher.NeedGarbageCollection; +import pro.gravit.launchserver.Reconfigurable; +import pro.gravit.utils.command.Command; +import pro.gravit.utils.command.SubCommand; +import pro.gravit.utils.helper.LogHelper; -public abstract class AbstractLimiter extends Component implements NeedGarbageCollection { +public abstract class AbstractLimiter extends Component implements NeedGarbageCollection, Reconfigurable { public int rateLimit; public int rateLimitMillis; public List exclude = new ArrayList<>(); + @Override + public Map getCommands() { + Map commands = new HashMap<>(); + commands.put("gc", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + long size = map.size(); + garbageCollection(); + LogHelper.info("Cleared %d entity", size); + } + }); + commands.put("clear", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + long size = map.size(); + map.clear(); + LogHelper.info("Cleared %d entity", size); + } + }); + commands.put("addExclude", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + exclude.add(getFromString(args[0])); + } + }); + commands.put("rmExclude", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + exclude.remove(getFromString(args[0])); + } + }); + commands.put("clearExclude", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + exclude.clear(); + } + }); + + return commands; + } + + protected abstract T getFromString(String str); + @Override public void garbageCollection() { long time = System.currentTimeMillis(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java index 5b3346c3..1e7ca11a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java @@ -9,7 +9,7 @@ import pro.gravit.launchserver.socket.response.auth.AuthResponse; import pro.gravit.utils.HookException; -public class AuthLimiterComponent extends AbstractLimiter implements NeedGarbageCollection, AutoCloseable { +public class AuthLimiterComponent extends IPLimiter implements NeedGarbageCollection, AutoCloseable { private transient LaunchServer srv; @Override public void preInit(LaunchServer launchServer) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/IPLimiter.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/IPLimiter.java new file mode 100644 index 00000000..3f4fd5f0 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/IPLimiter.java @@ -0,0 +1,8 @@ +package pro.gravit.launchserver.components; + +public abstract class IPLimiter extends AbstractLimiter { + @Override + protected String getFromString(String str) { + return str; + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/RegLimiterComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/RegLimiterComponent.java index 8a6327cd..4fbdd053 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/RegLimiterComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/RegLimiterComponent.java @@ -8,7 +8,7 @@ import pro.gravit.launchserver.manangers.hook.AuthHookManager; import pro.gravit.utils.HookException; -public class RegLimiterComponent extends AbstractLimiter implements NeedGarbageCollection, AutoCloseable { +public class RegLimiterComponent extends IPLimiter implements NeedGarbageCollection, AutoCloseable { public transient LaunchServer launchServer; public String message;