neko/internal/session/manager.go

471 lines
11 KiB
Go
Raw Normal View History

package session
import (
2021-08-29 17:09:13 +02:00
"errors"
2020-11-14 22:54:35 +01:00
"sync"
"sync/atomic"
2020-11-14 22:54:35 +01: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-15 13:34:14 +01:00
manager := &SessionManagerCtx{
2022-03-27 00:08:06 +01:00
logger: log.With().Str("module", "session").Logger(),
config: config,
settings: types.Settings{
PrivateMode: config.PrivateMode,
2024-04-20 14:27:15 +02:00
LockedLogins: config.LockedLogins,
2024-04-21 20:10:16 +02:00
LockedControls: config.LockedControls || config.ControlProtection,
ControlProtection: config.ControlProtection,
2022-03-27 00:08:06 +01:00
ImplicitHosting: config.ImplicitHosting,
InactiveCursors: config.InactiveCursors,
MercifulReconnect: config.MercifulReconnect,
},
2021-10-24 01:09:41 +02:00
tokens: make(map[string]string),
sessions: make(map[string]*SessionCtx),
2021-11-12 15:27:05 +00:00
cursors: make(map[types.Session][]types.Cursor),
2021-10-24 01:09:41 +02:00
emmiter: events.New(),
}
2021-03-15 13:34:14 +01: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-15 16:28:38 +01:00
Name: "API Session",
IsAdmin: true,
CanLogin: true,
CanConnect: false,
CanWatch: true,
CanHost: true,
CanAccessClipboard: true,
2021-03-15 13:34:14 +01:00
},
}
}
2023-03-27 18:33:51 +02:00
// try to load sessions from file
manager.load()
2021-03-15 13:34:14 +01:00
return manager
}
2020-11-01 16:09:48 +01:00
type SessionManagerCtx struct {
2021-10-24 01:09:41 +02:00
logger zerolog.Logger
config *config.Session
2022-03-27 00:08:06 +01:00
settings types.Settings
settingsMu sync.Mutex
tokens map[string]string
2021-03-13 21:11:48 +01:00
sessions map[string]*SessionCtx
sessionsMu sync.Mutex
2021-10-24 01:09:41 +02:00
hostId atomic.Value
2021-10-24 01:09:41 +02:00
2021-11-12 15:27:05 +00:00
cursors map[types.Session][]types.Cursor
2021-10-24 01:09:41 +02:00
cursorsMu sync.Mutex
2021-03-13 21:11:48 +01:00
emmiter events.EventEmmiter
2021-03-15 13:34:14 +01:00
apiSession *SessionCtx
2020-12-05 23:31:41 +01:00
}
2021-03-13 22:54:34 +01: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-13 21:43:36 +01:00
}
2020-12-05 23:05:46 +01:00
2021-03-13 21:43:36 +01:00
manager.sessionsMu.Lock()
if _, ok := manager.sessions[id]; ok {
manager.sessionsMu.Unlock()
2021-08-29 17:09:13 +02:00
return nil, "", types.ErrSessionAlreadyExists
}
if _, ok := manager.tokens[token]; ok {
2021-03-13 21:11:48 +01:00
manager.sessionsMu.Unlock()
2021-08-29 17:09:13 +02:00
return nil, "", errors.New("session token already exists")
2020-12-05 23:05:46 +01:00
}
2021-03-13 21:11:48 +01:00
session := &SessionCtx{
id: id,
token: token,
2021-03-13 21:11:48 +01:00
manager: manager,
2021-03-15 13:34:14 +01:00
logger: manager.logger.With().Str("session_id", id).Logger(),
2021-03-13 21:11:48 +01:00
profile: profile,
2020-12-05 23:05:46 +01:00
}
manager.tokens[token] = id
2021-03-13 21:11:48 +01:00
manager.sessions[id] = session
manager.sessionsMu.Unlock()
2020-12-03 20:05:44 +01:00
2020-12-02 18:59:32 +01:00
manager.emmiter.Emit("created", session)
2023-03-27 18:33:51 +02:00
manager.save()
return session, token, nil
}
2020-12-05 22:18:45 +01:00
func (manager *SessionManagerCtx) Update(id string, profile types.MemberProfile) error {
2021-03-13 21:11:48 +01:00
manager.sessionsMu.Lock()
2020-12-05 23:05:46 +01:00
2021-03-13 21:11:48 +01:00
session, ok := manager.sessions[id]
2020-12-05 22:18:45 +01:00
if !ok {
2021-03-13 21:11:48 +01:00
manager.sessionsMu.Unlock()
2021-08-29 17:09:13 +02:00
return types.ErrSessionNotFound
2020-12-05 23:05:46 +01:00
}
old := session.profile
2020-12-05 22:18:45 +01:00
session.profile = profile
2021-03-13 21:11:48 +01:00
manager.sessionsMu.Unlock()
2020-12-05 22:18:45 +01:00
manager.emmiter.Emit("profile_changed", session, profile, old)
2023-03-27 18:33:51 +02:00
manager.save()
2020-12-06 19:29:39 +01:00
session.profileChanged()
2020-12-05 22:18:45 +01:00
return nil
}
2020-11-25 22:06:13 +01:00
func (manager *SessionManagerCtx) Delete(id string) error {
2021-03-13 21:11:48 +01:00
manager.sessionsMu.Lock()
session, ok := manager.sessions[id]
2020-11-25 22:06:13 +01:00
if !ok {
2021-03-13 21:11:48 +01:00
manager.sessionsMu.Unlock()
2021-08-29 17:09:13 +02:00
return types.ErrSessionNotFound
2020-11-25 22:06:13 +01:00
}
2021-03-16 10:53:03 +01:00
delete(manager.tokens, session.token)
2021-03-13 21:11:48 +01:00
delete(manager.sessions, id)
manager.sessionsMu.Unlock()
2020-11-25 22:06:13 +01:00
2021-03-14 00:50:08 +01:00
if session.State().IsConnected {
session.DestroyWebSocketPeer("session deleted")
2021-03-25 14:08:26 +01:00
}
if session.State().IsWatching {
2021-08-29 19:17:10 +02:00
session.GetWebRTCPeer().Destroy()
2020-11-01 16:09:48 +01:00
}
2020-12-03 20:05:44 +01:00
manager.emmiter.Emit("deleted", session)
2023-03-27 18:33:51 +02:00
manager.save()
2021-08-29 19:17:10 +02:00
return nil
2020-11-01 16:09:48 +01:00
}
func (manager *SessionManagerCtx) Disconnect(id string) error {
manager.sessionsMu.Lock()
session, ok := manager.sessions[id]
if !ok {
manager.sessionsMu.Unlock()
return types.ErrSessionNotFound
}
manager.sessionsMu.Unlock()
if session.State().IsConnected {
session.DestroyWebSocketPeer("session disconnected")
}
if session.State().IsWatching {
session.GetWebRTCPeer().Destroy()
}
return nil
}
2020-12-05 23:05:46 +01:00
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
2021-03-13 21:11:48 +01:00
manager.sessionsMu.Lock()
defer manager.sessionsMu.Unlock()
2020-12-05 23:05:46 +01:00
2021-03-13 21:11:48 +01:00
session, ok := manager.sessions[id]
2020-12-05 23:05:46 +01: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-15 13:34:14 +01:00
if ok {
return manager.Get(id)
}
// is API session
if manager.apiSession != nil && manager.apiSession.token == token {
return manager.apiSession, true
}
2021-03-15 13:34:14 +01:00
return nil, false
}
2021-03-13 21:11:48 +01: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
}
2024-04-21 20:10:16 +02:00
func (manager *SessionManagerCtx) Range(f func(session types.Session) bool) {
manager.sessionsMu.Lock()
defer manager.sessionsMu.Unlock()
for _, session := range manager.sessions {
if !f(session) {
return
}
}
}
2020-11-01 16:09:48 +01:00
// ---
// host
// ---
2020-12-02 17:23:35 +01:00
2024-05-06 23:47:13 +02:00
func (manager *SessionManagerCtx) setHost(session, host types.Session) {
var hostId string
if host != nil {
hostId = host.ID()
}
2021-03-13 21:37:00 +01:00
manager.hostId.Store(hostId)
2024-05-06 23:47:13 +02:00
manager.emmiter.Emit("host_changed", session, host)
}
func (manager *SessionManagerCtx) GetHost() (types.Session, bool) {
hostId, ok := manager.hostId.Load().(string)
if !ok || hostId == "" {
return nil, false
}
2020-11-14 22:54:35 +01:00
return manager.Get(hostId)
}
func (manager *SessionManagerCtx) isHost(host types.Session) bool {
hostId, ok := manager.hostId.Load().(string)
return ok && hostId == host.ID()
}
2021-10-24 01:09:41 +02:00
// ---
// cursors
// ---
2021-11-01 17:31:00 +01:00
func (manager *SessionManagerCtx) SetCursor(cursor types.Cursor, session types.Session) {
2021-10-24 01:09:41 +02:00
manager.cursorsMu.Lock()
defer manager.cursorsMu.Unlock()
2021-11-12 15:27:05 +00:00
list, ok := manager.cursors[session]
if !ok {
list = []types.Cursor{}
}
list = append(list, cursor)
manager.cursors[session] = list
2021-10-24 01:09:41 +02:00
}
2021-11-12 15:27:05 +00:00
func (manager *SessionManagerCtx) PopCursors() map[types.Session][]types.Cursor {
2021-10-24 01:09:41 +02:00
manager.cursorsMu.Lock()
defer manager.cursorsMu.Unlock()
cursors := manager.cursors
2021-11-12 15:27:05 +00:00
manager.cursors = make(map[types.Session][]types.Cursor)
2021-10-24 01:09:41 +02:00
return cursors
}
2020-11-01 16:09:48 +01:00
// ---
2021-03-13 21:11:48 +01:00
// broadcasts
2020-11-01 16:09:48 +01:00
// ---
2020-12-02 17:23:35 +01:00
func (manager *SessionManagerCtx) Broadcast(event string, payload any, exclude ...string) {
2021-09-18 00:56:03 +02:00
for _, session := range manager.List() {
2021-03-14 00:50:08 +01:00
if !session.State().IsConnected {
continue
}
if len(exclude) > 0 {
2021-09-18 00:56:03 +02:00
if in, _ := utils.ArrayIn(session.ID(), exclude); in {
continue
}
}
2021-09-01 21:16:36 +02:00
session.Send(event, payload)
}
}
func (manager *SessionManagerCtx) AdminBroadcast(event string, payload any, exclude ...string) {
2021-09-18 00:56:03 +02:00
for _, session := range manager.List() {
2021-03-14 00:50:08 +01:00
if !session.State().IsConnected || !session.Profile().IsAdmin {
2021-11-03 21:59:38 +01:00
continue
}
if len(exclude) > 0 {
2021-11-03 21:59:38 +01: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-03 21:59:38 +01:00
for _, session := range manager.List() {
if !session.State().IsConnected || !session.Profile().CanSeeInactiveCursors {
continue
}
if len(exclude) > 0 {
2021-09-18 00:56:03 +02:00
if in, _ := utils.ArrayIn(session.ID(), exclude); in {
continue
}
}
2021-09-01 21:16:36 +02:00
session.Send(event, payload)
}
}
2020-11-01 16:09:48 +01:00
// ---
// events
// ---
2020-12-02 17:23:35 +01:00
2020-12-02 18:59:32 +01:00
func (manager *SessionManagerCtx) OnCreated(listener func(session types.Session)) {
manager.emmiter.On("created", func(payload ...any) {
2020-12-02 18:59:32 +01:00
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnDeleted(listener func(session types.Session)) {
manager.emmiter.On("deleted", func(payload ...any) {
2020-12-02 18:59:32 +01:00
listener(payload[0].(*SessionCtx))
})
}
2020-11-30 18:52:16 +01:00
func (manager *SessionManagerCtx) OnConnected(listener func(session types.Session)) {
manager.emmiter.On("connected", func(payload ...any) {
2020-11-01 16:09:48 +01:00
listener(payload[0].(*SessionCtx))
})
}
2020-11-01 20:53:25 +01:00
func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Session)) {
manager.emmiter.On("disconnected", func(payload ...any) {
2020-11-01 20:53:25 +01:00
listener(payload[0].(*SessionCtx))
})
}
2020-12-02 11:24:20 +01:00
func (manager *SessionManagerCtx) OnProfileChanged(listener func(session types.Session, new, old types.MemberProfile)) {
manager.emmiter.On("profile_changed", func(payload ...any) {
listener(payload[0].(*SessionCtx), payload[1].(types.MemberProfile), payload[2].(types.MemberProfile))
2020-12-02 17:29:38 +01:00
})
}
2020-12-03 16:10:52 +01:00
func (manager *SessionManagerCtx) OnStateChanged(listener func(session types.Session)) {
manager.emmiter.On("state_changed", func(payload ...any) {
2020-12-02 18:59:32 +01:00
listener(payload[0].(*SessionCtx))
})
}
2024-05-06 23:47:13 +02:00
func (manager *SessionManagerCtx) OnHostChanged(listener func(session, host types.Session)) {
manager.emmiter.On("host_changed", func(payload ...any) {
2024-05-06 23:47:13 +02:00
if payload[1] == nil {
listener(payload[0].(*SessionCtx), nil)
2021-03-13 21:37:00 +01:00
} else {
2024-05-06 23:47:13 +02:00
listener(payload[0].(*SessionCtx), payload[1].(*SessionCtx))
2021-03-13 21:37:00 +01:00
}
})
}
func (manager *SessionManagerCtx) OnSettingsChanged(listener func(session types.Session, new, old types.Settings)) {
manager.emmiter.On("settings_changed", func(payload ...any) {
2024-05-05 00:03:32 +02:00
listener(payload[0].(types.Session), payload[1].(types.Settings), payload[2].(types.Settings))
2022-03-26 23:20:38 +01:00
})
}
2020-12-02 17:23:35 +01:00
// ---
2022-03-27 00:08:06 +01:00
// settings
2020-12-02 17:23:35 +01:00
// ---
2024-05-05 00:03:32 +02:00
func (manager *SessionManagerCtx) UpdateSettingsFunc(session types.Session, f func(settings *types.Settings) bool) {
2024-04-21 20:10:16 +02:00
manager.settingsMu.Lock()
new := manager.settings
if f(&new) {
old := manager.settings
manager.settings = new
manager.settingsMu.Unlock()
2024-05-05 00:03:32 +02:00
manager.updateSettings(session, new, old)
2024-04-21 20:10:16 +02:00
return
}
manager.settingsMu.Unlock()
}
2024-05-05 00:03:32 +02:00
func (manager *SessionManagerCtx) updateSettings(session types.Session, new, old types.Settings) {
2022-03-27 00:08:06 +01:00
// if private mode changed
if old.PrivateMode != new.PrivateMode {
// update webrtc paused state for all sessions
2024-05-06 23:47:13 +02:00
for _, s := range manager.List() {
enabled := s.PrivateModeEnabled()
2022-03-27 00:32:21 +01:00
// if session had control, it must release it
2024-05-06 23:47:13 +02:00
if enabled && s.IsHost() {
session.ClearHost()
2022-03-27 00:32:21 +01:00
}
// its webrtc connection will be paused or unpaused
2024-05-06 23:47:13 +02:00
if webrtcPeer := s.GetWebRTCPeer(); webrtcPeer != nil {
2022-03-27 00:32:21 +01:00
webrtcPeer.SetPaused(enabled)
2022-03-27 00:08:06 +01:00
}
}
}
2024-04-21 20:10:16 +02:00
// if control protection changed and controls are not locked
if old.ControlProtection != new.ControlProtection && new.ControlProtection && !new.LockedControls {
// if there is no admin, lock controls
hasAdmin := false
manager.Range(func(session types.Session) bool {
if session.Profile().IsAdmin && session.State().IsConnected {
hasAdmin = true
return false
}
return true
})
if !hasAdmin {
manager.settingsMu.Lock()
manager.settings.LockedControls = true
new.LockedControls = true
manager.settingsMu.Unlock()
}
}
2023-05-14 22:41:32 +02:00
// if contols have been locked
if old.LockedControls != new.LockedControls && new.LockedControls {
// if the host is not admin, it must release controls
host, hasHost := manager.GetHost()
if hasHost && !host.Profile().IsAdmin {
2024-05-06 23:47:13 +02:00
session.ClearHost()
2023-05-14 22:41:32 +02:00
}
}
2024-05-05 00:03:32 +02:00
manager.emmiter.Emit("settings_changed", session, new, old)
2020-12-02 11:24:20 +01:00
}
2021-04-24 20:53:37 +02:00
2022-03-27 00:08:06 +01:00
func (manager *SessionManagerCtx) Settings() types.Settings {
manager.settingsMu.Lock()
defer manager.settingsMu.Unlock()
return manager.settings
2021-11-01 17:49:02 +01:00
}
2021-04-24 20:53:37 +02:00
func (manager *SessionManagerCtx) CookieEnabled() bool {
return manager.config.CookieEnabled
}