refactor: perform more clean up in codebase
Some checks failed
Go CI Pipeline / ci (push) Has been cancelled
Some checks failed
Go CI Pipeline / ci (push) Has been cancelled
This commit is contained in:
parent
20b4b7ba2d
commit
127018b565
4 changed files with 34 additions and 19 deletions
22
forwarder.go
22
forwarder.go
|
|
@ -31,7 +31,7 @@ func (pf *PortForwarder) forward() error {
|
|||
localAddr := "127.0.0.1:" + pf.localPort
|
||||
remoteAddr := net.JoinHostPort(pf.remoteHost, pf.remotePort)
|
||||
|
||||
pf.logf("INFO", "Starting port forwarder: local %s -> remote %s (via SSH)", localAddr, remoteAddr)
|
||||
pf.logf("INFO", "Starting port forwarder: local -> remote (via SSH)", "Local Address", localAddr, "Remote Address", remoteAddr)
|
||||
|
||||
listener, err := net.Listen("tcp", localAddr)
|
||||
if err != nil {
|
||||
|
|
@ -39,20 +39,20 @@ func (pf *PortForwarder) forward() error {
|
|||
return fmt.Errorf("failed to listen on %s: %w", localAddr, err)
|
||||
}
|
||||
defer listener.Close()
|
||||
pf.logf("INFO", fmt.Sprintf("Listener active on %s", localAddr))
|
||||
pf.logf("INFO", "Listener active", "Local Address", localAddr)
|
||||
|
||||
for {
|
||||
localConn, err := listener.Accept()
|
||||
if err != nil {
|
||||
if opErr, ok := err.(*net.OpError); ok && opErr.Err.Error() == "use of closed network connection" {
|
||||
pf.logf("INFO", "Listener on %s closed, stopping forwarder.", localAddr)
|
||||
pf.logf("INFO", "Listener closed, stopping forwarder.", "Local Address", localAddr)
|
||||
return nil
|
||||
}
|
||||
pf.logf("ERROR", "Failed to accept incoming connection on %s: %v", localAddr, err)
|
||||
pf.logf("ERROR", "Failed to accept incoming connection:", "Local Address", localAddr, "Error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
pf.logf("INFO", fmt.Sprintf("Accepted connection from %s on %s", localConn.RemoteAddr(), localAddr))
|
||||
pf.logf("INFO", "Accepted connection:", "Remote Address", localConn.RemoteAddr(), "Local Address", localAddr)
|
||||
go pf.handleConnection(localConn, remoteAddr)
|
||||
}
|
||||
}
|
||||
|
|
@ -60,14 +60,14 @@ func (pf *PortForwarder) forward() error {
|
|||
func (pf *PortForwarder) handleConnection(localConn net.Conn, remoteAddr string) {
|
||||
defer localConn.Close()
|
||||
|
||||
pf.logf("INFO", "Dialing remote host %s via SSH tunnel for %s", remoteAddr, localConn.RemoteAddr())
|
||||
pf.logf("INFO", "Dialing remote host via SSH tunnel", "Local Address", remoteAddr, "Remote Address", localConn.RemoteAddr())
|
||||
remoteConn, err := pf.sshCon.Dial("tcp", remoteAddr)
|
||||
if err != nil {
|
||||
pf.logf("ERROR", "Failed to dial remote host %s via SSH: %v", remoteAddr, err)
|
||||
pf.logf("ERROR", "Failed to dial remote host via SSH:", "Remote Address", remoteAddr, "Error:", err)
|
||||
return
|
||||
}
|
||||
defer remoteConn.Close()
|
||||
pf.logf("INFO", "Connection to %s established. Starting data copy.", remoteAddr)
|
||||
pf.logf("INFO", "Connection established. Starting data copy.", "Remote Address", remoteAddr)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
|
|
@ -78,7 +78,7 @@ func (pf *PortForwarder) handleConnection(localConn net.Conn, remoteAddr string)
|
|||
bytesCopied, err := io.Copy(localConn, remoteConn)
|
||||
if err != nil {
|
||||
}
|
||||
pf.logf("INFO", "Finished copying remote->local (%d bytes) for %s", bytesCopied, localConn.RemoteAddr())
|
||||
pf.logf("INFO", "Finished copying remote->local", "Bytes copied", bytesCopied, "Remote Address", localConn.RemoteAddr())
|
||||
}()
|
||||
|
||||
go func() {
|
||||
|
|
@ -87,11 +87,11 @@ func (pf *PortForwarder) handleConnection(localConn net.Conn, remoteAddr string)
|
|||
bytesCopied, err := io.Copy(remoteConn, localConn)
|
||||
if err != nil {
|
||||
}
|
||||
pf.logf("INFO", "Finished copying local->remote (%d bytes) for %s", bytesCopied, localConn.RemoteAddr())
|
||||
pf.logf("INFO", "Finished copying local->remote", "Bytes copied", bytesCopied, "Remote Address", localConn.RemoteAddr())
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
pf.logf("INFO", "Closing forwarded connection for %s", localConn.RemoteAddr())
|
||||
pf.logf("INFO", "Closing forwarded connection", "Remote Address", localConn.RemoteAddr())
|
||||
}
|
||||
|
||||
func (pf *PortForwarder) logf(level, format string, v ...any) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue