2020-11-01 16:09:48 +01:00
|
|
|
package handler
|
2020-10-22 16:54:50 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/rs/zerolog"
|
2020-11-01 16:09:48 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-10-22 16:54:50 +02:00
|
|
|
|
2020-10-28 19:15:48 +01:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
|
|
|
"demodesk/neko/internal/utils"
|
2020-10-22 16:54:50 +02:00
|
|
|
)
|
|
|
|
|
2020-11-01 16:09:48 +01:00
|
|
|
func New(
|
|
|
|
sessions types.SessionManager,
|
|
|
|
desktop types.DesktopManager,
|
|
|
|
capture types.CaptureManager,
|
|
|
|
webrtc types.WebRTCManager,
|
|
|
|
) *MessageHandlerCtx {
|
|
|
|
return &MessageHandlerCtx{
|
2021-08-29 18:23:58 +02:00
|
|
|
logger: log.With().Str("module", "websocket").Str("submodule", "handler").Logger(),
|
2021-02-14 14:40:17 +01:00
|
|
|
sessions: sessions,
|
|
|
|
desktop: desktop,
|
|
|
|
capture: capture,
|
|
|
|
webrtc: webrtc,
|
2020-11-01 16:09:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageHandlerCtx struct {
|
2021-02-14 14:40:17 +01:00
|
|
|
logger zerolog.Logger
|
|
|
|
sessions types.SessionManager
|
|
|
|
webrtc types.WebRTCManager
|
|
|
|
desktop types.DesktopManager
|
|
|
|
capture types.CaptureManager
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 23:00:51 +02:00
|
|
|
func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketMessage) bool {
|
2020-11-16 16:56:03 +01:00
|
|
|
var err error
|
2021-08-29 23:00:51 +02:00
|
|
|
switch data.Event {
|
2021-09-09 22:08:39 +02:00
|
|
|
// System Events
|
|
|
|
case event.SYSTEM_LOGS:
|
|
|
|
payload := &message.SystemLogs{}
|
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
|
|
|
return h.systemLogs(session, payload)
|
|
|
|
})
|
|
|
|
|
2020-10-22 16:54:50 +02:00
|
|
|
// Signal Events
|
2020-11-30 18:27:25 +01:00
|
|
|
case event.SIGNAL_REQUEST:
|
2021-04-30 14:05:07 +00:00
|
|
|
payload := &message.SignalVideo{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2021-04-30 14:05:07 +00:00
|
|
|
return h.signalRequest(session, payload)
|
|
|
|
})
|
2021-06-27 22:02:05 +02:00
|
|
|
case event.SIGNAL_RESTART:
|
|
|
|
err = h.signalRestart(session)
|
2021-11-25 00:07:17 +01:00
|
|
|
case event.SIGNAL_OFFER:
|
|
|
|
payload := &message.SignalDescription{}
|
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
|
|
|
return h.signalOffer(session, payload)
|
|
|
|
})
|
2020-10-22 16:54:50 +02:00
|
|
|
case event.SIGNAL_ANSWER:
|
2021-11-25 00:07:17 +01:00
|
|
|
payload := &message.SignalDescription{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2020-11-16 16:56:03 +01:00
|
|
|
return h.signalAnswer(session, payload)
|
|
|
|
})
|
2021-02-02 20:43:33 +01:00
|
|
|
case event.SIGNAL_CANDIDATE:
|
|
|
|
payload := &message.SignalCandidate{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2021-02-02 20:43:33 +01:00
|
|
|
return h.signalCandidate(session, payload)
|
|
|
|
})
|
2021-02-05 20:35:30 +01:00
|
|
|
case event.SIGNAL_VIDEO:
|
|
|
|
payload := &message.SignalVideo{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2021-02-05 20:35:30 +01:00
|
|
|
return h.signalVideo(session, payload)
|
|
|
|
})
|
2020-10-22 16:54:50 +02:00
|
|
|
|
|
|
|
// Control Events
|
|
|
|
case event.CONTROL_RELEASE:
|
2020-11-16 16:56:03 +01:00
|
|
|
err = h.controlRelease(session)
|
2020-10-22 16:54:50 +02:00
|
|
|
case event.CONTROL_REQUEST:
|
2020-11-16 16:56:03 +01:00
|
|
|
err = h.controlRequest(session)
|
2022-01-30 01:25:04 +01:00
|
|
|
case event.CONTROL_KEYPRESS:
|
|
|
|
payload := &message.ControlKey{}
|
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
|
|
|
return h.controlKeyPress(session, payload)
|
|
|
|
})
|
|
|
|
case event.CONTROL_KEYDOWN:
|
|
|
|
payload := &message.ControlKey{}
|
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
|
|
|
return h.controlKeyDown(session, payload)
|
|
|
|
})
|
|
|
|
case event.CONTROL_KEYUP:
|
|
|
|
payload := &message.ControlKey{}
|
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
|
|
|
return h.controlKeyUp(session, payload)
|
|
|
|
})
|
2022-01-30 17:53:22 +01:00
|
|
|
case event.CONTROL_CUT:
|
|
|
|
err = h.controlCut(session)
|
2022-01-30 01:25:04 +01:00
|
|
|
case event.CONTROL_COPY:
|
|
|
|
err = h.controlCopy(session)
|
|
|
|
case event.CONTROL_PASTE:
|
|
|
|
err = h.controlPaste(session)
|
|
|
|
case event.CONTROL_SELECT_ALL:
|
|
|
|
err = h.controlSelectAll(session)
|
2020-10-22 16:54:50 +02:00
|
|
|
|
2020-11-30 17:53:05 +01:00
|
|
|
// Screen Events
|
|
|
|
case event.SCREEN_SET:
|
|
|
|
payload := &message.ScreenSize{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2020-11-30 17:53:05 +01:00
|
|
|
return h.screenSet(session, payload)
|
|
|
|
})
|
|
|
|
|
2020-11-30 17:40:38 +01:00
|
|
|
// Clipboard Events
|
|
|
|
case event.CLIPBOARD_SET:
|
|
|
|
payload := &message.ClipboardData{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2020-11-30 17:40:38 +01:00
|
|
|
return h.clipboardSet(session, payload)
|
|
|
|
})
|
|
|
|
|
2020-11-30 17:45:53 +01:00
|
|
|
// Keyboard Events
|
2021-01-15 16:53:03 +01:00
|
|
|
case event.KEYBOARD_MAP:
|
|
|
|
payload := &message.KeyboardMap{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2021-01-15 16:53:03 +01:00
|
|
|
return h.keyboardMap(session, payload)
|
2020-11-30 17:45:53 +01:00
|
|
|
})
|
2021-01-15 16:53:03 +01:00
|
|
|
case event.KEYBOARD_MODIFIERS:
|
2020-11-30 17:45:53 +01:00
|
|
|
payload := &message.KeyboardModifiers{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2020-11-30 17:45:53 +01:00
|
|
|
return h.keyboardModifiers(session, payload)
|
|
|
|
})
|
2021-01-28 15:56:50 +01:00
|
|
|
|
|
|
|
// Send Events
|
|
|
|
case event.SEND_UNICAST:
|
|
|
|
payload := &message.SendUnicast{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2021-01-28 15:56:50 +01:00
|
|
|
return h.sendUnicast(session, payload)
|
|
|
|
})
|
|
|
|
case event.SEND_BROADCAST:
|
|
|
|
payload := &message.SendBroadcast{}
|
2021-08-29 23:00:51 +02:00
|
|
|
err = utils.Unmarshal(payload, data.Payload, func() error {
|
2021-01-28 15:56:50 +01:00
|
|
|
return h.sendBroadcast(session, payload)
|
|
|
|
})
|
2020-10-22 16:54:50 +02:00
|
|
|
default:
|
2020-12-12 17:40:10 +01:00
|
|
|
return false
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
2020-11-16 16:56:03 +01:00
|
|
|
|
2020-12-12 17:40:10 +01:00
|
|
|
if err != nil {
|
2022-01-30 17:47:28 +01:00
|
|
|
h.logger.Warn().Err(err).
|
|
|
|
Str("event", data.Event).
|
|
|
|
Str("session_id", session.ID()).
|
|
|
|
Msg("message handler has failed")
|
2020-12-12 17:40:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|