2020-01-19 12:30:09 +13:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
|
|
|
"n.eko.moe/neko/internal/event"
|
|
|
|
"n.eko.moe/neko/internal/message"
|
|
|
|
"n.eko.moe/neko/internal/session"
|
|
|
|
"n.eko.moe/neko/internal/utils"
|
|
|
|
"n.eko.moe/neko/internal/webrtc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MessageHandler struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
sessions *session.SessionManager
|
|
|
|
webrtc *webrtc.WebRTCManager
|
2020-01-21 05:22:24 +13:00
|
|
|
banned map[string]bool
|
|
|
|
locked bool
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:22:24 +13:00
|
|
|
func (h *MessageHandler) SocketConnected(id string, socket *websocket.Conn) (bool, string, error) {
|
|
|
|
ok, banned := h.banned[socket.RemoteAddr().String()]
|
|
|
|
if ok && banned {
|
|
|
|
return false, "you are banned", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.locked {
|
|
|
|
return false, "stream is currently locked", nil
|
|
|
|
}
|
|
|
|
return true, "", nil
|
2020-01-19 12:30:09 +13:00
|
|
|
}
|
|
|
|
|
2020-01-21 03:38:07 +13:00
|
|
|
func (h *MessageHandler) SocketDisconnected(id string) error {
|
2020-01-19 12:30:09 +13:00
|
|
|
return h.sessions.Destroy(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MessageHandler) Message(id string, raw []byte) error {
|
|
|
|
header := message.Message{}
|
|
|
|
if err := json.Unmarshal(raw, &header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
session, ok := h.sessions.Get(id)
|
|
|
|
if !ok {
|
|
|
|
errors.Errorf("unknown session id %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch header.Event {
|
2020-01-21 05:22:24 +13:00
|
|
|
// Signal Events
|
2020-01-21 03:38:07 +13:00
|
|
|
case event.SIGNAL_PROVIDE:
|
|
|
|
payload := message.Signal{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(&payload, raw, func() error {
|
|
|
|
return h.webrtc.CreatePeer(id, payload.SDP)
|
|
|
|
}), "%s failed", header.Event)
|
2020-01-23 06:16:40 +13:00
|
|
|
|
2020-01-21 05:22:24 +13:00
|
|
|
// Identity Events
|
2020-01-21 03:38:07 +13:00
|
|
|
case event.IDENTITY_DETAILS:
|
|
|
|
payload := &message.IdentityDetails{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.identityDetails(id, session, payload)
|
|
|
|
}), "%s failed", header.Event)
|
2020-01-23 06:16:40 +13:00
|
|
|
|
2020-01-21 05:22:24 +13:00
|
|
|
// Control Events
|
2020-01-19 12:30:09 +13:00
|
|
|
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-23 06:16:40 +13:00
|
|
|
|
2020-01-21 05:22:24 +13:00
|
|
|
// Chat Events
|
|
|
|
case event.CHAT_MESSAGE:
|
|
|
|
payload := &message.ChatRecieve{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.chat(id, session, payload)
|
|
|
|
}), "%s failed", header.Event)
|
|
|
|
case event.CHAT_EMOJI:
|
|
|
|
payload := &message.EmojiRecieve{}
|
|
|
|
return errors.Wrapf(
|
|
|
|
utils.Unmarshal(payload, raw, func() error {
|
|
|
|
return h.chatEmoji(id, session, payload)
|
|
|
|
}), "%s failed", header.Event)
|
2020-01-23 06:16:40 +13:00
|
|
|
|
2020-01-21 05:22:24 +13:00
|
|
|
// Admin Events
|
|
|
|
case event.ADMIN_LOCK:
|
|
|
|
return errors.Wrapf(h.adminLock(id, session), "%s failed", header.Event)
|
2020-01-23 06:16:40 +13:00
|
|
|
case event.ADMIN_UNLOCK:
|
|
|
|
return errors.Wrapf(h.adminUnlock(id, session), "%s failed", header.Event)
|
|
|
|
case event.ADMIN_CONTROL:
|
2020-01-21 05:22:24 +13:00
|
|
|
return errors.Wrapf(h.adminControl(id, session), "%s failed", header.Event)
|
2020-01-23 06:16:40 +13:00
|
|
|
case event.ADMIN_RELEASE:
|
2020-01-21 05:22:24 +13:00
|
|
|
return errors.Wrapf(h.adminRelease(id, session), "%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)
|
2020-01-19 12:30:09 +13:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unknown message event %s", header.Event)
|
|
|
|
}
|
|
|
|
}
|