From 566a306d18e9a8542aa5e4f6737b48b9a153d022 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 28 Dec 2018 12:29:24 -0500 Subject: [PATCH] Clarity improvements. --- .../proxy/protocol/netty/GS4QueryHandler.java | 4 +- .../protocol/netty/LegacyPingEncoder.java | 2 +- .../netty/MinecraftCompressDecoder.java | 18 ++--- .../protocol/util/NettyPreconditions.java | 67 +++++++++++++++++++ 4 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/protocol/util/NettyPreconditions.java diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java index 560d0aafd..1b8ef41d8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java @@ -263,8 +263,6 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler if (isBasic) { return; } - - writeString(buf, "plugins"); StringBuilder pluginsString = new StringBuilder(); pluginsString.append(serverVersion).append(':').append(' '); @@ -279,7 +277,7 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler } } - writeString(buf, pluginsString.toString()); + write("plugins", pluginsString.toString()); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/LegacyPingEncoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/LegacyPingEncoder.java index 436eedaa5..f45981a78 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/LegacyPingEncoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/LegacyPingEncoder.java @@ -24,6 +24,6 @@ public class LegacyPingEncoder extends MessageToByteEncoder { private static void writeLegacyString(ByteBuf out, String string) { out.writeShort(string.length()); - out.writeBytes(string.getBytes(StandardCharsets.UTF_16BE)); + out.writeCharSequence(string, StandardCharsets.UTF_16BE); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java index ef7c934ae..f8afa0d48 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressDecoder.java @@ -1,6 +1,7 @@ package com.velocitypowered.proxy.protocol.netty; -import com.google.common.base.Preconditions; +import static com.velocitypowered.proxy.protocol.util.NettyPreconditions.checkFrame; + import com.velocitypowered.natives.compression.VelocityCompressor; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; @@ -22,23 +23,24 @@ public class MinecraftCompressDecoder extends MessageToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List out) throws Exception { - int uncompressedSize = ProtocolUtils.readVarInt(msg); - if (uncompressedSize == 0) { + int expectedUncompressedSize = ProtocolUtils.readVarInt(msg); + if (expectedUncompressedSize == 0) { // Strip the now-useless uncompressed size, this message is already uncompressed. out.add(msg.retainedSlice()); msg.skipBytes(msg.readableBytes()); return; } - Preconditions.checkState(uncompressedSize >= threshold, + checkFrame(expectedUncompressedSize >= threshold, "Uncompressed size %s is greater than threshold %s", - uncompressedSize, threshold); + expectedUncompressedSize, threshold); ByteBuf uncompressed = ctx.alloc() - .buffer(Math.min(uncompressedSize, MAXIMUM_INITIAL_BUFFER_SIZE)); + .buffer(Math.min(expectedUncompressedSize, MAXIMUM_INITIAL_BUFFER_SIZE)); try { compressor.inflate(msg, uncompressed); - Preconditions.checkState(uncompressedSize == uncompressed.readableBytes(), - "Mismatched compression sizes"); + checkFrame(expectedUncompressedSize == uncompressed.readableBytes(), + "Mismatched compression sizes (got %s, expected %s)", + uncompressed.readableBytes(), expectedUncompressedSize); out.add(uncompressed); } catch (Exception e) { uncompressed.release(); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/NettyPreconditions.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/NettyPreconditions.java new file mode 100644 index 000000000..5c5cabde1 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/util/NettyPreconditions.java @@ -0,0 +1,67 @@ +package com.velocitypowered.proxy.protocol.util; + +import com.google.common.base.Strings; +import io.netty.handler.codec.CorruptedFrameException; + +/** + * Extends {@link com.google.common.base.Preconditions} for Netty's {@link CorruptedFrameException}. + */ +public class NettyPreconditions { + private NettyPreconditions() { + throw new AssertionError(); + } + + /** + * Throws {@link CorruptedFrameException} if {@code b} is false. + * @param b the expression to check + * @param message the message to include in the thrown {@link CorruptedFrameException} + */ + public static void checkFrame(boolean b, String message) { + if (!b) { + throw new CorruptedFrameException(message); + } + } + + /** + * Throws {@link CorruptedFrameException} if {@code b} is false. + * @param b the expression to check + * @param message the message to include in the thrown {@link CorruptedFrameException}, formatted + * like {@link com.google.common.base.Preconditions#checkArgument(boolean)} and + * friends + * @param arg1 the first argument to format the message with + */ + public static void checkFrame(boolean b, String message, Object arg1) { + if (!b) { + throw new CorruptedFrameException(Strings.lenientFormat(message, arg1)); + } + } + + /** + * Throws {@link CorruptedFrameException} if {@code b} is false. + * @param b the expression to check + * @param message the message to include in the thrown {@link CorruptedFrameException}, formatted + * like {@link com.google.common.base.Preconditions#checkArgument(boolean)} and + * friends + * @param arg1 the first argument to format the message with + * @param arg2 the second argument to format the message with + */ + public static void checkFrame(boolean b, String message, Object arg1, Object arg2) { + if (!b) { + throw new CorruptedFrameException(Strings.lenientFormat(message, arg1, arg2)); + } + } + + /** + * Throws {@link CorruptedFrameException} if {@code b} is false. + * @param b the expression to check + * @param message the message to include in the thrown {@link CorruptedFrameException}, formatted + * like {@link com.google.common.base.Preconditions#checkArgument(boolean)} and + * friends + * @param args the arguments to format the message with- + */ + public static void checkFrame(boolean b, String message, Object... args) { + if (!b) { + throw new CorruptedFrameException(Strings.lenientFormat(message, args)); + } + } +}