diff --git a/tunnel/default.go b/tunnel/default.go index 7d2d3984..92821589 100644 --- a/tunnel/default.go +++ b/tunnel/default.go @@ -418,6 +418,8 @@ func (t *tun) Listen(addr string) (Listener, error) { accept: make(chan *socket, 128), // the channel to close closed: make(chan bool), + // tunnel closed channel + tunClosed: t.closed, // the connection conn: c, // the listener socket diff --git a/tunnel/listener.go b/tunnel/listener.go index 6c803eb9..070b313b 100644 --- a/tunnel/listener.go +++ b/tunnel/listener.go @@ -11,6 +11,8 @@ type tunListener struct { accept chan *socket // the channel to close closed chan bool + // the tunnel closed channel + tunClosed chan bool // the connection conn Conn // the listener socket @@ -74,6 +76,7 @@ func (t *tunListener) Addr() string { return t.addr } +// Close closes tunnel listener func (t *tunListener) Close() error { select { case <-t.closed: @@ -90,6 +93,10 @@ func (t *tunListener) Accept() (Conn, error) { // if the socket is closed return case <-t.closed: return nil, io.EOF + case <-t.tunClosed: + // close the listener when the tunnel closes + close(t.closed) + return nil, io.EOF // wait for a new connection case c, ok := <-t.accept: if !ok { diff --git a/tunnel/socket.go b/tunnel/socket.go index b1c55797..ac738efc 100644 --- a/tunnel/socket.go +++ b/tunnel/socket.go @@ -79,6 +79,7 @@ func (s *socket) Recv(m *transport.Message) error { return nil } +// Close closes the socket func (s *socket) Close() error { select { case <-s.closed: