diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/admin/ExecCommandResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/admin/ExecCommandResponse.java index f787dbad..62c33bb3 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/admin/ExecCommandResponse.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/json/admin/ExecCommandResponse.java @@ -18,11 +18,7 @@ public String getType() { @Override public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) { - if (!client.isAuth) { - service.sendObject(ctx, new ErrorRequestEvent("Access denied")); - return; - } - if (!client.permissions.canAdmin) { + if (!client.isAuth || !client.permissions.canAdmin) { service.sendObject(ctx, new ErrorRequestEvent("Access denied")); return; } diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ConsoleMain.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ConsoleMain.java index 96d8079e..db87c980 100644 --- a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ConsoleMain.java +++ b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ConsoleMain.java @@ -2,6 +2,9 @@ import ru.gravit.launcher.server.ServerWrapper; import ru.gravit.utils.command.CommandHandler; +import ru.gravit.utils.command.JLineCommandHandler; +import ru.gravit.utils.command.StdCommandHandler; +import ru.gravit.utils.command.basic.HelpCommand; import ru.gravit.utils.helper.LogHelper; import java.io.IOException; @@ -20,13 +23,21 @@ public static void main(String[] args) throws IOException { Class.forName("jline.Terminal"); // JLine2 available - commandHandler = new RemoteJLineCommandHandler(); + commandHandler = new JLineCommandHandler(); LogHelper.info("JLine2 terminal enabled"); } catch (ClassNotFoundException ignored) { - commandHandler = new RemoteStdCommandHandler(true); + commandHandler = new StdCommandHandler(true); LogHelper.warning("JLine2 isn't in classpath, using std"); } + registerCommands(); LogHelper.info("CommandHandler started. Use 'exit' to exit this console"); commandHandler.run(); } + public static void registerCommands() + { + commandHandler.registerCommand("help", new HelpCommand(commandHandler)); + commandHandler.registerCommand("exit", new ExitCommand()); + commandHandler.registerCommand("logListener", new LogListenerCommand()); + commandHandler.registerCommand("exec", new ExecCommand()); + } } diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ExecCommand.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ExecCommand.java new file mode 100644 index 00000000..f6ae564b --- /dev/null +++ b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ExecCommand.java @@ -0,0 +1,24 @@ +package ru.gravit.launchserver.console; + +import ru.gravit.launcher.events.request.ExecCommandRequestEvent; +import ru.gravit.launcher.request.admin.ExecCommandRequest; +import ru.gravit.utils.command.Command; +import ru.gravit.utils.helper.LogHelper; + +public class ExecCommand extends Command { + @Override + public String getArgsDescription() { + return null; + } + + @Override + public String getUsageDescription() { + return null; + } + + @Override + public void invoke(String... args) throws Exception { + ExecCommandRequestEvent request = new ExecCommandRequest(String.join(" ")).request(); + if(!request.success) LogHelper.error("Error executing command"); + } +} diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ExitCommand.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ExitCommand.java new file mode 100644 index 00000000..52c80c45 --- /dev/null +++ b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/ExitCommand.java @@ -0,0 +1,23 @@ +package ru.gravit.launchserver.console; + +import ru.gravit.utils.command.Command; + +public class ExitCommand extends Command { + public ExitCommand() { + } + + @Override + public String getArgsDescription() { + return null; + } + + @Override + public String getUsageDescription() { + return null; + } + + @Override + public void invoke(String... args) throws Exception { + System.exit(0); + } +} diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/LogListenerCommand.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/LogListenerCommand.java new file mode 100644 index 00000000..d2a2533f --- /dev/null +++ b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/LogListenerCommand.java @@ -0,0 +1,39 @@ +package ru.gravit.launchserver.console; + +import ru.gravit.launcher.events.request.LogEvent; +import ru.gravit.launcher.request.websockets.LegacyRequestBridge; +import ru.gravit.launcher.request.websockets.RequestInterface; +import ru.gravit.utils.command.Command; +import ru.gravit.utils.helper.LogHelper; + +public class LogListenerCommand extends Command { + public class LogListenerRequest implements RequestInterface + { + @Override + public String getType() { + return "addLogListener"; + } + } + @Override + public String getArgsDescription() { + return null; + } + + @Override + public String getUsageDescription() { + return null; + } + + @Override + public void invoke(String... args) throws Exception { + LogHelper.info("Send log listener request"); + LegacyRequestBridge.service.sendObject(new LogListenerRequest()); + LogHelper.info("Add log handler"); + LegacyRequestBridge.service.registerHandler((result) -> { + if(result instanceof LogEvent) + { + System.out.println(((LogEvent) result).string); + } + }); + } +} diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteJLineCommandHandler.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteJLineCommandHandler.java deleted file mode 100644 index 7e4ffa53..00000000 --- a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteJLineCommandHandler.java +++ /dev/null @@ -1,15 +0,0 @@ -package ru.gravit.launchserver.console; - -import ru.gravit.utils.command.JLineCommandHandler; - -import java.io.IOException; - -public class RemoteJLineCommandHandler extends JLineCommandHandler { - public RemoteJLineCommandHandler() throws IOException { - } - - @Override - public void eval(String line, boolean bell) { - if (line.equals("exit")) System.exit(0); - } -} diff --git a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteStdCommandHandler.java b/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteStdCommandHandler.java deleted file mode 100644 index 5329e59a..00000000 --- a/LaunchServerConsole/src/main/java/ru/gravit/launchserver/console/RemoteStdCommandHandler.java +++ /dev/null @@ -1,14 +0,0 @@ -package ru.gravit.launchserver.console; - -import ru.gravit.utils.command.StdCommandHandler; - -public class RemoteStdCommandHandler extends StdCommandHandler { - public RemoteStdCommandHandler(boolean readCommands) { - super(readCommands); - } - - @Override - public void eval(String line, boolean bell) { - if (line.equals("exit")) System.exit(0); - } -} diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java new file mode 100644 index 00000000..a1da5e1c --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java @@ -0,0 +1,26 @@ +package ru.gravit.launcher.request.admin; + +import ru.gravit.launcher.LauncherAPI; +import ru.gravit.launcher.events.request.ExecCommandRequestEvent; +import ru.gravit.launcher.request.Request; +import ru.gravit.launcher.request.websockets.LegacyRequestBridge; +import ru.gravit.launcher.request.websockets.RequestInterface; + +public class ExecCommandRequest extends Request implements RequestInterface { + @LauncherAPI + public String cmd; + + public ExecCommandRequest(String cmd) { + this.cmd = cmd; + } + + @Override + protected ExecCommandRequestEvent requestDo() throws Exception { + return (ExecCommandRequestEvent) LegacyRequestBridge.sendRequest(this); + } + + @Override + public String getType() { + return "execCmd"; + } +} diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java index 9dd11fdf..bf7790c8 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -113,6 +113,8 @@ public void registerResults() { registerResult("restoreSession", RestoreSessionRequestEvent.class); registerResult("getSecureToken", GetSecureTokenRequestEvent.class); registerResult("verifySecureToken", VerifySecureTokenRequestEvent.class); + registerResult("log", LogEvent.class); + registerResult("execCmd", ExecCommandRequestEvent.class); } public void registerHandler(EventHandler eventHandler) { diff --git a/libLauncher/src/main/java/ru/gravit/launcher/events/request/ExecCommandRequestEvent.java b/libLauncher/src/main/java/ru/gravit/launcher/events/request/ExecCommandRequestEvent.java index 8cc33688..0446d8a1 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/events/request/ExecCommandRequestEvent.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/events/request/ExecCommandRequestEvent.java @@ -6,10 +6,10 @@ public class ExecCommandRequestEvent implements ResultInterface { @Override public String getType() { - return "execCmd"; + return "cmdExec"; } @LauncherNetworkAPI - boolean success; + public boolean success; public ExecCommandRequestEvent(boolean success) { this.success = success;