neko/internal/websocket/handler/handler.go

183 lines
5.2 KiB
Go
Raw Normal View History

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