From e1273a3c5289f4b0d4faa3b91710f9886f5eb0c9 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Wed, 20 Sep 2023 13:01:07 +0200 Subject: [PATCH] Keep list of possible dialout sessions. --- backend_server.go | 12 ++++-------- hub.go | 14 +++++++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/backend_server.go b/backend_server.go index 84a7faf..32efa16 100644 --- a/backend_server.go +++ b/backend_server.go @@ -674,15 +674,11 @@ func (b *BackendServer) startDialout(roomid string, backend *Backend, request *B return returnDialoutError(http.StatusBadRequest, err) } - // TODO: Add direct lookup of internal sessions that are not in a room and support dialout. var session *ClientSession - for _, s := range b.hub.sessions { - if s.GetRoom() == nil && s.ClientType() == HelloClientTypeInternal { - clientSession, ok := s.(*ClientSession) - if ok && clientSession.GetClient() != nil && clientSession.HasFeature(ClientFeatureStartDialout) { - session = clientSession - break - } + for s := range b.hub.dialoutSessions { + if s.GetClient() != nil { + session = s + break } } if session == nil { diff --git a/hub.go b/hub.go index fcbea99..a2eb83f 100644 --- a/hub.go +++ b/hub.go @@ -150,6 +150,7 @@ type Hub struct { expiredSessions map[Session]bool anonymousSessions map[*ClientSession]time.Time expectHelloClients map[*Client]time.Time + dialoutSessions map[*ClientSession]bool backendTimeout time.Duration backend *BackendClient @@ -338,6 +339,7 @@ func NewHub(config *goconf.ConfigFile, events AsyncEvents, rpcServer *GrpcServer expiredSessions: make(map[Session]bool), anonymousSessions: make(map[*ClientSession]time.Time), expectHelloClients: make(map[*Client]time.Time), + dialoutSessions: make(map[*ClientSession]bool), backendTimeout: backendTimeout, backend: backend, @@ -641,6 +643,7 @@ func (h *Hub) removeSession(session Session) (removed bool) { delete(h.expiredSessions, session) if session, ok := session.(*ClientSession); ok { delete(h.anonymousSessions, session) + delete(h.dialoutSessions, session) } h.mu.Unlock() return @@ -802,8 +805,12 @@ func (h *Hub) processRegister(client *Client, message *ClientMessage, backend *B h.sessions[sessionIdData.Sid] = session h.clients[sessionIdData.Sid] = client delete(h.expectHelloClients, client) - if userId == "" && auth.Type != HelloClientTypeInternal { + if userId == "" && session.ClientType() != HelloClientTypeInternal { h.startWaitAnonymousSessionRoomLocked(session) + } else if session.ClientType() == HelloClientTypeInternal && session.HasFeature(ClientFeatureStartDialout) { + // TODO: There is a small race condition for sessions that take some time + // between connecting and joining a room. + h.dialoutSessions[session] = true } h.mu.Unlock() @@ -1250,6 +1257,7 @@ func (h *Hub) processRoom(client *Client, message *ClientMessage) { h.startWaitAnonymousSessionRoom(session) } } + return } @@ -1389,6 +1397,10 @@ func (h *Hub) processJoinRoom(session *ClientSession, message *ClientMessage, ro h.mu.Lock() // The session now joined a room, don't expire if it is anonymous. delete(h.anonymousSessions, session) + if session.ClientType() == HelloClientTypeInternal && session.HasFeature(ClientFeatureStartDialout) { + // An internal session in a room can not be used for dialout. + delete(h.dialoutSessions, session) + } h.mu.Unlock() session.SetRoom(r) if room.Room.Permissions != nil {