neko/server/internal/session/manager.go

471 lines
11 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: config.PrivateMode,
2024-04-21 00:27:15 +12:00
LockedLogins: config.LockedLogins,
2024-04-22 06:10:16 +12:00
LockedControls: config.LockedControls || config.ControlProtection,
ControlProtection: config.ControlProtection,
2022-03-27 12:08:06 +13:00
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
},
}
}
2023-03-28 05:33:51 +13:00
// try to load sessions from file
manager.load()
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)
2023-03-28 05:33:51 +13:00
manager.save()
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
}
old := session.profile
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, profile, old)
2023-03-28 05:33:51 +13:00
manager.save()
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 {
session.DestroyWebSocketPeer("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)
2023-03-28 05:33:51 +13:00
manager.save()
2021-08-30 05:17:10 +12:00
return nil
2020-11-02 04:09:48 +13: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-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
}
2024-04-22 06:10:16 +12: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-02 04:09:48 +13:00
// ---
// host
// ---
2020-12-03 05:23:35 +13:00
2024-05-07 09:47:13 +12:00
func (manager *SessionManagerCtx) setHost(session, host types.Session) {
var hostId string
if host != nil {
hostId = host.ID()
}
2021-03-14 09:37:00 +13:00
manager.hostId.Store(hostId)
2024-05-07 09:47:13 +12: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-15 10:54:35 +13: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 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
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-03 05:29:38 +13:00
})
}
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))
})
}
2024-05-07 09:47:13 +12:00
func (manager *SessionManagerCtx) OnHostChanged(listener func(session, host types.Session)) {
manager.emmiter.On("host_changed", func(payload ...any) {
2024-05-07 09:47:13 +12:00
if payload[1] == nil {
listener(payload[0].(*SessionCtx), nil)
2021-03-14 09:37:00 +13:00
} else {
2024-05-07 09:47:13 +12:00
listener(payload[0].(*SessionCtx), payload[1].(*SessionCtx))
2021-03-14 09:37:00 +13: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 10:03:32 +12:00
listener(payload[0].(types.Session), payload[1].(types.Settings), payload[2].(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
// ---
2024-05-05 10:03:32 +12:00
func (manager *SessionManagerCtx) UpdateSettingsFunc(session types.Session, f func(settings *types.Settings) bool) {
2024-04-22 06:10:16 +12:00
manager.settingsMu.Lock()
new := manager.settings
if f(&new) {
old := manager.settings
manager.settings = new
manager.settingsMu.Unlock()
2024-05-05 10:03:32 +12:00
manager.updateSettings(session, new, old)
2024-04-22 06:10:16 +12:00
return
}
manager.settingsMu.Unlock()
}
2024-05-05 10:03:32 +12:00
func (manager *SessionManagerCtx) updateSettings(session types.Session, new, old types.Settings) {
2022-03-27 12:08:06 +13:00
// if private mode changed
if old.PrivateMode != new.PrivateMode {
// update webrtc paused state for all sessions
2024-05-07 09:47:13 +12:00
for _, s := range manager.List() {
enabled := s.PrivateModeEnabled()
2022-03-27 12:32:21 +13:00
// if session had control, it must release it
2024-05-07 09:47:13 +12:00
if enabled && s.IsHost() {
session.ClearHost()
2022-03-27 12:32:21 +13:00
}
// its webrtc connection will be paused or unpaused
2024-05-07 09:47:13 +12:00
if webrtcPeer := s.GetWebRTCPeer(); webrtcPeer != nil {
2022-03-27 12:32:21 +13:00
webrtcPeer.SetPaused(enabled)
2022-03-27 12:08:06 +13:00
}
}
}
2024-04-22 06:10:16 +12: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-15 08:41:32 +12: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-07 09:47:13 +12:00
session.ClearHost()
2023-05-15 08:41:32 +12:00
}
}
2024-05-05 10:03:32 +12:00
manager.emmiter.Emit("settings_changed", session, 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
}