diff --git a/pkg/client/tunnel.go b/pkg/client/tunnel.go index 7b57388..db167e5 100644 --- a/pkg/client/tunnel.go +++ b/pkg/client/tunnel.go @@ -19,10 +19,11 @@ import ( ) const ( - PORT_START = 29168 - PORT_LIMIT = 500 + portStart = 29168 + portLimit = 500 ) +// Tunnel represents the connection between local and remote server type Tunnel struct { TargetHost string TargetPort string @@ -121,6 +122,7 @@ func (tunnel *Tunnel) handleConnection(local net.Conn) { local.Close() } +// Close closes the tunnel connection func (tunnel *Tunnel) Close() { if tunnel.Client != nil { tunnel.Client.Close() @@ -131,6 +133,7 @@ func (tunnel *Tunnel) Close() { } } +// Configure establishes the tunnel between localhost and remote machine func (tunnel *Tunnel) Configure() error { config, err := makeConfig(tunnel.SSHInfo) if err != nil { @@ -153,6 +156,7 @@ func (tunnel *Tunnel) Configure() error { return nil } +// Start starts the connection handler loop func (tunnel *Tunnel) Start() { defer tunnel.Close() @@ -166,13 +170,14 @@ func (tunnel *Tunnel) Start() { } } +// NewTunnel instantiates a new tunnel struct from given ssh info func NewTunnel(sshInfo *shared.SSHInfo, dbUrl string) (*Tunnel, error) { uri, err := url.Parse(dbUrl) if err != nil { return nil, err } - listenPort, err := connection.AvailablePort(PORT_START, PORT_LIMIT) + listenPort, err := connection.FindAvailablePort(portStart, portLimit) if err != nil { return nil, err } diff --git a/pkg/connection/port.go b/pkg/connection/port.go index 4c87010..a596730 100644 --- a/pkg/connection/port.go +++ b/pkg/connection/port.go @@ -7,10 +7,9 @@ import ( "strings" ) -// Check if the TCP port available on localhost -func portAvailable(port int) bool { +// IsPortAvailable returns true if there's no listeners on a given port +func IsPortAvailable(port int) bool { conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%v", port)) - if err != nil { if strings.Index(err.Error(), "connection refused") > 0 { return true @@ -22,10 +21,10 @@ func portAvailable(port int) bool { return false } -// Get available TCP port on localhost by trying available ports in a range -func AvailablePort(start int, limit int) (int, error) { +// FindAvailablePort returns the first available TCP port in the range +func FindAvailablePort(start int, limit int) (int, error) { for i := start; i <= (start + limit); i++ { - if portAvailable(i) { + if IsPortAvailable(i) { return i, nil } } diff --git a/pkg/connection/port_test.go b/pkg/connection/port_test.go index a2b3114..4ee3eae 100644 --- a/pkg/connection/port_test.go +++ b/pkg/connection/port_test.go @@ -10,12 +10,12 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_portAvailable(t *testing.T) { +func TestIsPortAvailable(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("FIXME") } - assert.Equal(t, true, portAvailable(30000)) + assert.Equal(t, true, IsPortAvailable(30000)) serv, err := net.Listen("tcp", "127.0.0.1:30000") if err != nil { @@ -35,16 +35,16 @@ func Test_portAvailable(t *testing.T) { } }() - assert.Equal(t, false, portAvailable(30000)) - assert.Equal(t, true, portAvailable(30001)) + assert.Equal(t, false, IsPortAvailable(30000)) + assert.Equal(t, true, IsPortAvailable(30001)) } -func Test_getAvailablePort(t *testing.T) { +func TestFindAvailablePort(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("FIXME") } - port, err := AvailablePort(30000, 1) + port, err := FindAvailablePort(30000, 1) assert.Equal(t, nil, err) assert.Equal(t, 30000, port) @@ -65,11 +65,11 @@ func Test_getAvailablePort(t *testing.T) { } }() - port, err = AvailablePort(30000, 0) + port, err = FindAvailablePort(30000, 0) assert.EqualError(t, err, "No available port") assert.Equal(t, -1, port) - port, err = AvailablePort(30000, 1) + port, err = FindAvailablePort(30000, 1) assert.Equal(t, nil, err) assert.Equal(t, 30001, port) }