2020-10-23 03:54:50 +13:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2020-11-26 10:06:13 +13:00
|
|
|
"fmt"
|
2020-11-15 10:54:35 +13:00
|
|
|
"sync"
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
"github.com/kataras/go-events"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-02-15 02:40:17 +13:00
|
|
|
"demodesk/neko/internal/config"
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/utils"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-12-03 05:36:06 +13:00
|
|
|
func New(config *config.Session) *SessionManagerCtx {
|
2021-03-16 01:34:14 +13:00
|
|
|
manager := &SessionManagerCtx{
|
2021-03-14 09:11:48 +13:00
|
|
|
logger: log.With().Str("module", "session").Logger(),
|
|
|
|
config: config,
|
|
|
|
host: nil,
|
|
|
|
hostMu: sync.Mutex{},
|
2021-03-14 10:17:49 +13:00
|
|
|
tokens: make(map[string]string),
|
2021-03-14 09:11:48 +13:00
|
|
|
sessions: make(map[string]*SessionCtx),
|
|
|
|
sessionsMu: sync.Mutex{},
|
|
|
|
emmiter: events.New(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
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-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
type SessionManagerCtx struct {
|
2021-03-14 09:11:48 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
config *config.Session
|
|
|
|
host types.Session
|
|
|
|
hostMu sync.Mutex
|
2021-03-14 10:17:49 +13:00
|
|
|
tokens map[string]string
|
2021-03-14 09:11:48 +13:00
|
|
|
sessions map[string]*SessionCtx
|
|
|
|
sessionsMu sync.Mutex
|
|
|
|
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) {
|
2021-03-14 10:17:49 +13:00
|
|
|
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()
|
2021-03-14 10:17:49 +13:00
|
|
|
if _, ok := manager.sessions[id]; ok {
|
|
|
|
manager.sessionsMu.Unlock()
|
2021-04-25 07:37:43 +12:00
|
|
|
return nil, "", fmt.Errorf("session id already exists")
|
2021-03-14 10:17:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := manager.tokens[token]; ok {
|
2021-03-14 09:11:48 +13:00
|
|
|
manager.sessionsMu.Unlock()
|
2021-04-25 07:37:43 +12:00
|
|
|
return nil, "", fmt.Errorf("session token already exists")
|
2020-12-06 11:05:46 +13:00
|
|
|
}
|
|
|
|
|
2021-03-14 09:11:48 +13:00
|
|
|
session := &SessionCtx{
|
|
|
|
id: id,
|
2021-03-14 10:17:49 +13:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-14 10:17:49 +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)
|
2021-03-14 10:17:49 +13:00
|
|
|
return session, token, nil
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
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-04-25 07:37:43 +12:00
|
|
|
return fmt.Errorf("session id not found")
|
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-04-25 07:37:43 +12:00
|
|
|
return fmt.Errorf("session id not found")
|
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-26 02:08:26 +13:00
|
|
|
var errs []error
|
2021-03-14 12:50:08 +13:00
|
|
|
if session.State().IsConnected {
|
2021-03-26 02:08:26 +13:00
|
|
|
err := session.GetWebSocketPeer().Destroy()
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.State().IsWatching {
|
|
|
|
err := session.GetWebRTCPeer().Destroy()
|
|
|
|
errs = append(errs, err)
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2020-12-04 08:05:44 +13:00
|
|
|
manager.emmiter.Emit("deleted", session)
|
2021-03-26 02:08:26 +13:00
|
|
|
|
|
|
|
return utils.ErrorsJoin(errs)
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-14 10:17:49 +13:00
|
|
|
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-14 10:17:49 +13:00
|
|
|
}
|
|
|
|
|
2021-03-16 01:34:14 +13:00
|
|
|
return nil, false
|
2021-03-14 10:17:49 +13:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-11-15 10:54:35 +13:00
|
|
|
manager.hostMu.Lock()
|
2020-11-01 11:53:19 +13:00
|
|
|
manager.host = host
|
2021-03-14 09:37:00 +13:00
|
|
|
manager.hostMu.Unlock()
|
|
|
|
|
|
|
|
manager.emmiter.Emit("host_changed", host)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) GetHost() types.Session {
|
2020-11-15 10:54:35 +13:00
|
|
|
manager.hostMu.Lock()
|
|
|
|
defer manager.hostMu.Unlock()
|
|
|
|
|
2020-11-01 11:53:19 +13:00
|
|
|
return manager.host
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (manager *SessionManagerCtx) ClearHost() {
|
2021-03-14 09:37:00 +13:00
|
|
|
manager.SetHost(nil)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-11-19 08:30:33 +13:00
|
|
|
func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) {
|
2021-03-14 09:11:48 +13:00
|
|
|
manager.sessionsMu.Lock()
|
|
|
|
defer manager.sessionsMu.Unlock()
|
2020-11-15 11:05:09 +13:00
|
|
|
|
2021-03-14 09:11:48 +13:00
|
|
|
for id, session := range manager.sessions {
|
2021-03-14 12:50:08 +13:00
|
|
|
if !session.State().IsConnected {
|
2020-10-23 03:54:50 +13:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if exclude != nil {
|
|
|
|
if in, _ := utils.ArrayIn(id, exclude); in {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := session.Send(v); err != nil {
|
2020-11-19 08:30:33 +13:00
|
|
|
manager.logger.Warn().Err(err).Msgf("broadcasting event has failed")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 09:34:39 +13:00
|
|
|
func (manager *SessionManagerCtx) AdminBroadcast(v interface{}, exclude interface{}) {
|
2021-03-14 09:11:48 +13:00
|
|
|
manager.sessionsMu.Lock()
|
|
|
|
defer manager.sessionsMu.Unlock()
|
2020-11-19 09:34:39 +13:00
|
|
|
|
2021-03-14 09:11:48 +13:00
|
|
|
for id, session := range manager.sessions {
|
2021-03-14 12:50:08 +13:00
|
|
|
if !session.State().IsConnected || !session.Profile().IsAdmin {
|
2020-11-19 09:34:39 +13:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if exclude != nil {
|
|
|
|
if in, _ := utils.ArrayIn(id, exclude); in {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := session.Send(v); err != nil {
|
|
|
|
manager.logger.Warn().Err(err).Msgf("broadcasting admin event has failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ...interface{}) {
|
|
|
|
listener(payload[0].(*SessionCtx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *SessionManagerCtx) OnDeleted(listener func(session types.Session)) {
|
|
|
|
manager.emmiter.On("deleted", func(payload ...interface{}) {
|
|
|
|
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 ...interface{}) {
|
2020-11-02 04:09:48 +13:00
|
|
|
listener(payload[0].(*SessionCtx))
|
2020-10-23 03:54:50 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-02 08:53:25 +13:00
|
|
|
func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Session)) {
|
|
|
|
manager.emmiter.On("disconnected", func(payload ...interface{}) {
|
|
|
|
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 ...interface{}) {
|
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 ...interface{}) {
|
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 ...interface{}) {
|
|
|
|
if payload[0] == nil {
|
|
|
|
listener(nil)
|
|
|
|
} else {
|
|
|
|
listener(payload[0].(*SessionCtx))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:23:35 +13:00
|
|
|
// ---
|
|
|
|
// config
|
|
|
|
// ---
|
|
|
|
|
2020-12-02 23:24:20 +13:00
|
|
|
func (manager *SessionManagerCtx) ImplicitHosting() bool {
|
|
|
|
return manager.config.ImplicitHosting
|
|
|
|
}
|
2021-04-25 06:53:37 +12:00
|
|
|
|
|
|
|
func (manager *SessionManagerCtx) CookieEnabled() bool {
|
|
|
|
return manager.config.CookieEnabled
|
|
|
|
}
|