2020-11-02 04:09:48 +13:00
|
|
|
package handler
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
2020-11-02 04:09:48 +13:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
|
|
|
"demodesk/neko/internal/utils"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func New(
|
|
|
|
sessions types.SessionManager,
|
|
|
|
desktop types.DesktopManager,
|
|
|
|
capture types.CaptureManager,
|
|
|
|
webrtc types.WebRTCManager,
|
|
|
|
) *MessageHandlerCtx {
|
|
|
|
logger := log.With().Str("module", "handler").Logger()
|
|
|
|
|
|
|
|
return &MessageHandlerCtx{
|
|
|
|
logger: logger,
|
|
|
|
sessions: sessions,
|
|
|
|
desktop: desktop,
|
|
|
|
capture: capture,
|
|
|
|
webrtc: webrtc,
|
|
|
|
banned: make(map[string]bool),
|
|
|
|
locked: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageHandlerCtx struct {
|
2020-10-23 03:54:50 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
sessions types.SessionManager
|
|
|
|
webrtc types.WebRTCManager
|
2020-11-02 04:09:48 +13:00
|
|
|
desktop types.DesktopManager
|
|
|
|
capture types.CaptureManager
|
2020-10-23 03:54:50 +13:00
|
|
|
banned map[string]bool
|
|
|
|
locked bool
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) Connected(id string, socket types.WebSocket) (bool, string) {
|
2020-10-23 03:54:50 +13:00
|
|
|
address := socket.Address()
|
2020-11-02 04:09:48 +13:00
|
|
|
if address != "" {
|
2020-10-23 03:54:50 +13:00
|
|
|
ok, banned := h.banned[address]
|
|
|
|
if ok && banned {
|
|
|
|
h.logger.Debug().Str("address", address).Msg("banned")
|
2020-11-02 04:09:48 +13:00
|
|
|
return false, "banned"
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2020-11-02 04:09:48 +13:00
|
|
|
} else {
|
|
|
|
h.logger.Debug().Msg("no remote address")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if h.locked {
|
|
|
|
session, ok := h.sessions.Get(id)
|
|
|
|
if !ok || !session.Admin() {
|
|
|
|
h.logger.Debug().Msg("server locked")
|
2020-11-02 04:09:48 +13:00
|
|
|
return false, "locked"
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
return true, ""
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) Disconnected(id string) error {
|
|
|
|
// TODO: Refactor.
|
2020-10-23 03:54:50 +13:00
|
|
|
if h.locked && len(h.sessions.Admins()) == 0 {
|
|
|
|
h.locked = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.sessions.Destroy(id)
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) Message(id string, raw []byte) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
header := message.Message{}
|
|
|
|
if err := json.Unmarshal(raw, &header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
session, ok := h.sessions.Get(id)
|
|
|
|
if !ok {
|
2020-10-29 10:02:42 +13:00
|
|
|
return errors.Errorf("unknown session id %s", id)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
switch header.Event {
|
|
|
|
// Signal Events
|
|
|
|
case event.SIGNAL_ANSWER:
|
|
|
|
payload := &message.SignalAnswer{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.signalAnswer(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
|
|
|
|
// Control Events
|
|
|
|
case event.CONTROL_RELEASE:
|
2020-11-01 10:43:47 +13:00
|
|
|
return errors.Wrapf(h.controlRelease(session), "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.CONTROL_REQUEST:
|
2020-11-01 10:43:47 +13:00
|
|
|
return errors.Wrapf(h.controlRequest(session), "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.CONTROL_GIVE:
|
|
|
|
payload := &message.Control{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.controlGive(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
case event.CONTROL_CLIPBOARD:
|
|
|
|
payload := &message.Clipboard{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.controlClipboard(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
case event.CONTROL_KEYBOARD:
|
|
|
|
payload := &message.Keyboard{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.controlKeyboard(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
|
|
|
|
// Screen Events
|
|
|
|
case event.SCREEN_RESOLUTION:
|
2020-11-01 10:43:47 +13:00
|
|
|
return errors.Wrapf(h.screenResolution(session), "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.SCREEN_CONFIGURATIONS:
|
2020-11-01 10:43:47 +13:00
|
|
|
return errors.Wrapf(h.screenConfigurations(session), "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.SCREEN_SET:
|
|
|
|
payload := &message.ScreenResolution{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.screenSet(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
|
|
|
|
// Boradcast Events
|
|
|
|
case event.BORADCAST_CREATE:
|
|
|
|
payload := &message.BroadcastCreate{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.boradcastCreate(session, payload)
|
|
|
|
}), "%s failed", header.Event)
|
|
|
|
case event.BORADCAST_DESTROY:
|
|
|
|
return errors.Wrapf(h.boradcastDestroy(session), "%s failed", header.Event)
|
|
|
|
|
|
|
|
// Admin Events
|
|
|
|
case event.ADMIN_LOCK:
|
2020-11-01 10:43:47 +13:00
|
|
|
return errors.Wrapf(h.adminLock(session), "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.ADMIN_UNLOCK:
|
2020-11-01 10:43:47 +13:00
|
|
|
return errors.Wrapf(h.adminUnlock(session), "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.ADMIN_CONTROL:
|
2020-11-01 10:43:47 +13:00
|
|
|
return errors.Wrapf(h.adminControl(session), "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.ADMIN_RELEASE:
|
2020-11-01 10:43:47 +13:00
|
|
|
return errors.Wrapf(h.adminRelease(session), "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.ADMIN_GIVE:
|
|
|
|
payload := &message.Admin{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.adminGive(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
case event.ADMIN_BAN:
|
|
|
|
payload := &message.Admin{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.adminBan(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
case event.ADMIN_KICK:
|
|
|
|
payload := &message.Admin{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.adminKick(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
case event.ADMIN_MUTE:
|
|
|
|
payload := &message.Admin{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.adminMute(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
case event.ADMIN_UNMUTE:
|
|
|
|
payload := &message.Admin{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
2020-11-01 10:43:47 +13:00
|
|
|
return h.adminUnmute(session, payload)
|
2020-10-23 03:54:50 +13:00
|
|
|
}), "%s failed", header.Event)
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown message event %s", header.Event)
|
|
|
|
}
|
|
|
|
}
|