2020-11-02 04:09:48 +13:00
|
|
|
package handler
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"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{
|
2021-02-15 02:40:17 +13:00
|
|
|
logger: logger,
|
|
|
|
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
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-12-13 05:40:10 +13:00
|
|
|
func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) bool {
|
2020-10-23 03:54:50 +13:00
|
|
|
header := message.Message{}
|
|
|
|
if err := json.Unmarshal(raw, &header); err != nil {
|
2020-12-13 05:40:10 +13:00
|
|
|
h.logger.Error().Err(err).Msg("message parsing has failed")
|
|
|
|
return false
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-17 04:56:03 +13:00
|
|
|
var err error
|
2020-10-23 03:54:50 +13:00
|
|
|
switch header.Event {
|
|
|
|
// Signal Events
|
2020-12-01 06:27:25 +13:00
|
|
|
case event.SIGNAL_REQUEST:
|
|
|
|
err = h.signalRequest(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.SIGNAL_ANSWER:
|
|
|
|
payload := &message.SignalAnswer{}
|
2020-11-17 04:56:03 +13:00
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.signalAnswer(session, payload)
|
|
|
|
})
|
2021-02-03 08:43:33 +13:00
|
|
|
case event.SIGNAL_CANDIDATE:
|
|
|
|
payload := &message.SignalCandidate{}
|
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.signalCandidate(session, payload)
|
|
|
|
})
|
2021-02-06 08:35:30 +13:00
|
|
|
case event.SIGNAL_VIDEO:
|
|
|
|
payload := &message.SignalVideo{}
|
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.signalVideo(session, payload)
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
// Control Events
|
|
|
|
case event.CONTROL_RELEASE:
|
2020-11-17 04:56:03 +13:00
|
|
|
err = h.controlRelease(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.CONTROL_REQUEST:
|
2020-11-17 04:56:03 +13:00
|
|
|
err = h.controlRequest(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
2020-12-01 05:53:05 +13:00
|
|
|
// Screen Events
|
|
|
|
case event.SCREEN_SET:
|
|
|
|
payload := &message.ScreenSize{}
|
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.screenSet(session, payload)
|
|
|
|
})
|
|
|
|
|
2020-12-01 05:40:38 +13:00
|
|
|
// Clipboard Events
|
|
|
|
case event.CLIPBOARD_SET:
|
|
|
|
payload := &message.ClipboardData{}
|
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
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{}
|
2020-12-01 05:45:53 +13:00
|
|
|
err = utils.Unmarshal(payload, raw, 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{}
|
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.keyboardModifiers(session, payload)
|
|
|
|
})
|
2021-01-29 03:56:50 +13:00
|
|
|
|
|
|
|
// Send Events
|
|
|
|
case event.SEND_UNICAST:
|
|
|
|
payload := &message.SendUnicast{}
|
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.sendUnicast(session, payload)
|
|
|
|
})
|
|
|
|
case event.SEND_BROADCAST:
|
|
|
|
payload := &message.SendBroadcast{}
|
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.sendBroadcast(session, payload)
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
default:
|
2021-03-16 22:25:13 +13:00
|
|
|
h.logger.Warn().Str("event", header.Event).Msg("unknown message event")
|
2020-12-13 05:40:10 +13:00
|
|
|
return false
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
2020-11-17 04:56:03 +13:00
|
|
|
|
2020-12-13 05:40:10 +13:00
|
|
|
if err != nil {
|
2021-03-16 22:22:03 +13:00
|
|
|
h.logger.Error().Err(err).Str("event", header.Event).Msg("message handler has failed")
|
2020-12-13 05:40:10 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|