| 1 | package net.minecraft.server.rcon.thread;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.BufferedInputStream;
|
| 5 | import java.io.ByteArrayOutputStream;
|
| 6 | import java.io.DataOutputStream;
|
| 7 | import java.io.IOException;
|
| 8 | import java.net.Socket;
|
| 9 | import java.nio.charset.StandardCharsets;
|
| 10 | import java.util.Locale;
|
| 11 | import net.minecraft.server.ServerInterface;
|
| 12 | import net.minecraft.server.rcon.PktUtils;
|
| 13 | import org.slf4j.Logger;
|
| 14 |
|
| 15 | public class RconClient extends GenericThread {
|
| 16 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 17 | private static final int SERVERDATA_AUTH = 3;
|
| 18 | private static final int SERVERDATA_EXECCOMMAND = 2;
|
| 19 | private static final int SERVERDATA_RESPONSE_VALUE = 0;
|
| 20 | private static final int SERVERDATA_AUTH_RESPONSE = 2;
|
| 21 | private static final int SERVERDATA_AUTH_FAILURE = -1;
|
| 22 | private boolean authed;
|
| 23 | private final Socket client;
|
| 24 | private final byte[] buf = new byte[1460];
|
| 25 | private final String rconPassword;
|
| 26 | private final ServerInterface serverInterface;
|
| 27 |
|
| 28 | public RconClient(final ServerInterface serverInterface, final String rconPassword, final Socket socket) {
|
| 29 | super("RCON Client " + socket.getInetAddress());
|
| 30 | this.serverInterface = serverInterface;
|
| 31 | this.client = socket;
|
| 32 |
|
| 33 | try {
|
| 34 | this.client.setSoTimeout(0);
|
| 35 | } catch (Exception ignored) {
|
| 36 | this.running = false;
|
| 37 | }
|
| 38 |
|
| 39 | this.rconPassword = rconPassword;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public void run() {
|
| 44 | try {
|
| 45 | try {
|
| 46 | while (this.running) {
|
| 47 | BufferedInputStream inputStream = new BufferedInputStream(this.client.getInputStream());
|
| 48 | int read = inputStream.read(this.buf, 0, 1460);
|
| 49 | if (10 > read) {
|
| 50 | return;
|
| 51 | }
|
| 52 |
|
| 53 | int offset = 0;
|
| 54 | int pktsize = PktUtils.intFromByteArray(this.buf, 0, read);
|
| 55 | if (pktsize != read - 4) {
|
| 56 | return;
|
| 57 | }
|
| 58 |
|
| 59 | offset += 4;
|
| 60 | int requestid = PktUtils.intFromByteArray(this.buf, offset, read);
|
| 61 | offset += 4;
|
| 62 | int cmd = PktUtils.intFromByteArray(this.buf, offset);
|
| 63 | offset += 4;
|
| 64 | switch (cmd) {
|
| 65 | case 2:
|
| 66 | if (this.authed) {
|
| 67 | String command = PktUtils.stringFromByteArray(this.buf, offset, read);
|
| 68 |
|
| 69 | try {
|
| 70 | this.sendCmdResponse(requestid, this.serverInterface.runCommand(command));
|
| 71 | } catch (Exception e) {
|
| 72 | this.sendCmdResponse(requestid, "Error executing: " + command + " (" + e.getMessage() + ")");
|
| 73 | }
|
| 74 | break;
|
| 75 | }
|
| 76 |
|
| 77 | this.sendAuthFailure();
|
| 78 | break;
|
| 79 | case 3:
|
| 80 | String password = PktUtils.stringFromByteArray(this.buf, offset, read);
|
| 81 | offset += password.length();
|
| 82 | if (!password.isEmpty() && password.equals(this.rconPassword)) {
|
| 83 | this.authed = true;
|
| 84 | this.send(requestid, 2, "");
|
| 85 | break;
|
| 86 | }
|
| 87 |
|
| 88 | this.authed = false;
|
| 89 | this.sendAuthFailure();
|
| 90 | break;
|
| 91 | default:
|
| 92 | this.sendCmdResponse(requestid, String.format(Locale.ROOT, "Unknown request %s", Integer.toHexString(cmd)));
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | return;
|
| 97 | } catch (IOException var15) {
|
| 98 | } catch (Exception e) {
|
| 99 | LOGGER.error("Exception whilst parsing RCON input", e);
|
| 100 | }
|
| 101 | } finally {
|
| 102 | this.closeSocket();
|
| 103 | LOGGER.info("Thread {} shutting down", this.name);
|
| 104 | this.running = false;
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private void send(final int requestid, final int cmd, final String str) throws IOException {
|
| 109 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1248);
|
| 110 | DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
|
| 111 | byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
|
| 112 | dataOutputStream.writeInt(Integer.reverseBytes(bytes.length + 10));
|
| 113 | dataOutputStream.writeInt(Integer.reverseBytes(requestid));
|
| 114 | dataOutputStream.writeInt(Integer.reverseBytes(cmd));
|
| 115 | dataOutputStream.write(bytes);
|
| 116 | dataOutputStream.write(0);
|
| 117 | dataOutputStream.write(0);
|
| 118 | this.client.getOutputStream().write(outputStream.toByteArray());
|
| 119 | }
|
| 120 |
|
| 121 | private void sendAuthFailure() throws IOException {
|
| 122 | this.send(-1, 2, "");
|
| 123 | }
|
| 124 |
|
| 125 | private void sendCmdResponse(final int requestid, String response) throws IOException {
|
| 126 | int len = response.length();
|
| 127 |
|
| 128 | do {
|
| 129 | int dataLen = 4096 <= len ? 4096 : len;
|
| 130 | this.send(requestid, 0, response.substring(0, dataLen));
|
| 131 | response = response.substring(dataLen);
|
| 132 | len = response.length();
|
| 133 | } while (0 != len);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public void stop() {
|
| 138 | this.running = false;
|
| 139 | this.closeSocket();
|
| 140 | super.stop();
|
| 141 | }
|
| 142 |
|
| 143 | private void closeSocket() {
|
| 144 | try {
|
| 145 | this.client.close();
|
| 146 | } catch (IOException e) {
|
| 147 | LOGGER.warn("Failed to close socket", e);
|
| 148 | }
|
| 149 | }
|
| 150 | }
|
| 151 |
|