move server to server directory.

This commit is contained in:
Miroslav Šedivý
2024-06-23 17:48:14 +02:00
parent da45f62ca8
commit 5b98344205
211 changed files with 18 additions and 10 deletions

View File

@ -0,0 +1,70 @@
package room
import (
"net/http"
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
"github.com/demodesk/neko/pkg/utils"
)
type BroadcastStatusPayload struct {
URL string `json:"url,omitempty"`
IsActive bool `json:"is_active"`
}
func (h *RoomHandler) broadcastStatus(w http.ResponseWriter, r *http.Request) error {
broadcast := h.capture.Broadcast()
return utils.HttpSuccess(w, BroadcastStatusPayload{
IsActive: broadcast.Started(),
URL: broadcast.Url(),
})
}
func (h *RoomHandler) boradcastStart(w http.ResponseWriter, r *http.Request) error {
data := &BroadcastStatusPayload{}
if err := utils.HttpJsonRequest(w, r, data); err != nil {
return err
}
if data.URL == "" {
return utils.HttpBadRequest("missing broadcast URL")
}
broadcast := h.capture.Broadcast()
if broadcast.Started() {
return utils.HttpUnprocessableEntity("server is already broadcasting")
}
if err := broadcast.Start(data.URL); err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
h.sessions.AdminBroadcast(
event.BORADCAST_STATUS,
message.BroadcastStatus{
IsActive: broadcast.Started(),
URL: broadcast.Url(),
})
return utils.HttpSuccess(w)
}
func (h *RoomHandler) boradcastStop(w http.ResponseWriter, r *http.Request) error {
broadcast := h.capture.Broadcast()
if !broadcast.Started() {
return utils.HttpUnprocessableEntity("server is not broadcasting")
}
broadcast.Stop()
h.sessions.AdminBroadcast(
event.BORADCAST_STATUS,
message.BroadcastStatus{
IsActive: broadcast.Started(),
URL: broadcast.Url(),
})
return utils.HttpSuccess(w)
}

View File

@ -0,0 +1,107 @@
package room
import (
// TODO: Unused now.
//"bytes"
//"strings"
"net/http"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
)
type ClipboardPayload struct {
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
}
func (h *RoomHandler) clipboardGetText(w http.ResponseWriter, r *http.Request) error {
data, err := h.desktop.ClipboardGetText()
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
return utils.HttpSuccess(w, ClipboardPayload{
Text: data.Text,
HTML: data.HTML,
})
}
func (h *RoomHandler) clipboardSetText(w http.ResponseWriter, r *http.Request) error {
data := &ClipboardPayload{}
if err := utils.HttpJsonRequest(w, r, data); err != nil {
return err
}
err := h.desktop.ClipboardSetText(types.ClipboardText{
Text: data.Text,
HTML: data.HTML,
})
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
return utils.HttpSuccess(w)
}
func (h *RoomHandler) clipboardGetImage(w http.ResponseWriter, r *http.Request) error {
bytes, err := h.desktop.ClipboardGetBinary("image/png")
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/png")
_, err = w.Write(bytes)
return err
}
/* TODO: Unused now.
func (h *RoomHandler) clipboardSetImage(w http.ResponseWriter, r *http.Request) error {
err := r.ParseMultipartForm(MAX_UPLOAD_SIZE)
if err != nil {
return utils.HttpBadRequest("failed to parse multipart form").WithInternalErr(err)
}
//nolint
defer r.MultipartForm.RemoveAll()
file, header, err := r.FormFile("file")
if err != nil {
return utils.HttpBadRequest("no file received").WithInternalErr(err)
}
defer file.Close()
mime := header.Header.Get("Content-Type")
if !strings.HasPrefix(mime, "image/") {
return utils.HttpBadRequest("file must be image")
}
buffer := new(bytes.Buffer)
_, err = buffer.ReadFrom(file)
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err).WithInternalMsg("unable to read from uploaded file")
}
err = h.desktop.ClipboardSetBinary("image/png", buffer.Bytes())
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err).WithInternalMsg("unable set image to clipboard")
}
return utils.HttpSuccess(w)
}
func (h *RoomHandler) clipboardGetTargets(w http.ResponseWriter, r *http.Request) error {
targets, err := h.desktop.ClipboardGetTargets()
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
return utils.HttpSuccess(w, targets)
}
*/

View File

