2023-04-27 09:03:40 +08:00
|
|
|
package websrv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-05 04:11:12 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/common"
|
2023-04-27 09:03:40 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2023-10-05 04:11:12 +08:00
|
|
|
func ConnectionInitHandler(browserConnectionId string, browserConnectionContext context.Context, fromBrowser *common.SafeChannel, wg *sync.WaitGroup) {
|
2023-09-30 07:05:23 +08:00
|
|
|
log := log.WithField("_routine", "ConnectionInitHandler").WithField("browserConnectionId", browserConnectionId)
|
|
|
|
|
|
|
|
log.Debugf("starting")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
defer wg.Done()
|
2023-09-07 22:54:27 +08:00
|
|
|
defer log.Debugf("finished")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
2023-07-06 20:34:48 +08:00
|
|
|
BrowserConnectionsMutex.RLock()
|
2023-09-30 07:05:23 +08:00
|
|
|
browserConnection := BrowserConnections[browserConnectionId]
|
2023-07-06 20:34:48 +08:00
|
|
|
BrowserConnectionsMutex.RUnlock()
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Intercept the fromBrowserMessage channel to get the sessionToken
|
2023-10-05 04:11:12 +08:00
|
|
|
for {
|
|
|
|
fromBrowserMessage, ok := fromBrowser.Receive()
|
|
|
|
if !ok {
|
|
|
|
//Received all messages. Channel is closed
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
if browserConnection.SessionToken == "" {
|
|
|
|
var fromBrowserMessageAsMap = fromBrowserMessage.(map[string]interface{})
|
|
|
|
|
|
|
|
if fromBrowserMessageAsMap["type"] == "connection_init" {
|
|
|
|
var payloadAsMap = fromBrowserMessageAsMap["payload"].(map[string]interface{})
|
|
|
|
var headersAsMap = payloadAsMap["headers"].(map[string]interface{})
|
|
|
|
var sessionToken = headersAsMap["X-Session-Token"]
|
|
|
|
if sessionToken != nil {
|
|
|
|
sessionToken := headersAsMap["X-Session-Token"].(string)
|
|
|
|
log.Infof("[SessionTokenReader] intercepted session token %v", sessionToken)
|
2023-09-30 07:05:23 +08:00
|
|
|
BrowserConnectionsMutex.Lock()
|
2023-04-27 09:03:40 +08:00
|
|
|
browserConnection.SessionToken = sessionToken
|
2023-09-30 07:05:23 +08:00
|
|
|
BrowserConnectionsMutex.Unlock()
|
|
|
|
|
2023-10-03 22:53:47 +08:00
|
|
|
go SendUserGraphqlConnectionStablishedSysMsg(sessionToken, browserConnectionId)
|
2023-10-05 04:11:12 +08:00
|
|
|
fromBrowser.Close()
|
2023-09-30 07:05:23 +08:00
|
|
|
break
|
2023-04-27 09:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|