From 56fc4f89ba331de75489a3bf658327afe6e6097d Mon Sep 17 00:00:00 2001
From: Christine Dodrill <me@christine.website>
Date: Wed, 5 Apr 2017 14:48:08 -0700
Subject: [PATCH] tun2: loop forever at the foot of HandleConn

---
 lib/tun2/server.go | 86 ++++++++++++++++++++++++----------------------
 1 file changed, 44 insertions(+), 42 deletions(-)

diff --git a/lib/tun2/server.go b/lib/tun2/server.go
index ede8c05..fa0fd2f 100644
--- a/lib/tun2/server.go
+++ b/lib/tun2/server.go
@@ -361,52 +361,54 @@ func (s *Server) HandleConn(c net.Conn, isKCP bool) {
 	ticker := time.NewTicker(5 * time.Second)
 	defer ticker.Stop()
 
-	select {
-	case <-ticker.C:
-		err := connection.Ping()
-		if err != nil {
-			cancel()
-		}
-	case <-ctx.Done():
-		s.connlock.Lock()
-		delete(s.conns, c)
-		s.connlock.Unlock()
-
-		var conns []*Connection
-
-		val, ok := s.domains.Get(auth.Domain)
-		if ok {
-			conns, ok = val.([]*Connection)
-			if !ok {
-				ln.Error(err, connection.F(), ln.F{
-					"action": "looking_up_for_disconnect_removal",
-				})
-				return
+	for {
+		select {
+		case <-ticker.C:
+			err := connection.Ping()
+			if err != nil {
+				cancel()
 			}
-		}
+		case <-ctx.Done():
+			s.connlock.Lock()
+			delete(s.conns, c)
+			s.connlock.Unlock()
 
-		for i, cntn := range conns {
-			if cntn.id == connection.id {
-				conns[i] = conns[len(conns)-1]
-				conns = conns[:len(conns)-1]
+			var conns []*Connection
+
+			val, ok := s.domains.Get(auth.Domain)
+			if ok {
+				conns, ok = val.([]*Connection)
+				if !ok {
+					ln.Error(err, connection.F(), ln.F{
+						"action": "looking_up_for_disconnect_removal",
+					})
+					return
+				}
 			}
+
+			for i, cntn := range conns {
+				if cntn.id == connection.id {
+					conns[i] = conns[len(conns)-1]
+					conns = conns[:len(conns)-1]
+				}
+			}
+
+			if len(conns) != 0 {
+				s.domains.Set(auth.Domain, conns)
+			} else {
+				s.domains.Remove(auth.Domain)
+			}
+
+			ln.Log(connection.F(), ln.F{
+				"action": "client_disconnecting",
+			})
+
+			controlStream.Close()
+			session.Close()
+			c.Close()
+
+			return
 		}
-
-		if len(conns) != 0 {
-			s.domains.Set(auth.Domain, conns)
-		} else {
-			s.domains.Remove(auth.Domain)
-		}
-
-		ln.Log(connection.F(), ln.F{
-			"action": "client_disconnecting",
-		})
-
-		controlStream.Close()
-		session.Close()
-		c.Close()
-
-		return
 	}
 }