144 lines
4.2 KiB
Go
144 lines
4.2 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
"unicode"
|
|
)
|
|
|
|
const (
|
|
activeWindowPath = "/v1/active-window"
|
|
maxWindowReportSize = 2048
|
|
maxDisplayDimension = 16384
|
|
)
|
|
|
|
// ActiveWindow describes the frontmost eligible window in the guest's main
|
|
// display coordinate system. Bounds are in screen points with a top-left origin.
|
|
type ActiveWindow struct {
|
|
ID uint32 `json:"id"`
|
|
AppName string `json:"app_name"`
|
|
X int `json:"x"`
|
|
Y int `json:"y"`
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
DisplayWidth int `json:"display_width"`
|
|
DisplayHeight int `json:"display_height"`
|
|
}
|
|
|
|
type windowReport struct {
|
|
window *ActiveWindow
|
|
receivedAt time.Time
|
|
}
|
|
|
|
// SetWindowReporting changes the transient reporting request for a registered VM.
|
|
func (s *Server) SetWindowReporting(vmKey string, enabled bool) bool {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
vm := s.vms[normalizeVMRegistryKey(vmKey)]
|
|
if vm == nil {
|
|
return false
|
|
}
|
|
if (vm.windowReportGen != 0) == enabled {
|
|
return true
|
|
}
|
|
vm.windowReportGen = 0
|
|
vm.windowReport = windowReport{}
|
|
if enabled {
|
|
s.nextWindowGen++
|
|
vm.windowReportGen = s.nextWindowGen
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ActiveWindow returns the latest eligible window if it arrived within maxAge.
|
|
func (s *Server) ActiveWindow(vmKey string, maxAge time.Duration) (ActiveWindow, bool) {
|
|
window, _, ok := s.ActiveWindowSample(vmKey, maxAge)
|
|
return window, ok
|
|
}
|
|
|
|
// ActiveWindowSample also returns the time the host received the report.
|
|
func (s *Server) ActiveWindowSample(vmKey string, maxAge time.Duration) (ActiveWindow, time.Time, bool) {
|
|
if maxAge <= 0 {
|
|
return ActiveWindow{}, time.Time{}, false
|
|
}
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
vm := s.vms[normalizeVMRegistryKey(vmKey)]
|
|
if vm == nil || vm.windowReportGen == 0 || vm.windowReport.window == nil ||
|
|
time.Since(vm.windowReport.receivedAt) > maxAge {
|
|
return ActiveWindow{}, time.Time{}, false
|
|
}
|
|
return *vm.windowReport.window, vm.windowReport.receivedAt, true
|
|
}
|
|
|
|
func (w ActiveWindow) valid() bool {
|
|
return w.ID != 0 && strings.TrimSpace(w.AppName) != "" && len(w.AppName) <= 256 &&
|
|
strings.IndexFunc(w.AppName, unicode.IsControl) < 0 &&
|
|
w.DisplayWidth > 0 && w.DisplayWidth <= maxDisplayDimension &&
|
|
w.DisplayHeight > 0 && w.DisplayHeight <= maxDisplayDimension &&
|
|
w.Width > 0 && w.Width <= maxDisplayDimension && w.Height > 0 && w.Height <= maxDisplayDimension &&
|
|
w.X > -w.Width && w.X < w.DisplayWidth && w.Y > -w.Height && w.Y < w.DisplayHeight
|
|
}
|
|
|
|
func (s *Server) handleActiveWindow(w http.ResponseWriter, r *http.Request) {
|
|
vm := s.authenticateToken(bearerToken(r))
|
|
if vm == nil {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", http.MethodPost)
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
if vm.windowReportGen == 0 {
|
|
http.Error(w, "window reporting is disabled", http.StatusForbidden)
|
|
return
|
|
}
|
|
controller := http.NewResponseController(w)
|
|
if controller.SetReadDeadline(time.Now().Add(3*time.Second)) == nil {
|
|
defer controller.SetReadDeadline(time.Time{})
|
|
}
|
|
var req struct {
|
|
Generation uint64 `json:"generation"`
|
|
Window *ActiveWindow `json:"window"`
|
|
}
|
|
dec := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxWindowReportSize))
|
|
dec.DisallowUnknownFields()
|
|
err := dec.Decode(&req)
|
|
if err == nil {
|
|
err = dec.Decode(&struct{}{})
|
|
if err == io.EOF {
|
|
err = nil
|
|
} else if err == nil {
|
|
err = errors.New("multiple JSON values")
|
|
}
|
|
}
|
|
if err != nil || (req.Window != nil && !req.Window.valid()) {
|
|
var sizeErr *http.MaxBytesError
|
|
if errors.As(err, &sizeErr) {
|
|
http.Error(w, "window report too large", http.StatusRequestEntityTooLarge)
|
|
return
|
|
}
|
|
http.Error(w, "invalid window report", http.StatusBadRequest)
|
|
return
|
|
}
|
|
s.mu.Lock()
|
|
cur := s.vms[vm.key]
|
|
current := cur != nil && cur.token == vm.token && vm.windowReportGen == req.Generation &&
|
|
cur.windowReportGen == req.Generation
|
|
if current {
|
|
cur.windowReport = windowReport{window: req.Window, receivedAt: time.Now()}
|
|
}
|
|
s.mu.Unlock()
|
|
if !current {
|
|
http.Error(w, "window report expired", http.StatusConflict)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|