@ -0,0 +1,109 @@
package room
import (
"net/http"
"github.com/go-chi/chi"
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
"github.com/demodesk/neko/pkg/utils"
)
type ControlStatusPayload struct {
HasHost bool `json:"has_host"`
HostId string `json:"host_id,omitempty"`
}
type ControlTargetPayload struct {
ID string `json:"id"`
}
func (h *RoomHandler) controlStatus(w http.ResponseWriter, r *http.Request) error {
host, hasHost := h.sessions.GetHost()
var hostId string
if hasHost {
hostId = host.ID()
}
return utils.HttpSuccess(w, ControlStatusPayload{
HasHost: hasHost,
HostId: hostId,
})
}
func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
host, hasHost := h.sessions.GetHost()
if hasHost {
// TODO: Some throttling mechanism to prevent spamming.
// let host know that someone wants to take control
host.Send(
event.CONTROL_REQUEST,
message.SessionID{
ID: session.ID(),
})
return utils.HttpError(http.StatusAccepted, "control request sent")
}
if h.sessions.Settings().LockedControls && !session.Profile().IsAdmin {
return utils.HttpForbidden("controls are locked")
}
session.SetAsHost()
return utils.HttpSuccess(w)
}
func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
if !session.IsHost() {
return utils.HttpUnprocessableEntity("session is not the host")
}
h.desktop.ResetKeys()
session.ClearHost()
return utils.HttpSuccess(w)
}
func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
session.SetAsHost()
return utils.HttpSuccess(w)
}
func (h *RoomHandler) controlGive(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
sessionId := chi.URLParam(r, "sessionId")
target, ok := h.sessions.Get(sessionId)
if !ok {
return utils.HttpNotFound("target session was not found")
}
if !target.Profile().CanHost {
return utils.HttpBadRequest("target session is not allowed to host")
}
target.SetAsHostBy(session)
return utils.HttpSuccess(w)
}
func (h *RoomHandler) controlReset(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
_, hasHost := h.sessions.GetHost()
if hasHost {
h.desktop.ResetKeys()
session.ClearHost()
}
return utils.HttpSuccess(w)
}

View File

@ -0,0 +1,126 @@
package room
import (
"context"
"net/http"
"github.com/rs/zerolog/log"
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
)
type RoomHandler struct {
sessions types.SessionManager
desktop types.DesktopManager
capture types.CaptureManager
privateModeImage []byte
}
func New(
sessions types.SessionManager,
desktop types.DesktopManager,
capture types.CaptureManager,
) *RoomHandler {
h := &RoomHandler{
sessions: sessions,
desktop: desktop,
capture: capture,
}
// generate fallback image for private mode when needed
sessions.OnSettingsChanged(func(session types.Session, new, old types.Settings) {
if old.PrivateMode && !new.PrivateMode {
log.Debug().Msg("clearing private mode fallback image")
h.privateModeImage = nil
return
}
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
}
log.Debug().Msg("using private mode fallback image")
h.privateModeImage = bytes
}
})
return h
}
func (h *RoomHandler) Route(r types.Router) {
r.With(auth.AdminsOnly).Route("/settings", func(r types.Router) {
r.Post("/", h.settingsSet)
r.Get("/", h.settingsGet)
})
r.With(auth.AdminsOnly).Route("/broadcast", func(r types.Router) {
r.Get("/", h.broadcastStatus)
r.Post("/start", h.boradcastStart)
r.Post("/stop", h.boradcastStop)
})
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!
//r.Post("/image", h.clipboardSetImage)
// TODO: Refactor. If there would be implemented custom target
// retrieval, this endpoint would be useful.
//r.Get("/targets", h.clipboardGetTargets)
})
r.With(auth.CanHostOnly).Route("/keyboard", func(r types.Router) {
r.Get("/map", h.keyboardMapGet)
r.With(auth.HostsOnly).Post("/map", h.keyboardMapSet)
r.Get("/modifiers", h.keyboardModifiersGet)
r.With(auth.HostsOnly).Post("/modifiers", h.keyboardModifiersSet)
})
r.With(auth.CanHostOnly).Route("/control", func(r types.Router) {
r.Get("/", h.controlStatus)
r.Post("/request", h.controlRequest)
r.Post("/release", h.controlRelease)
r.With(auth.AdminsOnly).Post("/take", h.controlTake)
r.With(auth.AdminsOnly).Post("/give/{sessionId}", h.controlGive)
r.With(auth.AdminsOnly).Post("/reset", h.controlReset)
})
r.With(auth.CanWatchOnly).Route("/screen", func(r types.Router) {
r.Get("/", h.screenConfiguration)
r.With(auth.AdminsOnly).Post("/", h.screenConfigurationChange)
r.With(auth.AdminsOnly).Get("/configurations", h.screenConfigurationsList)
r.Get("/cast.jpg", h.screenCastGet)
r.With(auth.AdminsOnly).Get("/shot.jpg", h.screenShotGet)
})
r.With(h.uploadMiddleware).Route("/upload", func(r types.Router) {
r.Post("/drop", h.uploadDrop)
r.Post("/dialog", h.uploadDialogPost)
r.Delete("/dialog", h.uploadDialogClose)
})
}
func (h *RoomHandler) uploadMiddleware(w http.ResponseWriter, r *http.Request) (context.Context, error) {
session, ok := auth.GetSession(r)
if !ok || (!session.IsHost() && (!session.Profile().CanHost || !h.sessions.Settings().ImplicitHosting)) {
return nil, utils.HttpForbidden("without implicit hosting, only host can upload files")
}
return nil, nil
}

