200 lines
6.4 KiB
Go
200 lines
6.4 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
)
|
|
|
|
const (
|
|
consoleRouteQMP = "qmp"
|
|
consoleRouteDriver = "driver"
|
|
)
|
|
|
|
// consoleRoute says how the host reaches one VM's screen and input: through
|
|
// its own QMP console, or through the console its driver serves.
|
|
type consoleRoute struct {
|
|
kind string
|
|
vm driver.VMInfo
|
|
console driver.ConsoleDriver // set for consoleRouteDriver
|
|
}
|
|
|
|
// driverFor returns the registered driver that owns vm, or nil.
|
|
func (a *App) driverFor(vm driver.VMInfo) driver.Driver {
|
|
id := driver.CanonicalDriverID(vm.DriverID)
|
|
if id == "" {
|
|
id = driver.BuiltinReimsMacOSDriverID
|
|
}
|
|
a.mu.Lock()
|
|
router := a.driverRouter
|
|
a.mu.Unlock()
|
|
if router == nil {
|
|
return nil
|
|
}
|
|
drv, err := router.DriverByID(id)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return drv
|
|
}
|
|
|
|
// resolveConsoleRoute picks the console of vm. A driver that declares
|
|
// `consoleEndpoint: "driver"` and the `console` capability serves its own;
|
|
// every other VM needs a QMP console, which a hosted VM does not have.
|
|
func (a *App) resolveConsoleRoute(vm driver.VMInfo) (consoleRoute, error) {
|
|
if cd, ok := driver.DriverServesConsole(a.driverFor(vm)); ok && driver.HasCapability(vm.Capabilities, driver.CapConsole) {
|
|
return consoleRoute{kind: consoleRouteDriver, vm: vm, console: cd}, nil
|
|
}
|
|
if !driver.IsHosted(vm.Capabilities) && (len(vm.Capabilities) == 0 || driver.HasCapability(vm.Capabilities, driver.CapQMP)) {
|
|
return consoleRoute{kind: consoleRouteQMP, vm: vm}, nil
|
|
}
|
|
return consoleRoute{}, fmt.Errorf("computer_use_unavailable: driver %q has no console the host can reach", vm.DriverID)
|
|
}
|
|
|
|
// consoleCapture writes one full frame of the guest screen to a fresh temp
|
|
// file and returns its path and format. cleanup removes the file. External
|
|
// agent calls use this so disabling builtin.computer-use blocks QMP access.
|
|
func (a *App) consoleCapture(ctx context.Context, route consoleRoute, maxEdge int) (path, format string, cleanup func(), err error) {
|
|
return a.consoleCaptureWithMode(ctx, route, maxEdge, false)
|
|
}
|
|
|
|
// consoleCaptureInternal captures a frame for desktop-owned features
|
|
// (thumbnails and screenshot copy) using the internal QMP fallback when
|
|
// builtin.computer-use is disabled.
|
|
func (a *App) consoleCaptureInternal(ctx context.Context, route consoleRoute, maxEdge int) (path, format string, cleanup func(), err error) {
|
|
return a.consoleCaptureWithMode(ctx, route, maxEdge, true)
|
|
}
|
|
|
|
func (a *App) consoleCaptureWithMode(ctx context.Context, route consoleRoute, maxEdge int, internal bool) (path, format string, cleanup func(), err error) {
|
|
a.mu.Lock()
|
|
if a.cfg == nil {
|
|
a.mu.Unlock()
|
|
return "", "", nil, errors.New("application configuration is unavailable")
|
|
}
|
|
tempDir := a.cfg.TempDir()
|
|
a.mu.Unlock()
|
|
if err := os.MkdirAll(tempDir, 0o755); err != nil {
|
|
return "", "", nil, err
|
|
}
|
|
file, err := os.CreateTemp(tempDir, "console-*.frame")
|
|
if err != nil {
|
|
return "", "", nil, err
|
|
}
|
|
path = file.Name()
|
|
if err := file.Close(); err != nil {
|
|
_ = os.Remove(path)
|
|
return "", "", nil, err
|
|
}
|
|
cleanup = func() { _ = os.Remove(path) }
|
|
switch route.kind {
|
|
case consoleRouteDriver:
|
|
res, err := route.console.ConsoleScreenshot(ctx, route.vm.Name, driver.ConsoleScreenshotRequest{Path: path, MaxEdge: maxEdge})
|
|
if err != nil {
|
|
cleanup()
|
|
return "", "", nil, err
|
|
}
|
|
if res.Path != "" && !strings.EqualFold(filepath.Clean(res.Path), filepath.Clean(path)) {
|
|
// A driver may pick its own frame file. Accept it only as a plain
|
|
// file inside the data folder or the system temp folder, and let
|
|
// it keep the file: only the host's unused temp file goes away.
|
|
a.mu.Lock()
|
|
dataRoot := ""
|
|
if a.cfg != nil {
|
|
dataRoot = a.cfg.DataRoot
|
|
}
|
|
a.mu.Unlock()
|
|
own := res.Path
|
|
if !pathInsideAny(own, dataRoot, os.TempDir()) {
|
|
cleanup()
|
|
return "", "", nil, fmt.Errorf("driver %q wrote its frame outside the folders the host allows", route.vm.DriverID)
|
|
}
|
|
cleanup()
|
|
path, cleanup = own, func() {}
|
|
}
|
|
format = strings.ToLower(strings.TrimSpace(res.Format))
|
|
switch format {
|
|
case "":
|
|
format = "png"
|
|
case "png", "ppm":
|
|
default:
|
|
cleanup()
|
|
return "", "", nil, fmt.Errorf("driver %q returned an unsupported frame format %q", route.vm.DriverID, res.Format)
|
|
}
|
|
if st, err := os.Lstat(path); err != nil || !st.Mode().IsRegular() || st.Size() == 0 {
|
|
cleanup()
|
|
return "", "", nil, fmt.Errorf("driver %q wrote no frame", route.vm.DriverID)
|
|
}
|
|
return path, format, cleanup, nil
|
|
default:
|
|
service := a.computerUseInternalService()
|
|
if !internal {
|
|
var svcErr error
|
|
service, svcErr = a.computerUseService()
|
|
if svcErr != nil {
|
|
cleanup()
|
|
return "", "", nil, svcErr
|
|
}
|
|
}
|
|
if route.vm.Directory == "" {
|
|
cleanup()
|
|
return "", "", nil, fmt.Errorf("VM %q has no console directory", route.vm.Name)
|
|
}
|
|
format, err = service.Screendump(ctx, route.vm.Directory, path)
|
|
if err != nil {
|
|
cleanup()
|
|
return "", "", nil, err
|
|
}
|
|
return path, format, cleanup, nil
|
|
}
|
|
}
|
|
|
|
// consolePointer runs one pointer action on the VM's console.
|
|
func (a *App) consolePointer(ctx context.Context, route consoleRoute, req driver.ConsolePointerRequest) error {
|
|
if route.kind == consoleRouteDriver {
|
|
return route.console.ConsolePointer(ctx, route.vm.Name, req)
|
|
}
|
|
service, err := a.computerUseService()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dir := route.vm.Directory
|
|
switch strings.ToLower(strings.TrimSpace(req.Action)) {
|
|
case "move":
|
|
available, err := service.PointerAvailable(ctx, dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !available {
|
|
return errors.New("VM has no active absolute pointer")
|
|
}
|
|
return service.MouseMove(ctx, dir, req.X, req.Y, req.Width, req.Height)
|
|
case "button":
|
|
if req.Down == nil {
|
|
return errors.New("button action requires down")
|
|
}
|
|
return service.MouseButton(ctx, dir, req.Button, *req.Down)
|
|
case "click":
|
|
return service.MouseClick(ctx, dir, req.Button)
|
|
case "scroll":
|
|
return service.MouseScroll(ctx, dir, req.Delta, req.Horizontal)
|
|
}
|
|
return fmt.Errorf("unsupported pointer action %q", req.Action)
|
|
}
|
|
|
|
// consoleKey presses one key on the VM's console.
|
|
func (a *App) consoleKey(ctx context.Context, route consoleRoute, key string, modifiers []string) error {
|
|
if route.kind == consoleRouteDriver {
|
|
return route.console.ConsoleKey(ctx, route.vm.Name, driver.ConsoleKeyRequest{Key: key, Modifiers: modifiers})
|
|
}
|
|
service, err := a.computerUseService()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return service.PressKey(ctx, route.vm.Directory, key, modifiers...)
|
|
}
|