2023-04-27 09:03:40 +08:00
|
|
|
package websrv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-05-10 02:37:58 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/common"
|
2024-05-02 21:45:32 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/gql_actions"
|
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/hasura"
|
2023-05-10 02:37:58 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/msgpatch"
|
2023-04-27 09:03:40 +08:00
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/websrv/reader"
|
|
|
|
"github.com/iMDT/bbb-graphql-middleware/internal/websrv/writer"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"net/http"
|
2023-05-10 02:37:58 +08:00
|
|
|
"nhooyr.io/websocket"
|
2023-11-24 21:49:23 +08:00
|
|
|
"os"
|
2023-04-27 09:03:40 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var lastBrowserConnectionId int
|
|
|
|
|
|
|
|
// Buffer size of the channels
|
|
|
|
var bufferSize = 100
|
|
|
|
|
|
|
|
// active browser connections
|
|
|
|
var BrowserConnections = make(map[string]*common.BrowserConnection)
|
2023-07-06 20:34:48 +08:00
|
|
|
var BrowserConnectionsMutex = &sync.RWMutex{}
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Handle client connection
|
|
|
|
// This is the connection that comes from browser
|
|
|
|
func ConnectionHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log := log.WithField("_routine", "ConnectionHandler")
|
2024-03-29 04:17:13 +08:00
|
|
|
common.ActivitiesOverviewStarted("__BrowserConnection")
|
|
|
|
defer common.ActivitiesOverviewCompleted("__BrowserConnection")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Obtain id for this connection
|
|
|
|
lastBrowserConnectionId++
|
|
|
|
browserConnectionId := "BC" + fmt.Sprintf("%010d", lastBrowserConnectionId)
|
|
|
|
log = log.WithField("browserConnectionId", browserConnectionId)
|
|
|
|
|
|
|
|
// Starts a context that will be dependent on the connection, so we can cancel subroutines when the connection is dropped
|
|
|
|
browserConnectionContext, browserConnectionContextCancel := context.WithCancel(r.Context())
|
|
|
|
defer browserConnectionContextCancel()
|
|
|
|
|
|
|
|
// Add sub-protocol
|
|
|
|
var acceptOptions websocket.AcceptOptions
|
|
|
|
acceptOptions.Subprotocols = append(acceptOptions.Subprotocols, "graphql-ws")
|
2023-11-24 21:49:23 +08:00
|
|
|
bbbOrigin := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_ORIGIN")
|
|
|
|
if bbbOrigin != "" {
|
|
|
|
acceptOptions.OriginPatterns = append(acceptOptions.OriginPatterns, bbbOrigin)
|
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
|
2024-03-13 07:12:55 +08:00
|
|
|
browserWsConn, err := websocket.Accept(w, r, &acceptOptions)
|
2024-03-28 02:29:38 +08:00
|
|
|
browserWsConn.SetReadLimit(9999999) //10MB
|
2023-04-27 09:03:40 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error: %v", err)
|
|
|
|
}
|
2024-03-13 07:12:55 +08:00
|
|
|
defer browserWsConn.Close(websocket.StatusInternalError, "the sky is falling")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
var thisConnection = common.BrowserConnection{
|
2024-01-24 07:20:16 +08:00
|
|
|
Id: browserConnectionId,
|
|
|
|
ActiveSubscriptions: make(map[string]common.GraphQlSubscription, 1),
|
|
|
|
Context: browserConnectionContext,
|
|
|
|
ConnAckSentToBrowser: false,
|
2023-04-27 09:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BrowserConnectionsMutex.Lock()
|
|
|
|
BrowserConnections[browserConnectionId] = &thisConnection
|
|
|
|
BrowserConnectionsMutex.Unlock()
|
|
|
|
|
|
|
|
defer func() {
|
2023-05-25 21:15:02 +08:00
|
|
|
msgpatch.RemoveConnCacheDir(browserConnectionId)
|
2023-04-27 09:03:40 +08:00
|
|
|
BrowserConnectionsMutex.Lock()
|
2024-03-18 22:28:11 +08:00
|
|
|
_, bcExists := BrowserConnections[browserConnectionId]
|
|
|
|
if bcExists {
|
|
|
|
sessionTokenRemoved := BrowserConnections[browserConnectionId].SessionToken
|
|
|
|
delete(BrowserConnections, browserConnectionId)
|
|
|
|
go SendUserGraphqlConnectionClosedSysMsg(sessionTokenRemoved, browserConnectionId)
|
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
BrowserConnectionsMutex.Unlock()
|
2023-09-07 22:54:27 +08:00
|
|
|
|
|
|
|
log.Infof("connection removed")
|
2023-04-27 09:03:40 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Log it
|
2023-09-07 22:54:27 +08:00
|
|
|
log.Infof("connection accepted")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Create channels
|
2023-12-15 01:01:47 +08:00
|
|
|
fromBrowserToHasuraConnectionEstablishingChannel := common.NewSafeChannel(bufferSize)
|
|
|
|
fromBrowserToHasuraChannel := common.NewSafeChannel(bufferSize)
|
2024-05-02 21:45:32 +08:00
|
|
|
fromBrowserToGqlActionsChannel := common.NewSafeChannel(bufferSize)
|
2023-12-15 01:01:47 +08:00
|
|
|
fromHasuraToBrowserChannel := common.NewSafeChannel(bufferSize)
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Ensure a hasura client is running while the browser is connected
|
|
|
|
go func() {
|
2023-09-07 22:54:27 +08:00
|
|
|
log.Debugf("starting hasura client")
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
BrowserConnectedLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-browserConnectionContext.Done():
|
|
|
|
break BrowserConnectedLoop
|
|
|
|
default:
|
|
|
|
{
|
2023-09-07 22:54:27 +08:00
|
|
|
log.Debugf("creating hasura client")
|
2023-07-06 20:34:48 +08:00
|
|
|
BrowserConnectionsMutex.RLock()
|
2023-04-27 09:03:40 +08:00
|
|
|
thisBrowserConnection := BrowserConnections[browserConnectionId]
|
2023-07-06 20:34:48 +08:00
|
|
|
BrowserConnectionsMutex.RUnlock()
|
2023-04-28 05:30:36 +08:00
|
|
|
if thisBrowserConnection != nil {
|
2024-01-24 07:20:16 +08:00
|
|
|
log.Debugf("created hasura client")
|
2024-05-02 21:45:32 +08:00
|
|
|
hasura.HasuraClient(thisBrowserConnection, r.Cookies(), fromBrowserToHasuraChannel, fromHasuraToBrowserChannel)
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Ensure a gql-actions client is running while the browser is connected
|
|
|
|
go func() {
|
|
|
|
log.Debugf("starting gql-actions client")
|
|
|
|
|
|
|
|
BrowserConnectedLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-browserConnectionContext.Done():
|
|
|
|
break BrowserConnectedLoop
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
log.Debugf("creating gql-actions client")
|
|
|
|
BrowserConnectionsMutex.RLock()
|
|
|
|
thisBrowserConnection := BrowserConnections[browserConnectionId]
|
|
|
|
BrowserConnectionsMutex.RUnlock()
|
|
|
|
if thisBrowserConnection != nil {
|
|
|
|
log.Debugf("created gql-actions client")
|
|
|
|
|
|
|
|
BrowserConnectionsMutex.Lock()
|
|
|
|
thisBrowserConnection.GraphqlActionsContext, thisBrowserConnection.GraphqlActionsContextCancel = context.WithCancel(browserConnectionContext)
|
|
|
|
BrowserConnectionsMutex.Unlock()
|
|
|
|
|
|
|
|
gql_actions.GraphqlActionsClient(thisBrowserConnection, r.Cookies(), fromBrowserToGqlActionsChannel, fromBrowserToHasuraChannel, fromHasuraToBrowserChannel)
|
2023-04-28 05:30:36 +08:00
|
|
|
}
|
2023-04-27 09:03:40 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Configure the wait group (to hold this routine execution until both are completed)
|
|
|
|
var wgAll sync.WaitGroup
|
|
|
|
wgAll.Add(3)
|
|
|
|
|
|
|
|
var wgReader sync.WaitGroup
|
|
|
|
wgReader.Add(1)
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
// Reads from browser connection, writes into fromBrowserToHasuraChannel and fromBrowserToHasuraConnectionEstablishingChannel
|
2024-05-02 21:45:32 +08:00
|
|
|
go reader.BrowserConnectionReader(
|
|
|
|
browserConnectionId,
|
|
|
|
browserConnectionContext,
|
|
|
|
browserConnectionContextCancel,
|
|
|
|
browserWsConn,
|
|
|
|
fromBrowserToGqlActionsChannel,
|
|
|
|
fromBrowserToHasuraChannel,
|
|
|
|
fromBrowserToHasuraConnectionEstablishingChannel,
|
|
|
|
[]*sync.WaitGroup{&wgAll, &wgReader})
|
2023-04-27 09:03:40 +08:00
|
|
|
go func() {
|
|
|
|
wgReader.Wait()
|
2024-03-13 07:12:55 +08:00
|
|
|
log.Debug("BrowserConnectionReader finished, closing Write Channel")
|
|
|
|
fromHasuraToBrowserChannel.Close()
|
2023-04-27 09:03:40 +08:00
|
|
|
thisConnection.Disconnected = true
|
|
|
|
}()
|
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
// Reads from fromHasuraToBrowserChannel, writes to browser connection
|
2024-03-13 07:12:55 +08:00
|
|
|
go writer.BrowserConnectionWriter(browserConnectionId, browserConnectionContext, browserWsConn, fromHasuraToBrowserChannel, &wgAll)
|
2023-04-27 09:03:40 +08:00
|
|
|
|
2023-12-15 01:01:47 +08:00
|
|
|
go ConnectionInitHandler(browserConnectionId, browserConnectionContext, fromBrowserToHasuraConnectionEstablishingChannel, &wgAll)
|
2023-04-27 09:03:40 +08:00
|
|
|
|
|
|
|
// Wait until all routines are finished
|
|
|
|
wgAll.Wait()
|
|
|
|
}
|
2023-09-30 07:05:23 +08:00
|
|
|
|
|
|
|
func InvalidateSessionTokenConnections(sessionTokenToInvalidate string) {
|
|
|
|
BrowserConnectionsMutex.RLock()
|
2024-03-18 22:28:11 +08:00
|
|
|
connectionsToProcess := make([]*common.BrowserConnection, 0)
|
2023-09-30 07:05:23 +08:00
|
|
|
for _, browserConnection := range BrowserConnections {
|
2024-03-18 22:28:11 +08:00
|
|
|
if browserConnection.SessionToken == sessionTokenToInvalidate {
|
|
|
|
connectionsToProcess = append(connectionsToProcess, browserConnection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BrowserConnectionsMutex.RUnlock()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, browserConnection := range connectionsToProcess {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(bc *common.BrowserConnection) {
|
|
|
|
defer wg.Done()
|
2024-05-02 21:45:32 +08:00
|
|
|
invalidateHasuraConnectionForSessionToken(bc, sessionTokenToInvalidate)
|
2024-03-18 22:28:11 +08:00
|
|
|
}(browserConnection)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2024-05-02 21:45:32 +08:00
|
|
|
func invalidateHasuraConnectionForSessionToken(bc *common.BrowserConnection, sessionToken string) {
|
2024-03-18 22:28:11 +08:00
|
|
|
if bc.HasuraConnection == nil {
|
|
|
|
return // If there's no Hasura connection, there's nothing to invalidate.
|
|
|
|
}
|
|
|
|
|
|
|
|
hasuraConnectionId := bc.HasuraConnection.Id
|
|
|
|
|
|
|
|
// Send message to stop receiving new messages from the browser.
|
|
|
|
bc.HasuraConnection.FreezeMsgFromBrowserChan.Send(true)
|
2024-05-02 21:45:32 +08:00
|
|
|
bc.GraphqlActionsContextCancel()
|
2024-03-18 22:28:11 +08:00
|
|
|
|
|
|
|
// Wait until there are no active mutations.
|
|
|
|
for iterationCount := 0; iterationCount < 20; iterationCount++ {
|
|
|
|
activeMutationFound := false
|
|
|
|
bc.ActiveSubscriptionsMutex.RLock()
|
|
|
|
for _, subscription := range bc.ActiveSubscriptions {
|
|
|
|
if subscription.Type == common.Mutation {
|
|
|
|
activeMutationFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bc.ActiveSubscriptionsMutex.RUnlock()
|
|
|
|
|
|
|
|
if !activeMutationFound {
|
|
|
|
break // Exit the loop if no active mutations are found.
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond) // Wait a bit before checking again.
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Processing invalidate request for sessionToken %v (hasura connection %v)", sessionToken, hasuraConnectionId)
|
|
|
|
|
|
|
|
// Cancel the Hasura connection context to clean up resources.
|
|
|
|
if bc.HasuraConnection != nil && bc.HasuraConnection.ContextCancelFunc != nil {
|
|
|
|
bc.HasuraConnection.ContextCancelFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Processed invalidate request for sessionToken %v (hasura connection %v)", sessionToken, hasuraConnectionId)
|
|
|
|
|
|
|
|
// Send a reconnection confirmation message
|
|
|
|
go SendUserGraphqlReconnectionForcedEvtMsg(sessionToken)
|
|
|
|
}
|