neko/server/internal/session/session.go

284 lines
7.1 KiB
Go
Raw Normal View History

package session
import (
2021-09-29 12:01:03 +13:00
"sync"
"time"
2021-09-29 12:01:03 +13:00
"github.com/rs/zerolog"
2020-10-30 06:07:04 +13:00
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/types/event"
)
// client is expected to reconnect within 5 second
// if some unexpected websocket disconnect happens
const WS_DELAYED_DURATION = 5 * time.Second
2020-11-02 04:09:48 +13:00
type SessionCtx struct {
2021-09-29 12:01:03 +13:00
id string
token string
logger zerolog.Logger
manager *SessionManagerCtx
profile types.MemberProfile
state types.SessionState
2021-03-04 06:46:08 +13:00
websocketPeer types.WebSocketPeer
2021-09-29 12:01:03 +13:00
websocketMu sync.Mutex
// websocket delayed set connected events
wsDelayedMu sync.Mutex
wsDelayedTimer *time.Timer
2021-09-29 12:01:03 +13:00
webrtcPeer types.WebRTCPeer
webrtcMu sync.Mutex
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) ID() string {
return session.id
}
2021-03-14 12:45:51 +13:00
func (session *SessionCtx) Profile() types.MemberProfile {
2021-03-14 12:32:52 +13:00
return session.profile
2020-12-09 03:37:48 +13:00
}
2020-12-07 07:29:39 +13:00
func (session *SessionCtx) profileChanged() {
2021-03-14 12:45:51 +13:00
if !session.profile.CanHost && session.IsHost() {
2024-05-07 09:47:13 +12:00
session.ClearHost()
2020-12-07 07:29:39 +13:00
}
2021-03-26 02:08:26 +13:00
if (!session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch) && session.state.IsWatching {
2021-09-29 12:01:03 +13:00
session.GetWebRTCPeer().Destroy()
2020-12-07 07:29:39 +13:00
}
2021-03-14 12:45:51 +13:00
if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected {
session.DestroyWebSocketPeer("profile changed")
2020-12-07 07:29:39 +13:00
}
2022-03-27 11:20:38 +13:00
// update webrtc paused state
if webrtcPeer := session.GetWebRTCPeer(); webrtcPeer != nil {
webrtcPeer.SetPaused(session.PrivateModeEnabled())
}
2020-12-07 07:29:39 +13:00
}
2021-03-14 12:50:08 +13:00
func (session *SessionCtx) State() types.SessionState {
return session.state
}
2020-11-26 08:26:11 +13:00
2020-12-03 05:29:38 +13:00
func (session *SessionCtx) IsHost() bool {
return session.manager.isHost(session)
2020-11-26 08:02:53 +13:00
}
2024-05-07 09:47:13 +12:00
func (session *SessionCtx) SetAsHost() {
session.manager.setHost(session, session)
}
func (session *SessionCtx) SetAsHostBy(host types.Session) {
session.manager.setHost(session, host)
}
func (session *SessionCtx) ClearHost() {
session.manager.setHost(session, nil)
}
2022-03-27 11:20:38 +13:00
func (session *SessionCtx) PrivateModeEnabled() bool {
2022-03-27 12:08:06 +13:00
return session.manager.Settings().PrivateMode && !session.profile.IsAdmin
2022-03-27 11:20:38 +13:00
}
2021-11-02 05:31:00 +13:00
func (session *SessionCtx) SetCursor(cursor types.Cursor) {
2022-03-27 12:08:06 +13:00
if session.manager.Settings().InactiveCursors && session.profile.SendsInactiveCursor {
2021-11-02 05:49:02 +13:00
session.manager.SetCursor(cursor, session)
}
2021-10-24 07:25:18 +13:00
}
2020-12-03 05:29:38 +13:00
// ---
2021-10-01 06:42:25 +13:00
// websocket
2020-12-03 05:29:38 +13:00
// ---
// Connect WebSocket peer sets current peer and emits connected event. It also destroys the
// previous peer, if there was one. If the peer is already set, it will be ignored.
func (session *SessionCtx) ConnectWebSocketPeer(websocketPeer types.WebSocketPeer) {
2021-09-29 12:01:03 +13:00
session.websocketMu.Lock()
isCurrentPeer := websocketPeer == session.websocketPeer
2021-09-29 12:01:03 +13:00
session.websocketPeer, websocketPeer = websocketPeer, session.websocketPeer
session.websocketMu.Unlock()
// ignore if already set
if isCurrentPeer {
return
}
session.logger.Info().Msg("set websocket connected")
2023-05-15 10:28:45 +12:00
// update state
now := time.Now()
session.state.IsConnected = true
2023-05-15 10:28:45 +12:00
session.state.ConnectedSince = &now
session.state.NotConnectedSince = nil
session.manager.emmiter.Emit("connected", session)
// if there is a previous peer, destroy it
if websocketPeer != nil {
2021-09-29 12:01:03 +13:00
websocketPeer.Destroy("connection replaced")
}
2020-12-03 05:29:38 +13:00
}
// Disconnect WebSocket peer sets current peer to nil and emits disconnected event. It also
// allows for a delayed disconnect. That means, the peer will not be disconnected immediately,
// but after a delay. If the peer is connected again before the delay, the disconnect will be
// cancelled.
//
// If the peer is not the current peer or the peer is nil, it will be ignored.
func (session *SessionCtx) DisconnectWebSocketPeer(websocketPeer types.WebSocketPeer, delayed bool) {
2021-09-29 12:01:03 +13:00
session.websocketMu.Lock()
isCurrentPeer := websocketPeer == session.websocketPeer && websocketPeer != nil
2021-10-01 06:42:25 +13:00
session.websocketMu.Unlock()
// ignore if not current peer
2021-10-01 06:42:25 +13:00
if !isCurrentPeer {
2021-03-24 03:38:37 +13:00
return
}
//
// ws delayed
//
var wsDelayedTimer *time.Timer
if delayed {
wsDelayedTimer = time.AfterFunc(WS_DELAYED_DURATION, func() {
session.DisconnectWebSocketPeer(websocketPeer, false)
})
}
session.wsDelayedMu.Lock()
if session.wsDelayedTimer != nil {
session.wsDelayedTimer.Stop()
}
session.wsDelayedTimer = wsDelayedTimer
session.wsDelayedMu.Unlock()
if delayed {
session.logger.Info().Msg("delayed websocket disconnected")
return
}
//
// not delayed
//
session.logger.Info().Msg("set websocket disconnected")
2023-05-15 10:28:45 +12:00
now := time.Now()
session.state.IsConnected = false
2023-05-15 10:28:45 +12:00
session.state.ConnectedSince = nil
session.state.NotConnectedSince = &now
2021-10-01 06:42:25 +13:00
session.manager.emmiter.Emit("disconnected", session)
session.websocketMu.Lock()
if websocketPeer == session.websocketPeer {
session.websocketPeer = nil
}
session.websocketMu.Unlock()
2020-12-03 05:29:38 +13:00
}
// Destroy WebSocket peer disconnects the peer and destroys it. It ensures that the peer is
// disconnected immediately even though normal flow would be to disconnect it delayed.
func (session *SessionCtx) DestroyWebSocketPeer(reason string) {
2021-09-29 12:01:03 +13:00
session.websocketMu.Lock()
peer := session.websocketPeer
session.websocketMu.Unlock()
2021-09-29 12:01:03 +13:00
if peer == nil {
return
}
// disconnect peer first, so that it is not used anymore
session.DisconnectWebSocketPeer(peer, false)
// destroy it afterwards
peer.Destroy(reason)
2021-03-26 02:08:26 +13:00
}
// Send event to websocket peer.
func (session *SessionCtx) Send(event string, payload any) {
session.websocketMu.Lock()
peer := session.websocketPeer
session.websocketMu.Unlock()
2021-09-29 12:01:03 +13:00
if peer != nil {
peer.Send(event, payload)
}
}
2020-12-03 05:29:38 +13:00
// ---
// webrtc
// ---
// Set webrtc peer and destroy the old one, if there is old one.
2021-02-15 05:11:21 +13:00
func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) {
2021-09-29 12:01:03 +13:00
session.webrtcMu.Lock()
session.webrtcPeer, webrtcPeer = webrtcPeer, session.webrtcPeer
session.webrtcMu.Unlock()
2021-09-29 12:01:03 +13:00
if webrtcPeer != nil && webrtcPeer != session.webrtcPeer {
webrtcPeer.Destroy()
}
2020-12-03 05:29:38 +13:00
}
// Set if current webrtc peer is connected or not. Since there might be lefover calls from
// webrtc peer, that are not used anymore, we need to check if the webrtc peer is still the
// same as the one we are setting the connected state for.
//
// If webrtc peer is disconnected, we don't expect it to be reconnected, so we set it to nil
// and send a signal close to the client. New connection is expected to use a new webrtc peer.
func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) {
2021-09-29 12:01:03 +13:00
session.webrtcMu.Lock()
2021-10-01 06:42:25 +13:00
isCurrentPeer := webrtcPeer == session.webrtcPeer
session.webrtcMu.Unlock()
if !isCurrentPeer {
return
}
2022-02-13 06:55:56 +13:00
session.logger.Info().
Bool("connected", connected).
Msg("set webrtc connected")
2023-05-15 10:28:45 +12:00
// update state
2021-02-27 03:04:50 +13:00
session.state.IsWatching = connected
2023-05-15 10:28:45 +12:00
if now := time.Now(); connected {
session.state.WatchingSince = &now
session.state.NotWatchingSince = nil
} else {
session.state.WatchingSince = nil
session.state.NotWatchingSince = &now
}
2020-12-04 04:10:52 +13:00
session.manager.emmiter.Emit("state_changed", session)
2021-10-01 06:42:25 +13:00
if connected {
return
2020-12-03 05:29:38 +13:00
}
2021-10-01 06:42:25 +13:00
session.webrtcMu.Lock()
2022-02-13 06:46:00 +13:00
isCurrentPeer = webrtcPeer == session.webrtcPeer
if isCurrentPeer {
2021-10-01 06:42:25 +13:00
session.webrtcPeer = nil
}
session.webrtcMu.Unlock()
2022-02-13 06:46:00 +13:00
if isCurrentPeer {
session.Send(event.SIGNAL_CLOSE, nil)
}
2020-12-03 05:29:38 +13:00
}
// Get current WebRTC peer. Nil if not connected.
2021-02-06 06:14:45 +13:00
func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer {
2021-09-29 12:01:03 +13:00
session.webrtcMu.Lock()
defer session.webrtcMu.Unlock()
2021-02-15 05:11:21 +13:00
return session.webrtcPeer
2021-02-03 08:43:33 +13:00
}