More lint
This commit is contained in:
parent
a5b8e02a98
commit
d1a7e6ecfe
@ -19,10 +19,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
PORT_START = 29168
|
portStart = 29168
|
||||||
PORT_LIMIT = 500
|
portLimit = 500
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Tunnel represents the connection between local and remote server
|
||||||
type Tunnel struct {
|
type Tunnel struct {
|
||||||
TargetHost string
|
TargetHost string
|
||||||
TargetPort string
|
TargetPort string
|
||||||
@ -121,6 +122,7 @@ func (tunnel *Tunnel) handleConnection(local net.Conn) {
|
|||||||
local.Close()
|
local.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes the tunnel connection
|
||||||
func (tunnel *Tunnel) Close() {
|
func (tunnel *Tunnel) Close() {
|
||||||
if tunnel.Client != nil {
|
if tunnel.Client != nil {
|
||||||
tunnel.Client.Close()
|
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 {
|
func (tunnel *Tunnel) Configure() error {
|
||||||
config, err := makeConfig(tunnel.SSHInfo)
|
config, err := makeConfig(tunnel.SSHInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -153,6 +156,7 @@ func (tunnel *Tunnel) Configure() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start starts the connection handler loop
|
||||||
func (tunnel *Tunnel) Start() {
|
func (tunnel *Tunnel) Start() {
|
||||||
defer tunnel.Close()
|
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) {
|
func NewTunnel(sshInfo *shared.SSHInfo, dbUrl string) (*Tunnel, error) {
|
||||||
uri, err := url.Parse(dbUrl)
|
uri, err := url.Parse(dbUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
listenPort, err := connection.AvailablePort(PORT_START, PORT_LIMIT)
|
listenPort, err := connection.FindAvailablePort(portStart, portLimit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Check if the TCP port available on localhost
|
// IsPortAvailable returns true if there's no listeners on a given port
|
||||||
func portAvailable(port int) bool {
|
func IsPortAvailable(port int) bool {
|
||||||
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%v", port))
|
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%v", port))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Index(err.Error(), "connection refused") > 0 {
|
if strings.Index(err.Error(), "connection refused") > 0 {
|
||||||
return true
|
return true
|
||||||
@ -22,10 +21,10 @@ func portAvailable(port int) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get available TCP port on localhost by trying available ports in a range
|
// FindAvailablePort returns the first available TCP port in the range
|
||||||
func AvailablePort(start int, limit int) (int, error) {
|
func FindAvailablePort(start int, limit int) (int, error) {
|
||||||
for i := start; i <= (start + limit); i++ {
|
for i := start; i <= (start + limit); i++ {
|
||||||
if portAvailable(i) {
|
if IsPortAvailable(i) {
|
||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,12 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_portAvailable(t *testing.T) {
|
func TestIsPortAvailable(t *testing.T) {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
t.Skip("FIXME")
|
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")
|
serv, err := net.Listen("tcp", "127.0.0.1:30000")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -35,16 +35,16 @@ func Test_portAvailable(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
assert.Equal(t, false, portAvailable(30000))
|
assert.Equal(t, false, IsPortAvailable(30000))
|
||||||
assert.Equal(t, true, portAvailable(30001))
|
assert.Equal(t, true, IsPortAvailable(30001))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_getAvailablePort(t *testing.T) {
|
func TestFindAvailablePort(t *testing.T) {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
t.Skip("FIXME")
|
t.Skip("FIXME")
|
||||||
}
|
}
|
||||||
|
|
||||||
port, err := AvailablePort(30000, 1)
|
port, err := FindAvailablePort(30000, 1)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, 30000, port)
|
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.EqualError(t, err, "No available port")
|
||||||
assert.Equal(t, -1, port)
|
assert.Equal(t, -1, port)
|
||||||
|
|
||||||
port, err = AvailablePort(30000, 1)
|
port, err = FindAvailablePort(30000, 1)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, 30001, port)
|
assert.Equal(t, 30001, port)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user