diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index e165406e..5e2d3f3e 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -287,6 +287,8 @@ public static void main(String... args) throws Throwable { public final ReloadManager reloadManager; + public final ReconfigurableManager reconfigurableManager; + public final BuildHookManager buildHookManager; @@ -378,6 +380,11 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { config = Launcher.gson.fromJson(reader,Config.class); } config.verify(); + for(AuthProvider provider : config.authProvider) + { + provider.init(); + } + config.authHandler.init(); // build hooks, anti-brutforce and other buildHookManager = new BuildHookManager(); @@ -386,6 +393,8 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { sessionManager = new SessionManager(); mirrorManager = new MirrorManager(); reloadManager = new ReloadManager(); + reconfigurableManager = new ReconfigurableManager(); + GarbageManager.registerNeedGC(sessionManager); GarbageManager.registerNeedGC(limiter); if(config.permissionsHandler instanceof Reloadable) @@ -409,6 +418,27 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException { } }); + if(config.permissionsHandler instanceof Reconfigurable) + reconfigurableManager.registerReconfigurable("permissionsHandler",(Reconfigurable) config.permissionsHandler); + if(config.authHandler instanceof Reconfigurable) + reconfigurableManager.registerReconfigurable("authHandler",(Reconfigurable) config.authHandler); + for(int i=0;i { + try { + mirrorManager.addMirror(s); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + }); + // init modules modulesManager.initModules(); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/Reconfigurable.java b/LaunchServer/src/main/java/ru/gravit/launchserver/Reconfigurable.java new file mode 100644 index 00000000..0c83c2c8 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/Reconfigurable.java @@ -0,0 +1,6 @@ +package ru.gravit.launchserver; + +public interface Reconfigurable { + void reconfig(String action,String[] args); + void printConfigHelp(); +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java index 22a3d473..362fd2ab 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java @@ -65,4 +65,9 @@ public static void registerHandlers() { public abstract String uuidToUsername(UUID uuid) throws IOException; + + public void init() + { + + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/MySQLAuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/MySQLAuthHandler.java index a4321f4c..0cdde91d 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/MySQLAuthHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/MySQLAuthHandler.java @@ -22,7 +22,8 @@ public final class MySQLAuthHandler extends CachedAuthHandler { private transient String queryByUsernameSQL; private transient String updateAuthSQL; private transient String updateServerIDSQL; - public MySQLAuthHandler() + @Override + public void init() { // Prepare SQL queries queryByUUIDSQL = String.format("SELECT %s, %s, %s, %s FROM %s WHERE %s=? LIMIT 1", diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java index 1d40a27d..7f01bf0a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProvider.java @@ -63,4 +63,8 @@ public static String getProviderName(Class clazz) } return null; } + public void init() + { + + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProviderResult.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProviderResult.java index d34f90c1..4432bb8d 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProviderResult.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/AuthProviderResult.java @@ -1,7 +1,7 @@ package ru.gravit.launchserver.auth.provider; +import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.auth.ClientPermissions; -import ru.gravit.launchserver.manangers.PermissionsManager; public class AuthProviderResult { @@ -12,7 +12,7 @@ public class AuthProviderResult { public AuthProviderResult(String username, String accessToken) { this.username = username; this.accessToken = accessToken; - permissions = PermissionsManager.getPermissions(username); + permissions = LaunchServer.server.config.permissionsHandler.getPermissions(username); } public AuthProviderResult(String username, String accessToken, ClientPermissions permissions) { diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/RejectAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/RejectAuthProvider.java index 56299bd0..16ca7081 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/RejectAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/RejectAuthProvider.java @@ -1,9 +1,13 @@ package ru.gravit.launchserver.auth.provider; +import ru.gravit.launchserver.Reconfigurable; import ru.gravit.launchserver.auth.AuthException; +import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.SecurityHelper; -public final class RejectAuthProvider extends AuthProvider { +import java.util.ArrayList; + +public final class RejectAuthProvider extends AuthProvider implements Reconfigurable { public RejectAuthProvider() { } @@ -12,7 +16,7 @@ public RejectAuthProvider(String message) { } private String message; - private String[] whitelist; + private ArrayList whitelist; @Override public AuthProviderResult auth(String login, String password, String ip) throws AuthException { @@ -33,4 +37,35 @@ public AuthProviderResult auth(String login, String password, String ip) throws public void close() { // Do nothing } + + @Override + public void reconfig(String action, String[] args) { + if(action.equals("message")) + { + message = args[0]; + LogHelper.info("New reject message: %s", message); + } + else if(action.equals("whitelist.add")) + { + if(whitelist == null) whitelist = new ArrayList<>(); + whitelist.add(args[0]); + } + else if(action.equals("whitelist.remove")) + { + if(whitelist == null) whitelist = new ArrayList<>(); + whitelist.remove(args[0]); + } + else if(action.equals("whitelist.clear")) + { + whitelist.clear(); + } + } + + @Override + public void printConfigHelp() { + LogHelper.info("message [new message] - set message"); + LogHelper.info("whitelist.add [username] - add username to whitelist"); + LogHelper.info("whitelist.remove [username] - remove username into whitelist"); + LogHelper.info("whitelist.clear - clear whitelist"); + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java index b910495f..d0457100 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/handler/CommandHandler.java @@ -43,9 +43,7 @@ import ru.gravit.launchserver.command.hash.UnindexAssetCommand; import ru.gravit.launchserver.command.modules.LoadModuleCommand; import ru.gravit.launchserver.command.modules.ModulesCommand; -import ru.gravit.launchserver.command.service.ReloadAllCommand; -import ru.gravit.launchserver.command.service.ReloadCommand; -import ru.gravit.launchserver.command.service.ReloadInfoCommand; +import ru.gravit.launchserver.command.service.*; import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.VerifyHelper; @@ -142,7 +140,11 @@ protected CommandHandler(LaunchServer server) { //Register service commands registerCommand("reload",new ReloadCommand(server)); registerCommand("reloadAll",new ReloadAllCommand(server)); - registerCommand("reloadInfo",new ReloadInfoCommand(server)); + registerCommand("reloadList",new ReloadListCommand(server)); + registerCommand("config", new ConfigCommand(server)); + registerCommand("configHelp", new ConfigHelpCommand(server)); + registerCommand("configList", new ConfigListCommand(server)); + registerCommand("swapAuthProvider", new SwapAuthProviderCommand(server)); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigCommand.java new file mode 100644 index 00000000..137e9895 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigCommand.java @@ -0,0 +1,30 @@ +package ru.gravit.launchserver.command.service; + +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.command.Command; +import ru.gravit.utils.helper.LogHelper; + +public class ConfigCommand extends Command { + public ConfigCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[name] [action] [more args]"; + } + + @Override + public String getUsageDescription() { + return "call reconfigurable action"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args,2); + LogHelper.info("Call %s module % action",args[0],args[1]); + String[] new_args = new String[args.length - 2]; + System.arraycopy(args, 2, new_args, 0, args.length - 2); + server.reconfigurableManager.call(args[0],args[1],new_args); + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigHelpCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigHelpCommand.java new file mode 100644 index 00000000..1e2f3d8c --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigHelpCommand.java @@ -0,0 +1,28 @@ +package ru.gravit.launchserver.command.service; + +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.command.Command; +import ru.gravit.utils.helper.LogHelper; + +public class ConfigHelpCommand extends Command { + public ConfigHelpCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[name]"; + } + + @Override + public String getUsageDescription() { + return "print help for config command"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args,1); + LogHelper.info("Help %s module",args[0]); + server.reconfigurableManager.printHelp(args[0]); + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigListCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigListCommand.java new file mode 100644 index 00000000..186d0e1e --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ConfigListCommand.java @@ -0,0 +1,25 @@ +package ru.gravit.launchserver.command.service; + +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.command.Command; + +public class ConfigListCommand extends Command { + public ConfigListCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[name]"; + } + + @Override + public String getUsageDescription() { + return "print help for config command"; + } + + @Override + public void invoke(String... args) throws Exception { + server.reconfigurableManager.printReconfigurables(); + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ReloadInfoCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ReloadListCommand.java similarity index 74% rename from LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ReloadInfoCommand.java rename to LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ReloadListCommand.java index ebfa04a1..f7e427af 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ReloadInfoCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/ReloadListCommand.java @@ -4,8 +4,8 @@ import ru.gravit.launchserver.command.Command; import ru.gravit.utils.helper.LogHelper; -public class ReloadInfoCommand extends Command { - public ReloadInfoCommand(LaunchServer server) { +public class ReloadListCommand extends Command { + public ReloadListCommand(LaunchServer server) { super(server); } @@ -21,8 +21,6 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { - verifyArgs(args,1); - LogHelper.info("Reload %s config",args[0]); server.reloadManager.printReloadables(); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/SwapAuthProviderCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/SwapAuthProviderCommand.java new file mode 100644 index 00000000..97fac0b2 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/service/SwapAuthProviderCommand.java @@ -0,0 +1,61 @@ +package ru.gravit.launchserver.command.service; + +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.auth.provider.AcceptAuthProvider; +import ru.gravit.launchserver.auth.provider.AuthProvider; +import ru.gravit.launchserver.auth.provider.RejectAuthProvider; +import ru.gravit.launchserver.command.Command; +import ru.gravit.utils.helper.LogHelper; + +public class SwapAuthProviderCommand extends Command { + public AuthProvider[] providersCache; + public SwapAuthProviderCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[index] [accept/reject/undo] [message(for reject)]"; + } + + @Override + public String getUsageDescription() { + return "Change authProvider"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args,2); + if(providersCache == null) providersCache = new AuthProvider[server.config.authProvider.length]; + int index = Integer.valueOf(args[0]); + if(args[1].equals("accept")) + { + if(providersCache[index] == null) + { + AcceptAuthProvider provider = new AcceptAuthProvider(); + providersCache[index] = server.config.authProvider[index]; + server.config.authProvider[index] = provider; + LogHelper.info("AuthProvider[%d] is AcceptAuthProvider",index); + } + else LogHelper.error("Changes detected. Use undo"); + } else if(args[1].equals("reject")) + { + if(providersCache[index] == null) + { + RejectAuthProvider rejectAuthProvider; + if(args.length < 3) rejectAuthProvider = new RejectAuthProvider(); + else rejectAuthProvider = new RejectAuthProvider(args[2]); + providersCache[index] = server.config.authProvider[index]; + server.config.authProvider[index] = rejectAuthProvider; + LogHelper.info("AuthProvider[%d] is RejectAuthProvider",index); + } + else LogHelper.error("Changes detected. Use undo"); + } else if(args[1].equals("undo")) + { + if(providersCache[index] == null) LogHelper.error("Cache clean. Undo impossible"); + server.config.authProvider[index].close(); + server.config.authProvider[index] = providersCache[index]; + providersCache[index] = null; + } + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/PermissionsManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/PermissionsManager.java deleted file mode 100644 index ea5c75b7..00000000 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/PermissionsManager.java +++ /dev/null @@ -1,24 +0,0 @@ -package ru.gravit.launchserver.manangers; - -import ru.gravit.launchserver.auth.ClientPermissions; - -public class PermissionsManager { - private static PermissionsFunction function = PermissionsManager::returnDefault; - public static void registerPermissionsFunction(PermissionsFunction function) - { - PermissionsManager.function = function; - } - public static ClientPermissions getPermissions(String username) - { - return function.getPermission(username); - } - @FunctionalInterface - public interface PermissionsFunction - { - ClientPermissions getPermission(String username); - } - public static ClientPermissions returnDefault(String username) - { - return ClientPermissions.DEFAULT; - } -} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ReconfigurableManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ReconfigurableManager.java new file mode 100644 index 00000000..45d32a1f --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/ReconfigurableManager.java @@ -0,0 +1,32 @@ +package ru.gravit.launchserver.manangers; + +import ru.gravit.launchserver.Reconfigurable; +import ru.gravit.launchserver.Reloadable; +import ru.gravit.utils.helper.LogHelper; +import ru.gravit.utils.helper.VerifyHelper; + +import java.util.HashMap; +import java.util.Objects; + +public class ReconfigurableManager { + private final HashMap RECONFIGURABLE = new HashMap<>(); + public void registerReconfigurable(String name, Reconfigurable reconfigurable) + { + VerifyHelper.verifyIDName(name); + VerifyHelper.putIfAbsent(RECONFIGURABLE, name, Objects.requireNonNull(reconfigurable, "adapter"), + String.format("Reloadable has been already registered: '%s'", name)); + } + public void printHelp(String name) + { + RECONFIGURABLE.get(name).printConfigHelp(); + } + public void call(String name, String action, String[] args) throws Exception { + RECONFIGURABLE.get(name).reconfig(action,args); + } + public void printReconfigurables() + { + LogHelper.info("Print reconfigurables"); + RECONFIGURABLE.forEach((k, v) -> LogHelper.subInfo(k)); + LogHelper.info("Found %d reconfigurables", RECONFIGURABLE.size()); + } +}