2020-10-31 06:16:21 +13:00
|
|
|
package room
|
|
|
|
|
|
|
|
import (
|
2021-01-09 11:42:53 +13:00
|
|
|
"net/http"
|
|
|
|
|
2020-10-31 06:16:21 +13:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
|
|
|
|
"demodesk/neko/internal/types"
|
2020-11-17 07:37:52 +13:00
|
|
|
"demodesk/neko/internal/http/auth"
|
2021-01-09 11:42:53 +13:00
|
|
|
"demodesk/neko/internal/utils"
|
2020-10-31 06:16:21 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type RoomHandler struct {
|
2020-12-09 02:31:15 +13:00
|
|
|
sessions types.SessionManager
|
|
|
|
desktop types.DesktopManager
|
|
|
|
capture types.CaptureManager
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(
|
|
|
|
sessions types.SessionManager,
|
2020-11-02 04:54:06 +13:00
|
|
|
desktop types.DesktopManager,
|
|
|
|
capture types.CaptureManager,
|
2020-10-31 06:16:21 +13:00
|
|
|
) *RoomHandler {
|
2020-12-09 02:31:15 +13:00
|
|
|
// Init
|
2020-10-31 06:16:21 +13:00
|
|
|
|
|
|
|
return &RoomHandler{
|
2020-12-09 02:31:15 +13:00
|
|
|
sessions: sessions,
|
|
|
|
desktop: desktop,
|
|
|
|
capture: capture,
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 06:16:25 +13:00
|
|
|
func (h *RoomHandler) Route(r chi.Router) {
|
2020-11-19 09:56:42 +13:00
|
|
|
r.With(auth.AdminsOnly).Route("/broadcast", func(r chi.Router) {
|
2020-11-19 10:35:50 +13:00
|
|
|
r.Get("/", h.broadcastStatus)
|
|
|
|
r.Post("/start", h.boradcastStart)
|
|
|
|
r.Post("/stop", h.boradcastStop)
|
2020-10-31 22:48:24 +13:00
|
|
|
})
|
2020-10-31 06:16:21 +13:00
|
|
|
|
2020-11-17 07:37:52 +13:00
|
|
|
r.With(auth.HostsOnly).Route("/clipboard", func(r chi.Router) {
|
2020-11-19 10:35:50 +13:00
|
|
|
r.Get("/", h.clipboardRead)
|
|
|
|
r.Post("/", h.clipboardWrite)
|
2020-11-01 00:35:02 +13:00
|
|
|
})
|
2020-11-17 10:48:20 +13:00
|
|
|
|
2021-01-13 11:35:46 +13:00
|
|
|
r.Route("/keyboard", func(r chi.Router) {
|
2021-01-16 04:53:03 +13:00
|
|
|
r.Get("/map", h.keyboardMapGet)
|
|
|
|
r.With(auth.HostsOnly).Post("/map", h.keyboardMapSet)
|
2021-01-13 11:35:46 +13:00
|
|
|
|
|
|
|
r.Get("/modifiers", h.keyboardModifiersGet)
|
|
|
|
r.With(auth.HostsOnly).Post("/modifiers", h.keyboardModifiersSet)
|
2020-11-19 09:56:42 +13:00
|
|
|
})
|
|
|
|
|
2020-11-17 10:48:20 +13:00
|
|
|
r.Route("/control", func(r chi.Router) {
|
2020-11-19 11:14:28 +13:00
|
|
|
r.Get("/", h.controlStatus)
|
2020-11-19 10:35:50 +13:00
|
|
|
r.Post("/request", h.controlRequest)
|
|
|
|
r.Post("/release", h.controlRelease)
|
2020-11-19 09:10:40 +13:00
|
|
|
|
2020-11-19 10:35:50 +13:00
|
|
|
r.With(auth.AdminsOnly).Post("/take", h.controlTake)
|
|
|
|
r.With(auth.AdminsOnly).Post("/give", h.controlGive)
|
2020-11-30 03:58:26 +13:00
|
|
|
r.With(auth.AdminsOnly).Post("/reset", h.controlReset)
|
2020-11-17 10:48:20 +13:00
|
|
|
})
|
2020-11-19 09:34:39 +13:00
|
|
|
|
2020-11-19 09:56:42 +13:00
|
|
|
r.Route("/screen", func(r chi.Router) {
|
2020-11-19 10:35:50 +13:00
|
|
|
r.Get("/", h.screenConfiguration)
|
2020-11-19 09:53:06 +13:00
|
|
|
|
2020-11-19 10:35:50 +13:00
|
|
|
r.With(auth.AdminsOnly).Post("/", h.screenConfigurationChange)
|
|
|
|
r.With(auth.AdminsOnly).Get("/configurations", h.screenConfigurationsList)
|
2020-11-19 09:53:06 +13:00
|
|
|
})
|
2021-01-07 06:57:50 +13:00
|
|
|
|
2021-01-09 11:42:53 +13:00
|
|
|
r.With(h.uploadMiddleware).Route("/upload", func(r chi.Router) {
|
2021-01-08 06:28:23 +13:00
|
|
|
r.Post("/drop", h.uploadDrop)
|
2021-01-07 06:57:50 +13:00
|
|
|
})
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|
2021-01-09 11:42:53 +13:00
|
|
|
|
|
|
|
func (h *RoomHandler) uploadMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session := auth.GetSession(r)
|
2021-01-10 01:42:08 +13:00
|
|
|
if !session.IsHost() && (!session.CanHost() || !h.sessions.ImplicitHosting()) {
|
2021-01-09 11:42:53 +13:00
|
|
|
utils.HttpForbidden(w, "Without implicit hosting, only host can upload files.")
|
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|