Advertisement
Guest User

Untitled

a guest
Mar 18th, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.98 KB | None | 0 0
  1. diff --git a/main.go b/main.go
  2. index 461a4402..c393a2cd 100644
  3. --- a/main.go
  4. +++ b/main.go
  5. @@ -139,11 +139,11 @@ func main() {
  6.     cookie := newOrLoadCookie(config)
  7.     metrics := server.NewLocalMetrics(logger, startupLogger, db, config)
  8.     sessionRegistry := server.NewLocalSessionRegistry(metrics)
  9. -   sessionCache := server.NewLocalSessionCache(config.GetSession().TokenExpirySec)
  10. -   consoleSessionCache := server.NewLocalSessionCache(config.GetConsole().TokenExpirySec)
  11. -   loginAttemptCache := server.NewLocalLoginAttemptCache()
  12.     statusRegistry := server.NewStatusRegistry(logger, config, sessionRegistry, jsonpbMarshaler)
  13.     tracker := server.StartLocalTracker(logger, config, sessionRegistry, statusRegistry, metrics, jsonpbMarshaler)
  14. +   sessionCache := server.NewLocalSessionCache(sessionRegistry, tracker, config.GetSession().TokenExpirySec)
  15. +   consoleSessionCache := server.NewLocalSessionCache(sessionRegistry, tracker, config.GetConsole().TokenExpirySec)
  16. +   loginAttemptCache := server.NewLocalLoginAttemptCache()
  17.     router := server.NewLocalMessageRouter(sessionRegistry, tracker, jsonpbMarshaler)
  18.     leaderboardCache := server.NewLocalLeaderboardCache(logger, startupLogger, db)
  19.     leaderboardRankCache := server.NewLocalLeaderboardRankCache(ctx, startupLogger, db, config.GetLeaderboard(), leaderboardCache)
  20. diff --git a/server/session_cache.go b/server/session_cache.go
  21. index e1d40310..660b9463 100644
  22. --- a/server/session_cache.go
  23. +++ b/server/session_cache.go
  24. @@ -20,6 +20,7 @@ import (
  25.     "time"
  26.  
  27.     "github.com/gofrs/uuid"
  28. +   "github.com/heroiclabs/nakama-common/runtime"
  29.  )
  30.  
  31.  type SessionCache interface {
  32. @@ -52,17 +53,21 @@ type LocalSessionCache struct {
  33.     ctx         context.Context
  34.     ctxCancelFn context.CancelFunc
  35.  
  36. -   cache map[uuid.UUID]*sessionCacheUser
  37. +   tracker         Tracker
  38. +   sessionRegistry SessionRegistry
  39. +   cache           map[uuid.UUID]*sessionCacheUser
  40.  }
  41.  
  42. -func NewLocalSessionCache(tokenExpirySec int64) SessionCache {
  43. +func NewLocalSessionCache(sessionRegistry SessionRegistry, tracker Tracker, tokenExpirySec int64) SessionCache {
  44.     ctx, ctxCancelFn := context.WithCancel(context.Background())
  45.  
  46.     s := &LocalSessionCache{
  47.         ctx:         ctx,
  48.         ctxCancelFn: ctxCancelFn,
  49.  
  50. -       cache: make(map[uuid.UUID]*sessionCacheUser),
  51. +       tracker:         tracker,
  52. +       sessionRegistry: sessionRegistry,
  53. +       cache:           make(map[uuid.UUID]*sessionCacheUser),
  54.     }
  55.  
  56.     go func() {
  57. @@ -173,6 +178,16 @@ func (s *LocalSessionCache) RemoveAll(userID uuid.UUID) {
  58.  func (s *LocalSessionCache) Ban(userIDs []uuid.UUID) {
  59.     s.Lock()
  60.     for _, userID := range userIDs {
  61. +       if _, ok := s.cache[userID]; !ok {
  62. +           continue
  63. +       }
  64. +
  65. +       for _, presence := range s.tracker.ListPresenceIDByStream(PresenceStream{Mode: StreamModeNotifications, Subject: userID}) {
  66. +           if activeSession := s.sessionRegistry.Get(presence.SessionID); activeSession != nil {
  67. +               activeSession.Close("you banned", runtime.PresenceReasonDisconnect)
  68. +           }
  69. +       }
  70. +
  71.         delete(s.cache, userID)
  72.     }
  73.     s.Unlock()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement