neko/server/internal/websocket/handler.go

173 lines
5.0 KiB
Go
Raw Normal View History

2020-01-19 12:30:09 +13:00
package websocket
import (
2020-01-25 04:47:37 +13:00
"encoding/json"
2020-01-19 12:30:09 +13:00
2020-01-25 04:47:37 +13:00
"github.com/pkg/errors"
2020-01-19 12:30:09 +13:00
"github.com/rs/zerolog"
2020-01-25 04:47:37 +13:00
"n.eko.moe/neko/internal/types"
"n.eko.moe/neko/internal/types/event"
"n.eko.moe/neko/internal/types/message"
2020-01-21 03:38:07 +13:00
"n.eko.moe/neko/internal/utils"
2020-01-19 12:30:09 +13:00
)
2020-01-25 04:47:37 +13:00
type MessageHandler struct {
2020-01-19 12:30:09 +13:00
logger zerolog.Logger
2020-01-25 04:47:37 +13:00
sessions types.SessionManager
webrtc types.WebRTCManager
2020-04-06 10:34:51 +12:00
remote types.RemoteManager
2020-01-25 04:47:37 +13:00
banned map[string]bool
locked bool
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) Connected(id string, socket *WebSocket) (bool, string, error) {
address := socket.Address()
2020-04-05 15:49:43 +12:00
if address == "" {
h.logger.Debug().Msg("no remote address")
2020-01-25 04:47:37 +13:00
} else {
2020-04-05 15:49:43 +12:00
ok, banned := h.banned[address]
2020-01-25 04:47:37 +13:00
if ok && banned {
2020-04-05 15:49:43 +12:00
h.logger.Debug().Str("address", address).Msg("banned")
return false, "banned", nil
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
}
2020-01-21 03:38:07 +13:00
2020-01-25 04:47:37 +13:00
if h.locked {
2020-04-07 08:26:16 +12:00
session, ok := h.sessions.Get(id)
if !ok || !session.Admin() {
h.logger.Debug().Msg("server locked")
return false, "locked", nil
2020-04-07 08:26:16 +12:00
}
2020-01-25 04:47:37 +13:00
}
2020-01-19 12:30:09 +13:00
2020-01-25 04:47:37 +13:00
return true, "", nil
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) Disconnected(id string) error {
2020-04-07 08:14:08 +12:00
if h.locked && len(h.sessions.Admins()) == 0 {
h.locked = false
}
2020-01-25 04:47:37 +13:00
return h.sessions.Destroy(id)
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) Message(id string, raw []byte) error {
header := message.Message{}
if err := json.Unmarshal(raw, &header); err != nil {
2020-01-21 05:22:24 +13:00
return err
}
2020-01-25 04:47:37 +13:00
session, ok := h.sessions.Get(id)
2020-01-21 05:22:24 +13:00
if !ok {
2020-01-25 04:47:37 +13:00
errors.Errorf("unknown session id %s", id)
2020-01-19 12:30:09 +13:00
}
2020-01-25 04:47:37 +13:00
switch header.Event {
// Signal Events
2020-02-13 12:13:33 +13:00
case event.SIGNAL_ANSWER:
payload := &message.SignalAnswer{}
2020-01-25 04:47:37 +13:00
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
2020-02-13 12:13:33 +13:00
return h.signalAnswer(id, session, payload)
2020-01-25 04:47:37 +13:00
}), "%s failed", header.Event)
// Control Events
case event.CONTROL_RELEASE:
return errors.Wrapf(h.controlRelease(id, session), "%s failed", header.Event)
case event.CONTROL_REQUEST:
2020-06-16 08:26:47 +12:00
payload := &message.ControlRequest{}
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.controlRequest(id, session, payload)
}), "%s failed", header.Event)
2020-01-25 04:47:37 +13: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-26 03:29:52 +13: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 10:55:14 +12: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-25 04:47:37 +13:00
// Chat Events
case event.CHAT_MESSAGE:
payload := &message.ChatReceive{}
2020-01-25 04:47:37 +13: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-25 04:47:37 +13:00
return errors.Wrapf(
utils.Unmarshal(payload, raw, func() error {
return h.chatEmote(id, session, payload)
}), "%s failed", header.Event)
2020-02-11 18:15:59 +13: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-01-25 04:47:37 +13:00
// Admin Events
case event.ADMIN_LOCK:
return errors.Wrapf(h.adminLock(id, session), "%s failed", header.Event)
case event.ADMIN_UNLOCK:
return errors.Wrapf(h.adminUnlock(id, session), "%s failed", header.Event)
case event.ADMIN_CONTROL:
return errors.Wrapf(h.adminControl(id, session), "%s failed", header.Event)
case event.ADMIN_RELEASE:
return errors.Wrapf(h.adminRelease(id, session), "%s failed", header.Event)
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-19 12:30:09 +13:00
}
}