diff --git a/src/main/java/net/raphimc/viaprotocolhack/netty/VPHLegacyPipeline.java b/src/main/java/net/raphimc/viaprotocolhack/netty/VPHLegacyPipeline.java new file mode 100644 index 0000000..1d77349 --- /dev/null +++ b/src/main/java/net/raphimc/viaprotocolhack/netty/VPHLegacyPipeline.java @@ -0,0 +1,142 @@ +/* + * This file is part of ViaProtocolHack - https://github.com/RaphiMC/ViaProtocolHack + * Copyright (C) 2023 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaprotocolhack.netty; + +import com.viaversion.viaversion.api.connection.UserConnection; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import net.raphimc.viabedrock.netty.BatchLengthCodec; +import net.raphimc.viabedrock.netty.PacketEncapsulationCodec; +import net.raphimc.viabedrock.protocol.BedrockBaseProtocol; +import net.raphimc.vialegacy.netty.PreNettyLengthPrepender; +import net.raphimc.vialegacy.netty.PreNettyLengthRemover; +import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.baseprotocols.PreNettyBaseProtocol; +import net.raphimc.viaprotocolhack.netty.viabedrock.DisconnectHandler; +import net.raphimc.viaprotocolhack.netty.viabedrock.RakMessageEncapsulationCodec; +import net.raphimc.viaprotocolhack.util.VersionEnum; + +public abstract class VPHLegacyPipeline extends ChannelInboundHandlerAdapter { + + public static final String VIA_DECODER_NAME = "via-decoder"; + public static final String VIA_ENCODER_NAME = "via-encoder"; + + public static final String VIALEGACY_PRE_NETTY_LENGTH_PREPENDER_NAME = "vialegacy-pre-netty-length-prepender"; + public static final String VIALEGACY_PRE_NETTY_LENGTH_REMOVER_NAME = "vialegacy-pre-netty-length-remover"; + + public static final String VIABEDROCK_DISCONNECT_HANDLER_NAME = "viabedrock-disconnect-handler"; + public static final String VIABEDROCK_FRAME_ENCAPSULATION_HANDLER_NAME = "viabedrock-frame-encapsulation"; + public static final String VIABEDROCK_PACKET_ENCAPSULATION_HANDLER_NAME = "viabedrock-packet-encapsulation"; + + protected final UserConnection user; + protected final VersionEnum version; + + public VPHLegacyPipeline(final UserConnection user, final VersionEnum version) { + this.user = user; + this.version = version; + } + + @Override + public void handlerAdded(ChannelHandlerContext ctx) { + ctx.pipeline().addBefore(this.packetDecoderName(), VIA_DECODER_NAME, this.createViaDecoder()); + ctx.pipeline().addBefore(this.packetEncoderName(), VIA_ENCODER_NAME, this.createViaEncoder()); + + if (this.version.isOlderThanOrEqualTo(VersionEnum.r1_6_4)) { + this.user.getProtocolInfo().getPipeline().add(PreNettyBaseProtocol.INSTANCE); + ctx.pipeline().addBefore(this.lengthSplitterName(), VIALEGACY_PRE_NETTY_LENGTH_PREPENDER_NAME, this.createViaLegacyPreNettyLengthPrepender()); + ctx.pipeline().addBefore(this.lengthPrependerName(), VIALEGACY_PRE_NETTY_LENGTH_REMOVER_NAME, this.createViaLegacyPreNettyLengthRemover()); + } else if (this.version.equals(VersionEnum.bedrockLatest)) { + this.user.getProtocolInfo().getPipeline().add(BedrockBaseProtocol.INSTANCE); + ctx.pipeline().addBefore(this.lengthSplitterName(), VIABEDROCK_DISCONNECT_HANDLER_NAME, this.createViaBedrockDisconnectHandler()); + ctx.pipeline().addBefore(this.lengthSplitterName(), VIABEDROCK_FRAME_ENCAPSULATION_HANDLER_NAME, this.createViaBedrockFrameEncapsulationHandler()); + this.replaceLengthSplitter(ctx, this.createViaBedrockBatchLengthCodec()); + ctx.pipeline().remove(this.lengthPrependerName()); + ctx.pipeline().addBefore(VIA_DECODER_NAME, VIABEDROCK_PACKET_ENCAPSULATION_HANDLER_NAME, this.createViaBedrockPacketEncapsulationHandler()); + } + } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + if (CompressionReorderEvent.INSTANCE.equals(evt)) { + final int decoderIndex = ctx.pipeline().names().indexOf(decompressName()); + if (decoderIndex == -1) return; + + if (decoderIndex > ctx.pipeline().names().indexOf(VIA_DECODER_NAME)) { + final ChannelHandler decoder = ctx.pipeline().get(VIA_DECODER_NAME); + final ChannelHandler encoder = ctx.pipeline().get(VIA_ENCODER_NAME); + + ctx.pipeline().remove(decoder); + ctx.pipeline().remove(encoder); + + ctx.pipeline().addAfter(decompressName(), VIA_DECODER_NAME, decoder); + ctx.pipeline().addAfter(compressName(), VIA_ENCODER_NAME, encoder); + } + } + + super.userEventTriggered(ctx, evt); + } + + protected ChannelHandler createViaDecoder() { + return new ViaDecoder(this.user); + } + + protected ChannelHandler createViaEncoder() { + return new ViaEncoder(this.user); + } + + protected ChannelHandler createViaLegacyPreNettyLengthPrepender() { + return new PreNettyLengthPrepender(this.user); + } + + protected ChannelHandler createViaLegacyPreNettyLengthRemover() { + return new PreNettyLengthRemover(this.user); + } + + protected ChannelHandler createViaBedrockDisconnectHandler() { + return new DisconnectHandler(); + } + + protected ChannelHandler createViaBedrockFrameEncapsulationHandler() { + return new RakMessageEncapsulationCodec(); + } + + protected ChannelHandler createViaBedrockBatchLengthCodec() { + return new BatchLengthCodec(); + } + + protected ChannelHandler createViaBedrockPacketEncapsulationHandler() { + return new PacketEncapsulationCodec(); + } + + protected void replaceLengthSplitter(final ChannelHandlerContext ctx, final ChannelHandler handler) { + ctx.pipeline().replace(this.lengthSplitterName(), this.lengthSplitterName(), handler); + } + + protected abstract String decompressName(); + + protected abstract String compressName(); + + protected abstract String packetDecoderName(); + + protected abstract String packetEncoderName(); + + protected abstract String lengthSplitterName(); + + protected abstract String lengthPrependerName(); + +} diff --git a/src/main/java/net/raphimc/viaprotocolhack/netty/ViaDecoder.java b/src/main/java/net/raphimc/viaprotocolhack/netty/ViaDecoder.java new file mode 100644 index 0000000..40ad5ff --- /dev/null +++ b/src/main/java/net/raphimc/viaprotocolhack/netty/ViaDecoder.java @@ -0,0 +1,70 @@ +/* + * This file is part of ViaProtocolHack - https://github.com/RaphiMC/ViaProtocolHack + * Copyright (C) 2023 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaprotocolhack.netty; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.exception.CancelCodecException; +import com.viaversion.viaversion.exception.CancelDecoderException; +import com.viaversion.viaversion.util.PipelineUtil; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToMessageDecoder; + +import java.util.List; + +public class ViaDecoder extends MessageToMessageDecoder { + + protected final UserConnection user; + + public ViaDecoder(final UserConnection user) { + this.user = user; + } + + @Override + protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { + if (!this.user.checkIncomingPacket()) throw CancelDecoderException.generate(null); + + final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(in); + try { + if (this.user.shouldTransformPacket()) { + this.user.transformIncoming(transformedBuf, CancelDecoderException::generate); + } + out.add(transformedBuf.retain()); + } finally { + transformedBuf.release(); + } + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + try { + super.channelRead(ctx, msg); + } catch (Throwable e) { + if (!PipelineUtil.containsCause(e, CancelCodecException.class)) { + throw e; + } + } + } + + @Override + public boolean isSharable() { + // Netty doesn't allow codecs to be shared, but we need it to be shared because of the pipeline reordering. + // The check if it is sharable is done in the constructor and can be bypassed by returning false during that check. + return this.user != null; + } +} diff --git a/src/main/java/net/raphimc/viaprotocolhack/netty/ViaEncoder.java b/src/main/java/net/raphimc/viaprotocolhack/netty/ViaEncoder.java new file mode 100644 index 0000000..9cd21cb --- /dev/null +++ b/src/main/java/net/raphimc/viaprotocolhack/netty/ViaEncoder.java @@ -0,0 +1,76 @@ +/* + * This file is part of ViaProtocolHack - https://github.com/RaphiMC/ViaProtocolHack + * Copyright (C) 2023 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaprotocolhack.netty; + +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.exception.CancelCodecException; +import com.viaversion.viaversion.exception.CancelEncoderException; +import com.viaversion.viaversion.util.PipelineUtil; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; +import io.netty.handler.codec.MessageToMessageEncoder; + +import java.util.List; + +public class ViaEncoder extends MessageToMessageEncoder { + + protected final UserConnection user; + + public ViaEncoder(final UserConnection user) { + this.user = user; + } + + @Override + protected void encode(ChannelHandlerContext ctx, ByteBuf byteBuf, List out) throws Exception { + if (!user.checkOutgoingPacket()) throw CancelEncoderException.generate(null); + if (!user.shouldTransformPacket()) { + out.add(byteBuf.retain()); + return; + } + + ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(byteBuf); + try { + user.transformOutgoing(transformedBuf, CancelEncoderException::generate); + + out.add(transformedBuf.retain()); + } finally { + transformedBuf.release(); + } + } + + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + try { + super.write(ctx, msg, promise); + } catch (Throwable e) { + if (!PipelineUtil.containsCause(e, CancelCodecException.class)) { + throw e; + } else { + promise.setSuccess(); + } + } + } + + @Override + public boolean isSharable() { + // Netty doesn't allow codecs to be shared, but we need it to be shared because of the pipeline reordering. + // The check if it is sharable is done in the constructor and can be bypassed by returning false during that check. + return this.user != null; + } +} diff --git a/src/main/java/net/raphimc/viaprotocolhack/util/VersionRange.java b/src/main/java/net/raphimc/viaprotocolhack/util/VersionRange.java new file mode 100644 index 0000000..f179557 --- /dev/null +++ b/src/main/java/net/raphimc/viaprotocolhack/util/VersionRange.java @@ -0,0 +1,60 @@ +/* + * This file is part of ViaProtocolHack - https://github.com/RaphiMC/ViaProtocolHack + * Copyright (C) 2023 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaprotocolhack.util; + +public class VersionRange { + + private final VersionEnum lowerBound; + private final VersionEnum upperBound; + + public VersionRange(VersionEnum lowerBound, VersionEnum upperBound) { + if (lowerBound == null && upperBound == null) { + throw new RuntimeException("Invalid protocol range"); + } + this.lowerBound = lowerBound; + this.upperBound = upperBound; + } + + public static VersionRange andNewer(final VersionEnum version) { + return new VersionRange(null, version); + } + + public static VersionRange singleton(final VersionEnum version) { + return new VersionRange(version, version); + } + + public static VersionRange andOlder(final VersionEnum version) { + return new VersionRange(version, null); + } + + public boolean contains(final VersionEnum protocolVersion) { + if (this.lowerBound != null && lowerBound.isOlderThan(protocolVersion)) return false; + + return this.upperBound == null || upperBound.isOlderThanOrEqualTo(protocolVersion); + } + + @Override + public String toString() { + if (lowerBound == null) return upperBound.getName() + "+"; + if (upperBound == null) return lowerBound.getName() + "-"; + if (lowerBound == upperBound) return lowerBound.getName(); + + return lowerBound.getName() + " - " + upperBound.getName(); + } + +}