From 862738d67919a50dd2f315468869214b31221a4b Mon Sep 17 00:00:00 2001 From: Lenni0451 <20379977+Lenni0451@users.noreply.github.com> Date: Sun, 5 Jan 2025 18:08:28 +0100 Subject: [PATCH] Change handshake intent from transfer when connecting first --- .../java/net/lenni0451/miniconnect/Main.java | 1 + .../miniconnect/proxy/StateRegistry.java | 5 +++ .../proxy/event/HandshakeIntentListener.java | 35 +++++++++++++++++++ .../states/play/screen/impl/MainScreen.java | 7 ++-- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/lenni0451/miniconnect/proxy/event/HandshakeIntentListener.java diff --git a/src/main/java/net/lenni0451/miniconnect/Main.java b/src/main/java/net/lenni0451/miniconnect/Main.java index be30820..eb03860 100644 --- a/src/main/java/net/lenni0451/miniconnect/Main.java +++ b/src/main/java/net/lenni0451/miniconnect/Main.java @@ -45,6 +45,7 @@ public void onEnable() { ViaProxy.EVENT_MANAGER.register(new HAProxyEnableHandler()); ViaProxy.EVENT_MANAGER.register(new TargetOnlineModeHandler()); ViaProxy.EVENT_MANAGER.register(new ProxyOnlineModeHandler()); + ViaProxy.EVENT_MANAGER.register(new HandshakeIntentListener()); } } diff --git a/src/main/java/net/lenni0451/miniconnect/proxy/StateRegistry.java b/src/main/java/net/lenni0451/miniconnect/proxy/StateRegistry.java index 2876a57..6a5bdab 100644 --- a/src/main/java/net/lenni0451/miniconnect/proxy/StateRegistry.java +++ b/src/main/java/net/lenni0451/miniconnect/proxy/StateRegistry.java @@ -16,6 +16,7 @@ public class StateRegistry { private final Map reconnectTargets = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build().asMap(); private final Map lobbyTargets = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build().asMap(); private final Set verificationQueue = Collections.newSetFromMap(CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build().asMap()); + private final Set changeHandshakeIntent = Collections.newSetFromMap(CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build().asMap()); public Map getConnectionTargets() { return this.connectionTargets; @@ -33,4 +34,8 @@ public Set getVerificationQueue() { return this.verificationQueue; } + public Set getChangeHandshakeIntent() { + return this.changeHandshakeIntent; + } + } diff --git a/src/main/java/net/lenni0451/miniconnect/proxy/event/HandshakeIntentListener.java b/src/main/java/net/lenni0451/miniconnect/proxy/event/HandshakeIntentListener.java new file mode 100644 index 0000000..6bcb52c --- /dev/null +++ b/src/main/java/net/lenni0451/miniconnect/proxy/event/HandshakeIntentListener.java @@ -0,0 +1,35 @@ +package net.lenni0451.miniconnect.proxy.event; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import net.lenni0451.lambdaevents.EventHandler; +import net.lenni0451.miniconnect.Main; +import net.lenni0451.miniconnect.utils.ChannelUtils; +import net.raphimc.netminecraft.constants.IntendedState; +import net.raphimc.netminecraft.constants.MCPipeline; +import net.raphimc.netminecraft.packet.Packet; +import net.raphimc.netminecraft.packet.impl.handshaking.C2SHandshakingClientIntentionPacket; +import net.raphimc.viaproxy.plugins.events.Client2ProxyChannelInitializeEvent; +import net.raphimc.viaproxy.plugins.events.types.ITyped; + +public class HandshakeIntentListener { + + @EventHandler + public void onClient2ProxyChannelInitialize(final Client2ProxyChannelInitializeEvent event) { + if (!event.getType().equals(ITyped.Type.POST)) return; + event.getChannel().pipeline().addBefore(MCPipeline.HANDLER_HANDLER_NAME, "miniconnect-intent-changer", new SimpleChannelInboundHandler() { + @Override + protected void channelRead0(ChannelHandlerContext channelHandlerContext, Packet packet) { + if (packet instanceof C2SHandshakingClientIntentionPacket clientIntention) { + if (clientIntention.intendedState.equals(IntendedState.TRANSFER)) { + if (Main.getInstance().getStateRegistry().getChangeHandshakeIntent().remove(ChannelUtils.getChannelAddress(event.getChannel()))) { + clientIntention.intendedState = IntendedState.LOGIN; + } + } + } + channelHandlerContext.fireChannelRead(packet); + } + }); + } + +} diff --git a/src/main/java/net/lenni0451/miniconnect/server/states/play/screen/impl/MainScreen.java b/src/main/java/net/lenni0451/miniconnect/server/states/play/screen/impl/MainScreen.java index 4c9ae25..dbb6d21 100644 --- a/src/main/java/net/lenni0451/miniconnect/server/states/play/screen/impl/MainScreen.java +++ b/src/main/java/net/lenni0451/miniconnect/server/states/play/screen/impl/MainScreen.java @@ -144,10 +144,9 @@ public void init(ScreenHandler screenHandler, ItemList itemList) { Logger.LOGGER.error("Failed to save player config", e); } } - Main.getInstance().getStateRegistry().getConnectionTargets().put( - ChannelUtils.getChannelAddress(screenHandler.getStateHandler().getChannel()), - playerConfig.toConnectionInfo() - ); + InetAddress channelAddress = ChannelUtils.getChannelAddress(screenHandler.getStateHandler().getChannel()); + Main.getInstance().getStateRegistry().getConnectionTargets().put(channelAddress, playerConfig.toConnectionInfo()); + Main.getInstance().getStateRegistry().getChangeHandshakeIntent().add(channelAddress); screenHandler.getStateHandler().send(new S2CTransferPacket(playerConfig.handshakeAddress, playerConfig.handshakePort)); } else { screenHandler.getStateHandler().send(new S2CSystemChatPacket(new StringComponent("§cYou need to set all options before connecting"), false));