From c979c611558c5b736f59f3c1efbf0bda55722dd6 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 14 Jun 2021 13:59:56 -0400 Subject: [PATCH] Add some internal injection points for the benefit of ViaVersion See #524 for more details --- .../proxy/connection/MinecraftConnection.java | 22 +++++++++++++++++-- .../protocol/VelocityConnectionEvent.java | 11 ++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/protocol/VelocityConnectionEvent.java diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java index ba172db42..773f6d359 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/MinecraftConnection.java @@ -39,16 +39,19 @@ import com.velocitypowered.proxy.connection.client.StatusSessionHandler; import com.velocitypowered.proxy.network.Connections; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.StateRegistry; +import com.velocitypowered.proxy.protocol.VelocityConnectionEvent; import com.velocitypowered.proxy.protocol.netty.MinecraftCipherDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftCipherEncoder; import com.velocitypowered.proxy.protocol.netty.MinecraftCompressDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftCompressorAndLengthEncoder; import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder; import com.velocitypowered.proxy.protocol.netty.MinecraftEncoder; +import com.velocitypowered.proxy.protocol.netty.MinecraftVarintLengthEncoder; import com.velocitypowered.proxy.util.except.QuietDecoderException; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.EventLoop; @@ -355,6 +358,7 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { public void setProtocolVersion(ProtocolVersion protocolVersion) { ensureInEventLoop(); + boolean changed = this.protocolVersion != protocolVersion; this.protocolVersion = protocolVersion; if (protocolVersion != ProtocolVersion.LEGACY) { this.channel.pipeline().get(MinecraftEncoder.class).setProtocolVersion(protocolVersion); @@ -364,6 +368,10 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { this.channel.pipeline().remove(MINECRAFT_ENCODER); this.channel.pipeline().remove(MINECRAFT_DECODER); } + + if (changed) { + channel.pipeline().fireUserEventTriggered(VelocityConnectionEvent.PROTOCOL_VERSION_CHANGED); + } } public @Nullable MinecraftSessionHandler getSessionHandler() { @@ -398,8 +406,14 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { ensureInEventLoop(); if (threshold == -1) { - channel.pipeline().remove(COMPRESSION_DECODER); - channel.pipeline().remove(COMPRESSION_ENCODER); + final ChannelHandler removedDecoder = channel.pipeline().remove(COMPRESSION_DECODER); + final ChannelHandler removedEncoder = channel.pipeline().remove(COMPRESSION_ENCODER); + + if (removedDecoder != null && removedEncoder != null) { + channel.pipeline().addBefore(MINECRAFT_DECODER, FRAME_ENCODER, + MinecraftVarintLengthEncoder.INSTANCE); + channel.pipeline().fireUserEventTriggered(VelocityConnectionEvent.COMPRESSION_DISABLED); + } } else { MinecraftCompressDecoder decoder = (MinecraftCompressDecoder) channel.pipeline() .get(COMPRESSION_DECODER); @@ -418,6 +432,8 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { channel.pipeline().remove(FRAME_ENCODER); channel.pipeline().addBefore(MINECRAFT_DECODER, COMPRESSION_DECODER, decoder); channel.pipeline().addBefore(MINECRAFT_ENCODER, COMPRESSION_ENCODER, encoder); + + channel.pipeline().fireUserEventTriggered(VelocityConnectionEvent.COMPRESSION_ENABLED); } } } @@ -440,6 +456,8 @@ public class MinecraftConnection extends ChannelInboundHandlerAdapter { .addBefore(FRAME_DECODER, CIPHER_DECODER, new MinecraftCipherDecoder(decryptionCipher)); channel.pipeline() .addBefore(FRAME_ENCODER, CIPHER_ENCODER, new MinecraftCipherEncoder(encryptionCipher)); + + channel.pipeline().fireUserEventTriggered(VelocityConnectionEvent.ENCRYPTION_ENABLED); } public @Nullable MinecraftConnectionAssociation getAssociation() { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/VelocityConnectionEvent.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/VelocityConnectionEvent.java new file mode 100644 index 000000000..74ea7d6bd --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/VelocityConnectionEvent.java @@ -0,0 +1,11 @@ +package com.velocitypowered.proxy.protocol; + +/** + * Describes various events fired during the course of a connection. + */ +public enum VelocityConnectionEvent { + COMPRESSION_ENABLED, + COMPRESSION_DISABLED, + ENCRYPTION_ENABLED, + PROTOCOL_VERSION_CHANGED +}