From 3f0c972b08f1bca212d805dd143ede2a4a463ff5 Mon Sep 17 00:00:00 2001 From: Gravita Date: Tue, 22 Jun 2021 12:09:34 +0700 Subject: [PATCH] [FEATURE] WhitelistComponent --- .../command/service/ComponentCommand.java | 2 + .../launchserver/components/Component.java | 1 + .../components/WhitelistComponent.java | 99 +++++++++++++++++++ .../config/LaunchServerConfig.java | 2 +- 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/components/WhitelistComponent.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java index 1aa49d0b..5e9769e3 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java @@ -63,6 +63,7 @@ public void invoke(String... args) throws Exception { if (component instanceof AutoCloseable) { ((AutoCloseable) component).close(); } + server.unregisterObject("component." + componentName, component); server.config.components.remove(componentName); logger.info("Component %s unloaded. Use 'config launchserver save' to save changes"); } @@ -94,6 +95,7 @@ public void invoke(String... args) throws Exception { component.setComponentName(componentName); server.config.components.put(componentName, component); component.init(server); + server.registerObject("component." + componentName, component); logger.info("Component %s ready. Use 'config launchserver save' to save changes"); } catch (Throwable throwable) { logger.error(throwable); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java index 604bc0b4..01b07b99 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java @@ -14,6 +14,7 @@ public static void registerComponents() { providers.register("regLimiter", RegLimiterComponent.class); providers.register("commandRemover", CommandRemoverComponent.class); providers.register("proguard", ProGuardComponent.class); + providers.register("whitelist", WhitelistComponent.class); registredComp = true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/WhitelistComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/WhitelistComponent.java new file mode 100644 index 00000000..7d7ca533 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/WhitelistComponent.java @@ -0,0 +1,99 @@ +package pro.gravit.launchserver.components; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.Reconfigurable; +import pro.gravit.launchserver.socket.Client; +import pro.gravit.launchserver.socket.response.auth.AuthResponse; +import pro.gravit.launchserver.socket.response.auth.JoinServerResponse; +import pro.gravit.utils.HookException; +import pro.gravit.utils.command.Command; +import pro.gravit.utils.command.SubCommand; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class WhitelistComponent extends Component implements AutoCloseable, Reconfigurable { + private transient LaunchServer server; + private transient final Logger logger = LogManager.getLogger(); + public String message = "auth.message.techwork"; + public boolean enabled = true; + public List whitelist = new ArrayList<>(); + + @Override + public void init(LaunchServer launchServer) { + this.server = launchServer; + this.server.authHookManager.preHook.registerHook(this::hookAuth); + this.server.authHookManager.joinServerHook.registerHook(this::hookJoin); + } + + public boolean hookAuth(AuthResponse.AuthContext context, Client client) throws HookException { + if (enabled) { + if (!whitelist.contains(context.login)) { + throw new HookException(message); + } + } + return false; + } + + public boolean hookJoin(JoinServerResponse response, Client client) throws HookException { + if (enabled) { + if (!whitelist.contains(response.username)) { + throw new HookException(message); + } + } + return false; + } + + @Override + public void close() throws Exception { + this.server.authHookManager.preHook.unregisterHook(this::hookAuth); + this.server.authHookManager.joinServerHook.unregisterHook(this::hookJoin); + } + + @Override + public Map getCommands() { + var commands = defaultCommandsMap(); + commands.put("setmessage", new SubCommand("[new message]", "set message") { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + message = args[0]; + logger.info("Message: {}", args[0]); + } + }); + commands.put("whitelist.add", new SubCommand("[login]", "add login to whitelist") { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + whitelist.add(args[0]); + logger.info("{} added to whitelist", args[0]); + } + }); + commands.put("whitelist.remove", new SubCommand("[login]", "remove login from whitelist") { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + whitelist.remove(args[0]); + logger.info("{} removed from whitelist", args[0]); + } + }); + commands.put("disable", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + enabled = false; + logger.info("Whitelist disabled"); + } + }); + commands.put("enable", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + enabled = true; + logger.info("Whitelist enabled"); + } + }); + return commands; + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java index 7cb46ffe..0aa86f96 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java @@ -208,7 +208,7 @@ public void init(LaunchServer.ReloadType type) { server.registerObject("auth.".concat(pair.name).concat(".provider"), pair.provider); server.registerObject("auth.".concat(pair.name).concat(".handler"), pair.handler); server.registerObject("auth.".concat(pair.name).concat(".core"), pair.core); - server.registerObject("auth.".concat(pair.name).concat(".social"), pair.core); + server.registerObject("auth.".concat(pair.name).concat(".social"), pair.social); server.registerObject("auth.".concat(pair.name).concat(".texture"), pair.textureProvider); } }