From f394d87b26591bca9e7372005d3d2c9dec545ebe Mon Sep 17 00:00:00 2001 From: Ronit Sabhaya Date: Mon, 1 Dec 2025 16:52:57 -0600 Subject: [PATCH] fix: connect to backend immediately on channel active for server-first protocols(#794) (#813) - Closes #794 - TCP port forwarding currently fails for server-first protocols (SMTP, FTP, SSH, PostgreSQL) because the backend connection is only established when the client sends data. --- Sources/SocketForwarder/ConnectHandler.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/SocketForwarder/ConnectHandler.swift b/Sources/SocketForwarder/ConnectHandler.swift index ef74db0b..c4e9c61e 100644 --- a/Sources/SocketForwarder/ConnectHandler.swift +++ b/Sources/SocketForwarder/ConnectHandler.swift @@ -35,9 +35,6 @@ extension ConnectHandler: ChannelInboundHandler { typealias OutboundOut = ByteBuffer func channelRead(context: ChannelHandlerContext, data: NIOAny) { - if self.pendingBytes.isEmpty { - self.connectToServer(context: context) - } self.pendingBytes.append(data) } @@ -46,6 +43,12 @@ extension ConnectHandler: ChannelInboundHandler { self.log?[metadataKey: "proxy"] = "\(context.channel.localAddress?.description ?? "none")" self.log?[metadataKey: "server"] = "\(context.channel.remoteAddress?.description ?? "none")" } + + func channelActive(context: ChannelHandlerContext) { + self.log?.trace("frontend - channel active, connecting to backend") + self.connectToServer(context: context) + context.fireChannelActive() + } } extension ConnectHandler: RemovableChannelHandler {