From 076dac6a847a2824b05b7ac3d721f614ef4b13ae Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 12 Jan 2019 08:31:15 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=BE=D0=B5=20=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BE=D0=BC=20ExecCommandRequest/ExecCommandResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/LaunchServer.java | 1 + .../launchserver/response/Response.java | 2 + .../response/admin/ExecCommandResponse.java | 42 +++++++++++++++++++ .../request/admin/ExecCommandRequest.java | 41 ++++++++++++++++++ .../gravit/launcher/request/RequestType.java | 2 +- .../launcher/serialize/SerializeLimits.java | 2 + 6 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/response/admin/ExecCommandResponse.java create mode 100644 LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java index edf26110..2c4be483 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/LaunchServer.java @@ -139,6 +139,7 @@ public static final class Config { public boolean enabledProGuard; public boolean stripLineNumbers; public boolean deleteTempFiles; + public boolean enableRcon; public String startScript; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/Response.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/Response.java index cc1784d6..8101a2a4 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/response/Response.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/Response.java @@ -9,6 +9,7 @@ import ru.gravit.launcher.serialize.HInput; import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.response.admin.ExecCommandResponse; import ru.gravit.launchserver.response.auth.AuthResponse; import ru.gravit.launchserver.response.auth.AuthServerResponse; import ru.gravit.launchserver.response.auth.ChangeServerResponse; @@ -60,6 +61,7 @@ public static void registerResponses() { registerResponse(RequestType.SERVERAUTH.getNumber(), AuthServerResponse::new); registerResponse(RequestType.SETPROFILE.getNumber(), SetProfileResponse::new); registerResponse(RequestType.CHANGESERVER.getNumber(), ChangeServerResponse::new); + registerResponse(RequestType.EXECCOMMAND.getNumber(), ExecCommandResponse::new); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/response/admin/ExecCommandResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/response/admin/ExecCommandResponse.java new file mode 100644 index 00000000..7aae8b8b --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/response/admin/ExecCommandResponse.java @@ -0,0 +1,42 @@ +package ru.gravit.launchserver.response.admin; + +import ru.gravit.launcher.serialize.HInput; +import ru.gravit.launcher.serialize.HOutput; +import ru.gravit.launcher.serialize.SerializeLimits; +import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.response.Response; +import ru.gravit.launchserver.socket.Client; +import ru.gravit.utils.helper.LogHelper; + +import java.io.IOException; + +public class ExecCommandResponse extends Response { + public ExecCommandResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) { + super(server, session, input, output, ip); + } + + @Override + public void reply() throws Exception { + Client clientData = server.sessionManager.getClient(session); + if(!clientData.isAuth || !clientData.permissions.canAdmin || !server.config.enableRcon) requestError("Access denied"); + writeNoError(output); + String cmd = input.readString(SerializeLimits.MAX_COMMAND); + LogHelper.Output loutput = message -> { + try { + output.writeBoolean(true); + output.writeString(message, SerializeLimits.MAX_COMMAND); + } catch (IOException e) { + LogHelper.error(e); + } + }; + LogHelper.addOutput(loutput); + try + { + server.commandHandler.eval(cmd,false); + output.writeBoolean(false); + } finally { + LogHelper.removeOutput(loutput); + } + writeNoError(output); + } +} 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..2b4a0e01 --- /dev/null +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/admin/ExecCommandRequest.java @@ -0,0 +1,41 @@ +package ru.gravit.launcher.request.admin; + +import ru.gravit.launcher.request.Request; +import ru.gravit.launcher.request.RequestType; +import ru.gravit.launcher.serialize.HInput; +import ru.gravit.launcher.serialize.HOutput; +import ru.gravit.launcher.serialize.SerializeLimits; +import ru.gravit.utils.helper.LogHelper; + +public class ExecCommandRequest extends Request { + public LogHelper.Output loutput; + public String cmd; + + public ExecCommandRequest(LogHelper.Output output, String cmd) { + this.loutput = output; + this.cmd = cmd; + } + + @Override + public Integer getType() { + return RequestType.EXECCOMMAND.getNumber(); + } + + @Override + protected Boolean requestDo(HInput input, HOutput output) throws Exception { + readError(input); + output.writeString(cmd, SerializeLimits.MAX_COMMAND); + boolean isContinue = true; + while (isContinue) + { + isContinue = input.readBoolean(); + if(isContinue) + { + String log = input.readString(SerializeLimits.MAX_COMMAND); + if(loutput != null) loutput.println(log); + } + } + readError(input); + return true; + } +} diff --git a/libLauncher/src/main/java/ru/gravit/launcher/request/RequestType.java b/libLauncher/src/main/java/ru/gravit/launcher/request/RequestType.java index 25571eab..74cecc0d 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/request/RequestType.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/request/RequestType.java @@ -11,7 +11,7 @@ public enum RequestType implements EnumSerializer.Itf { LEGACYLAUNCHER(1), UPDATE(2), UPDATE_LIST(3), // Update requests AUTH(4), JOIN_SERVER(5), CHECK_SERVER(6), // Auth requests PROFILE_BY_USERNAME(7), PROFILE_BY_UUID(8), BATCH_PROFILE_BY_USERNAME(9), // Profile requests - PROFILES(10), SERVERAUTH(11), SETPROFILE(12), LAUNCHER(13), CHANGESERVER(14), + PROFILES(10), SERVERAUTH(11), SETPROFILE(12), LAUNCHER(13), CHANGESERVER(14),EXECCOMMAND(15), CUSTOM(255); // Custom requests private static final EnumSerializer SERIALIZER = new EnumSerializer<>(RequestType.class); diff --git a/libLauncher/src/main/java/ru/gravit/launcher/serialize/SerializeLimits.java b/libLauncher/src/main/java/ru/gravit/launcher/serialize/SerializeLimits.java index d11f3a3f..f118eeec 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/serialize/SerializeLimits.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/serialize/SerializeLimits.java @@ -19,4 +19,6 @@ public class SerializeLimits { public static final int MAX_DIGEST = 512; @LauncherAPI public static final int MAX_HWID_STR = 1024; + @LauncherAPI + public static final int MAX_COMMAND = 2048; }