neko/server/internal/websocket/admin.go

305 lines
6.5 KiB
Go
Raw Normal View History

2020-01-21 05:22:24 +13:00
package websocket
import (
2020-01-24 04:23:26 +13:00
"strings"
2020-01-21 05:22:24 +13:00
"n.eko.moe/neko/internal/event"
"n.eko.moe/neko/internal/message"
"n.eko.moe/neko/internal/session"
)
func (h *MessageHandler) adminLock(id string, session *session.Session) error {
2020-01-24 04:23:26 +13:00
if !session.Admin {
h.logger.Debug().Msg("user not admin")
return nil
}
if h.locked {
h.logger.Debug().Msg("server already locked...")
2020-01-21 05:22:24 +13:00
return nil
}
h.locked = true
if err := h.sessions.Brodcast(
message.Admin{
Event: event.ADMIN_LOCK,
ID: id,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_LOCK)
return err
}
return nil
}
func (h *MessageHandler) adminUnlock(id string, session *session.Session) error {
2020-01-24 04:23:26 +13:00
if !session.Admin {
h.logger.Debug().Msg("user not admin")
return nil
}
if !h.locked {
h.logger.Debug().Msg("server not locked...")
2020-01-21 05:22:24 +13:00
return nil
}
h.locked = false
if err := h.sessions.Brodcast(
message.Admin{
Event: event.ADMIN_UNLOCK,
ID: id,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_UNLOCK)
return err
}
return nil
}
func (h *MessageHandler) adminControl(id string, session *session.Session) error {
if !session.Admin {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("user not admin")
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-23 06:16:40 +13:00
host, ok := h.sessions.GetHost()
2020-01-21 05:22:24 +13:00
h.sessions.SetHost(id)
2020-01-23 06:16:40 +13:00
if ok {
if err := h.sessions.Brodcast(
message.AdminTarget{
Event: event.ADMIN_CONTROL,
ID: id,
Target: host.ID,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_CONTROL)
return err
}
} else {
if err := h.sessions.Brodcast(
message.Admin{
Event: event.ADMIN_CONTROL,
ID: id,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_CONTROL)
return err
}
2020-01-21 05:22:24 +13:00
}
return nil
}
func (h *MessageHandler) adminRelease(id string, session *session.Session) error {
if !session.Admin {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("user not admin")
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-23 06:16:40 +13:00
host, ok := h.sessions.GetHost()
2020-01-21 05:22:24 +13:00
h.sessions.ClearHost()
2020-01-23 06:16:40 +13:00
if ok {
if err := h.sessions.Brodcast(
message.AdminTarget{
Event: event.ADMIN_RELEASE,
ID: id,
Target: host.ID,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_RELEASE)
return err
}
} else {
if err := h.sessions.Brodcast(
message.Admin{
Event: event.ADMIN_RELEASE,
ID: id,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_RELEASE)
return err
}
2020-01-21 05:22:24 +13:00
}
return nil
}
2020-01-24 04:23:26 +13:00
func (h *MessageHandler) adminGive(id string, session *session.Session, payload *message.Admin) error {
2020-01-21 05:22:24 +13:00
if !session.Admin {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("user not admin")
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-24 04:23:26 +13:00
if !h.sessions.Has(payload.ID) {
h.logger.Debug().Str("id", payload.ID).Msg("user does not exist")
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-24 04:23:26 +13:00
// set host
h.sessions.SetHost(payload.ID)
// let everyone know
if err := h.sessions.Brodcast(
message.AdminTarget{
Event: event.CONTROL_GIVE,
ID: id,
Target: payload.ID,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.CONTROL_LOCKED)
return err
2020-01-23 06:16:40 +13:00
}
2020-01-24 04:23:26 +13:00
return nil
}
func (h *MessageHandler) adminMute(id string, session *session.Session, payload *message.Admin) error {
if !session.Admin {
h.logger.Debug().Msg("user not admin")
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-24 04:23:26 +13:00
target, ok := h.sessions.Get(payload.ID)
if !ok {
h.logger.Debug().Str("id", payload.ID).Msg("can't find session id")
return nil
}
2020-01-21 05:22:24 +13:00
2020-01-24 04:23:26 +13:00
if target.Admin {
h.logger.Debug().Msg("target is an admin, baling")
return nil
2020-01-21 05:22:24 +13:00
}
2020-01-24 04:23:26 +13:00
target.Muted = true
2020-01-21 05:22:24 +13:00
if err := h.sessions.Brodcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
2020-01-24 04:23:26 +13:00
Event: event.ADMIN_MUTE,
2020-01-23 06:16:40 +13:00
Target: target.ID,
ID: id,
2020-01-21 05:22:24 +13:00
}, nil); err != nil {
2020-01-24 04:23:26 +13:00
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_UNMUTE)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}
2020-01-24 04:23:26 +13:00
func (h *MessageHandler) adminUnmute(id string, session *session.Session, payload *message.Admin) error {
2020-01-21 05:22:24 +13:00
if !session.Admin {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("user not admin")
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-23 06:16:40 +13:00
target, ok := h.sessions.Get(payload.ID)
if !ok {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Str("id", payload.ID).Msg("can't find target session")
2020-01-23 06:16:40 +13:00
return nil
}
2020-01-24 04:23:26 +13:00
target.Muted = false
2020-01-21 05:22:24 +13:00
if err := h.sessions.Brodcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
2020-01-24 04:23:26 +13:00
Event: event.ADMIN_UNMUTE,
2020-01-23 06:16:40 +13:00
Target: target.ID,
ID: id,
2020-01-21 05:22:24 +13:00
}, nil); err != nil {
2020-01-24 04:23:26 +13:00
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_UNMUTE)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}
2020-01-24 04:23:26 +13:00
func (h *MessageHandler) adminKick(id string, session *session.Session, payload *message.Admin) error {
2020-01-21 05:22:24 +13:00
if !session.Admin {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("user not admin")
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-23 06:16:40 +13:00
target, ok := h.sessions.Get(payload.ID)
if !ok {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Str("id", payload.ID).Msg("can't find session id")
2020-01-23 06:16:40 +13:00
return nil
2020-01-21 05:22:24 +13:00
}
2020-01-23 06:16:40 +13:00
if target.Admin {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("target is an admin, baling")
2020-01-23 06:16:40 +13:00
return nil
}
2020-01-24 04:23:26 +13:00
if err := target.Kick(message.Disconnect{
Event: event.SYSTEM_DISCONNECT,
Message: "You have been kicked",
}); err != nil {
return err
}
2020-01-23 06:16:40 +13:00
2020-01-21 05:22:24 +13:00
if err := h.sessions.Brodcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
2020-01-24 04:23:26 +13:00
Event: event.ADMIN_KICK,
2020-01-23 06:16:40 +13:00
Target: target.ID,
ID: id,
2020-01-24 04:23:26 +13:00
}, []string{payload.ID}); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_KICK)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}
2020-01-24 04:23:26 +13:00
func (h *MessageHandler) adminBan(id string, session *session.Session, payload *message.Admin) error {
2020-01-21 05:22:24 +13:00
if !session.Admin {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("user not admin")
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-23 06:16:40 +13:00
target, ok := h.sessions.Get(payload.ID)
if !ok {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Str("id", payload.ID).Msg("can't find session id")
2020-01-23 06:16:40 +13:00
return nil
2020-01-21 05:22:24 +13:00
}
2020-01-24 04:23:26 +13:00
if target.Admin {
h.logger.Debug().Msg("target is an admin, baling")
return nil
}
remote := target.RemoteAddr()
if remote == nil {
h.logger.Debug().Msg("no remote address, baling")
return nil
}
address := strings.SplitN(*remote, ":", -1)
if len(address[0]) < 1 {
h.logger.Debug().Str("address", *remote).Msg("no remote address, baling")
return nil
}
h.logger.Debug().Str("address", *remote).Msg("adding address to banned")
h.banned[address[0]] = true
if err := target.Kick(message.Disconnect{
Event: event.SYSTEM_DISCONNECT,
Message: "You have been banned",
}); err != nil {
return err
}
2020-01-23 06:16:40 +13:00
2020-01-21 05:22:24 +13:00
if err := h.sessions.Brodcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
2020-01-24 04:23:26 +13:00
Event: event.ADMIN_BAN,
2020-01-23 06:16:40 +13:00
Target: target.ID,
ID: id,
2020-01-24 04:23:26 +13:00
}, []string{payload.ID}); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.ADMIN_BAN)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}