From 25b341a5457e76dc67cab220c8f827f598c615a4 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:20:49 +0200 Subject: [PATCH] Fixed packets being sent too early after connecting --- .../client2proxy/Client2ProxyHandler.java | 22 ++++++++++--------- .../PassthroughClient2ProxyHandler.java | 10 +++++---- 2 files changed, 18 insertions(+), 14 deletions(-) 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 03f9ddc..0538817 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java @@ -229,18 +229,20 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler { this.proxyConnection.connectToServer(serverAddress, serverVersion).addListeners((ThrowingChannelFutureListener) f -> { if (f.isSuccess()) { - if (Options.SERVER_HAPROXY_PROTOCOL) { - this.proxyConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(this.proxyConnection.getC2P(), this.proxyConnection.getChannel(), clientVersion)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); - } - - handshakeParts[0] = serverAddress.getAddress(); - this.proxyConnection.getChannel().writeAndFlush(new C2SHandshakePacket(clientVersion.getOriginalVersion(), String.join("\0", handshakeParts), serverAddress.getPort(), packet.intendedState)).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, (ChannelFutureListener) f2 -> { - if (f2.isSuccess()) { - this.proxyConnection.setP2sConnectionState(packet.intendedState); + f.channel().eventLoop().submit(() -> { // Reschedule so the packets get sent after the channel is fully initialized and active + if (Options.SERVER_HAPROXY_PROTOCOL) { + this.proxyConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(this.proxyConnection.getC2P(), this.proxyConnection.getChannel(), clientVersion)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } - }); - ChannelUtil.restoreAutoRead(this.proxyConnection.getC2P()); + handshakeParts[0] = serverAddress.getAddress(); + this.proxyConnection.getChannel().writeAndFlush(new C2SHandshakePacket(clientVersion.getOriginalVersion(), String.join("\0", handshakeParts), serverAddress.getPort(), packet.intendedState)).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, (ChannelFutureListener) f2 -> { + if (f2.isSuccess()) { + this.proxyConnection.setP2sConnectionState(packet.intendedState); + } + }); + + ChannelUtil.restoreAutoRead(this.proxyConnection.getC2P()); + }); } }, (ThrowingChannelFutureListener) f -> { if (!f.isSuccess()) { diff --git a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/passthrough/PassthroughClient2ProxyHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/passthrough/PassthroughClient2ProxyHandler.java index faa455f..97eb6fc 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/passthrough/PassthroughClient2ProxyHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/passthrough/PassthroughClient2ProxyHandler.java @@ -82,11 +82,13 @@ public class PassthroughClient2ProxyHandler extends SimpleChannelInboundHandler< this.proxyConnection.connect(serverAddress).addListeners((ThrowingChannelFutureListener) f -> { if (f.isSuccess()) { - if (Options.SERVER_HAPROXY_PROTOCOL) { - this.proxyConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(this.proxyConnection.getC2P(), this.proxyConnection.getChannel(), null)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); - } + f.channel().eventLoop().submit(() -> { // Reschedule so the packets get sent after the channel is fully initialized and active + if (Options.SERVER_HAPROXY_PROTOCOL) { + this.proxyConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(this.proxyConnection.getC2P(), this.proxyConnection.getChannel(), null)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + } - ChannelUtil.restoreAutoRead(this.proxyConnection.getC2P()); + ChannelUtil.restoreAutoRead(this.proxyConnection.getC2P()); + }); } }, (ThrowingChannelFutureListener) f -> { if (!f.isSuccess()) {