From 61cc49c405cfaddc121839cb56ba63b8cb23c627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=90=98=F0=9D=90=A8=F0=9D=90=AC=F0=9D=90=9E?= =?UTF-8?q?=F0=9D=90=9B=F0=9D=90=B2=F0=9D=90=AD=F0=9D=90=9E?= <54016243+yosebyte@users.noreply.github.com> Date: Tue, 3 Dec 2024 08:30:40 +0000 Subject: [PATCH] Reuse remote connection --- internal/tunnel/client.go | 11 +++++++++-- internal/tunnel/server.go | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/tunnel/client.go b/internal/tunnel/client.go index 6c89929..4f62a29 100644 --- a/internal/tunnel/client.go +++ b/internal/tunnel/client.go @@ -44,8 +44,15 @@ func Client(parsedURL *url.URL) error { } defer targetConn.Close() targetConn.SetNoDelay(true) - log.Info("Target connection established, starting data exchange: [%v] <-> [%v]", linkAddr, targetAddr) - util.HandleConn(linkConn, targetConn) + remoteConn, err := net.DialTCP("tcp", nil, linkAddr) + if err != nil { + log.Error("Unable to dial target address: [%v], %v", linkAddr, err) + return + } + defer remoteConn.Close() + remoteConn.SetNoDelay(true) + log.Info("Target connection established, starting data exchange: [%v] <-> [%v]", remoteConn, targetAddr) + util.HandleConn(remoteConn, targetConn) log.Info("Connection closed successfully") }() } diff --git a/internal/tunnel/server.go b/internal/tunnel/server.go index a1525d3..8697d11 100644 --- a/internal/tunnel/server.go +++ b/internal/tunnel/server.go @@ -72,8 +72,14 @@ func Server(parsedURL *url.URL, whiteList *sync.Map) error { targetConn.Close() return } + remoteConn, err := linkListen.AcceptTCP() + if err != nil { + log.Error("Unable to accept connections form link address: [%v] %v", linkAddr, err) + return + } + defer remoteConn.Close() log.Info("Starting data exchange: [%v] <-> [%v]", clientAddr, targetAddr) - util.HandleConn(linkConn, targetConn) + util.HandleConn(remoteConn, targetConn) log.Info("Connection closed successfully") }(targetConn) }