diff --git a/build.gradle b/build.gradle index a223640..48a4c21 100644 --- a/build.gradle +++ b/build.gradle @@ -65,8 +65,8 @@ repositories { } dependencies { - include "com.viaversion:viaversion:4.8.0-23w35a-SNAPSHOT" - include("com.viaversion:viabackwards-common:4.8.0-23w35a-SNAPSHOT") { + include "com.viaversion:viaversion:4.8.0-1.20.2-pre2-SNAPSHOT" + include("com.viaversion:viabackwards-common:4.8.0-1.20.2-pre2-SNAPSHOT") { exclude group: "com.viaversion", module: "viaversion" exclude group: "io.netty", module: "netty-all" exclude group: "com.google.guava", module: "guava" diff --git a/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinProtocolVersion.java b/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinProtocolVersion.java index adad4e7..7fa1664 100644 --- a/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinProtocolVersion.java +++ b/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinProtocolVersion.java @@ -51,7 +51,7 @@ public abstract class MixinProtocolVersion { remaps.put("1.18/1.18.1", new Pair<>("1.18-1.18.1", null)); remaps.put("1.19.1/2", new Pair<>("1.19.1-1.19.2", null)); remaps.put("1.20/1.20.1", new Pair<>("1.20-1.20.1", null)); - remaps.put("1.20.2", new Pair<>("23w35a", null)); + remaps.put("1.20.2", new Pair<>("1.20.2-pre2", null)); } @Redirect(method = "", at = @At(value = "INVOKE", target = "Lcom/viaversion/viaversion/api/protocol/version/ProtocolVersion;register(ILjava/lang/String;)Lcom/viaversion/viaversion/api/protocol/version/ProtocolVersion;")) diff --git a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java index 55978e7..0d309f3 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java @@ -214,8 +214,11 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler { this.proxyConnection.setClassicMpPass(classicMpPass); this.proxyConnection.getPacketHandlers().add(new StatusPacketHandler(this.proxyConnection)); this.proxyConnection.getPacketHandlers().add(new CustomPayloadPacketHandler(this.proxyConnection)); + this.proxyConnection.getPacketHandlers().add(new CompressionPacketHandler(this.proxyConnection)); this.proxyConnection.getPacketHandlers().add(new LoginPacketHandler(this.proxyConnection)); - this.proxyConnection.getPacketHandlers().add(new ConfigurationPacketHandler(this.proxyConnection)); + if (clientVersion.isNewerThanOrEqualTo(VersionEnum.r1_20_2) || serverVersion.isNewerThanOrEqualTo(VersionEnum.r1_20_2)) { + this.proxyConnection.getPacketHandlers().add(new ConfigurationPacketHandler(this.proxyConnection)); + } this.proxyConnection.getPacketHandlers().add(new ResourcePackPacketHandler(this.proxyConnection)); this.proxyConnection.getPacketHandlers().add(new UnexpectedPacketHandler(this.proxyConnection)); diff --git a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/CompressionPacketHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/CompressionPacketHandler.java new file mode 100644 index 0000000..928487f --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/CompressionPacketHandler.java @@ -0,0 +1,78 @@ +/* + * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy + * 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.viaproxy.proxy.packethandler; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import net.raphimc.netminecraft.constants.ConnectionState; +import net.raphimc.netminecraft.constants.MCPackets; +import net.raphimc.netminecraft.constants.MCPipeline; +import net.raphimc.netminecraft.packet.IPacket; +import net.raphimc.netminecraft.packet.PacketTypes; +import net.raphimc.netminecraft.packet.UnknownPacket; +import net.raphimc.netminecraft.packet.impl.login.S2CLoginCompressionPacket; +import net.raphimc.netminecraft.packet.impl.login.S2CLoginSuccessPacket1_7; +import net.raphimc.vialoader.util.VersionEnum; +import net.raphimc.viaproxy.cli.options.Options; +import net.raphimc.viaproxy.proxy.session.ProxyConnection; + +import java.util.List; + +public class CompressionPacketHandler extends PacketHandler { + + private final int setCompressionId; + + public CompressionPacketHandler(ProxyConnection proxyConnection) { + super(proxyConnection); + + this.setCompressionId = MCPackets.S2C_SET_COMPRESSION.getId(proxyConnection.getClientVersion().getVersion()); + } + + @Override + public boolean handleP2S(IPacket packet, List listeners) { + if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) { + if (unknownPacket.packetId == this.setCompressionId) { + final ByteBuf data = Unpooled.wrappedBuffer(unknownPacket.data); + final int compressionThreshold = PacketTypes.readVarInt(data); // compression threshold + this.proxyConnection.getChannel().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(compressionThreshold); + data.release(); + + return false; + } + } else if (packet instanceof S2CLoginSuccessPacket1_7) { + if (this.proxyConnection.getClientVersion().isNewerThanOrEqualTo(VersionEnum.r1_8)) { + if (Options.COMPRESSION_THRESHOLD > -1 && this.proxyConnection.getC2P().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).get() == -1) { + this.proxyConnection.getChannel().config().setAutoRead(false); + this.proxyConnection.getC2P().writeAndFlush(new S2CLoginCompressionPacket(Options.COMPRESSION_THRESHOLD)).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, (ChannelFutureListener) f -> { + if (f.isSuccess()) { + this.proxyConnection.getC2P().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(Options.COMPRESSION_THRESHOLD); + this.proxyConnection.getChannel().config().setAutoRead(true); + } + }); + } + } + } else if (packet instanceof S2CLoginCompressionPacket loginCompressionPacket) { + this.proxyConnection.getChannel().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(loginCompressionPacket.compressionThreshold); + return false; + } + + return true; + } + +} diff --git a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/ConfigurationPacketHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/ConfigurationPacketHandler.java index bdfc094..594d755 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/ConfigurationPacketHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/ConfigurationPacketHandler.java @@ -44,8 +44,7 @@ public class ConfigurationPacketHandler extends PacketHandler { @Override public boolean handleC2P(IPacket packet, List listeners) { - if (packet instanceof UnknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) { - final UnknownPacket unknownPacket = (UnknownPacket) packet; + if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) { if (unknownPacket.packetId == this.configurationAcknowledgedId) { this.proxyConnection.setC2pConnectionState(ConnectionState.CONFIGURATION); listeners.add(f -> { @@ -80,8 +79,7 @@ public class ConfigurationPacketHandler extends PacketHandler { @Override public boolean handleP2S(IPacket packet, List listeners) { - if (packet instanceof UnknownPacket && this.proxyConnection.getP2sConnectionState() == ConnectionState.PLAY) { - final UnknownPacket unknownPacket = (UnknownPacket) packet; + if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getP2sConnectionState() == ConnectionState.PLAY) { if (unknownPacket.packetId == this.startConfigurationId) { this.proxyConnection.getChannel().config().setAutoRead(false); } diff --git a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/CustomPayloadPacketHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/CustomPayloadPacketHandler.java index c320331..05ed823 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/CustomPayloadPacketHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/CustomPayloadPacketHandler.java @@ -46,8 +46,7 @@ public class CustomPayloadPacketHandler extends PacketHandler { @Override public boolean handleC2P(IPacket packet, List listeners) { - if (packet instanceof UnknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) { - final UnknownPacket unknownPacket = (UnknownPacket) packet; + if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) { if (unknownPacket.packetId == this.customPayloadId) { final ByteBuf data = Unpooled.wrappedBuffer(unknownPacket.data); final String channel = PacketTypes.readString(data, Short.MAX_VALUE); // channel @@ -55,14 +54,11 @@ public class CustomPayloadPacketHandler extends PacketHandler { return false; } } - } else if (packet instanceof C2SLoginCustomPayloadPacket) { - final C2SLoginCustomPayloadPacket loginCustomPayload = (C2SLoginCustomPayloadPacket) packet; + } else if (packet instanceof C2SLoginCustomPayloadPacket loginCustomPayload) { if (loginCustomPayload.response != null && this.proxyConnection.handleCustomPayload(loginCustomPayload.queryId, Unpooled.wrappedBuffer(loginCustomPayload.response))) { return false; } - } else if (packet instanceof C2SLoginKeyPacket1_7) { - final C2SLoginKeyPacket1_7 loginKeyPacket = (C2SLoginKeyPacket1_7) packet; - + } else if (packet instanceof C2SLoginKeyPacket1_7 loginKeyPacket) { if (this.proxyConnection.getClientVersion().isOlderThanOrEqualTo(VersionEnum.r1_12_2) && new String(loginKeyPacket.encryptedNonce, StandardCharsets.UTF_8).equals(OpenAuthModConstants.DATA_CHANNEL)) { // 1.8-1.12.2 OpenAuthMod response handling final ByteBuf byteBuf = Unpooled.wrappedBuffer(loginKeyPacket.encryptedSecretKey); this.proxyConnection.handleCustomPayload(PacketTypes.readVarInt(byteBuf), byteBuf); diff --git a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java index d216e3b..638fa73 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java @@ -67,26 +67,23 @@ public class LoginPacketHandler extends PacketHandler { @Override public boolean handleC2P(IPacket packet, List listeners) throws GeneralSecurityException { - if (packet instanceof UnknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) { - final UnknownPacket unknownPacket = (UnknownPacket) packet; + if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getC2pConnectionState() == ConnectionState.PLAY) { if (unknownPacket.packetId == this.chatSessionUpdateId && this.proxyConnection.getChannel().attr(MCPipeline.ENCRYPTION_ATTRIBUTE_KEY).get() == null) { return false; } - } else if (packet instanceof C2SLoginHelloPacket1_7) { + } else if (packet instanceof C2SLoginHelloPacket1_7 loginHelloPacket) { if (this.loginState != LoginState.FIRST_PACKET) throw CloseAndReturn.INSTANCE; this.loginState = LoginState.SENT_HELLO; - final C2SLoginHelloPacket1_7 loginHelloPacket = (C2SLoginHelloPacket1_7) packet; - if (packet instanceof C2SLoginHelloPacket1_19) { - final C2SLoginHelloPacket1_19 packet1_19 = (C2SLoginHelloPacket1_19) packet; + if (packet instanceof C2SLoginHelloPacket1_19 packet1_19) { if (packet1_19.expiresAt != null && packet1_19.expiresAt.isBefore(Instant.now())) { throw new IllegalStateException("Expired public key"); } } proxyConnection.setLoginHelloPacket(loginHelloPacket); - if (packet instanceof C2SLoginHelloPacket1_19_3) { - proxyConnection.setGameProfile(new GameProfile(((C2SLoginHelloPacket1_19_3) packet).uuid, loginHelloPacket.name)); + if (packet instanceof C2SLoginHelloPacket1_19_3 packet1_19_3) { + proxyConnection.setGameProfile(new GameProfile(packet1_19_3.uuid, loginHelloPacket.name)); } else { proxyConnection.setGameProfile(new GameProfile(null, loginHelloPacket.name)); } @@ -99,10 +96,9 @@ public class LoginPacketHandler extends PacketHandler { } return false; - } else if (packet instanceof C2SLoginKeyPacket1_7) { + } else if (packet instanceof C2SLoginKeyPacket1_7 loginKeyPacket) { if (this.loginState != LoginState.SENT_HELLO) throw CloseAndReturn.INSTANCE; this.loginState = LoginState.SENT_KEY; - final C2SLoginKeyPacket1_7 loginKeyPacket = (C2SLoginKeyPacket1_7) packet; if (loginKeyPacket.encryptedNonce != null) { if (!Arrays.equals(this.verifyToken, CryptUtil.decryptData(KEY_PAIR.getPrivate(), loginKeyPacket.encryptedNonce))) { @@ -148,9 +144,7 @@ public class LoginPacketHandler extends PacketHandler { @Override public boolean handleP2S(IPacket packet, List listeners) throws GeneralSecurityException, ExecutionException, InterruptedException { - if (packet instanceof S2CLoginKeyPacket1_7) { - final S2CLoginKeyPacket1_7 loginKeyPacket = (S2CLoginKeyPacket1_7) packet; - + if (packet instanceof S2CLoginKeyPacket1_7 loginKeyPacket) { final PublicKey publicKey = CryptUtil.decodeRsaPublicKey(loginKeyPacket.publicKey); final SecretKey secretKey = CryptUtil.generateSecretKey(); final String serverHash = new BigInteger(CryptUtil.computeServerIdHash(loginKeyPacket.serverId, publicKey, secretKey)).toString(16); @@ -179,21 +173,7 @@ public class LoginPacketHandler extends PacketHandler { } return false; - } else if (packet instanceof S2CLoginSuccessPacket1_7) { - final S2CLoginSuccessPacket1_7 loginSuccessPacket = (S2CLoginSuccessPacket1_7) packet; - - if (this.proxyConnection.getClientVersion().isNewerThanOrEqualTo(VersionEnum.r1_8)) { - if (Options.COMPRESSION_THRESHOLD > -1 && this.proxyConnection.getC2P().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).get() == -1) { - this.proxyConnection.getChannel().config().setAutoRead(false); - this.proxyConnection.getC2P().writeAndFlush(new S2CLoginCompressionPacket(Options.COMPRESSION_THRESHOLD)).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, (ChannelFutureListener) f -> { - if (f.isSuccess()) { - this.proxyConnection.getC2P().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(Options.COMPRESSION_THRESHOLD); - this.proxyConnection.getChannel().config().setAutoRead(true); - } - }); - } - } - + } else if (packet instanceof S2CLoginSuccessPacket1_7 loginSuccessPacket) { final ConnectionState nextState = this.proxyConnection.getClientVersion().isNewerThanOrEqualTo(VersionEnum.r1_20_2) ? ConnectionState.CONFIGURATION : ConnectionState.PLAY; this.proxyConnection.setGameProfile(new GameProfile(loginSuccessPacket.uuid, loginSuccessPacket.name)); @@ -207,11 +187,6 @@ public class LoginPacketHandler extends PacketHandler { this.proxyConnection.getChannel().config().setAutoRead(true); } }); - } else if (packet instanceof S2CLoginCompressionPacket) { - final S2CLoginCompressionPacket loginCompressionPacket = (S2CLoginCompressionPacket) packet; - - this.proxyConnection.getChannel().attr(MCPipeline.COMPRESSION_THRESHOLD_ATTRIBUTE_KEY).set(loginCompressionPacket.compressionThreshold); - return false; } return true; diff --git a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/ResourcePackPacketHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/ResourcePackPacketHandler.java index 5a50b19..11b9cbc 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/ResourcePackPacketHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/ResourcePackPacketHandler.java @@ -47,8 +47,7 @@ public class ResourcePackPacketHandler extends PacketHandler { @Override public boolean handleP2S(IPacket packet, List listeners) { - if (packet instanceof UnknownPacket && this.proxyConnection.getP2sConnectionState() == ConnectionState.PLAY) { - final UnknownPacket unknownPacket = (UnknownPacket) packet; + if (packet instanceof UnknownPacket unknownPacket && this.proxyConnection.getP2sConnectionState() == ConnectionState.PLAY) { if (unknownPacket.packetId == this.joinGameId) { listeners.add(f -> { if (f.isSuccess()) { diff --git a/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/Proxy2ServerChannelInitializer.java b/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/Proxy2ServerChannelInitializer.java index 8206213..315afbc 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/Proxy2ServerChannelInitializer.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/Proxy2ServerChannelInitializer.java @@ -23,6 +23,7 @@ import com.viaversion.viaversion.protocol.ProtocolPipelineImpl; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import io.netty.handler.codec.haproxy.HAProxyMessageEncoder; +import io.netty.handler.flow.FlowControlHandler; import io.netty.handler.proxy.HttpProxyHandler; import io.netty.handler.proxy.ProxyHandler; import io.netty.handler.proxy.Socks4ProxyHandler; @@ -30,6 +31,7 @@ import io.netty.handler.proxy.Socks5ProxyHandler; import net.raphimc.netminecraft.constants.MCPipeline; import net.raphimc.netminecraft.netty.connection.MinecraftChannelInitializer; import net.raphimc.netminecraft.packet.registry.PacketRegistryUtil; +import net.raphimc.vialoader.netty.VLPipeline; import net.raphimc.vialoader.util.VersionEnum; import net.raphimc.viaproxy.cli.options.Options; import net.raphimc.viaproxy.plugins.PluginManager; @@ -74,6 +76,7 @@ public class Proxy2ServerChannelInitializer extends MinecraftChannelInitializer super.initChannel(channel); channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakeRegistry(true)); channel.pipeline().addLast(new ViaProxyVLPipeline(user, proxyConnection.getServerVersion())); + channel.pipeline().addAfter(VLPipeline.VIA_CODEC_NAME, "via-" + MCPipeline.FLOW_CONTROL_HANDLER_NAME, new FlowControlHandler()); if (PluginManager.EVENT_MANAGER.call(new Proxy2ServerChannelInitializeEvent(ITyped.Type.POST, channel)).isCancelled()) { channel.close();