2020-01-19 12:30:09 +13:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-16 03:41:08 +13:00
|
|
|
"sync"
|
2020-01-19 12:30:09 +13:00
|
|
|
|
|
|
|
"github.com/kataras/go-events"
|
2020-01-25 04:47:37 +13:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-01-19 12:30:09 +13:00
|
|
|
|
2021-10-06 09:38:24 +13:00
|
|
|
"m1k1o/neko/internal/types"
|
|
|
|
"m1k1o/neko/internal/utils"
|
2022-03-29 19:15:02 +13:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2020-01-19 12:30:09 +13:00
|
|
|
)
|
|
|
|
|
2020-04-06 11:07:25 +12:00
|
|
|
func New(remote types.RemoteManager) *SessionManager {
|
2020-01-19 12:30:09 +13:00
|
|
|
return &SessionManager{
|
2020-01-25 04:47:37 +13:00
|
|
|
logger: log.With().Str("module", "session").Logger(),
|
2020-01-19 12:30:09 +13:00
|
|
|
host: "",
|
2020-04-06 11:07:25 +12:00
|
|
|
remote: remote,
|
2020-01-19 12:30:09 +13:00
|
|
|
members: make(map[string]*Session),
|
|
|
|
emmiter: events.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SessionManager struct {
|
2021-02-16 03:41:08 +13:00
|
|
|
mu sync.Mutex
|
2020-01-25 04:47:37 +13:00
|
|
|
logger zerolog.Logger
|
2020-01-19 12:30:09 +13:00
|
|
|
host string
|
2020-04-06 11:07:25 +12:00
|
|
|
remote types.RemoteManager
|
2020-01-19 12:30:09 +13:00
|
|
|
members map[string]*Session
|
|
|
|
emmiter events.EventEmmiter
|
2021-12-12 02:34:28 +13:00
|
|
|
// TODO: Handle locks in sessions as flags.
|
|
|
|
controlLocked bool
|
2022-03-29 19:15:02 +13:00
|
|
|
implicitControl bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *SessionManager) Init(cmd *cobra.Command) error {
|
|
|
|
cmd.PersistentFlags().Bool("implicit_control", "", "if enabled members can gain control implicitly. Valid values: none, free, on_click")
|
|
|
|
if err := viper.BindPFlag("implicit_control", cmd.PersistentFlags().Lookup("implicit_control")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.implicitControl = viper.GetBool("implicit_control")
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-02-27 01:46:10 +13:00
|
|
|
func (manager *SessionManager) New(id string, admin bool, socket types.WebSocket) types.Session {
|
2020-01-19 12:30:09 +13:00
|
|
|
session := &Session{
|
2020-01-25 04:47:37 +13:00
|
|
|
id: id,
|
|
|
|
admin: admin,
|
|
|
|
manager: manager,
|
2020-01-21 03:38:07 +13:00
|
|
|
socket: socket,
|
2020-01-25 04:47:37 +13:00
|
|
|
logger: manager.logger.With().Str("id", id).Logger(),
|
2020-01-21 03:38:07 +13:00
|
|
|
connected: false,
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
2020-01-25 04:47:37 +13:00
|
|
|
manager.members[id] = session
|
2021-07-23 06:58:15 +12:00
|
|
|
if !manager.remote.Streaming() && len(manager.members) > 0 {
|
2020-04-06 11:07:25 +12:00
|
|
|
manager.remote.StartStream()
|
|
|
|
}
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Unlock()
|
2020-04-06 11:07:25 +12:00
|
|
|
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.emmiter.Emit("created", id, session)
|
2020-01-19 12:30:09 +13:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) HasHost() bool {
|
|
|
|
return manager.host != ""
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) IsHost(id string) bool {
|
|
|
|
return manager.host == id
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) SetHost(id string) error {
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
2020-01-25 04:47:37 +13:00
|
|
|
_, ok := manager.members[id]
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Unlock()
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
if ok {
|
2020-01-25 04:47:37 +13:00
|
|
|
manager.host = id
|
|
|
|
manager.emmiter.Emit("host", id)
|
2020-01-19 12:30:09 +13:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-16 03:41:08 +13:00
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
return fmt.Errorf("invalid session id %s", id)
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) GetHost() (types.Session, bool) {
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
host, ok := manager.members[manager.host]
|
2020-01-19 12:30:09 +13:00
|
|
|
return host, ok
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) ClearHost() {
|
|
|
|
id := manager.host
|
|
|
|
manager.host = ""
|
|
|
|
manager.emmiter.Emit("host_cleared", id)
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) Has(id string) bool {
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
_, ok := manager.members[id]
|
2020-01-19 12:30:09 +13:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) Get(id string) (types.Session, bool) {
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
session, ok := manager.members[id]
|
2020-01-19 12:30:09 +13:00
|
|
|
return session, ok
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:34:28 +13:00
|
|
|
// TODO: Handle locks in sessions as flags.
|
|
|
|
func (manager *SessionManager) SetControlLocked(locked bool) {
|
|
|
|
manager.controlLocked = locked
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:15:02 +13:00
|
|
|
func (manager *SessionManager) CanControl(id string, isClickEvent bool) bool {
|
2021-12-12 02:34:28 +13:00
|
|
|
session, ok := manager.Get(id)
|
2022-03-29 19:15:02 +13:00
|
|
|
if !manager.IsHost(id) {
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
switch manager.sessions.ImplicitControl {
|
|
|
|
case types.session.ControlMode.OnMove:
|
|
|
|
ok = true
|
|
|
|
case types.session.ControlMode.OnClick:
|
|
|
|
if isClickEvent: {
|
|
|
|
// Switch host
|
|
|
|
manager.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 02:34:28 +13:00
|
|
|
return ok && (!manager.controlLocked || session.Admin())
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:14:08 +12:00
|
|
|
func (manager *SessionManager) Admins() []*types.Member {
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2020-04-07 08:14:08 +12:00
|
|
|
members := []*types.Member{}
|
|
|
|
for _, session := range manager.members {
|
|
|
|
if !session.connected || !session.admin {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
member := session.Member()
|
|
|
|
if member != nil {
|
|
|
|
members = append(members, member)
|
|
|
|
}
|
|
|
|
}
|
2021-02-16 03:41:08 +13:00
|
|
|
|
2020-04-07 08:14:08 +12:00
|
|
|
return members
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) Members() []*types.Member {
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
members := []*types.Member{}
|
|
|
|
for _, session := range manager.members {
|
|
|
|
if !session.connected {
|
|
|
|
continue
|
2020-01-21 03:38:07 +13:00
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
member := session.Member()
|
|
|
|
if member != nil {
|
|
|
|
members = append(members, member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return members
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2021-10-06 10:10:10 +13:00
|
|
|
func (manager *SessionManager) Destroy(id string) {
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
2020-01-25 04:47:37 +13:00
|
|
|
session, ok := manager.members[id]
|
2020-01-19 12:30:09 +13:00
|
|
|
if ok {
|
|
|
|
err := session.destroy()
|
2020-01-25 04:47:37 +13:00
|
|
|
delete(manager.members, id)
|
2020-04-06 11:07:25 +12:00
|
|
|
|
2021-07-23 06:58:15 +12:00
|
|
|
if manager.remote.Streaming() && len(manager.members) <= 0 {
|
2020-04-06 11:07:25 +12:00
|
|
|
manager.remote.StopStream()
|
|
|
|
}
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Unlock()
|
2020-04-06 11:07:25 +12:00
|
|
|
|
2020-04-07 08:14:08 +12:00
|
|
|
manager.emmiter.Emit("destroyed", id, session)
|
2021-12-03 07:56:25 +13:00
|
|
|
manager.logger.Err(err).Str("session_id", id).Msg("destroying session")
|
2021-10-06 10:10:10 +13:00
|
|
|
return
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
2020-04-06 11:07:25 +12:00
|
|
|
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Unlock()
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) Clear() error {
|
|
|
|
return nil
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-02-27 01:46:10 +13:00
|
|
|
func (manager *SessionManager) Broadcast(v interface{}, exclude interface{}) error {
|
2021-02-16 03:41:08 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
for id, session := range manager.members {
|
|
|
|
if !session.connected {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-19 12:30:09 +13:00
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
if exclude != nil {
|
|
|
|
if in, _ := utils.ArrayIn(id, exclude); in {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 12:30:09 +13:00
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
if err := session.Send(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-21 05:22:24 +13:00
|
|
|
}
|
2021-12-12 01:52:31 +13:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *SessionManager) AdminBroadcast(v interface{}, exclude interface{}) error {
|
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
|
|
|
for id, session := range manager.members {
|
|
|
|
if !session.connected || !session.admin {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if exclude != nil {
|
|
|
|
if in, _ := utils.ArrayIn(id, exclude); in {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := session.Send(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 05:22:24 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) OnHost(listener func(id string)) {
|
|
|
|
manager.emmiter.On("host", func(payload ...interface{}) {
|
2020-01-19 12:30:09 +13:00
|
|
|
listener(payload[0].(string))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) OnHostCleared(listener func(id string)) {
|
|
|
|
manager.emmiter.On("host_cleared", func(payload ...interface{}) {
|
2020-01-19 12:30:09 +13:00
|
|
|
listener(payload[0].(string))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:14:08 +12:00
|
|
|
func (manager *SessionManager) OnDestroy(listener func(id string, session types.Session)) {
|
2020-01-25 04:47:37 +13:00
|
|
|
manager.emmiter.On("destroyed", func(payload ...interface{}) {
|
2020-04-07 08:14:08 +12:00
|
|
|
listener(payload[0].(string), payload[1].(*Session))
|
2020-01-19 12:30:09 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) OnCreated(listener func(id string, session types.Session)) {
|
|
|
|
manager.emmiter.On("created", func(payload ...interface{}) {
|
2020-01-21 03:38:07 +13:00
|
|
|
listener(payload[0].(string), payload[1].(*Session))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
func (manager *SessionManager) OnConnected(listener func(id string, session types.Session)) {
|
|
|
|
manager.emmiter.On("connected", func(payload ...interface{}) {
|
|
|
|
listener(payload[0].(string), payload[1].(*Session))
|
2020-01-19 12:30:09 +13:00
|
|
|
})
|
|
|
|
}
|
2022-03-29 19:15:02 +13:00
|
|
|
|
|
|
|
func (manager *SessionManager) ImplicitControl() bool {
|
|
|
|
return manager.implicitControl
|
|
|
|
}
|