| 1 | package net.minecraft.server.network;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import io.netty.buffer.ByteBufAllocator;
|
| 6 | import io.netty.channel.ChannelFutureListener;
|
| 7 | import io.netty.channel.ChannelHandlerContext;
|
| 8 | import io.netty.channel.ChannelInboundHandlerAdapter;
|
| 9 | import java.net.SocketAddress;
|
| 10 | import java.util.Locale;
|
| 11 | import net.minecraft.server.ServerInfo;
|
| 12 | import org.slf4j.Logger;
|
| 13 |
|
| 14 | public class LegacyQueryHandler extends ChannelInboundHandlerAdapter {
|
| 15 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 16 | private final ServerInfo server;
|
| 17 |
|
| 18 | public LegacyQueryHandler(final ServerInfo server) {
|
| 19 | this.server = server;
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
|
| 24 | ByteBuf in = (ByteBuf)msg;
|
| 25 | in.markReaderIndex();
|
| 26 | boolean connectNormally = true;
|
| 27 |
|
| 28 | try {
|
| 29 | try {
|
| 30 | if (in.readUnsignedByte() != 254) {
|
| 31 | return;
|
| 32 | }
|
| 33 |
|
| 34 | SocketAddress socket = ctx.channel().remoteAddress();
|
| 35 | int length = in.readableBytes();
|
| 36 | if (length == 0) {
|
| 37 | LOGGER.debug("Ping: (<1.3.x) from {}", socket);
|
| 38 | String body = createVersion0Response(this.server);
|
| 39 | sendFlushAndClose(ctx, createLegacyDisconnectPacket(ctx.alloc(), body));
|
| 40 | } else {
|
| 41 | if (in.readUnsignedByte() != 1) {
|
| 42 | return;
|
| 43 | }
|
| 44 |
|
| 45 | if (in.isReadable()) {
|
| 46 | if (!readCustomPayloadPacket(in)) {
|
| 47 | return;
|
| 48 | }
|
| 49 |
|
| 50 | LOGGER.debug("Ping: (1.6) from {}", socket);
|
| 51 | } else {
|
| 52 | LOGGER.debug("Ping: (1.4-1.5.x) from {}", socket);
|
| 53 | }
|
| 54 |
|
| 55 | String body = createVersion1Response(this.server);
|
| 56 | sendFlushAndClose(ctx, createLegacyDisconnectPacket(ctx.alloc(), body));
|
| 57 | }
|
| 58 |
|
| 59 | in.release();
|
| 60 | connectNormally = false;
|
| 61 | } catch (RuntimeException var11) {
|
| 62 | }
|
| 63 | } finally {
|
| 64 | if (connectNormally) {
|
| 65 | in.resetReaderIndex();
|
| 66 | ctx.channel().pipeline().remove(this);
|
| 67 | ctx.fireChannelRead(msg);
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | private static boolean readCustomPayloadPacket(final ByteBuf in) {
|
| 73 | short packetId = in.readUnsignedByte();
|
| 74 | if (packetId != 250) {
|
| 75 | return false;
|
| 76 | }
|
| 77 |
|
| 78 | String channelId = LegacyProtocolUtils.readLegacyString(in);
|
| 79 | if (!"MC|PingHost".equals(channelId)) {
|
| 80 | return false;
|
| 81 | }
|
| 82 |
|
| 83 | int payloadSize = in.readUnsignedShort();
|
| 84 | if (in.readableBytes() != payloadSize) {
|
| 85 | return false;
|
| 86 | }
|
| 87 |
|
| 88 | short protocolVersion = in.readUnsignedByte();
|
| 89 | if (protocolVersion < 73) {
|
| 90 | return false;
|
| 91 | }
|
| 92 |
|
| 93 | String host = LegacyProtocolUtils.readLegacyString(in);
|
| 94 | int port = in.readInt();
|
| 95 | return port <= 65535;
|
| 96 | }
|
| 97 |
|
| 98 | private static String createVersion0Response(final ServerInfo server) {
|
| 99 | return String.format(Locale.ROOT, "%s§%d§%d", server.getMotd(), server.getPlayerCount(), server.getMaxPlayers());
|
| 100 | }
|
| 101 |
|
| 102 | private static String createVersion1Response(final ServerInfo server) {
|
| 103 | return String.format(
|
| 104 | Locale.ROOT, "§1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d", 127, server.getServerVersion(), server.getMotd(), server.getPlayerCount(), server.getMaxPlayers()
|
| 105 | );
|
| 106 | }
|
| 107 |
|
| 108 | private static void sendFlushAndClose(final ChannelHandlerContext ctx, final ByteBuf out) {
|
| 109 | ctx.pipeline().firstContext().writeAndFlush(out).addListener(ChannelFutureListener.CLOSE);
|
| 110 | }
|
| 111 |
|
| 112 | private static ByteBuf createLegacyDisconnectPacket(final ByteBufAllocator alloc, final String reason) {
|
| 113 | ByteBuf out = alloc.buffer();
|
| 114 | out.writeByte(255);
|
| 115 | LegacyProtocolUtils.writeLegacyString(out, reason);
|
| 116 | return out;
|
| 117 | }
|
| 118 | }
|
| 119 |
|