View File

@ -0,0 +1,47 @@
package room
import (
"net/http"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
)
func (h *RoomHandler) keyboardMapSet(w http.ResponseWriter, r *http.Request) error {
keyboardMap := types.KeyboardMap{}
if err := utils.HttpJsonRequest(w, r, &keyboardMap); err != nil {
return err
}
err := h.desktop.SetKeyboardMap(keyboardMap)
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
return utils.HttpSuccess(w)
}
func (h *RoomHandler) keyboardMapGet(w http.ResponseWriter, r *http.Request) error {
keyboardMap, err := h.desktop.GetKeyboardMap()
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
return utils.HttpSuccess(w, keyboardMap)
}
func (h *RoomHandler) keyboardModifiersSet(w http.ResponseWriter, r *http.Request) error {
keyboardModifiers := types.KeyboardModifiers{}
if err := utils.HttpJsonRequest(w, r, &keyboardModifiers); err != nil {
return err
}
h.desktop.SetKeyboardModifiers(keyboardModifiers)
return utils.HttpSuccess(w)
}
func (h *RoomHandler) keyboardModifiersGet(w http.ResponseWriter, r *http.Request) error {
keyboardModifiers := h.desktop.GetKeyboardModifiers()
return utils.HttpSuccess(w, keyboardModifiers)
}

View File

@ -0,0 +1,101 @@
package room
import (
"net/http"
"strconv"
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
"github.com/demodesk/neko/pkg/utils"
)
func (h *RoomHandler) screenConfiguration(w http.ResponseWriter, r *http.Request) error {
screenSize := h.desktop.GetScreenSize()
return utils.HttpSuccess(w, screenSize)
}
func (h *RoomHandler) screenConfigurationChange(w http.ResponseWriter, r *http.Request) error {
auth, _ := auth.GetSession(r)
data := &types.ScreenSize{}
if err := utils.HttpJsonRequest(w, r, data); err != nil {
return err
}
size, err := h.desktop.SetScreenSize(types.ScreenSize{
Width: data.Width,
Height: data.Height,
Rate: data.Rate,
})
if err != nil {
return utils.HttpUnprocessableEntity("cannot set screen size").WithInternalErr(err)
}
h.sessions.Broadcast(event.SCREEN_UPDATED, message.ScreenSizeUpdate{
ID: auth.ID(),
ScreenSize: size,
})
return utils.HttpSuccess(w, data)
}
// TODO: remove.
func (h *RoomHandler) screenConfigurationsList(w http.ResponseWriter, r *http.Request) error {
configurations := h.desktop.ScreenConfigurations()
return utils.HttpSuccess(w, configurations)
}
func (h *RoomHandler) screenShotGet(w http.ResponseWriter, r *http.Request) error {
quality, err := strconv.Atoi(r.URL.Query().Get("quality"))
if err != nil {
quality = 90
}
img := h.desktop.GetScreenshotImage()
bytes, err := utils.CreateJPGImage(img, quality)
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
_, err = w.Write(bytes)
return err
}
func (h *RoomHandler) screenCastGet(w http.ResponseWriter, r *http.Request) error {
// display fallback image when private mode is enabled even if screencast is not
if session, ok := auth.GetSession(r); ok && session.PrivateModeEnabled() {
if h.privateModeImage != nil {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
_, err := w.Write(h.privateModeImage)
return err
}
return utils.HttpBadRequest("private mode is enabled but no fallback image available")
}
screencast := h.capture.Screencast()
if !screencast.Enabled() {
return utils.HttpBadRequest("screencast pipeline is not enabled")
}
bytes, err := screencast.Image()
if err != nil {
return utils.HttpInternalServerError().WithInternalErr(err)
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Content-Type", "image/jpeg")
_, err = w.Write(bytes)
return err
}

View File

@ -0,0 +1,38 @@
package room
import (
"encoding/json"
"io"
"net/http"
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
)
func (h *RoomHandler) settingsGet(w http.ResponseWriter, r *http.Request) error {
settings := h.sessions.Settings()
return utils.HttpSuccess(w, settings)
}
func (h *RoomHandler) settingsSet(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
// We read the request body first and unmashal it inside the UpdateSettingsFunc
// to ensure atomicity of the operation.
body, err := io.ReadAll(r.Body)
if err != nil {
return utils.HttpBadRequest("unable to read request body").WithInternalErr(err)
}
h.sessions.UpdateSettingsFunc(session, func(settings *types.Settings) bool {
err = json.Unmarshal(body, settings)
return err == nil
})
if err != nil {
return utils.HttpBadRequest("unable to parse provided data").WithInternalErr(err)
}
return utils.HttpSuccess(w)
}

View File

@ -0,0 +1,172 @@
package room
import (
"io"
"net/http"
"os"
"path"
"strconv"
"github.com/demodesk/neko/pkg/utils"
)
// TODO: Extract file uploading to custom utility.
// maximum upload size of 32 MB
const maxUploadSize = 32 << 20
func (h *RoomHandler) uploadDrop(w http.ResponseWriter, r *http.Request) error {
if !h.desktop.IsUploadDropEnabled() {
return utils.HttpBadRequest("upload drop is disabled")
}
err := r.ParseMultipartForm(maxUploadSize)
if err != nil {
return utils.HttpBadRequest("failed to parse multipart form").WithInternalErr(err)
}
//nolint
defer r.MultipartForm.RemoveAll()
X, err := strconv.Atoi(r.FormValue("x"))
if err != nil {
return utils.HttpBadRequest("no X coordinate received").WithInternalErr(err)
}
Y, err := strconv.Atoi(r.FormValue("y"))
if err != nil {
return utils.HttpBadRequest("no Y coordinate received").WithInternalErr(err)
}
req_files := r.MultipartForm.File["files"]
if len(req_files) == 0 {
return utils.HttpBadRequest("no files received")
}
dir, err := os.MkdirTemp("", "neko-drop-*")
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to create temporary directory")
}
files := []string{}
for _, req_file := range req_files {
path := path.Join(dir, req_file.Filename)
srcFile, err := req_file.Open()
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to open uploaded file")
}
defer srcFile.Close()
dstFile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to open destination file")
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to copy uploaded file to destination file")
}
files = append(files, path)
}
if !h.desktop.DropFiles(X, Y, files) {
return utils.HttpInternalServerError().
WithInternalMsg("unable to drop files")
}
return utils.HttpSuccess(w)
}
func (h *RoomHandler) uploadDialogPost(w http.ResponseWriter, r *http.Request) error {
if !h.desktop.IsFileChooserDialogEnabled() {
return utils.HttpBadRequest("file chooser dialog is disabled")
}
err := r.ParseMultipartForm(maxUploadSize)
if err != nil {
return utils.HttpBadRequest("failed to parse multipart form").WithInternalErr(err)
}
//nolint
defer r.MultipartForm.RemoveAll()
req_files := r.MultipartForm.File["files"]
if len(req_files) == 0 {
return utils.HttpBadRequest("no files received")
}
if !h.desktop.IsFileChooserDialogOpened() {
return utils.HttpUnprocessableEntity("file chooser dialog is not open")
}
dir, err := os.MkdirTemp("", "neko-dialog-*")
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to create temporary directory")
}
for _, req_file := range req_files {
path := path.Join(dir, req_file.Filename)
srcFile, err := req_file.Open()
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to open uploaded file")
}
defer srcFile.Close()
dstFile, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to open destination file")
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to copy uploaded file to destination file")
}
}
if err := h.desktop.HandleFileChooserDialog(dir); err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to handle file chooser dialog")
}
return utils.HttpSuccess(w)
}
func (h *RoomHandler) uploadDialogClose(w http.ResponseWriter, r *http.Request) error {
if !h.desktop.IsFileChooserDialogEnabled() {
return utils.HttpBadRequest("file chooser dialog is disabled")
}
if !h.desktop.IsFileChooserDialogOpened() {
return utils.HttpUnprocessableEntity("file chooser dialog is not open")
}
h.desktop.CloseFileChooserDialog()
return utils.HttpSuccess(w)
}