neko/internal/api/room/handler.go

127 lines
3.6 KiB
Go
Raw Normal View History

2020-10-30 18:16:21 +01:00
package room
import (
2021-09-17 00:58:50 +02:00
"context"
"net/http"
2022-03-26 23:20:38 +01:00
"github.com/rs/zerolog/log"
2022-03-20 11:43:00 +01:00
"gitlab.com/demodesk/neko/server/pkg/auth"
"gitlab.com/demodesk/neko/server/pkg/types"
"gitlab.com/demodesk/neko/server/pkg/utils"
2020-10-30 18:16:21 +01:00
)
type RoomHandler struct {
sessions types.SessionManager
desktop types.DesktopManager
capture types.CaptureManager
2022-03-26 23:20:38 +01:00
privateModeImage []byte
2020-10-30 18:16:21 +01:00
}
func New(
sessions types.SessionManager,
2020-11-01 16:54:06 +01:00
desktop types.DesktopManager,
capture types.CaptureManager,
2020-10-30 18:16:21 +01:00
) *RoomHandler {
2022-03-26 23:20:38 +01:00
h := &RoomHandler{
sessions: sessions,
desktop: desktop,
capture: capture,
2020-10-30 18:16:21 +01:00
}
2022-03-26 23:20:38 +01:00
// generate fallback image for private mode when needed
2022-03-27 00:08:06 +01:00
sessions.OnSettingsChanged(func(new types.Settings, old types.Settings) {
if old.PrivateMode && !new.PrivateMode {
2022-03-26 23:20:38 +01:00
log.Debug().Msg("clearing private mode fallback image")
h.privateModeImage = nil
return
}
2022-03-27 00:08:06 +01:00
if !old.PrivateMode && new.PrivateMode {
img := h.desktop.GetScreenshotImage()
bytes, err := utils.CreateJPGImage(img, 90)
if err != nil {
log.Err(err).Msg("could not generate private mode fallback image")
return
}
2022-03-26 23:20:38 +01:00
2022-03-27 00:08:06 +01:00
log.Debug().Msg("using private mode fallback image")
h.privateModeImage = bytes
}
2022-03-26 23:20:38 +01:00
})
return h
2020-10-30 18:16:21 +01:00
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) Route(r types.Router) {
2022-03-27 00:26:25 +01:00
r.With(auth.AdminsOnly).Route("/settings", func(r types.Router) {
r.Post("/", h.settingsSet)
r.Get("/", h.settingsGet)
})
2021-09-17 00:58:50 +02:00
r.With(auth.AdminsOnly).Route("/broadcast", func(r types.Router) {
2020-11-18 22:35:50 +01:00
r.Get("/", h.broadcastStatus)
r.Post("/start", h.boradcastStart)
r.Post("/stop", h.boradcastStop)
2020-10-31 10:48:24 +01:00
})
2020-10-30 18:16:21 +01:00
2021-09-17 00:58:50 +02:00
r.With(auth.CanAccessClipboardOnly).With(auth.HostsOnly).Route("/clipboard", func(r types.Router) {
r.Get("/", h.clipboardGetText)
r.Post("/", h.clipboardSetText)
r.Get("/image.png", h.clipboardGetImage)
// TODO: Refactor. xclip is failing to set propper target type
// and this content is sent back to client as text in another
// clipboard update. Therefore endpoint is not usable!
2021-01-29 00:03:22 +01:00
//r.Post("/image", h.clipboardSetImage)
2021-02-02 18:28:32 +01:00
// TODO: Refactor. If there would be implemented custom target
// retrieval, this endpoint would be useful.
//r.Get("/targets", h.clipboardGetTargets)
2020-10-31 12:35:02 +01:00
})
2020-11-16 22:48:20 +01:00
2021-09-17 00:58:50 +02:00
r.With(auth.CanHostOnly).Route("/keyboard", func(r types.Router) {
2021-01-15 16:53:03 +01:00
r.Get("/map", h.keyboardMapGet)
r.With(auth.HostsOnly).Post("/map", h.keyboardMapSet)
2021-01-12 23:35:46 +01:00
r.Get("/modifiers", h.keyboardModifiersGet)
r.With(auth.HostsOnly).Post("/modifiers", h.keyboardModifiersSet)
2020-11-18 21:56:42 +01:00
})
2021-09-17 00:58:50 +02:00
r.With(auth.CanHostOnly).Route("/control", func(r types.Router) {
2020-11-18 23:14:28 +01:00
r.Get("/", h.controlStatus)
2020-11-18 22:35:50 +01:00
r.Post("/request", h.controlRequest)
r.Post("/release", h.controlRelease)
2020-11-18 22:35:50 +01:00
r.With(auth.AdminsOnly).Post("/take", h.controlTake)
r.With(auth.AdminsOnly).Post("/give", h.controlGive)
2020-11-29 15:58:26 +01:00
r.With(auth.AdminsOnly).Post("/reset", h.controlReset)
2020-11-16 22:48:20 +01:00
})
2021-09-17 00:58:50 +02:00
r.With(auth.CanWatchOnly).Route("/screen", func(r types.Router) {
2021-03-21 21:26:53 +01:00
r.Get("/", h.screenConfiguration)
2020-11-18 22:35:50 +01:00
r.With(auth.AdminsOnly).Post("/", h.screenConfigurationChange)
r.With(auth.AdminsOnly).Get("/configurations", h.screenConfigurationsList)
2021-03-21 21:26:53 +01:00
r.Get("/cast.jpg", h.screenCastGet)
r.With(auth.AdminsOnly).Get("/shot.jpg", h.screenShotGet)
2020-11-18 21:53:06 +01:00
})
2021-01-06 18:57:50 +01:00
2021-09-17 00:58:50 +02:00
r.With(h.uploadMiddleware).Route("/upload", func(r types.Router) {
2021-01-07 18:28:23 +01:00
r.Post("/drop", h.uploadDrop)
2021-01-18 10:34:33 +01:00
r.Post("/dialog", h.uploadDialogPost)
r.Delete("/dialog", h.uploadDialogClose)
2021-01-06 18:57:50 +01:00
})
2022-03-27 00:26:25 +01:00
2020-10-30 18:16:21 +01:00
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) uploadMiddleware(w http.ResponseWriter, r *http.Request) (context.Context, error) {
session, ok := auth.GetSession(r)
2022-03-27 00:08:06 +01:00
if !ok || (!session.IsHost() && (!session.Profile().CanHost || !h.sessions.Settings().ImplicitHosting)) {
2021-09-17 00:58:50 +02:00
return nil, utils.HttpForbidden("without implicit hosting, only host can upload files")
}
return nil, nil
}