|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-2022 Minekube. https://minekube.com |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + * @author Minekube |
| 23 | + * @link https://github.com/minekube/connect-java |
| 24 | + */ |
| 25 | + |
| 26 | +package com.minekube.connect.inject.velocity; |
| 27 | + |
| 28 | +import io.netty.buffer.ByteBuf; |
| 29 | +import io.netty.channel.Channel; |
| 30 | +import io.netty.channel.ChannelHandlerContext; |
| 31 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 32 | +import io.netty.channel.ChannelPipeline; |
| 33 | +import io.netty.util.ReferenceCountUtil; |
| 34 | +import java.lang.reflect.Field; |
| 35 | +import java.time.Instant; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.concurrent.ConcurrentHashMap; |
| 38 | + |
| 39 | +final class VelocityChatSessionPacketFilter extends ChannelInboundHandlerAdapter { |
| 40 | + static final String HANDLER_NAME = "connect_chat_session_filter"; |
| 41 | + |
| 42 | + private static final String MINECRAFT_DECODER = "minecraft-decoder"; |
| 43 | + private static final String PLAY_STATE = "PLAY"; |
| 44 | + private static final String SESSION_CHAT_PACKET = |
| 45 | + "com.velocitypowered.proxy.protocol.packet.chat.session.SessionPlayerChatPacket"; |
| 46 | + private static final String SESSION_COMMAND_PACKET = |
| 47 | + "com.velocitypowered.proxy.protocol.packet.chat.session.SessionPlayerCommandPacket"; |
| 48 | + private static final String UNSIGNED_COMMAND_PACKET = |
| 49 | + "com.velocitypowered.proxy.protocol.packet.chat.session.UnsignedPlayerCommandPacket"; |
| 50 | + private static final String CHAT_ACKNOWLEDGEMENT_PACKET = |
| 51 | + "com.velocitypowered.proxy.protocol.packet.chat.ChatAcknowledgementPacket"; |
| 52 | + private static final Instant EARLIEST_REASONABLE_CHAT_TIMESTAMP = Instant.EPOCH; |
| 53 | + private static final Map<String, Field> FIELDS = new ConcurrentHashMap<>(); |
| 54 | + |
| 55 | + private final boolean connectTunnel; |
| 56 | + private final ChatSessionPacketIdResolver resolver; |
| 57 | + |
| 58 | + VelocityChatSessionPacketFilter(boolean connectTunnel) { |
| 59 | + this(connectTunnel, VelocityChatSessionPacketFilter::resolveChatSessionPacketId); |
| 60 | + } |
| 61 | + |
| 62 | + VelocityChatSessionPacketFilter(boolean connectTunnel, ChatSessionPacketIdResolver resolver) { |
| 63 | + this.connectTunnel = connectTunnel; |
| 64 | + this.resolver = resolver; |
| 65 | + } |
| 66 | + |
| 67 | + static void inject(Channel channel, boolean connectTunnel) { |
| 68 | + if (!connectTunnel) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + ChannelPipeline pipeline = channel.pipeline(); |
| 73 | + if (pipeline.get(MINECRAFT_DECODER) == null || pipeline.get(HANDLER_NAME) != null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + pipeline.addAfter(MINECRAFT_DECODER, HANDLER_NAME, |
| 78 | + new VelocityChatSessionPacketFilter(true)); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| 83 | + Object sanitized = sanitizeCommandPacket(msg); |
| 84 | + if (sanitized != msg) { |
| 85 | + msg = sanitized; |
| 86 | + } |
| 87 | + |
| 88 | + if (connectTunnel && hasClassName(msg, CHAT_ACKNOWLEDGEMENT_PACKET)) { |
| 89 | + ReferenceCountUtil.release(msg); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if (connectTunnel && msg instanceof ByteBuf && shouldDrop(ctx, (ByteBuf) msg)) { |
| 94 | + ((ByteBuf) msg).release(); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + ctx.fireChannelRead(msg); |
| 99 | + } |
| 100 | + |
| 101 | + private static boolean hasClassName(Object value, String className) { |
| 102 | + return value != null && className.equals(value.getClass().getName()); |
| 103 | + } |
| 104 | + |
| 105 | + private Object sanitizeCommandPacket(Object msg) { |
| 106 | + if (!connectTunnel || !hasClassName(msg, SESSION_COMMAND_PACKET)) { |
| 107 | + return msg; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + Object timestamp = fieldValue(msg, "timeStamp"); |
| 112 | + if (!(timestamp instanceof Instant) |
| 113 | + || !((Instant) timestamp).isBefore(EARLIEST_REASONABLE_CHAT_TIMESTAMP)) { |
| 114 | + return msg; |
| 115 | + } |
| 116 | + |
| 117 | + Object command = fieldValue(msg, "command"); |
| 118 | + if (!(command instanceof String)) { |
| 119 | + return msg; |
| 120 | + } |
| 121 | + |
| 122 | + Class<?> unsignedCommandClass = Class.forName(UNSIGNED_COMMAND_PACKET, |
| 123 | + true, msg.getClass().getClassLoader()); |
| 124 | + Object unsignedCommand = unsignedCommandClass.getDeclaredConstructor().newInstance(); |
| 125 | + setFieldValue(unsignedCommand, "command", command); |
| 126 | + return unsignedCommand; |
| 127 | + } catch (ReflectiveOperationException | IllegalStateException ignored) { |
| 128 | + return msg; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private boolean shouldDrop(ChannelHandlerContext ctx, ByteBuf packet) { |
| 133 | + int chatSessionPacketId = resolver.resolve(ctx); |
| 134 | + if (chatSessionPacketId < 0) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + ByteBuf duplicate = packet.duplicate(); |
| 139 | + int packetId = readVarInt(duplicate); |
| 140 | + return packetId == chatSessionPacketId; |
| 141 | + } |
| 142 | + |
| 143 | + private static int resolveChatSessionPacketId(ChannelHandlerContext ctx) { |
| 144 | + try { |
| 145 | + Object decoder = ctx.pipeline().get(MINECRAFT_DECODER); |
| 146 | + if (decoder == null) { |
| 147 | + return -1; |
| 148 | + } |
| 149 | + |
| 150 | + Object state = fieldValue(decoder, "state"); |
| 151 | + if (!PLAY_STATE.equals(String.valueOf(state))) { |
| 152 | + return -1; |
| 153 | + } |
| 154 | + |
| 155 | + Object registry = fieldValue(decoder, "registry"); |
| 156 | + Object packetClassToId = fieldValue(registry, "packetClassToId"); |
| 157 | + if (!(packetClassToId instanceof Map)) { |
| 158 | + return -1; |
| 159 | + } |
| 160 | + |
| 161 | + for (Map.Entry<?, ?> entry : ((Map<?, ?>) packetClassToId).entrySet()) { |
| 162 | + Object key = entry.getKey(); |
| 163 | + if (key instanceof Class |
| 164 | + && SESSION_CHAT_PACKET.equals(((Class<?>) key).getName()) |
| 165 | + && entry.getValue() instanceof Number) { |
| 166 | + return ((Number) entry.getValue()).intValue() + 1; |
| 167 | + } |
| 168 | + } |
| 169 | + } catch (ReflectiveOperationException | IllegalStateException ignored) { |
| 170 | + return -1; |
| 171 | + } |
| 172 | + |
| 173 | + return -1; |
| 174 | + } |
| 175 | + |
| 176 | + private static Object fieldValue(Object target, String fieldName) throws ReflectiveOperationException { |
| 177 | + Field field = field(target.getClass(), fieldName); |
| 178 | + return field.get(target); |
| 179 | + } |
| 180 | + |
| 181 | + private static void setFieldValue(Object target, String fieldName, Object value) |
| 182 | + throws ReflectiveOperationException { |
| 183 | + Field field = field(target.getClass(), fieldName); |
| 184 | + field.set(target, value); |
| 185 | + } |
| 186 | + |
| 187 | + private static Field field(Class<?> type, String fieldName) { |
| 188 | + String key = type.getName() + "#" + fieldName; |
| 189 | + return FIELDS.computeIfAbsent(key, $ -> { |
| 190 | + try { |
| 191 | + Class<?> current = type; |
| 192 | + while (current != null) { |
| 193 | + try { |
| 194 | + Field field = current.getDeclaredField(fieldName); |
| 195 | + field.setAccessible(true); |
| 196 | + return field; |
| 197 | + } catch (NoSuchFieldException ignored) { |
| 198 | + current = current.getSuperclass(); |
| 199 | + } |
| 200 | + } |
| 201 | + throw new NoSuchFieldException(fieldName); |
| 202 | + } catch (NoSuchFieldException exception) { |
| 203 | + throw new IllegalStateException(exception); |
| 204 | + } |
| 205 | + }); |
| 206 | + } |
| 207 | + |
| 208 | + private static int readVarInt(ByteBuf buf) { |
| 209 | + int value = 0; |
| 210 | + int position = 0; |
| 211 | + byte currentByte; |
| 212 | + |
| 213 | + do { |
| 214 | + currentByte = buf.readByte(); |
| 215 | + value |= (currentByte & 0x7F) << position; |
| 216 | + position += 7; |
| 217 | + } while ((currentByte & 0x80) != 0 && position < 35); |
| 218 | + |
| 219 | + return value; |
| 220 | + } |
| 221 | + |
| 222 | + interface ChatSessionPacketIdResolver { |
| 223 | + int resolve(ChannelHandlerContext ctx); |
| 224 | + } |
| 225 | +} |
0 commit comments