diff --git a/src/main/java/de/florianmichael/viafabricplus/ViaFabricPlus.java b/src/main/java/de/florianmichael/viafabricplus/ViaFabricPlus.java index 59a237bb..905ad6af 100644 --- a/src/main/java/de/florianmichael/viafabricplus/ViaFabricPlus.java +++ b/src/main/java/de/florianmichael/viafabricplus/ViaFabricPlus.java @@ -62,7 +62,7 @@ public class ViaFabricPlus { // Protocol Translator ChatLengthCalculation.create(); - new ProtocolHack(); + ProtocolHack.init(); FinishMinecraftLoadCallback.EVENT.register(() -> { // General settings diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection.java index 3c5f5546..c27591fd 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection.java @@ -21,7 +21,8 @@ import de.florianmichael.viafabricplus.event.ChangeProtocolVersionCallback; import de.florianmichael.viafabricplus.event.DisconnectConnectionCallback; import de.florianmichael.viafabricplus.injection.access.IClientConnection; import de.florianmichael.viafabricplus.protocolhack.ProtocolHack; -import de.florianmichael.viafabricplus.protocolhack.ViaFabricPlusVLBPipeline; +import de.florianmichael.viafabricplus.protocolhack.netty.ViaFabricPlusVLBPipeline; +import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.RakNetClientConnection; import de.florianmichael.vialoadingbase.ViaLoadingBase; import de.florianmichael.vialoadingbase.netty.event.CompressionReorderEvent; import io.netty.channel.*; @@ -89,7 +90,7 @@ public abstract class MixinClientConnection extends SimpleChannelInboundHandler< ((IClientConnection) clientConnection).viafabricplus_captureAddress(address); if (ProtocolHack.getForcedVersions().containsKey(address) ? (ProtocolHack.getForcedVersions().get(address).getVersion() == BedrockProtocolVersion.bedrockLatest.getVersion()) : ProtocolHack.getTargetVersion().isEqualTo(BedrockProtocolVersion.bedrockLatest)) { - ProtocolHack.connectRakNet(clientConnection, address, lazy, class_); + RakNetClientConnection.connectRakNet(clientConnection, address, lazy, class_); cir.setReturnValue(clientConnection); } } @@ -131,7 +132,7 @@ public abstract class MixinClientConnection extends SimpleChannelInboundHandler< if (this.viafabricplus_compressionEnabled) throw new IllegalStateException("Compression is already enabled"); this.viafabricplus_compressionEnabled = true; - this.channel.pipeline().addBefore(ViaFabricPlusVLBPipeline.BATCH_LENGTH_HANDLER_NAME, ViaFabricPlusVLBPipeline.COMPRESSION_HANDLER_NAME, new ZLibCompression()); + this.channel.pipeline().addBefore(ViaFabricPlusVLBPipeline.VIA_BEDROCK_BATCH_LENGTH_HANDLER_NAME, ViaFabricPlusVLBPipeline.VIA_BEDROCK_COMPRESSION_HANDLER_NAME, new ZLibCompression()); } @Override @@ -139,7 +140,7 @@ public abstract class MixinClientConnection extends SimpleChannelInboundHandler< if (this.viafabricplus_compressionEnabled) throw new IllegalStateException("Compression is already enabled"); this.viafabricplus_compressionEnabled = true; - this.channel.pipeline().addBefore(ViaFabricPlusVLBPipeline.BATCH_LENGTH_HANDLER_NAME, ViaFabricPlusVLBPipeline.COMPRESSION_HANDLER_NAME, new SnappyCompression()); + this.channel.pipeline().addBefore(ViaFabricPlusVLBPipeline.VIA_BEDROCK_BATCH_LENGTH_HANDLER_NAME, ViaFabricPlusVLBPipeline.VIA_BEDROCK_COMPRESSION_HANDLER_NAME, new SnappyCompression()); } @Override @@ -147,6 +148,6 @@ public abstract class MixinClientConnection extends SimpleChannelInboundHandler< if (this.encrypted) throw new IllegalStateException("Encryption is already enabled"); this.encrypted = true; - this.channel.pipeline().addAfter(ViaFabricPlusVLBPipeline.FRAME_ENCAPSULATION_HANDLER_NAME, ViaFabricPlusVLBPipeline.ENCRYPTION_HANDLER_NAME, new AesEncryption(secretKey)); + this.channel.pipeline().addAfter(ViaFabricPlusVLBPipeline.VIA_BEDROCK_FRAME_ENCAPSULATION_HANDLER_NAME, ViaFabricPlusVLBPipeline.VIA_BEDROCK_ENCRYPTION_HANDLER_NAME, new AesEncryption(secretKey)); } } diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection_1.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection_1.java index 836bf668..fecab7c9 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection_1.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/base/MixinClientConnection_1.java @@ -39,7 +39,7 @@ public class MixinClientConnection_1 { @Inject(method = "initChannel", at = @At("TAIL")) public void hackNettyPipeline(Channel channel, CallbackInfo ci) { if (channel instanceof SocketChannel) { - ProtocolHack.hookProtocolHack(field_11663, channel, ((IClientConnection) field_11663).viafabricplus_capturedAddress()); + ProtocolHack.injectVLBPipeline(field_11663, channel, ((IClientConnection) field_11663).viafabricplus_capturedAddress()); } } } diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/ProtocolHack.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/ProtocolHack.java index 0f63db87..db8c09d6 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/ProtocolHack.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/ProtocolHack.java @@ -28,6 +28,7 @@ import de.florianmichael.viafabricplus.ViaFabricPlus; import de.florianmichael.viafabricplus.definition.v1_19_0.provider.CommandArgumentsProvider; import de.florianmichael.viafabricplus.event.ChangeProtocolVersionCallback; import de.florianmichael.viafabricplus.event.FinishViaLoadingBaseStartupCallback; +import de.florianmichael.viafabricplus.protocolhack.netty.ViaFabricPlusVLBPipeline; import de.florianmichael.viafabricplus.protocolhack.platform.ViaAprilFoolsPlatformImpl; import de.florianmichael.viafabricplus.protocolhack.platform.ViaBedrockPlatformImpl; import de.florianmichael.viafabricplus.protocolhack.platform.ViaLegacyPlatformImpl; @@ -41,18 +42,11 @@ import de.florianmichael.viafabricplus.protocolhack.provider.vialoadingbase.ViaF import de.florianmichael.vialoadingbase.ViaLoadingBase; import de.florianmichael.vialoadingbase.model.ComparableProtocolVersion; import de.florianmichael.vialoadingbase.model.Platform; -import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; -import io.netty.channel.epoll.EpollDatagramChannel; -import io.netty.channel.epoll.EpollSocketChannel; -import io.netty.channel.socket.nio.NioDatagramChannel; -import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.util.AttributeKey; import net.minecraft.SharedConstants; import net.minecraft.client.MinecraftClient; import net.minecraft.network.ClientConnection; -import net.minecraft.network.NetworkSide; -import net.minecraft.util.Lazy; import net.raphimc.viaaprilfools.api.AprilFoolsProtocolVersion; import net.raphimc.viabedrock.api.BedrockProtocolVersion; import net.raphimc.viabedrock.protocol.providers.BlobCacheProvider; @@ -64,13 +58,9 @@ import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.provide import net.raphimc.vialegacy.protocols.release.protocol1_3_1_2to1_2_4_5.providers.OldAuthProvider; import net.raphimc.vialegacy.protocols.release.protocol1_7_2_5to1_6_4.providers.EncryptionProvider; import net.raphimc.vialegacy.protocols.release.protocol1_8to1_7_6_10.providers.GameProfileFetcher; -import org.cloudburstmc.netty.channel.raknet.RakChannelFactory; -import org.cloudburstmc.netty.channel.raknet.config.RakChannelOption; -import org.jetbrains.annotations.NotNull; import java.net.InetSocketAddress; import java.util.*; -import java.util.concurrent.ThreadLocalRandom; public class ProtocolHack { public final static AttributeKey LOCAL_VIA_CONNECTION = AttributeKey.newInstance("viafabricplus-via-connection"); @@ -112,10 +102,7 @@ public class ProtocolHack { return ProtocolHack.getTargetVersion().isOlderThanOrEqualTo(version); } - /** - * This method adds the channel handlers required for ViaVersion, ViaBackwards, ViaAprilFools and ViaLegacy, it also tracks the Via and Minecraft Connection - */ - public static void hookProtocolHack(final ClientConnection connection, final Channel channel, final InetSocketAddress address) { + public static void injectVLBPipeline(final ClientConnection connection, final Channel channel, final InetSocketAddress address) { if (ProtocolHack.getForcedVersions().containsKey(address)) { channel.attr(ProtocolHack.FORCED_VERSION).set(ProtocolHack.getForcedVersions().get(address)); ProtocolHack.getForcedVersions().remove(address); @@ -129,40 +116,7 @@ public class ProtocolHack { channel.pipeline().addLast(new ViaFabricPlusVLBPipeline(user, address, ProtocolHack.getTargetVersion(channel))); } - /** - * This method represents the rak net connection for bedrock edition, it's a replacement of the ClientConnection#connect method - */ - public static void connectRakNet(final ClientConnection clientConnection, final InetSocketAddress address, final Lazy lazy, final Class channelType) { - Bootstrap nettyBoostrap = new Bootstrap(); - nettyBoostrap = nettyBoostrap.group((EventLoopGroup) lazy.get()); - nettyBoostrap = nettyBoostrap.handler(new ChannelInitializer<>() { - @Override - protected void initChannel(@NotNull Channel channel) throws Exception { - try { - channel.config().setOption(RakChannelOption.RAK_PROTOCOL_VERSION, 11); - channel.config().setOption(RakChannelOption.RAK_CONNECT_TIMEOUT, 4_000L); - channel.config().setOption(RakChannelOption.RAK_SESSION_TIMEOUT, 30_000L); - channel.config().setOption(RakChannelOption.RAK_GUID, ThreadLocalRandom.current().nextLong()); - } catch (Exception ignored) { - } - ChannelPipeline channelPipeline = channel.pipeline().addLast("timeout", new ReadTimeoutHandler(30)); - ClientConnection.addHandlers(channelPipeline, NetworkSide.CLIENTBOUND); - - channelPipeline.addLast("packet_handler", clientConnection); - - hookProtocolHack(clientConnection, channel, address); - } - }); - nettyBoostrap = nettyBoostrap.channelFactory(channelType == EpollSocketChannel.class ? RakChannelFactory.client(EpollDatagramChannel.class) : RakChannelFactory.client(NioDatagramChannel.class)); - - if (ProtocolHack.getRakNetPingSessions().contains(address)) { - nettyBoostrap.bind(new InetSocketAddress(0)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE).syncUninterruptibly(); - } else { - nettyBoostrap.connect(address.getAddress(), address.getPort()).syncUninterruptibly(); - } - } - - public ProtocolHack() { + public static void init() { ViaLoadingBase.ViaLoadingBaseBuilder builder = ViaLoadingBase.ViaLoadingBaseBuilder.create(); builder = builder.platform(new Platform("ViaBedrock", () -> true, ViaBedrockPlatformImpl::new, protocolVersions -> protocolVersions.add(BedrockProtocolVersion.bedrockLatest)), 0); diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/ViaFabricPlusVLBPipeline.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/ViaFabricPlusVLBPipeline.java similarity index 68% rename from src/main/java/de/florianmichael/viafabricplus/protocolhack/ViaFabricPlusVLBPipeline.java rename to src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/ViaFabricPlusVLBPipeline.java index da77496f..6d24ae40 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/ViaFabricPlusVLBPipeline.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/netty/ViaFabricPlusVLBPipeline.java @@ -15,14 +15,15 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package de.florianmichael.viafabricplus.protocolhack; +package de.florianmichael.viafabricplus.protocolhack.netty; import com.viaversion.viaversion.api.connection.UserConnection; -import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.DisconnectHandler; -import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.PingEncapsulationCodec; -import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.RakMessageEncapsulationCodec; -import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.library_fix.FixedUnconnectedPingEncoder; -import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.library_fix.FixedUnconnectedPongDecoder; +import de.florianmichael.viafabricplus.protocolhack.ProtocolHack; +import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.RakNetClientConnection; +import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.handler.PingEncapsulationCodec; +import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.handler.RakMessageEncapsulationCodec; +import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.handler.library_fix.FixedUnconnectedPingEncoder; +import de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.handler.library_fix.FixedUnconnectedPongDecoder; import de.florianmichael.vialoadingbase.model.ComparableProtocolVersion; import de.florianmichael.vialoadingbase.netty.VLBPipeline; import io.netty.channel.ChannelHandlerContext; @@ -42,14 +43,16 @@ import org.cloudburstmc.netty.handler.codec.raknet.common.UnconnectedPongDecoder import java.net.InetSocketAddress; public class ViaFabricPlusVLBPipeline extends VLBPipeline { - public final static String DISCONNECT_HANDLER_NAME = "disconnect_handler"; - public final static String FRAME_ENCAPSULATION_HANDLER_NAME = "frame_encapsulation"; - public final static String PING_ENCAPSULATION_HANDLER_NAME = "ping_encapsulation"; - public final static String PACKET_ENCAPSULATION_HANDLER_NAME = "packet_encapsulation"; - public final static String BATCH_LENGTH_HANDLER_NAME = "batch_length"; - public final static String COMPRESSION_HANDLER_NAME = "compression"; - public final static String ENCRYPTION_HANDLER_NAME = "encryption"; + // ViaBedrock (RakNet and Codec based) + public final static String VIA_BEDROCK_DISCONNECT_HANDLER_NAME = "via-bedrock-disconnect-handler"; + public final static String VIA_BEDROCK_FRAME_ENCAPSULATION_HANDLER_NAME = "via-bedrock-frame-encapsulation"; + public final static String VIA_BEDROCK_PING_ENCAPSULATION_HANDLER_NAME = "via-bedrock-ping-encapsulation"; + public final static String VIA_BEDROCK_PACKET_ENCAPSULATION_HANDLER_NAME = "via-bedrock-packet-encapsulation"; + public final static String VIA_BEDROCK_BATCH_LENGTH_HANDLER_NAME = "via-bedrock-batch_length"; + public final static String VIA_BEDROCK_COMPRESSION_HANDLER_NAME = "via-bedrock-compression"; + public final static String VIA_BEDROCK_ENCRYPTION_HANDLER_NAME = "via-bedrock-encryption"; + // ViaLegacy (pre Netty) public static final String VIA_LEGACY_DECODER_HANDLER_NAME = "via-legacy-decoder"; public static final String VIA_LEGACY_ENCODER_HANDLER_NAME = "via-legacy-encoder"; @@ -65,26 +68,30 @@ public class ViaFabricPlusVLBPipeline extends VLBPipeline { @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { + // ViaLoadingBase super.handlerAdded(ctx); final ChannelPipeline pipeline = ctx.channel().pipeline(); + // ViaLegacy if (this.version.isOlderThanOrEqualTo(LegacyProtocolVersion.r1_6_4)) { getInfo().getProtocolInfo().getPipeline().add(PreNettyBaseProtocol.INSTANCE); pipeline.addBefore("splitter", VIA_LEGACY_DECODER_HANDLER_NAME, new PreNettyLengthPrepender(getInfo())); pipeline.addBefore("prepender", VIA_LEGACY_ENCODER_HANDLER_NAME, new PreNettyLengthRemover(getInfo())); } - + + // ViaBedrock if (this.version.isEqualTo(BedrockProtocolVersion.bedrockLatest)) { getInfo().getProtocolInfo().getPipeline().add(BedrockBaseProtocol.INSTANCE); - pipeline.replace("splitter", BATCH_LENGTH_HANDLER_NAME, new BatchLengthCodec()); + pipeline.replace("splitter", VIA_BEDROCK_BATCH_LENGTH_HANDLER_NAME, new BatchLengthCodec()); - pipeline.addBefore(BATCH_LENGTH_HANDLER_NAME, DISCONNECT_HANDLER_NAME, new DisconnectHandler()); - pipeline.addAfter(DISCONNECT_HANDLER_NAME, FRAME_ENCAPSULATION_HANDLER_NAME, new RakMessageEncapsulationCodec()); - pipeline.addAfter(BATCH_LENGTH_HANDLER_NAME, PACKET_ENCAPSULATION_HANDLER_NAME, new PacketEncapsulationCodec()); + pipeline.addBefore(VIA_BEDROCK_BATCH_LENGTH_HANDLER_NAME, VIA_BEDROCK_DISCONNECT_HANDLER_NAME, RakNetClientConnection.DISCONNECT_HANDLER); + pipeline.addAfter(VIA_BEDROCK_DISCONNECT_HANDLER_NAME, VIA_BEDROCK_FRAME_ENCAPSULATION_HANDLER_NAME, new RakMessageEncapsulationCodec()); + pipeline.addAfter(VIA_BEDROCK_BATCH_LENGTH_HANDLER_NAME, VIA_BEDROCK_PACKET_ENCAPSULATION_HANDLER_NAME, new PacketEncapsulationCodec()); + // Replaced by the codecs pipeline.remove("prepender"); pipeline.remove("timeout"); @@ -97,10 +104,10 @@ public class ViaFabricPlusVLBPipeline extends VLBPipeline { rakChannel.parent().pipeline().replace(UnconnectedPongDecoder.NAME, UnconnectedPongDecoder.NAME, new FixedUnconnectedPongDecoder(rakChannel)); } - pipeline.replace(FRAME_ENCAPSULATION_HANDLER_NAME, PING_ENCAPSULATION_HANDLER_NAME, new PingEncapsulationCodec(address)); + pipeline.replace(VIA_BEDROCK_FRAME_ENCAPSULATION_HANDLER_NAME, VIA_BEDROCK_PING_ENCAPSULATION_HANDLER_NAME, new PingEncapsulationCodec(address)); - pipeline.remove(PACKET_ENCAPSULATION_HANDLER_NAME); - pipeline.remove(BATCH_LENGTH_HANDLER_NAME); + pipeline.remove(VIA_BEDROCK_PACKET_ENCAPSULATION_HANDLER_NAME); + pipeline.remove(VIA_BEDROCK_BATCH_LENGTH_HANDLER_NAME); } } } diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/DisconnectHandler.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/DisconnectHandler.java deleted file mode 100644 index 25b56662..00000000 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/DisconnectHandler.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock - * 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 de.florianmichael.viafabricplus.protocolhack.platform.viabedrock; - -import io.netty.channel.ChannelHandlerContext; -import io.netty.channel.ChannelOutboundHandlerAdapter; -import io.netty.channel.ChannelPromise; - -public class DisconnectHandler extends ChannelOutboundHandlerAdapter { - - private boolean calledDisconnect = false; - - @Override - public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { - if (ctx.channel().isActive() && !this.calledDisconnect) { - this.calledDisconnect = true; - ctx.disconnect(promise); // Send disconnect notification to the server and close the channel - } else { - super.close(ctx, promise); - } - } - -} diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/RakNetClientConnection.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/RakNetClientConnection.java new file mode 100644 index 00000000..46de082d --- /dev/null +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/RakNetClientConnection.java @@ -0,0 +1,81 @@ +/* + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 de.florianmichael.viafabricplus.protocolhack.platform.viabedrock; + +import de.florianmichael.viafabricplus.protocolhack.ProtocolHack; +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.*; +import io.netty.channel.epoll.EpollDatagramChannel; +import io.netty.channel.epoll.EpollSocketChannel; +import io.netty.channel.socket.nio.NioDatagramChannel; +import io.netty.handler.timeout.ReadTimeoutHandler; +import net.minecraft.network.ClientConnection; +import net.minecraft.network.NetworkSide; +import net.minecraft.util.Lazy; +import org.cloudburstmc.netty.channel.raknet.RakChannelFactory; +import org.cloudburstmc.netty.channel.raknet.config.RakChannelOption; +import org.jetbrains.annotations.NotNull; + +import java.net.InetSocketAddress; +import java.util.concurrent.ThreadLocalRandom; + +public class RakNetClientConnection { + public final static ChannelOutboundHandlerAdapter DISCONNECT_HANDLER = new ChannelOutboundHandlerAdapter() { + private boolean calledDisconnect = false; + + @Override + public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { + if (ctx.channel().isActive() && !this.calledDisconnect) { + this.calledDisconnect = true; + ctx.disconnect(promise); // Send disconnect notification to the server and close the channel + } else { + super.close(ctx, promise); + } + } + }; + + public static void connectRakNet(final ClientConnection clientConnection, final InetSocketAddress address, final Lazy lazy, final Class channelType) { + Bootstrap nettyBoostrap = new Bootstrap(); + nettyBoostrap = nettyBoostrap.group((EventLoopGroup) lazy.get()); + nettyBoostrap = nettyBoostrap.handler(new ChannelInitializer<>() { + @Override + protected void initChannel(@NotNull Channel channel) throws Exception { + try { + channel.config().setOption(RakChannelOption.RAK_PROTOCOL_VERSION, 11); + channel.config().setOption(RakChannelOption.RAK_CONNECT_TIMEOUT, 4_000L); + channel.config().setOption(RakChannelOption.RAK_SESSION_TIMEOUT, 30_000L); + channel.config().setOption(RakChannelOption.RAK_GUID, ThreadLocalRandom.current().nextLong()); + } catch (Exception ignored) { + } + ChannelPipeline channelPipeline = channel.pipeline().addLast("timeout", new ReadTimeoutHandler(30)); + ClientConnection.addHandlers(channelPipeline, NetworkSide.CLIENTBOUND); + + channelPipeline.addLast("packet_handler", clientConnection); + + ProtocolHack.injectVLBPipeline(clientConnection, channel, address); + } + }); + nettyBoostrap = nettyBoostrap.channelFactory(channelType == EpollSocketChannel.class ? RakChannelFactory.client(EpollDatagramChannel.class) : RakChannelFactory.client(NioDatagramChannel.class)); + + if (ProtocolHack.getRakNetPingSessions().contains(address)) { + nettyBoostrap.bind(new InetSocketAddress(0)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE).syncUninterruptibly(); + } else { + nettyBoostrap.connect(address.getAddress(), address.getPort()).syncUninterruptibly(); + } + } +} diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/PingEncapsulationCodec.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/PingEncapsulationCodec.java similarity index 93% rename from src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/PingEncapsulationCodec.java rename to src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/PingEncapsulationCodec.java index 101460fc..9a9adb90 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/PingEncapsulationCodec.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/PingEncapsulationCodec.java @@ -1,6 +1,6 @@ /* - * This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock - * Copyright (C) 2023 RK_01/RaphiMC and contributors + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package de.florianmichael.viafabricplus.protocolhack.platform.viabedrock; +package de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.handler; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/RakMessageEncapsulationCodec.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/RakMessageEncapsulationCodec.java similarity index 92% rename from src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/RakMessageEncapsulationCodec.java rename to src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/RakMessageEncapsulationCodec.java index 2f53890e..3bf081d8 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/RakMessageEncapsulationCodec.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/RakMessageEncapsulationCodec.java @@ -1,6 +1,6 @@ /* - * This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock - * Copyright (C) 2023 RK_01/RaphiMC and contributors + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package de.florianmichael.viafabricplus.protocolhack.platform.viabedrock; +package de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.handler; import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/library_fix/FixedUnconnectedPingEncoder.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/library_fix/FixedUnconnectedPingEncoder.java similarity index 98% rename from src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/library_fix/FixedUnconnectedPingEncoder.java rename to src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/library_fix/FixedUnconnectedPingEncoder.java index 00845160..c5843e6f 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/library_fix/FixedUnconnectedPingEncoder.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/library_fix/FixedUnconnectedPingEncoder.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.library_fix; +package de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.handler.library_fix; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; diff --git a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/library_fix/FixedUnconnectedPongDecoder.java b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/library_fix/FixedUnconnectedPongDecoder.java similarity index 99% rename from src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/library_fix/FixedUnconnectedPongDecoder.java rename to src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/library_fix/FixedUnconnectedPongDecoder.java index 6d8d8168..f940cb78 100644 --- a/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/library_fix/FixedUnconnectedPongDecoder.java +++ b/src/main/java/de/florianmichael/viafabricplus/protocolhack/platform/viabedrock/handler/library_fix/FixedUnconnectedPongDecoder.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.library_fix; +package de.florianmichael.viafabricplus.protocolhack.platform.viabedrock.handler.library_fix; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufUtil;