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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-15 04:03:12 +13:00
|
|
|
func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
header := message.Message{}
|
|
|
|
if err := json.Unmarshal(raw, &header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-17 04:56:03 +13:00
|
|
|
var err error
|
2020-10-23 03:54:50 +13:00
|
|
|
switch header.Event {
|
|
|
|
// Signal Events
|
|
|
|
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)
|
|
|
|
})
|
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
|
|
|
case event.CONTROL_GIVE:
|
|
|
|
payload := &message.Control{}
|
2020-11-17 04:56:03 +13:00
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.controlGive(session, payload)
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.CONTROL_CLIPBOARD:
|
|
|
|
payload := &message.Clipboard{}
|
2020-11-17 04:56:03 +13:00
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.controlClipboard(session, payload)
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.CONTROL_KEYBOARD:
|
|
|
|
payload := &message.Keyboard{}
|
2020-11-17 04:56:03 +13:00
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.controlKeyboard(session, payload)
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
// Screen Events
|
|
|
|
case event.SCREEN_RESOLUTION:
|
2020-11-17 10:04:50 +13:00
|
|
|
err = h.screenSize(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.SCREEN_CONFIGURATIONS:
|
2020-11-17 04:56:03 +13:00
|
|
|
err = h.screenConfigurations(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.SCREEN_SET:
|
|
|
|
payload := &message.ScreenResolution{}
|
2020-11-17 04:56:03 +13:00
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
2020-11-17 10:04:50 +13:00
|
|
|
return h.screenSizeChange(session, payload)
|
2020-11-17 04:56:03 +13:00
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
// Boradcast Events
|
|
|
|
case event.BORADCAST_CREATE:
|
|
|
|
payload := &message.BroadcastCreate{}
|
2020-11-17 04:56:03 +13:00
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.boradcastCreate(session, payload)
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.BORADCAST_DESTROY:
|
2020-11-17 04:56:03 +13:00
|
|
|
err = h.boradcastDestroy(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
// Admin Events
|
|
|
|
case event.ADMIN_CONTROL:
|
2020-11-17 04:56:03 +13:00
|
|
|
err = h.adminControl(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.ADMIN_RELEASE:
|
2020-11-17 04:56:03 +13:00
|
|
|
err = h.adminRelease(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
case event.ADMIN_GIVE:
|
|
|
|
payload := &message.Admin{}
|
2020-11-17 04:56:03 +13:00
|
|
|
err = utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.adminGive(session, payload)
|
|
|
|
})
|
2020-10-23 03:54:50 +13:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown message event %s", header.Event)
|
|
|
|
}
|
2020-11-17 04:56:03 +13:00
|
|
|
|
|
|
|
return errors.Wrapf(err, "%s failed", header.Event)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|