From 687ecb2d99c05bce261c2d2406f4284445ef40a7 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 3 Jan 2015 23:09:41 +0100 Subject: [PATCH] Better closing of connections Improved the handling about how to close the connections. --- Forward.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Forward.go b/Forward.go index 4f24e8d..a154dc2 100644 --- a/Forward.go +++ b/Forward.go @@ -9,6 +9,7 @@ import ( func forward(localConn net.Conn, config *ssh.ClientConfig) { + defer localConn.Close() currentRetriesServer := 0 currentRetriesRemote := 0 var sshClientConnection *ssh.Client = nil @@ -39,6 +40,7 @@ func forward(localConn net.Conn, config *ssh.ClientConfig) { // Success: log.Println(`Connected to the SSH server ` + serverAddrString) sshClientConnection = sshClientConn + defer sshClientConnection.Close() break } } @@ -67,6 +69,7 @@ func forward(localConn net.Conn, config *ssh.ClientConfig) { // Fine, the connections are up and ready :-) log.Printf("The remote end-point %s is connected.\n", remoteAddrString) + defer sshConn.Close() // To be able to close down both transfer threads, we create a channel: quit := make(chan bool) @@ -88,10 +91,6 @@ func forward(localConn net.Conn, config *ssh.ClientConfig) { // Now, close all the channels and therefore, force the other / second thread to go down: log.Println(`Close now all connections.`) - sshConn.Close() - localConn.Close() - sshClientConnection.Close() - return } }