212 lines
6.3 KiB
Go
Raw Normal View History

2022-09-12 22:36:56 +02:00
package handler
2020-01-18 23:30:09 +00:00
import (
2020-01-24 15:47:37 +00:00
"encoding/json"
2020-01-18 23:30:09 +00:00
2020-01-24 15:47:37 +00:00
"github.com/pkg/errors"
2020-01-18 23:30:09 +00:00
"github.com/rs/zerolog"
2022-09-12 22:36:56 +02:00
"github.com/rs/zerolog/log"
2020-01-18 23:30:09 +00:00
2021-10-05 22:38:24 +02:00
"m1k1o/neko/internal/types"
"m1k1o/neko/internal/types/event"
"m1k1o/neko/internal/types/message"
"m1k1o/neko/internal/utils"
2022-09-13 20:04:43 +02:00
"m1k1o/neko/internal/websocket/state"
2020-01-18 23:30:09 +00:00
)
2020-01-24 15:47:37 +00:00
type MessageHandler struct {
2022-09-17 12:43:17 +02:00
logger zerolog.Logger
sessions types.SessionManager
desktop types.DesktopManager
capture types.CaptureManager
webrtc types.WebRTCManager
state *state.State
2022-09-12 22:36:56 +02:00
}
func New(
sessions types.SessionManager,
desktop types.DesktopManager,
capture types.CaptureManager,
webrtc types.WebRTCManager,
2022-09-13 20:04:43 +02:00
state *state.State,
2022-09-12 22:36:56 +02:00
) *MessageHandler {
return &MessageHandler{
2022-09-17 12:43:17 +02:00
logger: log.With().Str("module", "websocket").Str("submodule", "handler").Logger(),
sessions: sessions,
desktop: desktop,
capture: capture,
webrtc: webrtc,
state: state,
2022-09-12 22:36:56 +02:00
}
2020-01-18 23:30:09 +00:00
}
2022-09-12 22:36:56 +02:00
func (h *MessageHandler) Connected(admin bool, address string) (bool, string) {
2020-04-05 03:49:43 +00:00
if address == "" {
h.logger.Debug().Msg("no remote address")
2020-01-24 15:47:37 +00:00
} else {
2022-09-13 20:04:43 +02:00
if h.state.IsBanned(address) {
2020-04-05 03:49:43 +00:00
h.logger.Debug().Str("address", address).Msg("banned")
2021-11-17 18:00:27 +01:00
return false, "banned"
2020-01-18 23:30:09 +00:00
}
2020-01-24 15:47:37 +00:00
}
2020-01-20 14:38:07 +00:00
2022-09-13 20:04:43 +02:00
if h.state.IsLocked("login") && !admin {
2021-01-14 21:41:00 +01:00
h.logger.Debug().Msg("server locked")
2021-11-17 18:00:27 +01:00
return false, "locked"
2020-01-24 15:47:37 +00:00
}
2020-01-18 23:30:09 +00:00
2021-11-17 18:00:27 +01:00
return true, ""
2020-01-18 23:30:09 +00:00
}
2021-10-05 23:10:10 +02:00
func (h *MessageHandler) Disconnected(id string) {
h.sessions.Destroy(id)
2020-01-18 23:30:09 +00:00
}
2020-01-24 15:47:37 +00:00
func (h *MessageHandler) Message(id string, raw []byte) error {
header := message.Message{}
if err := json.Unmarshal(raw, &header); err != nil {
2020-01-20 16:22:24 +00:00
return err
}
2020-01-24 15:47:37 +00:00
session, ok := h.sessions.Get(id)
2020-01-20 16:22:24 +00:00
if !ok {
2021-07-22 20:58:39 +02:00
return errors.Errorf("unknown session id %s", id)
2020-01-18 23:30:09 +00:00
}
2020-01-24 15:47:37 +00:00
switch header.Event {
// Signal Events
2021-12-02 23:43:36 +01:00
case event.SIGNAL_OFFER:
payload := &message.SignalOffer{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.signalRemoteOffer(id, session, payload)
}), "%s failed", header.Event)
2020-02-12 23:13:33 +00:00
case event.SIGNAL_ANSWER:
payload := &message.SignalAnswer{}
2020-01-24 15:47:37 +00:00
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
2021-12-02 23:43:36 +01:00
return h.signalRemoteAnswer(id, session, payload)
2020-01-24 15:47:37 +00:00
}), "%s failed", header.Event)
2023-03-18 00:49:25 +01:00
case event.SIGNAL_CANDIDATE:
payload := &message.SignalCandidate{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.signalRemoteCandidate(id, session, payload)
}), "%s failed", header.Event)
2020-01-24 15:47:37 +00:00
// Control Events
case event.CONTROL_RELEASE:
return errors.Wrapf(h.controlRelease(id, session), "%s failed", header.Event)
case event.CONTROL_REQUEST:
return errors.Wrapf(h.controlRequest(id, session), "%s failed", header.Event)
2020-01-24 15:47:37 +00:00
case event.CONTROL_GIVE:
payload := &message.Control{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.controlGive(id, session, payload)
}), "%s failed", header.Event)
2020-01-25 14:29:52 +00:00
case event.CONTROL_CLIPBOARD:
payload := &message.Clipboard{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.controlClipboard(id, session, payload)
}), "%s failed", header.Event)
2020-06-16 00:55:14 +02:00
case event.CONTROL_KEYBOARD:
payload := &message.Keyboard{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.controlKeyboard(id, session, payload)
}), "%s failed", header.Event)
2020-01-24 15:47:37 +00:00
// Chat Events
case event.CHAT_MESSAGE:
payload := &message.ChatReceive{}
2020-01-24 15:47:37 +00:00
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.chat(id, session, payload)
}), "%s failed", header.Event)
case event.CHAT_EMOTE:
payload := &message.EmoteReceive{}
2020-01-24 15:47:37 +00:00
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.chatEmote(id, session, payload)
}), "%s failed", header.Event)
2022-11-03 21:54:05 -04:00
// File Transfer Events
case event.FILETRANSFER_REFRESH:
2022-11-19 20:26:45 +01:00
return errors.Wrapf(h.FileTransferRefresh(session), "%s failed", header.Event)
2022-11-03 21:54:05 -04:00
2020-02-11 05:15:59 +00:00
// Screen Events
case event.SCREEN_RESOLUTION:
return errors.Wrapf(h.screenResolution(id, session), "%s failed", header.Event)
case event.SCREEN_CONFIGURATIONS:
return errors.Wrapf(h.screenConfigurations(id, session), "%s failed", header.Event)
case event.SCREEN_SET:
payload := &message.ScreenResolution{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.screenSet(id, session, payload)
}), "%s failed", header.Event)
2020-09-27 00:10:34 +02:00
// 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)
2020-01-24 15:47:37 +00:00
// Admin Events
case event.ADMIN_LOCK:
2021-11-16 22:50:11 +01:00
payload := &message.AdminLock{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.adminLock(id, session, payload)
}), "%s failed", header.Event)
2020-01-24 15:47:37 +00:00
case event.ADMIN_UNLOCK:
2021-11-16 22:50:11 +01:00
payload := &message.AdminLock{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.adminUnlock(id, session, payload)
}), "%s failed", header.Event)
2020-01-24 15:47:37 +00:00
case event.ADMIN_CONTROL:
return errors.Wrapf(h.adminControl(id, session), "%s failed", header.Event)
case event.ADMIN_RELEASE:
2022-09-12 22:36:56 +02:00
return errors.Wrapf(h.AdminRelease(id, session), "%s failed", header.Event)
2020-01-24 15:47:37 +00:00
case event.ADMIN_GIVE:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.adminGive(id, session, payload)
}), "%s failed", header.Event)
case event.ADMIN_BAN:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.adminBan(id, session, payload)
}), "%s failed", header.Event)
case event.ADMIN_KICK:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.adminKick(id, session, payload)
}), "%s failed", header.Event)
case event.ADMIN_MUTE:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.adminMute(id, session, payload)
}), "%s failed", header.Event)
case event.ADMIN_UNMUTE:
payload := &message.Admin{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.adminUnmute(id, session, payload)
}), "%s failed", header.Event)
default:
return errors.Errorf("unknown message event %s", header.Event)
2020-01-18 23:30:09 +00:00
}
}