| 1 | package net.minecraft.server.rcon.thread;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.io.IOException;
|
| 6 | import java.net.InetAddress;
|
| 7 | import java.net.ServerSocket;
|
| 8 | import java.net.Socket;
|
| 9 | import java.net.SocketTimeoutException;
|
| 10 | import java.util.List;
|
| 11 | import net.minecraft.server.ServerInterface;
|
| 12 | import net.minecraft.server.dedicated.DedicatedServerProperties;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 | import org.slf4j.Logger;
|
| 15 |
|
| 16 | public class RconThread extends GenericThread {
|
| 17 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 18 | private final ServerSocket socket;
|
| 19 | private final String rconPassword;
|
| 20 | private final List<RconClient> clients = Lists.newArrayList();
|
| 21 | private final ServerInterface serverInterface;
|
| 22 |
|
| 23 | private RconThread(final ServerInterface serverInterface, final ServerSocket socket, final String rconPassword) {
|
| 24 | super("RCON Listener");
|
| 25 | this.serverInterface = serverInterface;
|
| 26 | this.socket = socket;
|
| 27 | this.rconPassword = rconPassword;
|
| 28 | }
|
| 29 |
|
| 30 | private void clearClients() {
|
| 31 | this.clients.removeIf(client -> !client.isRunning());
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public void run() {
|
| 36 | try {
|
| 37 | while (this.running) {
|
| 38 | try {
|
| 39 | Socket client = this.socket.accept();
|
| 40 | RconClient rconClient = new RconClient(this.serverInterface, this.rconPassword, client);
|
| 41 | rconClient.start();
|
| 42 | this.clients.add(rconClient);
|
| 43 | this.clearClients();
|
| 44 | } catch (SocketTimeoutException ignored) {
|
| 45 | this.clearClients();
|
| 46 | } catch (IOException e) {
|
| 47 | if (this.running) {
|
| 48 | LOGGER.info("IO exception: ", e);
|
| 49 | }
|
| 50 | }
|
| 51 | }
|
| 52 | } finally {
|
| 53 | this.closeSocket(this.socket);
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | public static @Nullable RconThread create(final ServerInterface serverInterface) {
|
| 58 | DedicatedServerProperties settings = serverInterface.getProperties();
|
| 59 | String serverIp = serverInterface.getServerIp();
|
| 60 | if (serverIp.isEmpty()) {
|
| 61 | serverIp = "0.0.0.0";
|
| 62 | }
|
| 63 |
|
| 64 | int port = settings.rconPort;
|
| 65 | if (0 < port && 65535 >= port) {
|
| 66 | String password = settings.rconPassword;
|
| 67 | if (password.isEmpty()) {
|
| 68 | LOGGER.warn("No rcon password set in server.properties, rcon disabled!");
|
| 69 | return null;
|
| 70 | }
|
| 71 |
|
| 72 | try {
|
| 73 | ServerSocket socket = new ServerSocket(port, 0, InetAddress.getByName(serverIp));
|
| 74 | socket.setSoTimeout(500);
|
| 75 | RconThread result = new RconThread(serverInterface, socket, password);
|
| 76 | if (!result.start()) {
|
| 77 | return null;
|
| 78 | }
|
| 79 |
|
| 80 | LOGGER.info("RCON running on {}:{}", serverIp, port);
|
| 81 | return result;
|
| 82 | } catch (IOException e) {
|
| 83 | LOGGER.warn("Unable to initialise RCON on {}:{}", serverIp, port, e);
|
| 84 | return null;
|
| 85 | }
|
| 86 | } else {
|
| 87 | LOGGER.warn("Invalid rcon port {} found in server.properties, rcon disabled!", port);
|
| 88 | return null;
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void stop() {
|
| 94 | this.running = false;
|
| 95 | this.closeSocket(this.socket);
|
| 96 | super.stop();
|
| 97 |
|
| 98 | for (RconClient rconClient : this.clients) {
|
| 99 | if (rconClient.isRunning()) {
|
| 100 | rconClient.stop();
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | this.clients.clear();
|
| 105 | }
|
| 106 |
|
| 107 | private void closeSocket(final ServerSocket socket) {
|
| 108 | LOGGER.debug("closeSocket: {}", socket);
|
| 109 |
|
| 110 | try {
|
| 111 | socket.close();
|
| 112 | } catch (IOException e) {
|
| 113 | LOGGER.warn("Failed to close socket", e);
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|