neko/server/internal/websocket/handler/admin.go

326 lines
7.1 KiB
Go
Raw Normal View History

2022-09-13 08:36:56 +12:00
package handler
2020-01-21 05:22:24 +13:00
import (
2020-01-24 04:23:26 +13:00
"strings"
2021-10-06 09:38:24 +13:00
"m1k1o/neko/internal/types"
"m1k1o/neko/internal/types/event"
"m1k1o/neko/internal/types/message"
2020-01-21 05:22:24 +13:00
)
2021-11-17 10:50:11 +13:00
func (h *MessageHandler) adminLock(id string, session types.Session, payload *message.AdminLock) error {
2020-01-25 04:47:37 +13:00
if !session.Admin() {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("user not admin")
return nil
}
2022-09-14 06:04:43 +12:00
if h.state.IsLocked(payload.Resource) {
2021-11-17 10:50:11 +13:00
h.logger.Debug().Str("resource", payload.Resource).Msg("resource already locked...")
return nil
}
2022-11-20 08:26:45 +13:00
// allow only known resources
switch payload.Resource {
case "login":
case "control":
case "file_transfer":
default:
2021-11-17 10:50:11 +13:00
h.logger.Debug().Msg("unknown lock resource")
2020-01-21 05:22:24 +13:00
return nil
}
// TODO: Handle locks in sessions as flags.
if payload.Resource == "control" {
h.sessions.SetControlLocked(true)
}
2022-09-14 06:04:43 +12:00
h.state.Lock(payload.Resource, id)
2020-01-21 05:22:24 +13:00
if err := h.sessions.Broadcast(
2021-11-17 10:50:11 +13:00
message.AdminLock{
Event: event.ADMIN_LOCK,
ID: id,
Resource: payload.Resource,
2020-01-21 05:22:24 +13:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_LOCK)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}
2021-11-17 10:50:11 +13:00
func (h *MessageHandler) adminUnlock(id string, session types.Session, payload *message.AdminLock) error {
2020-01-25 04:47:37 +13:00
if !session.Admin() {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("user not admin")
return nil
}
2022-09-14 06:04:43 +12:00
if !h.state.IsLocked(payload.Resource) {
2021-11-17 10:50:11 +13:00
h.logger.Debug().Str("resource", payload.Resource).Msg("resource not locked...")
2020-01-21 05:22:24 +13:00
return nil
}
// TODO: Handle locks in sessions as flags.
if payload.Resource == "control" {
h.sessions.SetControlLocked(false)
}
2022-09-14 06:04:43 +12:00
h.state.Unlock(payload.Resource)
2020-01-21 05:22:24 +13:00
if err := h.sessions.Broadcast(
2021-11-17 10:50:11 +13:00
message.AdminLock{
Event: event.ADMIN_UNLOCK,
ID: id,
Resource: payload.Resource,
2020-01-21 05:22:24 +13:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_UNLOCK)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) adminControl(id string, session types.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()
2021-10-06 10:10:10 +13:00
err := h.sessions.SetHost(id)
if err != nil {
return err
}
2020-01-21 05:22:24 +13:00
2020-01-23 06:16:40 +13:00
if ok {
if err := h.sessions.Broadcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
Event: event.ADMIN_CONTROL,
ID: id,
2020-01-25 04:47:37 +13:00
Target: host.ID(),
2020-01-23 06:16:40 +13:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_CONTROL)
2020-01-23 06:16:40 +13:00
return err
}
} else {
if err := h.sessions.Broadcast(
2020-01-23 06:16:40 +13:00
message.Admin{
Event: event.ADMIN_CONTROL,
ID: id,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_CONTROL)
2020-01-23 06:16:40 +13:00
return err
}
2020-01-21 05:22:24 +13:00
}
return nil
}
2022-09-13 08:36:56 +12:00
func (h *MessageHandler) AdminRelease(id string, session types.Session) error {
2020-01-25 04:47:37 +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
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.Broadcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
Event: event.ADMIN_RELEASE,
ID: id,
2020-01-25 04:47:37 +13:00
Target: host.ID(),
2020-01-23 06:16:40 +13:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_RELEASE)
2020-01-23 06:16:40 +13:00
return err
}
} else {
if err := h.sessions.Broadcast(
2020-01-23 06:16:40 +13:00
message.Admin{
Event: event.ADMIN_RELEASE,
ID: id,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_RELEASE)
2020-01-23 06:16:40 +13:00
return err
}
2020-01-21 05:22:24 +13:00
}
return nil
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) adminGive(id string, session types.Session, payload *message.Admin) 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-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
2021-10-06 10:10:10 +13:00
err := h.sessions.SetHost(payload.ID)
if err != nil {
return err
}
2020-01-24 04:23:26 +13:00
// let everyone know
if err := h.sessions.Broadcast(
2020-01-24 04:23:26 +13:00
message.AdminTarget{
Event: event.CONTROL_GIVE,
ID: id,
Target: payload.ID,
}, nil); err != nil {
2024-07-21 00:48:54 +12:00
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_GIVE)
2020-01-24 04:23:26 +13:00
return err
2020-01-23 06:16:40 +13:00
}
2020-01-24 04:23:26 +13:00
return nil
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) adminMute(id string, session types.Session, payload *message.Admin) 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-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-25 04:47:37 +13:00
if target.Admin() {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("target is an admin, baling")
return nil
2020-01-21 05:22:24 +13:00
}
2020-01-25 04:47:37 +13:00
target.SetMuted(true)
2020-01-24 04:23:26 +13:00
if err := h.sessions.Broadcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
2020-01-24 04:23:26 +13:00
Event: event.ADMIN_MUTE,
2020-01-25 04:47:37 +13:00
Target: target.ID(),
2020-01-23 06:16:40 +13:00
ID: id,
2020-01-21 05:22:24 +13:00
}, nil); err != nil {
2024-07-21 00:48:54 +12:00
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_MUTE)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) adminUnmute(id string, session types.Session, payload *message.Admin) 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
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-25 04:47:37 +13:00
target.SetMuted(false)
2020-01-21 05:22:24 +13:00
if err := h.sessions.Broadcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
2020-01-24 04:23:26 +13:00
Event: event.ADMIN_UNMUTE,
2020-01-25 04:47:37 +13:00
Target: target.ID(),
2020-01-23 06:16:40 +13:00
ID: id,
2020-01-21 05:22:24 +13:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_UNMUTE)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) adminKick(id string, session types.Session, payload *message.Admin) 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
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-25 04:47:37 +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
}
if err := target.Kick("kicked"); err != nil {
2020-01-24 04:23:26 +13:00
return err
}
2020-01-23 06:16:40 +13:00
if err := h.sessions.Broadcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
2020-01-24 04:23:26 +13:00
Event: event.ADMIN_KICK,
2020-01-25 04:47:37 +13:00
Target: target.ID(),
2020-01-23 06:16:40 +13:00
ID: id,
2020-01-24 04:23:26 +13:00
}, []string{payload.ID}); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_KICK)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) adminBan(id string, session types.Session, payload *message.Admin) 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
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-25 04:47:37 +13:00
if target.Admin() {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("target is an admin, baling")
return nil
}
2020-01-25 04:47:37 +13:00
remote := target.Address()
2020-04-05 15:49:43 +12:00
if remote == "" {
2020-01-24 04:23:26 +13:00
h.logger.Debug().Msg("no remote address, baling")
return nil
}
2020-04-05 15:49:43 +12:00
address := strings.SplitN(remote, ":", -1)
2020-01-24 04:23:26 +13:00
if len(address[0]) < 1 {
2020-04-05 15:49:43 +12:00
h.logger.Debug().Str("address", remote).Msg("no remote address, baling")
2020-01-24 04:23:26 +13:00
return nil
}
2020-04-05 15:49:43 +12:00
h.logger.Debug().Str("address", remote).Msg("adding address to banned")
2022-09-14 06:04:43 +12:00
h.state.Ban(address[0], id)
2020-01-24 04:23:26 +13:00
if err := target.Kick("banned"); err != nil {
2020-01-24 04:23:26 +13:00
return err
}
2020-01-23 06:16:40 +13:00
if err := h.sessions.Broadcast(
2020-01-23 06:16:40 +13:00
message.AdminTarget{
2020-01-24 04:23:26 +13:00
Event: event.ADMIN_BAN,
2020-01-25 04:47:37 +13:00
Target: target.ID(),
2020-01-23 06:16:40 +13:00
ID: id,
2020-01-24 04:23:26 +13:00
}, []string{payload.ID}); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.ADMIN_BAN)
2020-01-21 05:22:24 +13:00
return err
}
return nil
}