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 } }