| 1 | package net.minecraft.server.rcon.thread;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.io.IOException;
|
| 6 | import java.net.DatagramPacket;
|
| 7 | import java.net.DatagramSocket;
|
| 8 | import java.net.InetAddress;
|
| 9 | import java.net.PortUnreachableException;
|
| 10 | import java.net.SocketAddress;
|
| 11 | import java.net.SocketTimeoutException;
|
| 12 | import java.net.UnknownHostException;
|
| 13 | import java.nio.charset.StandardCharsets;
|
| 14 | import java.util.Date;
|
| 15 | import java.util.Locale;
|
| 16 | import java.util.Map;
|
| 17 | import net.minecraft.server.ServerInterface;
|
| 18 | import net.minecraft.server.rcon.NetworkDataOutputStream;
|
| 19 | import net.minecraft.server.rcon.PktUtils;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | public class QueryThreadGs4 extends GenericThread {
|
| 26 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 27 | private static final String GAME_TYPE = "SMP";
|
| 28 | private static final String GAME_ID = "MINECRAFT";
|
| 29 | private static final long CHALLENGE_CHECK_INTERVAL = 30000L;
|
| 30 | private static final long RESPONSE_CACHE_TIME = 5000L;
|
| 31 | private long lastChallengeCheck;
|
| 32 | private final int port;
|
| 33 | private final int serverPort;
|
| 34 | private final int maxPlayers;
|
| 35 | private final String serverName;
|
| 36 | private final String worldName;
|
| 37 | private DatagramSocket socket;
|
| 38 | private final byte[] buffer = new byte[1460];
|
| 39 | private String hostIp;
|
| 40 | private String serverIp;
|
| 41 | private final Map<SocketAddress, QueryThreadGs4.RequestChallenge> validChallenges;
|
| 42 | private final NetworkDataOutputStream rulesResponse;
|
| 43 | private long lastRulesResponse;
|
| 44 | private final ServerInterface serverInterface;
|
| 45 |
|
| 46 | private QueryThreadGs4(final ServerInterface serverInterface, final int port) {
|
| 47 | super("Query Listener");
|
| 48 | this.serverInterface = serverInterface;
|
| 49 | this.port = port;
|
| 50 | this.serverIp = serverInterface.getServerIp();
|
| 51 | this.serverPort = serverInterface.getServerPort();
|
| 52 | this.serverName = serverInterface.getServerName();
|
| 53 | this.maxPlayers = serverInterface.getMaxPlayers();
|
| 54 | this.worldName = serverInterface.getLevelIdName();
|
| 55 | this.lastRulesResponse = 0L;
|
| 56 | this.hostIp = "0.0.0.0";
|
| 57 | if (!this.serverIp.isEmpty() && !this.hostIp.equals(this.serverIp)) {
|
| 58 | this.hostIp = this.serverIp;
|
| 59 | } else {
|
| 60 | this.serverIp = "0.0.0.0";
|
| 61 |
|
| 62 | try {
|
| 63 | InetAddress addr = InetAddress.getLocalHost();
|
| 64 | this.hostIp = addr.getHostAddress();
|
| 65 | } catch (UnknownHostException e) {
|
| 66 | LOGGER.warn("Unable to determine local host IP, please set server-ip in server.properties", e);
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | this.rulesResponse = new NetworkDataOutputStream(1460);
|
| 71 | this.validChallenges = Maps.newHashMap();
|
| 72 | }
|
| 73 |
|
| 74 | public static @Nullable QueryThreadGs4 create(final ServerInterface serverInterface) {
|
| 75 | int port = serverInterface.getProperties().queryPort;
|
| 76 | if (0 < port && 65535 >= port) {
|
| 77 | QueryThreadGs4 result = new QueryThreadGs4(serverInterface, port);
|
| 78 | return !result.start() ? null : result;
|
| 79 | } else {
|
| 80 | LOGGER.warn("Invalid query port {} found in server.properties (queries disabled)", port);
|
| 81 | return null;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | private void sendTo(final byte[] data, final DatagramPacket src) throws IOException {
|
| 86 | this.socket.send(new DatagramPacket(data, data.length, src.getSocketAddress()));
|
| 87 | }
|
| 88 |
|
| 89 | private boolean processPacket(final DatagramPacket packet) throws IOException {
|
| 90 | byte[] buf = packet.getData();
|
| 91 | int len = packet.getLength();
|
| 92 | SocketAddress socketAddress = packet.getSocketAddress();
|
| 93 | LOGGER.debug("Packet len {} [{}]", len, socketAddress);
|
| 94 | if (3 <= len && -2 == buf[0] && -3 == buf[1]) {
|
| 95 | LOGGER.debug("Packet '{}' [{}]", PktUtils.toHexString(buf[2]), socketAddress);
|
| 96 | switch (buf[2]) {
|
| 97 | case 0:
|
| 98 | if (!this.validChallenge(packet)) {
|
| 99 | LOGGER.debug("Invalid challenge [{}]", socketAddress);
|
| 100 | return false;
|
| 101 | } else if (15 == len) {
|
| 102 | this.sendTo(this.buildRuleResponse(packet), packet);
|
| 103 | LOGGER.debug("Rules [{}]", socketAddress);
|
| 104 | } else {
|
| 105 | NetworkDataOutputStream dos = new NetworkDataOutputStream(1460);
|
| 106 | dos.write(0);
|
| 107 | dos.writeBytes(this.getIdentBytes(packet.getSocketAddress()));
|
| 108 | dos.writeString(this.serverName);
|
| 109 | dos.writeString("SMP");
|
| 110 | dos.writeString(this.worldName);
|
| 111 | dos.writeString(Integer.toString(this.serverInterface.getPlayerCount()));
|
| 112 | dos.writeString(Integer.toString(this.maxPlayers));
|
| 113 | dos.writeShort((short)this.serverPort);
|
| 114 | dos.writeString(this.hostIp);
|
| 115 | this.sendTo(dos.toByteArray(), packet);
|
| 116 | LOGGER.debug("Status [{}]", socketAddress);
|
| 117 | }
|
| 118 | default:
|
| 119 | return true;
|
| 120 | case 9:
|
| 121 | this.sendChallenge(packet);
|
| 122 | LOGGER.debug("Challenge [{}]", socketAddress);
|
| 123 | return true;
|
| 124 | }
|
| 125 | } else {
|
| 126 | LOGGER.debug("Invalid packet [{}]", socketAddress);
|
| 127 | return false;
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | private byte[] buildRuleResponse(final DatagramPacket packet) throws IOException {
|
| 132 | long now = Util.getMillis();
|
| 133 | if (now < this.lastRulesResponse + 5000L) {
|
| 134 | byte[] data = this.rulesResponse.toByteArray();
|
| 135 | byte[] ident = this.getIdentBytes(packet.getSocketAddress());
|
| 136 | data[1] = ident[0];
|
| 137 | data[2] = ident[1];
|
| 138 | data[3] = ident[2];
|
| 139 | data[4] = ident[3];
|
| 140 | return data;
|
| 141 | }
|
| 142 |
|
| 143 | this.lastRulesResponse = now;
|
| 144 | this.rulesResponse.reset();
|
| 145 | this.rulesResponse.write(0);
|
| 146 | this.rulesResponse.writeBytes(this.getIdentBytes(packet.getSocketAddress()));
|
| 147 | this.rulesResponse.writeString("splitnum");
|
| 148 | this.rulesResponse.write(128);
|
| 149 | this.rulesResponse.write(0);
|
| 150 | this.rulesResponse.writeString("hostname");
|
| 151 | this.rulesResponse.writeString(this.serverName);
|
| 152 | this.rulesResponse.writeString("gametype");
|
| 153 | this.rulesResponse.writeString("SMP");
|
| 154 | this.rulesResponse.writeString("game_id");
|
| 155 | this.rulesResponse.writeString("MINECRAFT");
|
| 156 | this.rulesResponse.writeString("version");
|
| 157 | this.rulesResponse.writeString(this.serverInterface.getServerVersion());
|
| 158 | this.rulesResponse.writeString("plugins");
|
| 159 | this.rulesResponse.writeString(this.serverInterface.getPluginNames());
|
| 160 | this.rulesResponse.writeString("map");
|
| 161 | this.rulesResponse.writeString(this.worldName);
|
| 162 | this.rulesResponse.writeString("numplayers");
|
| 163 | this.rulesResponse.writeString(this.serverInterface.getPlayerCount() + "");
|
| 164 | this.rulesResponse.writeString("maxplayers");
|
| 165 | this.rulesResponse.writeString(this.maxPlayers + "");
|
| 166 | this.rulesResponse.writeString("hostport");
|
| 167 | this.rulesResponse.writeString(this.serverPort + "");
|
| 168 | this.rulesResponse.writeString("hostip");
|
| 169 | this.rulesResponse.writeString(this.hostIp);
|
| 170 | this.rulesResponse.write(0);
|
| 171 | this.rulesResponse.write(1);
|
| 172 | this.rulesResponse.writeString("player_");
|
| 173 | this.rulesResponse.write(0);
|
| 174 | String[] players = this.serverInterface.getPlayerNames();
|
| 175 |
|
| 176 | for (String player : players) {
|
| 177 | this.rulesResponse.writeString(player);
|
| 178 | }
|
| 179 |
|
| 180 | this.rulesResponse.write(0);
|
| 181 | return this.rulesResponse.toByteArray();
|
| 182 | }
|
| 183 |
|
| 184 | private byte[] getIdentBytes(final SocketAddress src) {
|
| 185 | return this.validChallenges.get(src).getIdentBytes();
|
| 186 | }
|
| 187 |
|
| 188 | private Boolean validChallenge(final DatagramPacket src) {
|
| 189 | SocketAddress sockAddr = src.getSocketAddress();
|
| 190 | if (!this.validChallenges.containsKey(sockAddr)) {
|
| 191 | return false;
|
| 192 | }
|
| 193 |
|
| 194 | byte[] data = src.getData();
|
| 195 | return this.validChallenges.get(sockAddr).getChallenge() == PktUtils.intFromNetworkByteArray(data, 7, src.getLength());
|
| 196 | }
|
| 197 |
|
| 198 | private void sendChallenge(final DatagramPacket src) throws IOException {
|
| 199 | QueryThreadGs4.RequestChallenge challenge = new QueryThreadGs4.RequestChallenge(src);
|
| 200 | this.validChallenges.put(src.getSocketAddress(), challenge);
|
| 201 | this.sendTo(challenge.getChallengeBytes(), src);
|
| 202 | }
|
| 203 |
|
| 204 | private void pruneChallenges() {
|
| 205 | if (this.running) {
|
| 206 | long now = Util.getMillis();
|
| 207 | if (now >= this.lastChallengeCheck + 30000L) {
|
| 208 | this.lastChallengeCheck = now;
|
| 209 | this.validChallenges.values().removeIf(challenge -> challenge.before(now));
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public void run() {
|
| 216 | LOGGER.info("Query running on {}:{}", this.serverIp, this.port);
|
| 217 | this.lastChallengeCheck = Util.getMillis();
|
| 218 | DatagramPacket request = new DatagramPacket(this.buffer, this.buffer.length);
|
| 219 |
|
| 220 | try {
|
| 221 | while (this.running) {
|
| 222 | try {
|
| 223 | this.socket.receive(request);
|
| 224 | this.pruneChallenges();
|
| 225 | this.processPacket(request);
|
| 226 | } catch (SocketTimeoutException ignored) {
|
| 227 | this.pruneChallenges();
|
| 228 | } catch (PortUnreachableException var9) {
|
| 229 | } catch (IOException e) {
|
| 230 | this.recoverSocketError(e);
|
| 231 | }
|
| 232 | }
|
| 233 | } finally {
|
| 234 | LOGGER.debug("closeSocket: {}:{}", this.serverIp, this.port);
|
| 235 | this.socket.close();
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public boolean start() {
|
| 241 | if (this.running) {
|
| 242 | return true;
|
| 243 | } else {
|
| 244 | return !this.initSocket() ? false : super.start();
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | private void recoverSocketError(final Exception e) {
|
| 249 | if (this.running) {
|
| 250 | LOGGER.warn("Unexpected exception", e);
|
| 251 | if (!this.initSocket()) {
|
| 252 | LOGGER.error("Failed to recover from exception, shutting down!");
|
| 253 | this.running = false;
|
| 254 | }
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | private boolean initSocket() {
|
| 259 | try {
|
| 260 | this.socket = new DatagramSocket(this.port, InetAddress.getByName(this.serverIp));
|
| 261 | this.socket.setSoTimeout(500);
|
| 262 | return true;
|
| 263 | } catch (Exception e) {
|
| 264 | LOGGER.warn("Unable to initialise query system on {}:{}", this.serverIp, this.port, e);
|
| 265 | return false;
|
| 266 | }
|
| 267 | }
|
| 268 |
|
| 269 | private static class RequestChallenge {
|
| 270 | private final long time = new Date().getTime();
|
| 271 | private final int challenge;
|
| 272 | private final byte[] identBytes;
|
| 273 | private final byte[] challengeBytes;
|
| 274 | private final String ident;
|
| 275 |
|
| 276 | public RequestChallenge(final DatagramPacket src) {
|
| 277 | byte[] buf = src.getData();
|
| 278 | this.identBytes = new byte[4];
|
| 279 | this.identBytes[0] = buf[3];
|
| 280 | this.identBytes[1] = buf[4];
|
| 281 | this.identBytes[2] = buf[5];
|
| 282 | this.identBytes[3] = buf[6];
|
| 283 | this.ident = new String(this.identBytes, StandardCharsets.UTF_8);
|
| 284 | this.challenge = RandomSource.createThreadLocalInstance().nextInt(16777216);
|
| 285 | this.challengeBytes = String.format(Locale.ROOT, "\t%s%d\u0000", this.ident, this.challenge).getBytes(StandardCharsets.UTF_8);
|
| 286 | }
|
| 287 |
|
| 288 | public Boolean before(final long time) {
|
| 289 | return this.time < time;
|
| 290 | }
|
| 291 |
|
| 292 | public int getChallenge() {
|
| 293 | return this.challenge;
|
| 294 | }
|
| 295 |
|
| 296 | public byte[] getChallengeBytes() {
|
| 297 | return this.challengeBytes;
|
| 298 | }
|
| 299 |
|
| 300 | public byte[] getIdentBytes() {
|
| 301 | return this.identBytes;
|
| 302 | }
|
| 303 |
|
| 304 | public String getIdent() {
|
| 305 | return this.ident;
|
| 306 | }
|
| 307 | }
|
| 308 | }
|
| 309 |
|