[FEATURE] Удаленное управление сервером ExecCommandRequest/ExecCommandResponse

This commit is contained in:
Gravit 2019-01-12 08:31:15 +07:00
parent 790dc5003a
commit 076dac6a84
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
6 changed files with 89 additions and 1 deletions

View file

@ -139,6 +139,7 @@ public static final class Config {
public boolean enabledProGuard;
public boolean stripLineNumbers;
public boolean deleteTempFiles;
public boolean enableRcon;
public String startScript;

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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<Boolean> {
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;
}
}

View file

@ -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<RequestType> SERIALIZER = new EnumSerializer<>(RequestType.class);

View file

@ -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;
}