Skip to content

Commit 4f4fabd

Browse files
Enriqueelliotchance
Enrique
authored andcommitted
Upgrade to Go Mods and prevent leaks closing tunnels (#2)
1 parent de385e5 commit 4f4fabd

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/elliotchance/sshtunnel
2+
3+
go 1.13
4+
5+
require golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2+
golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c h1:/nJuwDLoL/zrqY6gf57vxC+Pi+pZ8bfhpPkicO5H7W4=
3+
golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
4+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
5+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
6+
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
7+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

ssh_tunnel.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"log"
77
"net"
8+
"sync"
89
)
910

1011
type SSHTunnel struct {
@@ -13,6 +14,7 @@ type SSHTunnel struct {
1314
Remote *Endpoint
1415
Config *ssh.ClientConfig
1516
Log *log.Logger
17+
close chan interface{}
1618
}
1719

1820
func (tunnel *SSHTunnel) logf(fmt string, args ...interface{}) {
@@ -26,47 +28,59 @@ func (tunnel *SSHTunnel) Start() error {
2628
if err != nil {
2729
return err
2830
}
29-
defer listener.Close()
30-
3131
tunnel.Local.Port = listener.Addr().(*net.TCPAddr).Port
32-
3332
for {
3433
conn, err := listener.Accept()
3534
if err != nil {
3635
return err
3736
}
38-
3937
tunnel.logf("accepted connection")
40-
go tunnel.forward(conn)
38+
var wg sync.WaitGroup
39+
go tunnel.forward(conn, &wg)
40+
wg.Wait()
41+
tunnel.logf("tunnel closed")
42+
break
4143
}
44+
err = listener.Close()
45+
if err != nil {
46+
return err
47+
}
48+
return nil
4249
}
4350

44-
func (tunnel *SSHTunnel) forward(localConn net.Conn) {
51+
func (tunnel *SSHTunnel) forward(localConn net.Conn, wg *sync.WaitGroup) {
4552
serverConn, err := ssh.Dial("tcp", tunnel.Server.String(), tunnel.Config)
4653
if err != nil {
4754
tunnel.logf("server dial error: %s", err)
4855
return
4956
}
50-
5157
tunnel.logf("connected to %s (1 of 2)\n", tunnel.Server.String())
52-
5358
remoteConn, err := serverConn.Dial("tcp", tunnel.Remote.String())
5459
if err != nil {
5560
tunnel.logf("remote dial error: %s", err)
5661
return
5762
}
58-
5963
tunnel.logf("connected to %s (2 of 2)\n", tunnel.Remote.String())
60-
6164
copyConn := func(writer, reader net.Conn) {
6265
_, err := io.Copy(writer, reader)
6366
if err != nil {
6467
tunnel.logf("io.Copy error: %s", err)
6568
}
6669
}
67-
6870
go copyConn(localConn, remoteConn)
6971
go copyConn(remoteConn, localConn)
72+
<-tunnel.close
73+
tunnel.logf("close signal received, closing...")
74+
_ = localConn.Close()
75+
_ = serverConn.Close()
76+
_ = remoteConn.Close()
77+
wg.Done()
78+
return
79+
}
80+
81+
func (tunnel *SSHTunnel) Close() {
82+
tunnel.close <- struct{}{}
83+
return
7084
}
7185

7286
func NewSSHTunnel(tunnel string, auth ssh.AuthMethod, destination string) *SSHTunnel {
@@ -90,6 +104,7 @@ func NewSSHTunnel(tunnel string, auth ssh.AuthMethod, destination string) *SSHTu
90104
Local: localEndpoint,
91105
Server: server,
92106
Remote: NewEndpoint(destination),
107+
close: make(chan interface{}),
93108
}
94109

95110
return sshTunnel

0 commit comments

Comments
 (0)