2020-10-23 03:54:50 +13:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/rs/zerolog"
|
2020-10-30 06:07:04 +13:00
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
type SessionCtx struct {
|
2021-02-15 05:11:21 +13:00
|
|
|
id string
|
|
|
|
logger zerolog.Logger
|
|
|
|
manager *SessionManagerCtx
|
|
|
|
profile types.MemberProfile
|
|
|
|
websocketPeer types.WebSocketPeer
|
|
|
|
websocketConnected bool
|
|
|
|
webrtcPeer types.WebRTCPeer
|
|
|
|
webrtcConnected bool
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) ID() string {
|
2020-10-23 03:54:50 +13:00
|
|
|
return session.id
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
// ---
|
|
|
|
// profile
|
|
|
|
// ---
|
|
|
|
|
|
|
|
func (session *SessionCtx) VerifySecret(secret string) bool {
|
|
|
|
return session.profile.Secret == secret
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) Name() string {
|
2020-11-26 10:07:05 +13:00
|
|
|
return session.profile.Name
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-03 04:49:51 +13:00
|
|
|
func (session *SessionCtx) IsAdmin() bool {
|
2020-11-26 10:07:05 +13:00
|
|
|
return session.profile.IsAdmin
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
func (session *SessionCtx) CanLogin() bool {
|
|
|
|
return session.profile.CanLogin
|
2020-11-01 09:20:42 +13:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
func (session *SessionCtx) CanConnect() bool {
|
|
|
|
return session.profile.CanConnect
|
2020-11-28 07:59:54 +13:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
func (session *SessionCtx) CanWatch() bool {
|
|
|
|
return session.profile.CanWatch
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
func (session *SessionCtx) CanHost() bool {
|
|
|
|
return session.profile.CanHost
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
func (session *SessionCtx) CanAccessClipboard() bool {
|
|
|
|
return session.profile.CanAccessClipboard
|
|
|
|
}
|
2020-12-01 07:13:17 +13:00
|
|
|
|
2020-12-09 03:37:48 +13:00
|
|
|
func (session *SessionCtx) GetProfile() types.MemberProfile {
|
|
|
|
profile := session.profile
|
|
|
|
profile.Secret = ""
|
|
|
|
return profile
|
|
|
|
}
|
|
|
|
|
2020-12-07 07:29:39 +13:00
|
|
|
func (session *SessionCtx) profileChanged() {
|
|
|
|
if !session.CanHost() && session.IsHost() {
|
|
|
|
session.manager.ClearHost()
|
2020-12-07 09:34:23 +13:00
|
|
|
|
|
|
|
session.manager.Broadcast(
|
|
|
|
message.ControlHost{
|
|
|
|
Event: event.CONTROL_HOST,
|
|
|
|
HasHost: false,
|
|
|
|
}, nil)
|
2020-12-07 07:29:39 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if !session.CanWatch() && session.IsWatching() {
|
2021-02-15 05:11:21 +13:00
|
|
|
if err := session.webrtcPeer.Destroy(); err != nil {
|
2020-12-07 07:29:39 +13:00
|
|
|
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:34:23 +13:00
|
|
|
if (!session.CanConnect() || !session.CanLogin()) && session.IsConnected() {
|
2020-12-07 07:29:39 +13:00
|
|
|
if err := session.Disconnect("profile changed"); err != nil {
|
|
|
|
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
// ---
|
2020-12-09 03:37:48 +13:00
|
|
|
// state
|
2020-12-03 05:29:38 +13:00
|
|
|
// ---
|
2020-11-26 08:26:11 +13:00
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
func (session *SessionCtx) IsHost() bool {
|
|
|
|
return session.manager.host != nil && session.manager.host.ID() == session.ID()
|
2020-11-26 08:02:53 +13:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
func (session *SessionCtx) IsConnected() bool {
|
2021-02-15 05:11:21 +13:00
|
|
|
return session.websocketConnected
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-07 07:02:33 +13:00
|
|
|
func (session *SessionCtx) IsWatching() bool {
|
2021-02-15 05:11:21 +13:00
|
|
|
return session.webrtcConnected
|
2020-11-02 08:53:25 +13:00
|
|
|
}
|
|
|
|
|
2020-12-09 03:37:48 +13:00
|
|
|
func (session *SessionCtx) GetState() types.MemberState {
|
|
|
|
// TODO: Save state in member struct.
|
|
|
|
return types.MemberState{
|
|
|
|
IsConnected: session.IsConnected(),
|
|
|
|
IsWatching: session.IsWatching(),
|
2020-11-26 10:06:13 +13:00
|
|
|
}
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
// ---
|
|
|
|
// webscoket
|
|
|
|
// ---
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
|
|
|
|
if session.websocketPeer != nil {
|
|
|
|
if err := session.websocketPeer.Destroy(); err != nil {
|
2021-02-13 09:48:47 +13:00
|
|
|
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
session.websocketPeer = websocketPeer
|
2020-12-03 05:29:38 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
|
2021-02-15 05:11:21 +13:00
|
|
|
session.websocketConnected = connected
|
2020-12-04 04:45:41 +13:00
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
if connected {
|
|
|
|
session.manager.emmiter.Emit("connected", session)
|
2020-12-04 04:45:41 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
session.manager.emmiter.Emit("disconnected", session)
|
2021-02-15 05:11:21 +13:00
|
|
|
session.websocketPeer = nil
|
2020-12-04 04:45:41 +13:00
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
if session.webrtcPeer != nil {
|
|
|
|
if err := session.webrtcPeer.Destroy(); err != nil {
|
2020-12-06 09:05:09 +13:00
|
|
|
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
|
|
|
|
}
|
2020-12-03 05:29:38 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) Send(v interface{}) error {
|
2021-02-15 05:11:21 +13:00
|
|
|
if session.websocketPeer == nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-01 09:46:29 +13:00
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
return session.websocketPeer.Send(v)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-09 03:37:48 +13:00
|
|
|
func (session *SessionCtx) Disconnect(reason string) error {
|
|
|
|
if err := session.Send(
|
|
|
|
message.SystemDisconnect{
|
|
|
|
Event: event.SYSTEM_DISCONNECT,
|
|
|
|
Message: reason,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
if session.websocketPeer != nil {
|
|
|
|
if err := session.websocketPeer.Destroy(); err != nil {
|
2020-12-09 03:37:48 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
if session.webrtcPeer != nil {
|
|
|
|
if err := session.webrtcPeer.Destroy(); err != nil {
|
2020-12-09 03:37:48 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:29:38 +13:00
|
|
|
// ---
|
|
|
|
// webrtc
|
|
|
|
// ---
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) {
|
|
|
|
if session.webrtcPeer != nil {
|
|
|
|
if err := session.webrtcPeer.Destroy(); err != nil {
|
2021-02-13 09:48:47 +13:00
|
|
|
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
session.webrtcPeer = webrtcPeer
|
2020-12-03 05:29:38 +13:00
|
|
|
}
|
|
|
|
|
2021-02-15 05:17:06 +13:00
|
|
|
func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) {
|
|
|
|
if webrtcPeer != session.webrtcPeer {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-15 05:11:21 +13:00
|
|
|
session.webrtcConnected = connected
|
2020-12-04 04:10:52 +13:00
|
|
|
session.manager.emmiter.Emit("state_changed", session)
|
|
|
|
|
|
|
|
if !connected {
|
2021-02-15 05:11:21 +13:00
|
|
|
session.webrtcPeer = nil
|
2020-12-03 05:29:38 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 06:14:45 +13:00
|
|
|
func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer {
|
2021-02-15 05:11:21 +13:00
|
|
|
return session.webrtcPeer
|
2021-02-03 08:43:33 +13:00
|
|
|
}
|