Merge pull request #656 from milosgajdos83/tun-listener

Close the tunnel listener when the tunnel is cloed.
This commit is contained in:
Asim Aslam 2019-08-08 15:25:19 +01:00 committed by GitHub
commit 37988b596d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 0 deletions

View File

@ -430,6 +430,8 @@ func (t *tun) Listen(addr string) (Listener, error) {
accept: make(chan *socket, 128), accept: make(chan *socket, 128),
// the channel to close // the channel to close
closed: make(chan bool), closed: make(chan bool),
// tunnel closed channel
tunClosed: t.closed,
// the connection // the connection
conn: c, conn: c,
// the listener socket // the listener socket

View File

@ -11,6 +11,8 @@ type tunListener struct {
accept chan *socket accept chan *socket
// the channel to close // the channel to close
closed chan bool closed chan bool
// the tunnel closed channel
tunClosed chan bool
// the connection // the connection
conn Conn conn Conn
// the listener socket // the listener socket
@ -74,6 +76,7 @@ func (t *tunListener) Addr() string {
return t.addr return t.addr
} }
// Close closes tunnel listener
func (t *tunListener) Close() error { func (t *tunListener) Close() error {
select { select {
case <-t.closed: case <-t.closed:
@ -90,6 +93,10 @@ func (t *tunListener) Accept() (Conn, error) {
// if the socket is closed return // if the socket is closed return
case <-t.closed: case <-t.closed:
return nil, io.EOF 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 // wait for a new connection
case c, ok := <-t.accept: case c, ok := <-t.accept:
if !ok { if !ok {

View File

@ -79,6 +79,7 @@ func (s *socket) Recv(m *transport.Message) error {
return nil return nil
} }
// Close closes the socket
func (s *socket) Close() error { func (s *socket) Close() error {
select { select {
case <-s.closed: case <-s.closed: