neko/internal/session/manager.go

394 lines
9.0 KiB
Go
Raw Normal View History

package session
import (
2021-08-30 03:09:13 +12:00
"errors"
2020-11-15 10:54:35 +13:00
"sync"
"sync/atomic"
2020-11-15 10:54:35 +13:00
"github.com/kataras/go-events"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/demodesk/neko/internal/config"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
)
func New(config *config.Session) *SessionManagerCtx {
2021-03-16 01:34:14 +13:00
manager := &SessionManagerCtx{
2022-03-27 12:08:06 +13:00
logger: log.With().Str("module", "session").Logger(),
config: config,
settings: types.Settings{
PrivateMode: false, // By default disabled.
ImplicitHosting: config.ImplicitHosting,
InactiveCursors: config.InactiveCursors,
MercifulReconnect: config.MercifulReconnect,
},
2021-10-24 12:09:41 +13:00
tokens: make(map[string]string),
sessions: make(map[string]*SessionCtx),
2021-11-13 04:27:05 +13:00
cursors: make(map[types.Session][]types.Cursor),
2021-10-24 12:09:41 +13:00
emmiter: events.New(),
}
2021-03-16 01:34:14 +13:00
// create API session
if config.APIToken != "" {
manager.apiSession = &SessionCtx{
id: "API",
token: config.APIToken,
manager: manager,
logger: manager.logger.With().Str("session_id", "API").Logger(),
profile: types.MemberProfile{
2021-03-16 04:28:38 +13:00
Name: "API Session",
IsAdmin: true,
CanLogin: true,
CanConnect: false,
CanWatch: true,
CanHost: true,
CanAccessClipboard: true,
2021-03-16 01:34:14 +13:00
},
}
}
return manager
}
2020-11-02 04:09:48 +13:00
type SessionManagerCtx struct {
2021-10-24 12:09:41 +13:00
logger zerolog.Logger
config *config.Session
2022-03-27 12:08:06 +13:00
settings types.Settings
settingsMu sync.Mutex
tokens map[string]string
2021-03-14 09:11:48 +13:00
sessions map[string]*SessionCtx
sessionsMu sync.Mutex
2021-10-24 12:09:41 +13:00
hostId atomic.Value
2021-10-24 12:09:41 +13:00
2021-11-13 04:27:05 +13:00
cursors map[types.Session][]types.Cursor
2021-10-24 12:09:41 +13:00
cursorsMu sync.Mutex
2021-03-14 09:11:48 +13:00
emmiter events.EventEmmiter
2021-03-16 01:34:14 +13:00
apiSession *SessionCtx
2020-12-06 11:31:41 +13:00
}
2021-03-14 10:54:34 +13:00
func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, string, error) {
token, err := utils.NewUID(64)
if err != nil {
return nil, "", err
2021-03-14 09:43:36 +13:00
}
2020-12-06 11:05:46 +13:00
2021-03-14 09:43:36 +13:00
manager.sessionsMu.Lock()
if _, ok := manager.sessions[id]; ok {
manager.sessionsMu.Unlock()
2021-08-30 03:09:13 +12:00
return nil, "", types.ErrSessionAlreadyExists
}
if _, ok := manager.tokens[token]; ok {
2021-03-14 09:11:48 +13:00
manager.sessionsMu.Unlock()
2021-08-30 03:09:13 +12:00
return nil, "", errors.New("session token already exists")
2020-12-06 11:05:46 +13:00
}
2021-03-14 09:11:48 +13:00
session := &SessionCtx{
id: id,
token: token,
2021-03-14 09:11:48 +13:00
manager: manager,
2021-03-16 01:34:14 +13:00
logger: manager.logger.With().Str("session_id", id).Logger(),
2021-03-14 09:11:48 +13:00
profile: profile,
2020-12-06 11:05:46 +13:00
}
manager.tokens[token] = id
2021-03-14 09:11:48 +13:00
manager.sessions[id] = session
manager.sessionsMu.Unlock()
2020-12-04 08:05:44 +13:00
2020-12-03 06:59:32 +13:00
manager.emmiter.Emit("created", session)
return session, token, nil
}
2020-12-06 10:18:45 +13:00
func (manager *SessionManagerCtx) Update(id string, profile types.MemberProfile) error {
2021-03-14 09:11:48 +13:00
manager.sessionsMu.Lock()
2020-12-06 11:05:46 +13:00
2021-03-14 09:11:48 +13:00
session, ok := manager.sessions[id]
2020-12-06 10:18:45 +13:00
if !ok {
2021-03-14 09:11:48 +13:00
manager.sessionsMu.Unlock()
2021-08-30 03:09:13 +12:00
return types.ErrSessionNotFound
2020-12-06 11:05:46 +13:00
}
2020-12-06 10:18:45 +13:00
session.profile = profile
2021-03-14 09:11:48 +13:00
manager.sessionsMu.Unlock()
2020-12-06 10:18:45 +13:00
manager.emmiter.Emit("profile_changed", session)
2020-12-07 07:29:39 +13:00
session.profileChanged()
2020-12-06 10:18:45 +13:00
return nil
}
2020-11-26 10:06:13 +13:00
func (manager *SessionManagerCtx) Delete(id string) error {
2021-03-14 09:11:48 +13:00
manager.sessionsMu.Lock()
session, ok := manager.sessions[id]
2020-11-26 10:06:13 +13:00
if !ok {
2021-03-14 09:11:48 +13:00
manager.sessionsMu.Unlock()
2021-08-30 03:09:13 +12:00
return types.ErrSessionNotFound
2020-11-26 10:06:13 +13:00
}
2021-03-16 22:53:03 +13:00
delete(manager.tokens, session.token)
2021-03-14 09:11:48 +13:00
delete(manager.sessions, id)
manager.sessionsMu.Unlock()
2020-11-26 10:06:13 +13:00
2021-03-14 12:50:08 +13:00
if session.State().IsConnected {
2021-09-03 07:52:23 +12:00
session.GetWebSocketPeer().Destroy("session deleted")
2021-03-26 02:08:26 +13:00
}
if session.State().IsWatching {
2021-08-30 05:17:10 +12:00
session.GetWebRTCPeer().Destroy()
2020-11-02 04:09:48 +13:00
}
2020-12-04 08:05:44 +13:00
manager.emmiter.Emit("deleted", session)
2021-08-30 05:17:10 +12:00
return nil
2020-11-02 04:09:48 +13:00
}
2020-12-06 11:05:46 +13:00
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
2021-03-14 09:11:48 +13:00
manager.sessionsMu.Lock()
defer manager.sessionsMu.Unlock()
2020-12-06 11:05:46 +13:00
2021-03-14 09:11:48 +13:00
session, ok := manager.sessions[id]
2020-12-06 11:05:46 +13:00
return session, ok
}
func (manager *SessionManagerCtx) GetByToken(token string) (types.Session, bool) {
manager.sessionsMu.Lock()
id, ok := manager.tokens[token]
manager.sessionsMu.Unlock()
2021-03-16 01:34:14 +13:00
if ok {
return manager.Get(id)
}
// is API session
if manager.apiSession != nil && manager.apiSession.token == token {
return manager.apiSession, true
}
2021-03-16 01:34:14 +13:00
return nil, false
}
2021-03-14 09:11:48 +13:00
func (manager *SessionManagerCtx) List() []types.Session {
manager.sessionsMu.Lock()
defer manager.sessionsMu.Unlock()
var sessions []types.Session
for _, session := range manager.sessions {
sessions = append(sessions, session)
}
return sessions
}
2020-11-02 04:09:48 +13:00
// ---
// host
// ---
2020-12-03 05:23:35 +13:00
2020-11-02 04:09:48 +13:00
func (manager *SessionManagerCtx) SetHost(host types.Session) {
var hostId string
if host != nil {
hostId = host.ID()
}
2021-03-14 09:37:00 +13:00
manager.hostId.Store(hostId)
2021-03-14 09:37:00 +13:00
manager.emmiter.Emit("host_changed", host)
}
func (manager *SessionManagerCtx) GetHost() (types.Session, bool) {
hostId, ok := manager.hostId.Load().(string)
if !ok || hostId == "" {
return nil, false
}
2020-11-15 10:54:35 +13:00
return manager.Get(hostId)
}
2020-11-02 04:09:48 +13:00
func (manager *SessionManagerCtx) ClearHost() {
2021-03-14 09:37:00 +13:00
manager.SetHost(nil)
}
func (manager *SessionManagerCtx) isHost(host types.Session) bool {
hostId, ok := manager.hostId.Load().(string)
return ok && hostId == host.ID()
}
2021-10-24 12:09:41 +13:00
// ---
// cursors
// ---
2021-11-02 05:31:00 +13:00
func (manager *SessionManagerCtx) SetCursor(cursor types.Cursor, session types.Session) {
2021-10-24 12:09:41 +13:00
manager.cursorsMu.Lock()
defer manager.cursorsMu.Unlock()
2021-11-13 04:27:05 +13:00
list, ok := manager.cursors[session]
if !ok {
list = []types.Cursor{}
}
list = append(list, cursor)
manager.cursors[session] = list
2021-10-24 12:09:41 +13:00
}
2021-11-13 04:27:05 +13:00
func (manager *SessionManagerCtx) PopCursors() map[types.Session][]types.Cursor {
2021-10-24 12:09:41 +13:00
manager.cursorsMu.Lock()
defer manager.cursorsMu.Unlock()
cursors := manager.cursors
2021-11-13 04:27:05 +13:00
manager.cursors = make(map[types.Session][]types.Cursor)
2021-10-24 12:09:41 +13:00
return cursors
}
2020-11-02 04:09:48 +13:00
// ---
2021-03-14 09:11:48 +13:00
// broadcasts
2020-11-02 04:09:48 +13:00
// ---
2020-12-03 05:23:35 +13:00
func (manager *SessionManagerCtx) Broadcast(event string, payload any, exclude ...string) {
2021-09-18 10:56:03 +12:00
for _, session := range manager.List() {
2021-03-14 12:50:08 +13:00
if !session.State().IsConnected {
continue
}
if len(exclude) > 0 {
2021-09-18 10:56:03 +12:00
if in, _ := utils.ArrayIn(session.ID(), exclude); in {
continue
}
}
2021-09-02 07:16:36 +12:00
session.Send(event, payload)
}
}
func (manager *SessionManagerCtx) AdminBroadcast(event string, payload any, exclude ...string) {
2021-09-18 10:56:03 +12:00
for _, session := range manager.List() {
2021-03-14 12:50:08 +13:00
if !session.State().IsConnected || !session.Profile().IsAdmin {
2021-11-04 09:59:38 +13:00
continue
}
if len(exclude) > 0 {
2021-11-04 09:59:38 +13:00
if in, _ := utils.ArrayIn(session.ID(), exclude); in {
continue
}
}
session.Send(event, payload)
}
}
func (manager *SessionManagerCtx) InactiveCursorsBroadcast(event string, payload any, exclude ...string) {
2021-11-04 09:59:38 +13:00
for _, session := range manager.List() {
if !session.State().IsConnected || !session.Profile().CanSeeInactiveCursors {
continue
}
if len(exclude) > 0 {
2021-09-18 10:56:03 +12:00
if in, _ := utils.ArrayIn(session.ID(), exclude); in {
continue
}
}
2021-09-02 07:16:36 +12:00
session.Send(event, payload)
}
}
2020-11-02 04:09:48 +13:00
// ---
// events
// ---
2020-12-03 05:23:35 +13:00
2020-12-03 06:59:32 +13:00
func (manager *SessionManagerCtx) OnCreated(listener func(session types.Session)) {
manager.emmiter.On("created", func(payload ...any) {
2020-12-03 06:59:32 +13:00
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnDeleted(listener func(session types.Session)) {
manager.emmiter.On("deleted", func(payload ...any) {
2020-12-03 06:59:32 +13:00
listener(payload[0].(*SessionCtx))
})
}
2020-12-01 06:52:16 +13:00
func (manager *SessionManagerCtx) OnConnected(listener func(session types.Session)) {
manager.emmiter.On("connected", func(payload ...any) {
2020-11-02 04:09:48 +13:00
listener(payload[0].(*SessionCtx))
})
}
2020-11-02 08:53:25 +13:00
func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Session)) {
manager.emmiter.On("disconnected", func(payload ...any) {
2020-11-02 08:53:25 +13:00
listener(payload[0].(*SessionCtx))
})
}
2020-12-02 23:24:20 +13:00
2020-12-04 04:10:52 +13:00
func (manager *SessionManagerCtx) OnProfileChanged(listener func(session types.Session)) {
manager.emmiter.On("profile_changed", func(payload ...any) {
2020-12-03 05:29:38 +13:00
listener(payload[0].(*SessionCtx))
})
}
2020-12-04 04:10:52 +13:00
func (manager *SessionManagerCtx) OnStateChanged(listener func(session types.Session)) {
manager.emmiter.On("state_changed", func(payload ...any) {
2020-12-03 06:59:32 +13:00
listener(payload[0].(*SessionCtx))
})
}
2021-03-14 09:37:00 +13:00
func (manager *SessionManagerCtx) OnHostChanged(listener func(session types.Session)) {
manager.emmiter.On("host_changed", func(payload ...any) {
2021-03-14 09:37:00 +13:00
if payload[0] == nil {
listener(nil)
} else {
listener(payload[0].(*SessionCtx))
}
})
}
2022-03-27 12:08:06 +13:00
func (manager *SessionManagerCtx) OnSettingsChanged(listener func(new types.Settings, old types.Settings)) {
manager.emmiter.On("settings_changed", func(payload ...any) {
2022-03-27 12:08:06 +13:00
listener(payload[0].(types.Settings), payload[1].(types.Settings))
2022-03-27 11:20:38 +13:00
})
}
2020-12-03 05:23:35 +13:00
// ---
2022-03-27 12:08:06 +13:00
// settings
2020-12-03 05:23:35 +13:00
// ---
2022-03-27 12:08:06 +13:00
func (manager *SessionManagerCtx) UpdateSettings(new types.Settings) {
manager.settingsMu.Lock()
old := manager.settings
manager.settings = new
manager.settingsMu.Unlock()
// if private mode changed
if old.PrivateMode != new.PrivateMode {
// update webrtc paused state for all sessions
for _, session := range manager.List() {
2022-03-27 12:32:21 +13:00
enabled := session.PrivateModeEnabled()
// if session had control, it must release it
if enabled && session.IsHost() {
manager.ClearHost()
}
// its webrtc connection will be paused or unpaused
2022-03-27 12:08:06 +13:00
if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil {
2022-03-27 12:32:21 +13:00
webrtcPeer.SetPaused(enabled)
2022-03-27 12:08:06 +13:00
}
}
}
manager.emmiter.Emit("settings_changed", new, old)
2020-12-02 23:24:20 +13:00
}
2021-04-25 06:53:37 +12:00
2022-03-27 12:08:06 +13:00
func (manager *SessionManagerCtx) Settings() types.Settings {
manager.settingsMu.Lock()
defer manager.settingsMu.Unlock()
return manager.settings
2021-11-02 05:49:02 +13:00
}
2021-04-25 06:53:37 +12:00
func (manager *SessionManagerCtx) CookieEnabled() bool {
return manager.config.CookieEnabled
}