From 756c156486e0d69ac87e351d8429af457369d2f1 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Thu, 22 Oct 2026 13:14:53 -0700 Subject: [PATCH] Unify VM computer automation and redesign AI/MCP settings Add platform workers, bounded desktop/UI/command/file tools, and owner-scoped disposable VM creation with quotas and idle expiry. Prepare Linux and Windows guests through existing provisioning, retain legacy console support, and preserve account identity during Windows personalization. Assisted-by: gpt-6-astra Assisted-by: gpt-6.1-sol Assisted-by: claude-opus-5.5 --- dashboard/src/computer-tools.json | 631 ++++++++++++++++++++++++++++ dashboard/src/computer.rs | 339 +++++++++++++-- dashboard/src/host-protocol.json | 2 +- dashboard/src/host.rs | 2 +- dashboard/src/mcp.rs | 20 +- dashboard/web/components/TabBar.tsx | 10 + dashboard/web/pages/AI.tsx | 83 ++-- dashboard/web/pages/MCP.css | 223 +++++++--- dashboard/web/pages/MCP.tsx | 183 +++++--- dashboard/web/pages/MCPAccess.tsx | 50 ++- dashboard/web/pages/MCPConsent.tsx | 52 ++- dashboard/web/styles.css | 13 + dashboard/web/types/mcp.ts | 2 +- flake.nix | 1 + guest/DESIGN.md | 2 +- guest/computer-legacy-mac.py | 170 ++++++++ guest/computer-linux-launch | 7 + guest/computer-linux.nix | 7 + guest/computer-linux.py | 91 ++++ guest/computer-windows.cs | 249 +++++++++++ guest/windows-provision.ps1 | 67 ++- nixos/computer-ahk.nix | 11 + nixos/configuration.nix | 25 +- nixos/dashboard.nix | 1 + tools/computer-test.py | 298 +++++++++++++ tools/dashboard-host.py | 22 +- tools/dashboard-stage-test.py | 26 +- tools/dashboard-stage.py | 44 +- tools/dashboard-vms-test.py | 6 +- tools/deploy.py | 6 +- tools/mac-agent-gui.swift | 152 ++++++- tools/mac-agent-vm.md | 3 + tools/mac-computer.py | 297 +++++++++++-- tools/mac-vm-bridge.py | 5 + tools/mac-vms.py | 6 +- tools/release.py | 2 +- tools/vm-ai.py | 132 ++++++ tools/vm-computer.py | 556 ++++++++++++++++++++++++ tools/vm-rfb.py | 160 +++++++ tools/vm-screen.py | 149 +------ tools/vms.py | 137 ++++-- 41 files changed, 3759 insertions(+), 483 deletions(-) create mode 100644 dashboard/src/computer-tools.json create mode 100644 guest/computer-legacy-mac.py create mode 100644 guest/computer-linux-launch create mode 100644 guest/computer-linux.nix create mode 100644 guest/computer-linux.py create mode 100644 guest/computer-windows.cs create mode 100644 nixos/computer-ahk.nix create mode 100644 tools/computer-test.py create mode 100644 tools/vm-ai.py create mode 100644 tools/vm-computer.py create mode 100644 tools/vm-rfb.py diff --git a/dashboard/src/computer-tools.json b/dashboard/src/computer-tools.json new file mode 100644 index 0000000000000000000000000000000000000000..7fb2d32bcfc145d79b5aae1517fb3adb72394cfe --- /dev/null +++ b/dashboard/src/computer-tools.json @@ -0,0 +1,631 @@ +[ + { + "name": "computer_list", + "description": "List VMs granted to this connection, with their operating systems and current power state.", + "inputSchema": { + "type": "object", + "properties": {}, + "required": [], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": true + } + }, + { + "name": "computer_status", + "description": "Report the selected VM’s available runtimes, native UI support, permissions and desktop capabilities.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + } + }, + "required": [ + "vm" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": true + } + }, + { + "name": "computer_shot", + "description": "Capture the VM desktop as an image. Input coordinates are pixels in this screenshot, with (0,0) at its top left.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + } + }, + "required": [ + "vm" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": true + } + }, + { + "name": "computer_input", + "description": "Perform an ordered batch of desktop actions on Windows, macOS or Linux. Coordinates use screenshot pixels. Key chords use Control, Alt, Shift, Meta (Command on Mac, Windows/Super elsewhere), Enter, Tab, Escape, arrows, Home/End, PageUp/PageDown, F1–F12, or one ASCII character. Use text for literal Unicode and key for shortcuts; legacy Windows text supports ASCII on a US keyboard. Returns an optional screenshot after the batch. Stops at the first error; preceding actions may already have run.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "actions": { + "type": "array", + "items": { + "oneOf": [ + { + "type": "object", + "properties": { + "type": { + "const": "click" + }, + "x": { + "type": "integer", + "minimum": 0 + }, + "y": { + "type": "integer", + "minimum": 0 + }, + "button": { + "enum": [ + "left", + "right", + "middle" + ] + }, + "count": { + "type": "integer", + "minimum": 1, + "maximum": 3 + } + }, + "required": [ + "type", + "x", + "y" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "type": { + "const": "move" + }, + "x": { + "type": "integer", + "minimum": 0 + }, + "y": { + "type": "integer", + "minimum": 0 + } + }, + "required": [ + "type", + "x", + "y" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "type": { + "const": "drag" + }, + "x": { + "type": "integer", + "minimum": 0 + }, + "y": { + "type": "integer", + "minimum": 0 + }, + "to_x": { + "type": "integer", + "minimum": 0 + }, + "to_y": { + "type": "integer", + "minimum": 0 + }, + "button": { + "enum": [ + "left", + "right", + "middle" + ] + }, + "duration_ms": { + "type": "integer", + "minimum": 1, + "maximum": 5000 + } + }, + "required": [ + "type", + "x", + "y", + "to_x", + "to_y" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "type": { + "const": "scroll" + }, + "x": { + "type": "integer", + "minimum": 0 + }, + "y": { + "type": "integer", + "minimum": 0 + }, + "direction": { + "enum": [ + "up", + "down", + "left", + "right" + ] + }, + "amount": { + "type": "integer", + "minimum": 1, + "maximum": 100 + } + }, + "required": [ + "type", + "x", + "y", + "direction" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "type": { + "const": "key" + }, + "keys": { + "type": "array", + "items": { + "type": "string" + }, + "minItems": 1, + "maxItems": 8 + } + }, + "required": [ + "type", + "keys" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "type": { + "const": "text" + }, + "text": { + "type": "string", + "maxLength": 16384 + } + }, + "required": [ + "type", + "text" + ], + "additionalProperties": false + }, + { + "type": "object", + "properties": { + "type": { + "const": "wait" + }, + "duration_ms": { + "type": "integer", + "minimum": 1, + "maximum": 5000 + } + }, + "required": [ + "type", + "duration_ms" + ], + "additionalProperties": false + } + ] + }, + "minItems": 1, + "maxItems": 100 + }, + "screenshot": { + "type": "boolean", + "default": true + } + }, + "required": [ + "vm", + "actions" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + }, + { + "name": "computer_ui", + "description": "Read a bounded native accessibility tree: macOS AX, Windows UI Automation or Linux AT-SPI. Nodes include names, roles, values, targets, available actions, and screen bounds where supported. Optionally restrict to an application PID. Use a fresh returned target with computer_ui_action; custom-drawn apps may need screenshot/input.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "pid": { + "type": "integer", + "minimum": 1 + } + }, + "required": [ + "vm" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": true + } + }, + { + "name": "computer_ui_action", + "description": "Act on a native UI element from computer_ui. Pass its target unchanged and choose an action it reports, such as press, AXPress, focus, toggle, select or set_value. Read a fresh tree if the element changes.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "target": { + "type": "object" + }, + "operation": { + "type": "string" + }, + "value": { + "type": "string" + }, + "screenshot": { + "type": "boolean", + "default": true + } + }, + "required": [ + "vm", + "target", + "operation" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + }, + { + "name": "computer_exec", + "description": "Run a script in the VM’s logged-in desktop session. Runtimes come from computer_status: zsh/bash on Unix, PowerShell/cmd/AutoHotkey v2 on Windows. Default uses the platform shell. AHK sets Mouse/Pixel CoordMode to Screen; SendText types literal text and FileAppend(text,\"*\") writes stdout. Returns bounded stdout/stderr, exit status, timeout state and optional screenshot. Timeout stops the command group; effects already performed remain. Use computer_spawn for persistent apps.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "script": { + "type": "string", + "maxLength": 65536 + }, + "runtime": { + "type": "string", + "enum": [ + "default", + "zsh", + "bash", + "powershell", + "cmd", + "ahk" + ], + "default": "default" + }, + "timeout_ms": { + "type": "integer", + "minimum": 1, + "maximum": 180000, + "default": 60000 + }, + "screenshot": { + "type": "boolean", + "default": true + } + }, + "required": [ + "vm", + "script" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + }, + { + "name": "computer_spawn", + "description": "Launch a persistent app or command in the VM desktop. Returns a process handle for computer_process. Standard input/output are detached; use computer_exec when output is needed.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "script": { + "type": "string", + "maxLength": 65536 + }, + "runtime": { + "type": "string", + "enum": [ + "default", + "zsh", + "bash", + "powershell", + "cmd", + "ahk" + ], + "default": "default" + } + }, + "required": [ + "vm", + "script" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + }, + { + "name": "computer_process", + "description": "Inspect or stop a process launched by computer_spawn. Use its returned id; handles reject PID reuse.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "id": { + "type": "string" + }, + "operation": { + "enum": [ + "status", + "stop" + ] + } + }, + "required": [ + "vm", + "id", + "operation" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + }, + { + "name": "computer_file", + "description": "Inspect a guest path, list a directory, create directories, or remove one file. Paths are absolute inside the selected VM. Use computer_upload/download for file contents.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "path": { + "type": "string" + }, + "operation": { + "enum": [ + "stat", + "list", + "mkdir", + "remove" + ] + } + }, + "required": [ + "vm", + "path", + "operation" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + }, + { + "name": "computer_upload", + "description": "Create a one-use HTTP PUT URL for uploading a local file into the VM without putting bytes in model context. Send the raw file body to the URL within 10 minutes. At most 64 MiB; destination replacement requires overwrite=true. The upload commits only after all declared bytes arrive.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "path": { + "type": "string" + }, + "size": { + "type": "integer", + "minimum": 0, + "maximum": 67108864 + }, + "overwrite": { + "type": "boolean", + "default": false + } + }, + "required": [ + "vm", + "path", + "size" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + }, + { + "name": "computer_download", + "description": "Create a one-use HTTP GET URL for downloading a guest file to the caller’s filesystem without putting bytes in model context. URL expires after 10 minutes and remains subject to this connection’s access. At most 64 MiB.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string", + "description": "VM name from computer_list." + }, + "path": { + "type": "string" + } + }, + "required": [ + "vm", + "path" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": true + } + }, + { + "name": "computer_presets", + "description": "List approved Linux and Windows presets and enforced test-VM limits. Requires computer:create permission.", + "inputSchema": { + "type": "object", + "properties": {}, + "required": [], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": true + } + }, + { + "name": "computer_create", + "description": "Create a disposable VM from an approved preset. It belongs to your account, is tagged AI-owned, receives an ai- name, and is granted to this connection. Deleted after one hour without targeted tool calls. Maximum 2 per user, 8 per host, 4 vCPUs and 8 GiB RAM per VM. Base images stay unchanged.", + "inputSchema": { + "type": "object", + "properties": { + "preset": { + "type": "string" + }, + "name": { + "type": "string", + "pattern": "^[a-z0-9][a-z0-9-]{0,35}$" + }, + "vcpus": { + "type": "integer", + "minimum": 1, + "maximum": 4, + "default": 2 + }, + "memory_gib": { + "type": "integer", + "minimum": 1, + "maximum": 8, + "default": 4 + } + }, + "required": [ + "preset", + "name" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + }, + { + "name": "computer_vm", + "description": "Start, shut down, or reboot a granted VM. For AI-owned test VMs only: extend idle expiry by up to 24 hours from now, or delete the VM and its disposable disk. Listing VMs does not extend expiry.", + "inputSchema": { + "type": "object", + "properties": { + "vm": { + "type": "string" + }, + "operation": { + "type": "string", + "enum": [ + "start", + "shutdown", + "reboot", + "extend", + "delete" + ] + }, + "seconds": { + "type": "integer", + "minimum": 1, + "maximum": 86400, + "default": 3600 + } + }, + "required": [ + "vm", + "operation" + ], + "additionalProperties": false + }, + "annotations": { + "readOnlyHint": false + } + } +] diff --git a/dashboard/src/computer.rs b/dashboard/src/computer.rs index 2eeb4e7d18c820cca2c54f49547833a29901cb86..ece0d30cf7293a382311b0ca759bc47918cd06d9 100644 --- a/dashboard/src/computer.rs +++ b/dashboard/src/computer.rs @@ -4,19 +4,18 @@ use rmcp::{ model::{ CacheScope, CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock, ListToolsResult, PaginatedRequestParams, ServerCapabilities, ServerConfig, Tool, - ToolAnnotations, }, service::RequestContext, }; pub async fn resources(owner: &Value) -> Result> { - let domains = host::call(json!({"operation":"vm.domains"})).await?; + let domains = host::call(json!({"operation":"vm.computers"})).await?; Ok(array(&domains) .iter() .filter(|vm| { vm["owner"] == owner["id"] && array(&vm["capabilities"]).iter().any(|c| c == "computer") }) - .map(|vm| json!({"id":vm["name"],"name":vm["name"],"description":vm["description"]})) + .map(|vm| json!({"id":vm["name"],"name":vm["name"],"platform":vm["platform"],"os":vm["os"],"arch":vm["arch"],"state":vm["state"],"aiOwned":vm["aiOwned"] == true,"expiresAt":vm["expiresAt"]})) .collect()) } @@ -34,8 +33,68 @@ fn permitted(grant: &Value, vm: &str, scope: &str, owner: &Value) -> Result<()> Ok(()) } +struct Transfer { + headers: HeaderMap, + vm: String, + path: String, + size: u64, + upload: bool, + overwrite: bool, + expires: std::time::Instant, +} + +struct UploadCleanup { + vm: String, + path: Option, +} + +impl Drop for UploadCleanup { + fn drop(&mut self) { + if let Some(path) = self.path.take() { + let vm = self.vm.clone(); + tokio::spawn(async move { + let deadline = tokio::time::Instant::now() + Duration::from_secs(240); + loop { + match dispatch(&vm, json!({"action":"file","operation":"remove","path":path})).await { + Err(error) if error.status == 409 && tokio::time::Instant::now() < deadline => { + tokio::time::sleep(Duration::from_secs(1)).await; + } + _ => break, + } + } + }); + } + } +} + #[derive(Clone)] -struct Computer; +struct Computer { + app: Arc, + transfers: Arc>>, + transfer_slots: Arc, +} + +fn scope(action: &str, arguments: &serde_json::Map) -> &'static str { + if matches!(action, "status" | "shot" | "ui") + || (action == "process" + && arguments.get("operation").and_then(Value::as_str) == Some("status")) + { + "computer:read" + } else { + "computer:control" + } +} + +async fn dispatch(vm: &str, operation: Value) -> Result { + let value = + host::call(json!({"operation":"vm.computer","payload":{"name":vm,"request":operation}})) + .await?; + if let Some(error) = value["error"].as_str() { + return Err(Error::new(400, error)); + } + Ok(value) +} + impl ServerHandler for Computer { fn get_info(&self) -> ServerConfig { ServerConfig::new(ServerCapabilities::builder().enable_tools().build()) @@ -45,16 +104,8 @@ impl ServerHandler for Computer { _: Option, _: RequestContext, ) -> std::result::Result { - let tools = [ - ("computer_list", "List granted VMs with computer-use support.", json!({}), json!([])), - ("computer_status", "Check a VM's desktop permissions and scripting capabilities.", json!({"vm":{"type":"string"}}), json!(["vm"])), - ("computer_shot", "Capture the selected VM's current desktop as an image.", json!({"vm":{"type":"string"}}), json!(["vm"])), - ("computer_ui", "Read a bounded macOS accessibility tree, including roles, values, bounds and available actions. Use an application PID to inspect that app, or omit it for the frontmost app. Read a fresh tree before using its child paths.", json!({"vm":{"type":"string"},"pid":{"type":"integer","minimum":1}}), json!(["vm"])), - ("computer_exec", "Run a zsh script inside the selected Mac VM's logged-in desktop. Returns exit status, stdout/stderr (64 KiB each), and optionally a screenshot. Batch actions into one script. Use /usr/local/bin/agent-gui tree [PID], press PID 0.1.2, click X Y, key CODE [command|shift|option|control], and type - (UTF-8 stdin). Coordinates use macOS screen points; compare screenshot dimensions with display bounds before pixel input. sudo -n is available for guest setup. Timeout kills the script process group. For long-lived apps, redirect stdin/stdout/stderr before backgrounding. Never assume a timed-out command did not run.", json!({"vm":{"type":"string"},"script":{"type":"string","maxLength":65536},"timeout_ms":{"type":"integer","minimum":1,"maximum":180000,"default":60000},"screenshot":{"type":"boolean","default":true}}), json!(["vm","script"])), - ].into_iter().map(|(name, description, properties, required)| { - Tool::new(name, description, json!({"type":"object","properties":properties,"required":required,"additionalProperties":false}).as_object().unwrap().clone()) - .with_annotations(ToolAnnotations::new().read_only(name != "computer_exec")) - }).collect(); + let tools: Vec = serde_json::from_str(include_str!("computer-tools.json")) + .expect("computer tool schema"); Ok(ListToolsResult { tools, ttl_ms: Some(0), @@ -68,10 +119,39 @@ impl ServerHandler for Computer { context: RequestContext, ) -> std::result::Result { let result: Result = async { - let grant = &context.extensions.get::() - .and_then(|p| p.extensions.get::()) + let parts = context.extensions.get::() + .ok_or_else(|| Error::new(401, "This connection expired. Connect again."))?; + let grant = &parts.extensions.get::() .ok_or_else(|| Error::new(401, "This connection expired. Connect again."))?.0; let mut arguments = request.arguments.unwrap_or_default(); + if matches!(request.name.as_ref(), "computer_presets" | "computer_create") { + if !array(&grant["scopes"]).iter().any(|scope| scope == "computer:create") { + return Err(Error::new(403, "Reconnect with permission to create test VMs.")); + } + if request.name == "computer_presets" { + if !arguments.is_empty() { return Err(Error::new(400, "This tool takes no arguments.")); } + return host::call(json!({"operation":"vm.ai","payload":{"action":"presets","owner":grant["user"]}})).await; + } + if arguments.keys().any(|key| !["preset","name","vcpus","memory_gib"].contains(&key.as_str())) { + return Err(Error::new(400, "Use the fields listed for creating a test VM.")); + } + let created = host::call(json!({"operation":"vm.ai","payload":{"action":"create","owner":grant["user"], + "preset":arguments.get("preset"),"name":arguments.get("name"), + "vcpus":arguments.get("vcpus").unwrap_or(&json!(2)),"memory":arguments.get("memory_gib").unwrap_or(&json!(4))}})).await?; + let mut db = self.app.mcp.db.lock().unwrap(); + let tx = db.transaction()?; + let key = format!("grant:{}",string(&grant["id"])); + let mut current = mcp::get(&tx,&key)?; + if current["user"] != grant["user"] || !array(¤t["scopes"]).iter().any(|scope| scope == "computer:create") { + return Err(Error::new(403, "The connection changed while the VM was starting. The test VM will expire automatically.")); + } + let mut selected = array(¤t["resources"]).to_vec(); + selected.push(created["id"].clone()); + current["resources"] = json!(selected); + mcp::put(&tx,&key,¤t,0)?; + tx.commit()?; + return Ok(created); + } if request.name == "computer_list" { if !arguments.is_empty() { return Err(Error::new(400, "This tool takes no arguments.")); } return Ok(json!({"computers":resources(&json!({"id":grant["user"]})).await?.into_iter() @@ -83,20 +163,73 @@ impl ServerHandler for Computer { if !array(&grant["resources"]).iter().any(|id| id == &vm) { return Err(Error::new(403, "Choose a VM granted to this connection.")); } - let scope = if request.name == "computer_exec" { "computer:control" } else { "computer:read" }; + let action = request.name.strip_prefix("computer_").ok_or_else(|| Error::new(404,"No tool with that name."))?; let owner = host::call(json!({"operation":"vm.owner","payload":{"name":vm}})).await?; - permitted(grant, &vm, scope, &owner)?; - let operation = match request.name.as_ref() { - "computer_status" => json!({"action":"status"}), - "computer_shot" => json!({"action":"shot"}), - "computer_ui" => json!({"action":"ui","pid":arguments.remove("pid")}), - "computer_exec" => json!({"action":"exec", "script":arguments.remove("script"), - "timeout_ms":arguments.remove("timeout_ms").unwrap_or(json!(60000)), - "screenshot":arguments.remove("screenshot").unwrap_or(json!(true))}), - _ => return Err(Error::new(404, "No tool with that name.")), + permitted(grant, &vm, scope(action,&arguments), &owner)?; + if action == "vm" { + if arguments.keys().any(|key| !["operation","seconds"].contains(&key.as_str())) { + return Err(Error::new(400,"Use the fields listed for this VM operation.")); + } + return match arguments.get("operation").and_then(Value::as_str) { + Some(operation @ ("extend" | "delete")) => host::call(json!({"operation":"vm.ai","payload":{ + "action":operation,"name":vm,"owner":grant["user"],"seconds":arguments.get("seconds").unwrap_or(&json!(3600))}})).await, + Some(operation @ ("start" | "shutdown" | "reboot")) if !arguments.contains_key("seconds") => { + host::call(json!({"operation":"vm.act","payload":{"name":vm,"action":operation}})).await?; + Ok(json!({"id":vm,"requested":operation})) + }, + _ => Err(Error::new(400,"Choose start, shutdown, reboot, extend, or delete.")), + }; + } + if matches!(action,"upload"|"download") { + let path = arguments.remove("path").and_then(|v| v.as_str().map(str::to_owned)) + .filter(|p| !p.is_empty() && p.len() <= 4096 && !p.contains('\0')) + .ok_or_else(|| Error::new(400,"Choose an absolute guest file path."))?; + let upload = action == "upload"; + let overwrite = arguments.remove("overwrite").unwrap_or(json!(false)); + if !overwrite.is_boolean() { return Err(Error::new(400,"Choose whether to replace the destination.")); } + let size = if upload { + arguments.remove("size").and_then(|v|v.as_u64()).ok_or_else(||Error::new(400,"Provide the upload size in bytes."))? + } else { + let info = dispatch(&vm,json!({"action":"file","operation":"stat","path":path})).await?; + if info["directory"] == true { return Err(Error::new(400,"Choose a file to download.")); } + info["size"].as_u64().ok_or_else(||Error::new(400,"The guest did not report a file size."))? + }; + if size > 64*1024*1024 || !arguments.is_empty() { return Err(Error::new(400,"Transfer a file of at most 64 MiB using the listed fields.")); } + if upload { + let parent = path.rfind(['/', '\\']).filter(|index| index + 1 < path.len()) + .map(|index| &path[..=index]).ok_or_else(|| Error::new(400,"Choose an absolute guest file path."))?; + let info = dispatch(&vm,json!({"action":"file","operation":"stat","path":parent})).await?; + if info["directory"] != true { return Err(Error::new(400,"Choose an existing destination folder.")); } + } + let now = std::time::Instant::now(); + let mut transfers = self.transfers.lock().unwrap(); + transfers.retain(|_,t|t.expires>now); + if transfers.len()>=128 { return Err(Error::new(429,"Too many pending transfers. Finish one or wait for it to expire.")); } + let id = format!("{}{}",uuid::Uuid::new_v4().simple(),uuid::Uuid::new_v4().simple()); + let mut headers = HeaderMap::new(); + if let Some(value) = parts.headers.get("authorization") { headers.insert("authorization",value.clone()); } + transfers.insert(id.clone(),Transfer{headers,vm,path,size,upload,overwrite:overwrite==true,expires:now+Duration::from_secs(600)}); + return Ok(json!({"url":format!("{}mcp/computer/files/{id}",self.app.mcp.origin),"method":if upload {"PUT"} else {"GET"},"bytes":size,"expires_in":600})); + } + let fields: &[&str] = match action { + "status"|"shot" => &[], "ui" => &["pid"], + "input" => &["actions","screenshot"], + "ui_action" => &["target","operation","value","screenshot"], + "exec" => &["script","runtime","timeout_ms","screenshot"], + "spawn" => &["script","runtime"], "process" => &["id","operation"], + "file" => &["path","operation"], + _ => return Err(Error::new(404,"No tool with that name.")), }; - if !arguments.is_empty() { return Err(Error::new(400, "Use the fields listed for this tool.")); } - host::call(json!({"operation":"vm.computer","payload":{"name":vm,"request":operation}})).await + if arguments.keys().any(|k|!fields.contains(&k.as_str())) { return Err(Error::new(400,"Use the fields listed for this tool.")); } + if action == "file" && !matches!(arguments.get("operation").and_then(Value::as_str),Some("stat"|"list"|"mkdir"|"remove")) { + return Err(Error::new(400,"Choose stat, list, mkdir or remove.")); + } + if action == "exec" { arguments.entry("timeout_ms").or_insert(json!(60000)); } + if matches!(action,"exec"|"input"|"ui_action") { arguments.entry("screenshot").or_insert(json!(true)); } + if matches!(action,"exec"|"spawn") { arguments.entry("runtime").or_insert(json!("default")); } + arguments.insert("action".into(),json!(action)); + dispatch(&vm,Value::Object(arguments)).await + }.await; Ok(match result { Ok(mut value) => { @@ -113,8 +246,144 @@ impl ServerHandler for Computer { } } +async fn transfer_access(computer: &Computer, transfer: &Transfer) -> Result<()> { + if transfer.expires <= std::time::Instant::now() { + return Err(Error::new(410, "This transfer expired. Request a new URL.")); + } + let grant = computer + .app + .mcp + .authenticate(&transfer.headers, &computer.app.mcp.resource("computer"))?; + if !mcp::active_owner(&computer.app, &grant)? { + return Err(Error::new(401, "This connection expired. Connect again.")); + } + let owner = host::call(json!({"operation":"vm.owner","payload":{"name":transfer.vm}})).await?; + permitted(&grant, &transfer.vm, "computer:control", &owner) +} + +async fn transfer( + State(computer): State, + axum::extract::Path(id): axum::extract::Path, + request: Request, +) -> Result { + use base64::Engine; + let transfer = computer + .transfers + .lock() + .unwrap() + .remove(&id) + .ok_or_else(|| { + Error::new( + 410, + "This transfer expired or was already used. Request a new URL.", + ) + })?; + transfer_access(&computer, &transfer).await?; + let permit = computer + .transfer_slots + .clone() + .try_acquire_owned() + .map_err(|_| { + Error::new( + 429, + "Four file transfers are running. Finish one, then request a new URL.", + ) + })?; + if request.method() + != if transfer.upload { + Method::PUT + } else { + Method::GET + } + { + return Err(Error::new( + 405, + "Use the HTTP method returned with this URL.", + )); + } + if transfer.upload { + let temporary = format!( + "{}.snowglobe-{}", + transfer.path, + uuid::Uuid::new_v4().simple() + ); + let mut cleanup = UploadCleanup { + vm: transfer.vm.clone(), + path: Some(temporary.clone()), + }; + let result: Result = tokio::time::timeout_at(transfer.expires.into(), async { + let mut stream=request.into_body().into_data_stream(); + let mut offset=0_u64; + while let Some(chunk)=stream.next().await { + let chunk=chunk.map_err(|_|Error::new(400,"The upload ended early. Request a new URL and retry."))?; + for bytes in chunk.chunks(65536) { + if offset+bytes.len() as u64>transfer.size { return Err(Error::new(413,"The upload exceeds its declared size.")); } + transfer_access(&computer,&transfer).await?; + dispatch(&transfer.vm,json!({"action":"file","operation":"write","path":temporary,"offset":offset,"data":base64::engine::general_purpose::STANDARD.encode(bytes)})).await?; + offset+=bytes.len() as u64; + } + } + if offset!=transfer.size { return Err(Error::new(400,"The upload ended early. Request a new URL and retry.")); } + if offset==0 { dispatch(&transfer.vm,json!({"action":"file","operation":"write","path":temporary,"offset":0,"data":""})).await?; } + transfer_access(&computer,&transfer).await?; + dispatch(&transfer.vm,json!({"action":"file","operation":"commit","path":temporary,"destination":transfer.path,"overwrite":transfer.overwrite})).await?; + Ok(json!({"path":transfer.path,"bytes":offset})) + }).await.map_err(|_|Error::new(410,"This transfer expired. Request a new URL."))?; + if result.is_ok() { + cleanup.path = None; + } + Ok(axum::Json(result?).into_response()) + } else { + let size = transfer.size; + let (sender, receiver) = tokio::sync::mpsc::channel(1); + tokio::spawn(async move { + let _permit = permit; + let result: Result<()> = tokio::time::timeout_at(transfer.expires.into(), async { + let mut offset = 0; + while offset < transfer.size { + if sender.is_closed() { return Ok(()); } + transfer_access(&computer, &transfer).await?; + let length = (transfer.size - offset).min(65536); + let reply = dispatch(&transfer.vm, json!({"action":"file","operation":"read","path":transfer.path,"offset":offset,"length":length})).await?; + let bytes = base64::engine::general_purpose::STANDARD.decode(string(&reply["data"])) + .map_err(|_| Error::new(502, "The guest returned an incomplete file."))?; + if bytes.len() as u64 != length { + return Err(Error::new(409, "The guest file changed during the download.")); + } + if sender.send(Ok(Bytes::from(bytes))).await.is_err() { return Ok(()); } + offset += length; + } + Ok(()) + }).await.unwrap_or_else(|_| Err(Error::new(410, "This transfer expired. Request a new URL."))); + if let Err(error) = result { + let _ = sender.try_send(Err(std::io::Error::other(error.message))); + } + }); + let stream = futures::stream::unfold(receiver, |mut receiver| async { + receiver.recv().await.map(|chunk| (chunk, receiver)) + }); + Ok(Response::builder() + .header("content-type", "application/octet-stream") + .header("content-length", size) + .header("cache-control", "no-store") + .header("content-disposition", "attachment") + .body(axum::body::Body::from_stream(stream)) + .unwrap()) + } +} + pub fn router(app: Arc) -> Router { - mcp::router(app, "computer", || Ok(Computer)) + let computer = Computer { + app: app.clone(), + transfers: Arc::new(Mutex::new(HashMap::new())), + transfer_slots: Arc::new(Semaphore::new(4)), + }; + let handler = computer.clone(); + mcp::router(app, "computer", move || Ok(handler.clone())).merge( + Router::new() + .route("/mcp/computer/files/{id}", any(transfer)) + .with_state(computer), + ) } #[cfg(test)] @@ -138,4 +407,16 @@ mod tests { ); assert!(permitted(&grant, "m3-devbox", "computer:read", &Value::Null).is_err()); } + + #[test] + fn screen_access_does_not_grant_files_commands_or_lifecycle_changes() { + let args = serde_json::Map::new(); + for action in ["file", "upload", "download", "exec", "spawn", "input", "ui_action", "vm"] { + assert_eq!(scope(action, &args), "computer:control"); + } + let status = json!({"operation":"status"}); + let stop = json!({"operation":"stop"}); + assert_eq!(scope("process", status.as_object().unwrap()), "computer:read"); + assert_eq!(scope("process", stop.as_object().unwrap()), "computer:control"); + } } diff --git a/dashboard/src/host-protocol.json b/dashboard/src/host-protocol.json index b7460d92de0bbd6802c1da79f6228bdb25e29085..676610e003afc31b7dcf4f41e8d7b14387bdb3a0 100644 --- a/dashboard/src/host-protocol.json +++ b/dashboard/src/host-protocol.json @@ -1 +1 @@ -{"request":2097152,"response":16777216} +{"request":2097152,"response":16777216,"memory":536870912} diff --git a/dashboard/src/host.rs b/dashboard/src/host.rs index b1ecd8385d569ceec4a822337aa7019fb2d66336..a521b4b19440ef0d3166b5438f81d6f445770d80 100644 --- a/dashboard/src/host.rs +++ b/dashboard/src/host.rs @@ -31,7 +31,7 @@ pub async fn call(request: Value) -> Result { pub async fn open(request: Value) -> Result<(Value, tokio::net::UnixStream)> { let timeout = if matches!( request["operation"].as_str(), - Some("vm.create" | "vm.media" | "vm.preset") + Some("vm.create" | "vm.media" | "vm.preset" | "vm.ai") ) { 910 } else if matches!(request["operation"].as_str(), Some("vm.act" | "vm.computer")) { diff --git a/dashboard/src/mcp.rs b/dashboard/src/mcp.rs index 2994dd2f9372ce7711043620207b522c576b4500..d3e88c0586ca3251d62482b85d9473df7a9abfc4 100644 --- a/dashboard/src/mcp.rs +++ b/dashboard/src/mcp.rs @@ -8,7 +8,7 @@ use sha2::{Digest, Sha256}; use std::os::unix::fs::{DirBuilderExt, PermissionsExt}; const CATALOGS: &[(&str, &str, &[&str])] = &[ - ("computer", "Computers", &["computer:read", "computer:control", "offline_access"]), + ("computer", "Computers", &["computer:read", "computer:control", "computer:create", "offline_access"]), ( "observability", "Logs and traces", @@ -669,13 +669,13 @@ pub async fn oauth(State(app): State>, request: Request) -> Response { response } -fn chosen_resources(body: &Value, resources: &[Value], shale: bool) -> Result { +fn chosen_resources(body: &Value, resources: &[Value], shale: bool, allow_empty: bool) -> Result { if shale && body["resources"] == "all" { return Ok(json!("all")); } let chosen = body["resources"] .as_array() - .filter(|items| !items.is_empty() && items.len() <= resources.len()) + .filter(|items| (allow_empty || !items.is_empty()) && items.len() <= resources.len()) .ok_or_else(|| Error::new(400, "Choose each available resource once."))?; if chosen.iter().enumerate().any(|(index, item)| { !resources.iter().any(|resource| item == &resource["id"]) || chosen[..index].contains(item) @@ -861,7 +861,7 @@ pub async fn manage( "Link your Shale account before allowing repository access.", )); } - let chosen = chosen_resources(&body, &resources, shale_consent)?; + let chosen = chosen_resources(&body, &resources, shale_consent, array(&pending["scopes"]).iter().any(|s| s == "computer:create"))?; if list(&tx, "grant:")? .iter() .filter(|g| g["user"] == owner_id) @@ -911,7 +911,7 @@ pub async fn manage( "Link your Shale account before allowing repository access.", )); } - let chosen = chosen_resources(&body, &resources, shale_consent)?; + let chosen = chosen_resources(&body, &resources, shale_consent, array(&grant["scopes"]).iter().any(|s| s == "computer:create"))?; grant["resources"] = json!(chosen); tx.execute( "UPDATE records SET value=? WHERE key=?", @@ -936,7 +936,7 @@ mod tests { fn resource_updates_reject_empty_duplicates_and_foreign_choices() { let resources = vec![json!({"id":"alpha"}), json!({"id":"beta"})]; assert_eq!( - chosen_resources(&json!({"resources":["beta"]}), &resources, false).unwrap(), + chosen_resources(&json!({"resources":["beta"]}), &resources, false, false).unwrap(), json!(["beta"]) ); for selected in [ @@ -945,13 +945,15 @@ mod tests { json!(["foreign"]), json!([null]), ] { - assert!(chosen_resources(&json!({"resources":selected}), &resources, false).is_err()); + assert!(chosen_resources(&json!({"resources":selected}), &resources, false, false).is_err()); } assert_eq!( - chosen_resources(&json!({"resources":"all"}), &[], true).unwrap(), + chosen_resources(&json!({"resources":"all"}), &[], true, false).unwrap(), json!("all") ); - assert!(chosen_resources(&json!({"resources":"all"}), &resources, false).is_err()); + assert!(chosen_resources(&json!({"resources":"all"}), &resources, false, false).is_err()); + assert_eq!(chosen_resources(&json!({"resources":[]}), &resources, false, true).unwrap(), json!([])); + assert!(chosen_resources(&json!({"resources":["foreign"]}), &resources, false, true).is_err()); } use super::*; diff --git a/dashboard/web/components/TabBar.tsx b/dashboard/web/components/TabBar.tsx index fca401f118da2f9f9ad9468fa146c25ddc239281..fe0b9c63d786d511142c59926f4033029ee61317 100644 --- a/dashboard/web/components/TabBar.tsx +++ b/dashboard/web/components/TabBar.tsx @@ -9,15 +9,25 @@ export function TabBar(props: { label: string; children: JSX.Element }) { let root!: HTMLDivElement; let pill!: HTMLSpanElement; let ink!: HTMLDivElement; + let shown = false; const place = () => { ink.replaceChildren(...[...root.children].filter((el) => el !== pill && el !== ink).map((el) => el.cloneNode(true))); const chosen = root.querySelector(':scope > :is([aria-pressed="true"], [aria-current="page"])'); pill.hidden = ink.hidden = !chosen; if (!chosen) return; + // Back from display: none it jumps; a slide would start from stale geometry and can stall in a throttled tab. + const jump = !shown; + shown = pill.getClientRects().length > 0; + if (!shown) return; + if (jump) pill.style.transition = ink.style.transition = "none"; pill.style.left = `${chosen.offsetLeft}px`; pill.style.width = `${chosen.offsetWidth}px`; const right = root.clientWidth - chosen.offsetLeft - chosen.offsetWidth; ink.style.clipPath = `inset(2px ${right}px 2px ${chosen.offsetLeft}px round 7px)`; + if (jump) { + pill.getBoundingClientRect(); + pill.style.transition = ink.style.transition = ""; + } }; onMount(() => { place(); diff --git a/dashboard/web/pages/AI.tsx b/dashboard/web/pages/AI.tsx index b6704e7fa1ca2029a23463fc67a4db3df676b9da..81f1d082f522645232e65c78e0d7cae5eda49564 100644 --- a/dashboard/web/pages/AI.tsx +++ b/dashboard/web/pages/AI.tsx @@ -1,47 +1,74 @@ import { parseResponse } from "hono/client"; +import CopyIcon from "lucide-solid/icons/copy"; +import Download from "lucide-solid/icons/download"; +import EyeOff from "lucide-solid/icons/eye-off"; +import KeyRound from "lucide-solid/icons/key-round"; import { createResource, createSignal, For, Show } from "solid-js"; import { api, reason } from "../api.ts"; import { Copy } from "../components/Copy.tsx"; import { Loaded } from "../components/Loaded.tsx"; -import { toast } from "../components/Toast.tsx"; +import claude from "../../src/claude.svg?raw"; +import openai from "../../src/openai.svg?raw"; + +export function CopyField(props: { value: string; label: string }) { + return
{props.value}
; +} + +export function ClientMark(props: { name: string }) { + const logo = () => /claude/i.test(props.name) ? claude : /codex|openai|chatgpt/i.test(props.name) ? openai : ""; + return }>{(svg) => ; +} + +const WRAPPERS = [["claude", "Claude Code"], ["codex", "Codex"]] as const; export function AI(props: { viewing: boolean }) { const [info, { refetch }] = createResource(() => parseResponse(api.ai.$get())); const [key, setKey] = createSignal(""); const [busy, setBusy] = createSignal(false); + const [error, setError] = createSignal(""); + let show!: HTMLButtonElement; + let hide!: HTMLButtonElement; return {(data) => <> -
-

{data().model} on Snow Globe · {Math.round(data().context / 1024)}K context

-

API endpoint

-

OpenAI Responses and Chat Completions use /v1. Anthropic Messages uses this base URL.

-
-
-

API key

-

Use this key with the endpoint or enter it when a Snow Globe wrapper first starts.

-
- { - setBusy(true); - try { setKey((await parseResponse(api.ai.key.$post())).key); } - catch (error) { toast(reason(error)); } - finally { setBusy(false); } - }}>Access API key}> - Copy API key - +
+
{data().model}{Math.round(data().context / 1024)}K context
+

API endpoint

+ +

OpenAI Responses and Chat Completions use /v1. Anthropic Messages uses this base URL

+

API key

+
+ + + + }> + +
-
Show key value{key()}
+ +

Use it with the endpoint, or enter it when a wrapper first starts

-
+

Coding clients

-

Shell wrappers for installed Codex and Claude Code. Both start with automatic approval and use the local model.

-

Download a wrapper, then run its install command. Codex or Claude Code must already be installed.

-
{(client) =>
- Download snow-{client} - +

Bash wrappers for macOS and Linux that run your installed Claude Code or Codex on the local model. Both start with automatic approval.

+
{([id, name]) =>
+ +
+ + Install from a macOS or Linux terminal + +
}
-

Run snow-codex or snow-claude from a project. The first run asks for your API key.

-

API waits allow eight hours. Stock Codex approval reviews retain their 90-second deadline.

+

Keep ~/.local/bin on your PATH, then run snow-claude or snow-codex from a project. The first run asks for your API key

+

API waits allow eight hours. Stock Codex approval reviews keep their 90-second deadline

}; } diff --git a/dashboard/web/pages/MCP.css b/dashboard/web/pages/MCP.css index 4d06caf70289fa15aebb1d705295fc4d0c795889..a2ac5bb3add5fec8439210ff63a5e41bb3ce248d 100644 --- a/dashboard/web/pages/MCP.css +++ b/dashboard/web/pages/MCP.css @@ -1,59 +1,174 @@ -/* Settings use the dashboard's controls and flat, divided sections. */ -.mcp-page { max-width: 1000px; } -.mcp-page h2 { font-size: 14px; font-weight: 600; margin: 0 0 8px; color: var(--text); } -.mcp-page h3 { font-size: 13px; margin: 0; } -.mcp-page p { color: var(--text-2); margin: 8px 0; } -.mcp-page > .segmented { margin-bottom: 8px; } -.mcp-panel, .mcp-endpoint { padding: 20px 0; border-bottom: 1px solid var(--line); } -.mcp-section-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; } -.mcp-section-heading h2 { margin: 0; flex: none; } -.mcp-endpoint > p:first-child { margin: 0 0 16px; } -.mcp-endpoint .copy { max-width: 100%; min-width: 0; overflow-wrap: anywhere; white-space: normal; text-align: left; } -.mcp-endpoint .muted { font-size: 12px; } -.mcp-repositories, .mcp-install { margin: 12px 0; } -.mcp-repositories summary, .mcp-install summary { cursor: pointer; color: var(--text-2); } -.mcp-page .mcp-access-modes > label { padding: 6px 0; border: 0; background: none; } -.mcp-actions { display: flex; flex-wrap: wrap; gap: 8px; } -.mcp-access { padding: 0; margin: 20px 0; border: 0; min-width: 0; } -.mcp-access legend { font-weight: 600; margin-bottom: 12px; } -.mcp-access-modes { display: grid; gap: 10px; } -.mcp-access-modes > label { display: flex; align-items: start; gap: 12px; padding: 14px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; } -.mcp-access-modes > label:has(input:checked) { border-color: var(--accent); background: var(--accent-wash); } -.mcp-access-modes input { margin: 4px 0 0; accent-color: var(--accent); } -.mcp-access-modes span span { display: block; font-size: 12px; margin-top: 4px; } -.mcp-resources { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin: 16px 0; } -.mcp-resources .checkbox { align-items: start; overflow-wrap: anywhere; } -.mcp-resources small { display: block; margin-top: 4px; } -.mcp-repository-list > div { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; padding: 10px 0; border-top: 1px solid var(--line); overflow-wrap: anywhere; } -.mcp-client { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 16px; padding: 18px 0; border-top: 1px solid var(--line); } -.mcp-client > .mcp-actions { align-self: start; } -.mcp-client p { margin: 6px 0 0; overflow-wrap: anywhere; } -.mcp-edit-access { grid-column: 1 / -1; } +.mcp-page { max-width: 960px; } +.mcp-page .page-head { margin-bottom: 12px; } +.mcp-section { padding: 22px 0 6px; } +.mcp-section + .mcp-section { border-top: 1px solid var(--line); margin-top: 16px; } +.mcp-section h2 { margin: 0; color: var(--text); font-size: 15px; } +.mcp-section > p { margin: 6px 0; color: var(--text-2); max-width: 70ch; } +.mcp-section > p:first-child { margin-top: 0; } +.mcp-heading { display: flex; align-items: center; gap: 10px; min-height: 26px; flex-wrap: wrap; } +.mcp-heading .mcp-actions { margin-left: auto; } +.mcp-section h2.mcp-label { margin: 16px 0 6px; color: var(--text-2); font-size: 12px; font-weight: 600; } +.mcp-page .hint, .mcp-approval .hint { margin: 6px 0 0; color: var(--muted); font-size: 12px; } +.mcp-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; } + +/* A value you copy: the whole well is the Copy button. */ +.mcp-field > .copy, .mcp-field.masked { + display: flex; + align-items: center; + gap: 10px; + width: 100%; + min-height: 34px; + padding: 6px 10px; + border: 2px solid var(--line); + border-radius: var(--radius); + background: var(--well); + color: var(--text); + font: 12.5px/1.45 var(--mono); + text-align: left; + white-space: normal; + cursor: copy; + transition: border-color 150ms, background-color 150ms; +} +.mcp-field { flex: 1; min-width: 0; } +.mcp-field .value { flex: 1; min-width: 0; overflow-wrap: anywhere; } +.mcp-field .icon { flex: none; color: var(--muted); transition: color 150ms; } +.mcp-field > .copy:hover { border-color: var(--axis); background: var(--accent-wash); } +.mcp-field > .copy:hover .icon { color: var(--accent); } +.mcp-field.masked { box-sizing: border-box; cursor: default; color: var(--muted); letter-spacing: 0.1em; overflow: hidden; } +.mcp-field.masked .value { overflow-wrap: normal; white-space: nowrap; } + +.mcp-key { display: flex; gap: 8px; align-items: stretch; } +.mcp-key .button { height: auto; min-height: 34px; } +.mcp-section > .mcp-key + .error { margin: 6px 0 0; color: color-mix(in srgb, var(--critical) 80%, var(--text)); font-size: 13px; } + +.mcp-wrappers { display: grid; gap: 14px; margin: 14px 0 10px; } +.mcp-wrapper { display: grid; grid-template-columns: 28px minmax(0, 1fr); gap: 12px; align-items: start; } +.mcp-wrapper-main { display: grid; gap: 8px; min-width: 0; } +.mcp-wrapper-main > .mcp-label { margin: 2px 0 -4px; color: var(--text-2); font-size: 12px; font-weight: 600; } +.mcp-wrapper-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 26px; } + +.mcp-client-mark { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--surface); border: 1px solid var(--line); box-sizing: border-box; } +.mcp-client-mark .logo { display: grid; width: 16px; height: 16px; color: var(--text); } +.mcp-client-mark .logo svg { width: 100%; height: 100%; } +.mcp-client-mark .monogram { display: grid; place-items: center; width: 100%; height: 100%; border-radius: 7px; background: var(--accent-wash); color: var(--accent); font-size: 12px; font-weight: 700; } + +/* Shale account */ +.mcp-repositories { margin: 10px 0 0; } +.mcp-repositories summary { display: inline-flex; align-items: center; gap: 4px; color: var(--text-2); cursor: pointer; list-style: none; border-radius: 4px; } +.mcp-repositories summary::-webkit-details-marker { display: none; } +.mcp-repositories[open] summary .chevron { transform: rotate(90deg); } +.mcp-repository-list { margin: 8px 0 0; padding: 0; list-style: none; columns: 2 280px; column-gap: 24px; } +.mcp-repository-list li { display: flex; gap: 10px; padding: 4px 0; break-inside: avoid; font-size: 13px; overflow-wrap: anywhere; } +.mcp-repository-list li span:first-child { font-weight: 550; } +.mcp-link { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; margin-top: 6px; } +.mcp-link p { margin: 0; color: var(--text-2); } + +/* Connected clients: an edge-to-edge list, as the dashboard's tables are. */ +.mcp-section.flush { padding-bottom: 0; } +.mcp-clients { margin: 10px calc(-1 * var(--gutter)) 0; padding: 0; list-style: none; } +.mcp-client { + display: grid; + grid-template-columns: 28px minmax(0, 1fr) auto auto; + align-items: center; + column-gap: 12px; + padding: 8px 0 8px var(--gutter); + border-top: 1px solid var(--grid); + transition: background-color 150ms; +} +.mcp-client:last-child { border-bottom: 1px solid var(--grid); } +.mcp-client:hover, .mcp-client.editing { background: color-mix(in srgb, var(--hover) 60%, transparent); } +.mcp-client-main { min-width: 0; display: grid; gap: 4px; } +.mcp-client h3 { margin: 0; font-size: 13.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } +.mcp-page .chips, .mcp-resources .chips { gap: 4px 6px; } +.chip:is(.ability, .fact) { display: inline-flex; align-items: center; gap: 4px; background: none; border: 1px solid var(--line); line-height: 18px; color: var(--text-2); } +.chip.fact { padding: 0 6px; font-size: 11px; line-height: 16px; color: var(--muted); } +.chip.ability.strong { border-color: color-mix(in srgb, var(--warning) 45%, var(--line)); color: color-mix(in srgb, var(--warning) 60%, var(--text)); } +.mcp-client-when { color: var(--muted); font-size: 12px; white-space: nowrap; } +/* td.actions buttons, filling the row's height. */ +.mcp-row-actions { display: flex; align-self: stretch; } +.mcp-row-actions button { + min-height: 36px; + padding: 0 14px; + border: 0; + background: none; + color: var(--accent); + font-size: 13px; + font-weight: 500; + white-space: nowrap; + cursor: pointer; + transition: background-color 150ms; +} +.mcp-row-actions button:hover:not(:disabled) { background: var(--raised); } +.mcp-row-actions button.danger { color: color-mix(in srgb, var(--critical) 80%, var(--text)); } +.mcp-row-actions button.danger:hover:not(:disabled) { background: color-mix(in srgb, var(--critical) 12%, transparent); } +.mcp-row-actions button:disabled { opacity: 0.45; cursor: default; } +.mcp-row-actions button:focus-visible { outline-offset: -2px; } +.mcp-row-actions button[aria-expanded="true"] { background: var(--accent-wash); } + +.mcp-editor { grid-column: 2 / -1; padding: 6px var(--gutter) 10px 0; } +.mcp-editor > p:first-child { margin: 6px 0 10px; color: var(--text-2); } +.mcp-access { margin: 6px 0 0; padding: 0; border: 0; min-width: 0; } +.mcp-access legend { padding: 0; margin-bottom: 8px; color: var(--text-2); font-size: 12px; font-weight: 600; } +.mcp-resources { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px 16px; margin: 10px 0; } +.mcp-resources .checkbox { align-items: start; overflow-wrap: anywhere; color: var(--text); } +.mcp-resources .checkbox .box { margin-top: 2px; } +.mcp-resources .checkbox > span:last-child { display: grid; gap: 4px; min-width: 0; } +.mcp-resources small { display: block; font-size: 12px; } +.mcp-fixed { display: flex; gap: 6px; align-items: start; margin: 8px 0 12px; color: var(--muted); font-size: 12px; } +.mcp-fixed .icon { flex: none; margin-top: 2px; } +.mcp-editor .error { margin: 0 0 10px; font-size: 13px; } +.mcp-none { margin: 4px 0; padding: 10px 12px; border-radius: var(--radius); background: color-mix(in srgb, var(--warning) 12%, transparent); color: var(--text); font-size: 13px; } + +.mcp-empty { display: grid; justify-items: start; gap: 4px; padding: 14px 0 8px; } +.mcp-empty p { margin: 0; color: var(--muted); } +.mcp-empty .mcp-empty-title { color: var(--text-2); font-weight: 550; } + +@media (max-width: 760px) { + /* Every tab stays in view as a grid; the chosen one is filled directly, since the sliding highlight assumes one row. */ + .mcp-tabs .segmented { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); } + .mcp-tabs .segmented :is(.pill, .ink) { display: none; } + .mcp-tabs .segmented > a { justify-content: center; padding-block: 6px; } + .mcp-tabs .segmented > a[aria-current="page"] { background: var(--accent); color: var(--on-accent); } + .mcp-client { grid-template-columns: 28px minmax(0, 1fr); row-gap: 4px; padding-right: var(--gutter); align-items: start; } + .mcp-client h3 { white-space: normal; } + .mcp-client-when { grid-column: 2; } + .mcp-row-actions { grid-column: 2; margin-left: -14px; } + .mcp-row-actions button { min-height: 32px; } + .mcp-editor { grid-column: 1 / -1; padding-right: 0; } + .mcp-wrapper-head { flex-wrap: wrap; } +} + +/* Consent: a full-screen route, no shell. */ .mcp-authorization { min-height: 100%; display: grid; place-items: center; padding: 40px 24px; box-sizing: border-box; } -.mcp-approval { width: min(100%, 600px); padding: 32px; box-sizing: border-box; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; } -.mcp-approval-brand { color: var(--accent); font-size: 13px; font-weight: 600; margin-bottom: 28px; } -.mcp-approval h1 { font-size: 28px; line-height: 1.2; margin: 0; overflow-wrap: anywhere; } -.mcp-approval h2 { font-size: 15px; margin: 0 0 12px; } -.mcp-approval-intro { font-size: 16px; color: var(--text-2); margin: 12px 0 24px; } -.mcp-request-identity { padding: 16px 0; border-block: 1px solid var(--line); margin: 0; } -.mcp-request-identity > div { display: grid; grid-template-columns: 100px minmax(0, 1fr); gap: 16px; margin: 6px 0; } +.mcp-approval { width: min(100%, 560px); padding: 28px 32px 24px; box-sizing: border-box; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); } +.mcp-approval-brand { display: flex; align-items: center; gap: 8px; margin-bottom: 22px; color: var(--accent); font-weight: 700; font-size: 15px; } +.mcp-approval-brand img { width: 26px; height: 26px; } +.mcp-approval h1 { margin: 0; font-size: 26px; line-height: 1.2; letter-spacing: -0.01em; overflow-wrap: anywhere; } +.mcp-approval h2 { margin: 0 0 10px; color: var(--text); font-size: 14px; } +.mcp-approval-intro { margin: 8px 0 18px; color: var(--text-2); font-size: 15px; } +.mcp-request-identity { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; padding: 12px 14px; border-radius: var(--radius); background: var(--well); font-size: 13px; } +.mcp-request-identity > div { display: contents; } .mcp-request-identity dt { color: var(--muted); } -.mcp-request-identity dd { margin: 0; overflow-wrap: anywhere; } -.mcp-permissions, .mcp-link-step { padding: 24px 0; border-bottom: 1px solid var(--line); } -.mcp-permissions p { margin: 8px 0; } -.mcp-approval-actions { display: flex; justify-content: space-between; gap: 16px; padding-top: 24px; border-top: 1px solid var(--line); margin-top: 24px; } -.mcp-close { background: none; border: 0; color: var(--muted); cursor: pointer; margin-top: 20px; padding: 0; text-decoration: underline; } +.mcp-request-identity dd { margin: 0; overflow-wrap: anywhere; font-weight: 550; } +.mcp-permissions { padding: 20px 0 4px; } +.mcp-permissions ul { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; } +.mcp-permissions li { display: flex; gap: 10px; align-items: start; color: var(--text-2); } +.mcp-permissions li svg { flex: none; margin-top: 1px; color: var(--accent); } +.mcp-permissions li.strong { color: var(--text); font-weight: 550; } +.mcp-permissions li small { display: block; margin-top: 2px; color: var(--muted); font-size: 12px; font-weight: 400; } +.mcp-permissions li.strong svg { color: color-mix(in srgb, var(--warning) 70%, var(--text)); } +.mcp-approval .mcp-access { margin-top: 16px; } +.mcp-link-step { margin-top: 18px; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); } +.mcp-link-step p { margin: 0 0 12px; color: var(--text-2); } +.mcp-approval > .error { margin: 16px 0 0; } +.mcp-approval > .mcp-actions { margin-top: 16px; } +.mcp-approval-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); } +.mcp-approval-actions .spacer { flex: 1; } +.mcp-approval-actions .hint { margin: 0; } @media (max-width: 760px) { - .mcp-client { grid-template-columns: 1fr; } - .mcp-section-heading { flex-wrap: wrap; } - .mcp-endpoint .copy { width: 100%; } - .mcp-repository-list > div { grid-template-columns: 1fr; gap: 4px; } - .mcp-authorization { padding: 20px 12px; align-items: start; } - .mcp-approval { padding: 24px 20px; } + .mcp-authorization { padding: 12px; align-items: start; } + .mcp-approval { padding: 20px 18px 18px; } + .mcp-approval h1 { font-size: 22px; } + .mcp-approval-actions .button { flex: 1; } } - -.ai-downloads { display: grid; gap: 12px; } -.ai-downloads .copy { white-space: normal; overflow-wrap: anywhere; text-align: left; } - -.ai-key { display: block; margin-top: 8px; overflow-wrap: anywhere; user-select: text; } diff --git a/dashboard/web/pages/MCP.tsx b/dashboard/web/pages/MCP.tsx index d986a8041d5adfff4f235830b82c40ef9a18d339..b6ac1ebfefc1537bf75cb90de26bacbe815662b0 100644 --- a/dashboard/web/pages/MCP.tsx +++ b/dashboard/web/pages/MCP.tsx @@ -1,35 +1,55 @@ import { A, useNavigate, useParams } from "@solidjs/router"; import { parseResponse } from "hono/client"; -import { createEffect, createResource, createSignal, For, Show } from "solid-js"; +import ChevronRight from "lucide-solid/icons/chevron-right"; +import Eye from "lucide-solid/icons/eye"; +import GitBranch from "lucide-solid/icons/git-branch"; +import Lock from "lucide-solid/icons/lock"; +import MonitorCheck from "lucide-solid/icons/monitor-check"; +import MousePointer2 from "lucide-solid/icons/mouse-pointer-2"; +import RefreshCw from "lucide-solid/icons/refresh-cw"; +import { createEffect, createResource, createSignal, For, type JSX, Show } from "solid-js"; import { api, reason } from "../api.ts"; import { Ago } from "../components/Ago.tsx"; -import { Copy } from "../components/Copy.tsx"; import { showConfirmDialog } from "../components/Dialog.tsx"; import { Loaded } from "../components/Loaded.tsx"; +import { StatusLabel } from "../components/Status.tsx"; import { TabBar } from "../components/TabBar.tsx"; import { toast } from "../components/Toast.tsx"; -import { AI } from "./AI.tsx"; +import { AI, ClientMark, CopyField } from "./AI.tsx"; import { MCPAccess } from "./MCPAccess.tsx"; import type { Me } from "../types/model.ts"; -import type { Access, Catalog } from "../types/mcp.ts"; +import type { Access, Catalog, Connections } from "../types/mcp.ts"; import "./MCP.css"; const descriptions: Record = { - computer: "Inspect and control selected virtual machines, run commands, and install guest software.", + computer: "Inspect and control selected virtual machines, run commands, transfer files, and create disposable test VMs.", shale: "Read and edit issues across your Shale repositories.", observability: "Read logs and traces from your services.", }; +const VIEWING = "Switch back to your account to change access"; + +/** What a connection's scopes let it do, strongest first; scopes come from the client's request, never from settings. */ +function abilities(client: Connections["connections"][number]) { + const has = (scope: string) => client.scopes.includes(scope); + const out: [JSX.Element, string, boolean][] = []; + if (client.catalog === "computer") out.push(has("computer:control") ? [, "Control, commands, and files", true] : [, "View only", false]); + if (has("computer:create")) out.push([, "Creates test VMs", true]); + if (client.catalog === "shale") out.push(has("shale:write") ? [, "Edits issues", false] : [, "Read only", false]); + if (client.catalog === "observability") out.push([, "Read only", false]); + if (has("offline_access")) out.push([, "Stays signed in", false]); + return out; +} export function MCP(props: { me: Me }) { const params = useParams<{ catalog?: string }>(); const navigate = useNavigate(); - const ai = () => props.me.sections.includes("ai"); - createEffect(() => { if (!params.catalog || params.catalog === "ai" && !ai()) navigate(`/mcp/settings/${ai() ? "ai" : "observability"}`, { replace: true }); }); + createEffect(() => { if (!params.catalog) navigate("/mcp/settings/ai", { replace: true }); }); const [overview, { refetch }] = createResource(() => parseResponse(api.mcp.$get())); const [shale, { refetch: retryShale }] = createResource(() => params.catalog === "shale", () => parseResponse(api.mcp.shale.$get())); const [editing, setEditing] = createSignal(""); const [selection, setSelection] = createSignal([]); + const [saveError, setSaveError] = createSignal(""); const [connection, { refetch: retryConnection, mutate: clearConnection }] = createResource(() => editing() || false, async (id) => { clearConnection(undefined); const result = await parseResponse(api.mcp.connections[":id"].$get({ param: { id } })); @@ -38,76 +58,129 @@ export function MCP(props: { me: Me }) { }); const [busy, setBusy] = createSignal(false); createEffect(() => { params.catalog; setEditing(""); }); + const locked = () => busy() || props.me.viewing; const linkShale = async () => { setBusy(true); try { const result = await parseResponse(api.mcp.shale.$post({ json: {} })); window.location.assign(result.redirect); } catch (error) { toast(reason(error)); setBusy(false); } }; return
+

AI / MCP

{(data) => { const catalog = () => data().catalogs.find((catalog) => catalog.id === params.catalog); - const connections = () => data().connections.filter((connection) => connection.catalog === catalog()?.id); + const connections = (id: Catalog) => data().connections.filter((connection) => connection.catalog === id); return <> -

AI / MCP

- - AI - {(catalog) => {catalog.name}} - + +

No connector here.

- {(current) => <> -
+

{descriptions[current().id]}

-

Connector URL

-

Add this URL to your AI client’s MCP settings, then approve access.

+

Connector URL

+ +

Add this URL to your AI client’s MCP settings, then approve access

-

Shale account

- - -
+
+

Shale account

+ {(linked) => Linked } +
+ + +
+
- {(account) => Link your Shale account to connect clients.

}> -

Account linked

-
{account().resources.length} repositories available
{(resource) =>
{resource.name}{resource.description}
}
-

Your account has no repositories yet.

+ {(account) => +

Link your Shale account to connect clients.

+ +
}> + Your account has no repositories yet

}> +
{account().resources.length} repositories available +
    {(resource) =>
  • {resource.name}{resource.description}
  • }
+
+
}
-

Connected clients

- No clients connected. Add the connector URL to your AI client to get started.

}> -
{(client) => { +
+

Connected clients

+ +

No clients connected yet.

+

Clients appear here after you approve them.

+
}> +
    {(client) => { let editButton!: HTMLButtonElement; - const cancel = () => { setEditing(""); editButton.focus(); }; - return
    { - if (event.key === "Escape" && editing() === client.id && !busy()) { - event.preventDefault(); event.stopPropagation(); cancel(); - } + let editor!: HTMLDivElement; + const open = () => editing() === client.id; + const create = client.scopes.includes("computer:create"); + const close = () => { setEditing(""); editButton.focus(); }; + const save = async () => { + setBusy(true); setSaveError(""); + try { + await parseResponse(api.mcp.connections[":id"].$post({ param: { id: client.id }, json: { resources: selection() } })); + await refetch(); close(); + } catch (error) { setSaveError(reason(error)); } + finally { setBusy(false); } + }; + const empty = () => selection() !== "all" && !selection().length && !create; + return
  • { + if (event.key === "Escape" && open() && !busy()) { event.preventDefault(); event.stopPropagation(); close(); } }}> -

    {client.name}

    {client.resources === "all" ? "All Repositories" : client.resources.join(", ")}

    {client.scopes.includes("computer:control") ? "VM control and commands" : client.scopes.includes("shale:write") ? "Issue editing" : "Read only"} · Connected

    -
    -
    -
    - - {(details) =>

    Link your Shale account to change repository access.

    }> - - -
    -
    } -
    -
    -
  • ; }}
+ +
+

{client.name}

+
+ {client.catalog === "computer" ? "No VMs yet" : "Nothing selected"}}>{(name) => {name}}}> + All repositories + + {([icon, label, strong]) => {icon}{label}} +
+
+ Connected +
+ + +
+
+ + {(details) => { queueMicrotask(() => editor.querySelector("input:not(:disabled), .segmented button, .button")?.focus()); return +

Link your Shale account to change repository access.

+
+ }> + + +

{client.name} keeps the abilities it asked for. To change them, revoke it and connect again.

+ +
+ + + Choose at least one {current().id === "computer" ? "VM" : current().id === "shale" ? "repository" : "service"} +
+
; }} +
+
+ ; + }}
} diff --git a/dashboard/web/pages/MCPAccess.tsx b/dashboard/web/pages/MCPAccess.tsx index 6d2cdec905565c2626e1fb1864a095e23a53784f..265591ba7a9e63a85316bf99b386901f08dfc1fe 100644 --- a/dashboard/web/pages/MCPAccess.tsx +++ b/dashboard/web/pages/MCPAccess.tsx @@ -1,34 +1,54 @@ import { For, Show } from "solid-js"; import { Checkbox } from "../components/Checkbox.tsx"; +import { TabBar } from "../components/TabBar.tsx"; +import { datetime, until } from "../format.ts"; import type { Access, Catalog, Resources } from "../types/mcp.ts"; +const PLATFORMS: Record = { linux: "Linux", windows: "Windows", macos: "macOS" }; + +function facts(resource: Resources[number]) { + const platform = resource.platform && (PLATFORMS[resource.platform] ?? resource.platform); + return [platform && !resource.os?.toLowerCase().includes(platform.toLowerCase()) ? platform : null, resource.os, resource.arch].filter(Boolean); +} + export function MCPAccess(props: { catalog: Catalog; resources: Resources; value: Access; onChange: (value: Access) => void; disabled: boolean; + create: boolean; }) { return
{props.catalog === "computer" ? "Virtual machines" : props.catalog === "shale" ? "Repositories" : "Services"} -
- - -
+ + + + +

{props.value === "all" ? "Includes repositories you can access later" : "Only the repositories you choose"}

-
{(resource) => - { - const selected = props.value === "all" ? [] : props.value; - props.onChange(checked ? [...selected, resource.id] : selected.filter((id) => id !== resource.id)); - }}>{resource.name}{resource.description} - }
-

{props.catalog === "computer" ? "No supported VMs belong to your account." : props.catalog === "shale" ? "No repositories available to select." : "No services available to your account."}

+ + {props.catalog === "computer" ? props.create ? "No VMs yet. This client can create its own test VMs." : "No VMs with computer use belong to your account." + : props.catalog === "shale" ? "No repositories available to select." : "No services available to your account."} +

}> +
{(resource) => + { + const selected = props.value === "all" ? [] : props.value; + props.onChange(checked ? [...selected, resource.id] : selected.filter((id) => id !== resource.id)); + }}>{resource.name} + {resource.description}}> + + {(fact) => {fact}} + {resource.state} + Test VM + {(t) => Expires {until(t())}} + + + + }
+
; } diff --git a/dashboard/web/pages/MCPConsent.tsx b/dashboard/web/pages/MCPConsent.tsx index b1b65386536a5773471a41a07571ec3104e05731..2f0703cb778b0c21f8a05110641c51cadfb4aa6b 100644 --- a/dashboard/web/pages/MCPConsent.tsx +++ b/dashboard/web/pages/MCPConsent.tsx @@ -1,12 +1,31 @@ import { useBeforeLeave, useLocation, useParams } from "@solidjs/router"; import { parseResponse } from "hono/client"; -import { createEffect, createResource, createSignal, on, onCleanup, onMount, Show } from "solid-js"; +import Eye from "lucide-solid/icons/eye"; +import GitBranch from "lucide-solid/icons/git-branch"; +import MonitorCheck from "lucide-solid/icons/monitor-check"; +import MousePointer2 from "lucide-solid/icons/mouse-pointer-2"; +import RefreshCw from "lucide-solid/icons/refresh-cw"; +import { createEffect, createResource, createSignal, For, type JSX, on, onCleanup, onMount, Show } from "solid-js"; import { api, reason } from "../api.ts"; import { showConfirmDialog } from "../components/Dialog.tsx"; +import snowflake from "../snowflake.svg"; import { MCPAccess } from "./MCPAccess.tsx"; -import type { Access } from "../types/mcp.ts"; +import type { Access, Consent } from "../types/mcp.ts"; import "./MCP.css"; +/** Each requested ability as one line; `strong` ones act on the VMs rather than reading them. */ +function abilities(data: Consent) { + const has = (scope: string) => data.scopes.includes(scope); + const out: [JSX.Element, JSX.Element, boolean][] = [[, + data.catalog === "computer" ? "See VM status, screenshots, and on-screen controls" : data.catalog === "shale" ? "Read issues and comments" : "Read logs and traces", false]]; + if (has("computer:control")) out.push([, "Control the desktop, run commands, and transfer files in the VMs you choose", true]); + if (has("computer:create")) out.push([, <>Create up to 2 Linux or Windows test VMs + Each gets up to 4 vCPUs, 8 GiB memory, and an 80 GiB disk. They expire after an idle hour; extensions last up to 24 hours, true]); + if (has("shale:write")) out.push([, "Create issues, comment, and edit titles and status", false]); + if (has("offline_access")) out.push([, "Stay connected without signing in again", false]); + return out; +} + export function MCPConsent() { const params = useParams<{ id: string }>(); const location = useLocation(); @@ -46,39 +65,38 @@ export function MCPConsent() { redirect(result.redirect); } catch (error) { setError(reason(error)); setBusy(false); } }; + const create = () => !!details()?.scopes.includes("computer:create"); + const empty = () => access() !== "all" && !access().length && !create(); return
-
Snow Globe · MCP connection
+
the snow globe

{consent.state === "errored" ? "Connection unavailable" : "Loading connection…"}

}> - +
}> {(data) => <>

Connect {data().client}

-

{data().catalog === "computer" ? "Grant access to selected virtual machines." : data().catalog === "shale" ? "Grant access to Shale issues." : "Grant access to logs and traces."}

-
Signed in as
{data().account}
Return to
{data().redirectHost}
-

Requested access

-

{data().catalog === "computer" ? "Read VM status, screenshots, and application controls" : data().catalog === "shale" ? "Read issues and comments" : "Read logs and traces"}

-

Control the desktop, run commands, and install software inside the selected VMs

-

Create issues, comment, and edit issue titles and status

-

Stay connected without signing in again

+

{data().catalog === "computer" ? "Grant access to your virtual machines." : data().catalog === "shale" ? "Grant access to Shale issues." : "Grant access to logs and traces."}

+
Signed in as
{data().account}
Returns to
{data().redirectHost}
+

{data().client} will be able to

+
    {([icon, text, strong]) =>
  • {icon}{text}
  • }

Link your Shale account

Sign in to Shale, then return here to approve access.

}> - + -

You can change this connection’s access later in MCP settings.

+

You can change this later in AI / MCP settings

} -
+
- -
- + Choose at least one {details()?.catalog === "computer" ? "VM" : details()?.catalog === "shale" ? "repository" : "service"} + +
; } diff --git a/dashboard/web/styles.css b/dashboard/web/styles.css index ead64201830c9924bec0a159e6ccbd8646c6ec82..4c52aad6d07c632becebd8f089590be802728d28 100644 --- a/dashboard/web/styles.css +++ b/dashboard/web/styles.css @@ -289,6 +289,19 @@ button { font: inherit; color: inherit; } .view-as .button { justify-self: start; } @keyframes menu-in { from { opacity: 0; transform: translateY(6px); } } +@media (max-width: 760px) { + body:has(.mcp-page) { --sidebar: 56px; } + body:has(.mcp-page) .sidebar > .brand { justify-content: center; padding: 12px 0; font-size: 0; } + body:has(.mcp-page) .sidebar > .brand img { width: 30px; height: 30px; } + body:has(.mcp-page) .sidebar > .nav { padding-inline: 6px; } + body:has(.mcp-page) .sidebar > .nav :is(.nav-item, .nav-row .nav-item) { justify-content: center; padding: 0; } + body:has(.mcp-page) .sidebar > .nav :is(.nav-item .badge, .nav-item .chevron, .nav-toggle, .nav-group) { display: none; } + body:has(.mcp-page) .sidebar > .nav .label, body:has(.mcp-page) .whoami-button :is(.name, .icon) { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; } + body:has(.mcp-page) .whoami-button { justify-content: center; padding: 0; } + body:has(.mcp-page) .account-menu { width: 240px; } +} + + /* status ------------------------------------------------------------------ */ .status { width: 10px; height: 10px; display: block; transition: color 300ms; } diff --git a/dashboard/web/types/mcp.ts b/dashboard/web/types/mcp.ts index e068e4b027170ecdad85429906e44dad0147d529..449ffdaf972268b0d81a3c21e018e2f9c3aebe61 100644 --- a/dashboard/web/types/mcp.ts +++ b/dashboard/web/types/mcp.ts @@ -1,6 +1,6 @@ export type Catalog = "shale" | "observability" | "computer"; export type Access = "all" | string[]; -export type Resources = { id: string; name: string; description?: string }[]; +export type Resources = { id: string; name: string; description?: string; platform?: string; os?: string | null; arch?: string; state?: string; aiOwned?: boolean; expiresAt?: number | null }[]; export interface Connections { catalogs: { id: Catalog; name: string; endpoint: string }[]; diff --git a/flake.nix b/flake.nix index 93df49be6c286f21ad03d87042504899e1ba7c91..736e73613bf737275b93e7e9b3c19b99d0b20088 100644 --- a/flake.nix +++ b/flake.nix @@ -6,6 +6,7 @@ outputs = { self, nixpkgs, ... }: { packages.x86_64-linux.dashboard = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/dashboard.nix { }; packages.x86_64-linux.host-python = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/host-python.nix { }; + packages.x86_64-linux.computer-ahk = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/computer-ahk.nix { }; packages.x86_64-linux.dashboard-image = self.packages.x86_64-linux.dashboard.image; packages.x86_64-linux.installer = self.nixosConfigurations.installer.config.system.build.isoImage; diff --git a/guest/DESIGN.md b/guest/DESIGN.md index 4b4f8e69f3442c0de5f8edff2df4a5d06efc376c..56de9541b0c664efcfebd735812c3bf6f687ae20 100644 --- a/guest/DESIGN.md +++ b/guest/DESIGN.md @@ -1,6 +1,6 @@ # Guest display stream -The Windows executable is freestanding C99. `cursor.c` contains pixel conversion and dirty-rectangle detection; `windows.c` owns desktop capture and transport; `capture.c` implements modern desktop capture and optional window surfaces. Zig supplies the cross compiler and Windows headers, with no Zig or C runtime in the executable. x86 and x64 target Windows 7; ARM64 targets Windows 10. PE subsystem versions and imported DLLs are checked by `python3 guest/test.py`. Runtime validation uses Windows 10 and 11; Windows 7 runtime behavior is unverified. +The Windows executable is freestanding C99. `cursor.c` contains pixel conversion and dirty-rectangle detection; `windows.c` owns desktop capture and transport; `capture.c` implements modern desktop capture and optional window surfaces. Zig supplies the cross compiler and Windows headers, with no Zig or C runtime in the executable. x86 and x64 target Windows 7; ARM64 targets Windows 10. PE subsystem versions and imported DLLs are checked by `python3 guest/test.py`. Runtime validation uses Windows 7, 10 and 11. Build with `python3 guest/build.py --arch x86_64` (also `x86` and `aarch64`). In an elevated PowerShell session inside the logged-in administrator's desktop, run `./install.ps1 -Binary ./build/snowglobe-guest-x86_64.exe`. The installer uses Task Scheduler COM APIs available to PowerShell 2, installs into protected Program Files, and starts an elevated task at that user's logon. `-Remove` removes that task and executable. Elevation is required by the VirtIO serial driver's device ACL; capture must run in the interactive session, not session 0. The guest needs a compatible VirtIO serial driver and the channel named in `protocol.json`. A standard-user launcher requires a privileged handle broker; it is not part of this first backend. diff --git a/guest/computer-legacy-mac.py b/guest/computer-legacy-mac.py new file mode 100644 index 0000000000000000000000000000000000000000..744803d201f6beef6ccf24ac7909b3ef966c0730 --- /dev/null +++ b/guest/computer-legacy-mac.py @@ -0,0 +1,170 @@ +#!/usr/bin/python +"""Bounded console and files over the legacy guest's pinned private SSH connection.""" +import base64 +import ctypes +import json +import os +import select +import signal +import stat +import subprocess +import sys +import tempfile +import time + + +def identity(pid): + value = subprocess.Popen(['ps', '-p', str(pid), '-o', 'lstart='], stdout=subprocess.PIPE).communicate()[0] + return value.decode().strip().replace(':', '-') + + +def run(request): + action = request['action'] + if action == 'status': + return {'platform': 'macos', 'runtimes': ['bash', 'zsh'], + 'capabilities': ['shot', 'input', 'exec', 'spawn', 'process', 'file'], + 'max_timeout_ms': 180000, 'coordinate_units': 'screenshot_pixels'} + if action == 'input': + quartz = ctypes.CDLL('/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices') + core = ctypes.CDLL('/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation') + quartz.CGEventCreateKeyboardEvent.argtypes = [ctypes.c_void_p, ctypes.c_ushort, ctypes.c_bool] + quartz.CGEventCreateKeyboardEvent.restype = ctypes.c_void_p + quartz.CGEventKeyboardSetUnicodeString.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_void_p] + quartz.CGEventPost.argtypes = [ctypes.c_uint, ctypes.c_void_p] + core.CFRelease.argtypes = [ctypes.c_void_p] + for item in request['actions']: + if item['type'] != 'text': + raise ValueError('Use the VM console for pointer and key actions.') + text = item['text'] + for start in range(0, len(text), 8): + data = text[start:start + 8].encode('utf-16-le') + buffer = ctypes.create_string_buffer(data) + for down in (True, False): + event = quartz.CGEventCreateKeyboardEvent(None, 0, down) + if not event: + raise ValueError('The desktop refused keyboard input. Sign in and retry.') + try: + quartz.CGEventKeyboardSetUnicodeString(event, len(data) // 2, buffer) + quartz.CGEventPost(0, event) + finally: + core.CFRelease(event) + time.sleep(.015) + return {'completed': len(request['actions'])} + if action == 'process': + pid, separator, started = request['id'].partition(':') + if not separator or not pid.isdigit() or int(pid) <= 1: + raise ValueError('Use the process handle returned by computer_spawn.') + pid = int(pid) + if identity(pid) != started: + return {'running': False} + if request['operation'] == 'stop': + if os.getpgid(pid) != pid: + raise ValueError('The process is not an isolated computer task.') + os.killpg(pid, signal.SIGTERM) + return {'stopping': True} + return {'running': True, 'pid': pid} + if action == 'file': + path = request['path'] + if not path.startswith('/') or '\x00' in path: + raise ValueError('Use an absolute guest file path.') + operation = request['operation'] + if operation in ('stat', 'list'): + info = os.stat(path) + result = {'path': path, 'size': info.st_size, 'directory': stat.S_ISDIR(info.st_mode), 'modified': info.st_mtime} + if operation == 'list': + names = os.listdir(path) + result['entries'] = [{'name': name, 'directory': stat.S_ISDIR(os.lstat(os.path.join(path, name)).st_mode)} for name in names[:1000]] + result['truncated'] = len(names) > 1000 + return result + if operation == 'mkdir': + if not os.path.isdir(path): + os.makedirs(path) + return {'path': path} + if operation == 'remove': + os.unlink(path) + return {'removed': True} + if operation == 'read': + with open(path, 'rb') as incoming: + if not stat.S_ISREG(os.fstat(incoming.fileno()).st_mode): + raise ValueError('Choose a regular guest file.') + incoming.seek(request.get('offset', 0)) + data = incoming.read(request.get('length', 65536)) + return {'data': base64.b64encode(data).decode(), 'bytes': len(data)} + if operation == 'write': + offset = request.get('offset', 0) + flags = os.O_WRONLY | os.O_NOFOLLOW | (os.O_CREAT | os.O_EXCL if offset == 0 else 0) + with os.fdopen(os.open(path, flags, 0o600), 'wb') as outgoing: + info = os.fstat(outgoing.fileno()) + if not stat.S_ISREG(info.st_mode) or info.st_size != offset: + raise ValueError('The transfer offset changed. Start a new upload.') + outgoing.seek(offset) + data = base64.b64decode(request['data']) + outgoing.write(data) + return {'bytes': len(data)} + if operation == 'commit': + destination = request['destination'] + if not destination.startswith('/') or '\x00' in destination: + raise ValueError('Use an absolute guest destination path.') + if request.get('overwrite', False): + os.rename(path, destination) + else: + os.link(path, destination) + os.unlink(path) + return {'path': destination} + if action not in ('exec', 'spawn'): + raise ValueError('This legacy guest supports screenshot, keyboard, mouse, console, and file tools.') + runtime = request.get('runtime', 'default') + if runtime == 'default': + runtime = 'bash' + if runtime not in ('bash', 'zsh'): + raise ValueError('Choose bash or zsh for this guest.') + descriptor, path = tempfile.mkstemp(prefix='snowglobe-computer-') + try: + with os.fdopen(descriptor, 'wb') as outgoing: + outgoing.write(request['script'].encode('utf-8')) + with open('/dev/null', 'rb') as incoming: + with open('/dev/null', 'wb') as quiet: + process = subprocess.Popen(['/bin/' + runtime, path], stdin=incoming, + stdout=quiet if action == 'spawn' else subprocess.PIPE, + stderr=quiet if action == 'spawn' else subprocess.PIPE, preexec_fn=os.setsid) + if action == 'spawn': + return {'id': str(process.pid) + ':' + identity(process.pid), 'pid': process.pid} + streams = {process.stdout: 'stdout', process.stderr: 'stderr'} + output = {'stdout': b'', 'stderr': b''} + truncated = set() + deadline = time.time() + request['timeout_ms'] / 1000.0 + timed_out = False + try: + while streams or process.poll() is None: + if time.time() >= deadline: + timed_out = True + break + ready = select.select(list(streams), [], [], min(.1, max(0, deadline - time.time())))[0] + for stream in ready: + name = streams[stream] + data = os.read(stream.fileno(), 8192) + if not data: + del streams[stream] + continue + available = 65536 - len(output[name]) + output[name] += data[:available] + if len(data) > available: + truncated.add(name) + finally: + try: + os.killpg(process.pid, signal.SIGKILL) + except OSError: + pass + process.wait() + return {'exit': process.returncode, 'stdout': output['stdout'].decode('utf-8', 'replace'), + 'stderr': output['stderr'].decode('utf-8', 'replace'), 'timed_out': timed_out, + 'stdout_truncated': 'stdout' in truncated, 'stderr_truncated': 'stderr' in truncated} + finally: + if action != 'spawn': + os.unlink(path) + + +try: + print(json.dumps(run(request))) +except (ValueError, OSError, KeyError) as error: + print(json.dumps({'error': str(error)})) diff --git a/guest/computer-linux-launch b/guest/computer-linux-launch new file mode 100644 index 0000000000000000000000000000000000000000..32584fe5104da606737c575b4f75320bd8badc84 --- /dev/null +++ b/guest/computer-linux-launch @@ -0,0 +1,7 @@ +#!/bin/sh +[ "$#" -eq 0 ] || exit 0 +export PATH=/run/current-system/sw/bin:/usr/local/bin:/usr/bin:/bin +if [ -x /var/lib/snowglobe/computer/bin/snowglobe-python ]; then + exec /var/lib/snowglobe/computer/bin/snowglobe-python /usr/local/libexec/snowglobe-computer.py +fi +exec python3 /usr/local/libexec/snowglobe-computer.py diff --git a/guest/computer-linux.nix b/guest/computer-linux.nix new file mode 100644 index 0000000000000000000000000000000000000000..28b7b3b26ad01b2a817e6a7ba6783e0451ce310f --- /dev/null +++ b/guest/computer-linux.nix @@ -0,0 +1,7 @@ +{ pkgs ? import { } }: +let + python = pkgs.python3.withPackages (p: [ p.pygobject3 ]); +in pkgs.writeShellScriptBin "snowglobe-python" '' + export GI_TYPELIB_PATH="${pkgs.lib.makeSearchPath "lib/girepository-1.0" [ pkgs.at-spi2-core (pkgs.lib.getLib pkgs.glib) pkgs.gobject-introspection ]}''${GI_TYPELIB_PATH:+:$GI_TYPELIB_PATH}" + exec ${python}/bin/python3 "$@" +'' diff --git a/guest/computer-linux.py b/guest/computer-linux.py new file mode 100644 index 0000000000000000000000000000000000000000..18da6b459d3ffc4687c119e41637f6577f0c03dc --- /dev/null +++ b/guest/computer-linux.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +import json +import fcntl +import os +from pathlib import Path +import pwd +import re +import subprocess +import sys +import tempfile + +request = json.load(sys.stdin) +prepare = request.pop('_prepare', None) +stop = request.pop('_stop', None) +unit = prepare or stop or request.pop('_unit') +if not re.fullmatch(r'snowglobe-computer-request-[0-9a-f]{32}', unit): + raise ValueError('Invalid computer task.') +lease = '/run/snowglobe-computer-request' +if prepare or stop: + with os.fdopen(os.open(lease, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600), 'r+') as current: + fcntl.flock(current, fcntl.LOCK_EX) + if prepare or current.read() == unit: + current.seek(0) + current.truncate() + current.write(unit if prepare else '') + current.flush() + if prepare: + print(json.dumps({'prepared': True})) + sys.exit(0) +if stop: + subprocess.run(['systemctl', 'stop', unit], timeout=12, capture_output=True) + state = subprocess.check_output(['systemctl', 'show', unit, '-p', 'ActiveState', '--value'], text=True, timeout=3).strip() + if state not in ('inactive', 'failed'): + raise RuntimeError('The computer task did not stop.') + print(json.dumps({'stopped': True})) + sys.exit(0) +timeout = max(1, min(200, int(request.pop('_timeout')))) +sessions = subprocess.check_output(['loginctl', 'list-sessions', '--no-legend'], text=True).splitlines() +for line in sessions: + props = dict(item.split('=', 1) for item in subprocess.check_output([ + 'loginctl', 'show-session', line.split()[0], '-p', 'Active', '-p', 'Remote', '-p', 'Type', '-p', 'User', + ], text=True).splitlines()) + if props.get('Active') == 'yes' and props.get('Remote') == 'no' and props.get('Type') in ('x11', 'wayland'): + account = pwd.getpwuid(int(props['User'])) + break +else: + print(json.dumps({'error': 'Sign in to the VM desktop before using this tool.'})) + sys.exit(0) +env = dict(os.environ, HOME=account.pw_dir, USER=account.pw_name, LOGNAME=account.pw_name, + XDG_RUNTIME_DIR='/run/user/' + str(account.pw_uid), + DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/' + str(account.pw_uid) + '/bus') +for path in Path('/proc').glob('[0-9]*/environ'): + try: + if path.stat().st_uid != account.pw_uid: + continue + entries = dict(p.split(b'=', 1) for p in path.read_bytes().split(b'\0') if b'=' in p) + if b'WAYLAND_DISPLAY' in entries or b'DISPLAY' in entries: + for key in ('DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'XDG_SESSION_TYPE', 'GI_TYPELIB_PATH'): + if key.encode() in entries: + env[key] = entries[key.encode()].decode() + break + except (OSError, ValueError): + pass +source = request.pop('_worker') +requests = Path('/var/lib/snowglobe/computer-requests') +requests.mkdir(mode=0o700, parents=True, exist_ok=True) +with tempfile.TemporaryDirectory(dir=requests) as directory: + root = Path(directory) + (root / 'request').write_text(json.dumps(request)) + args = ['systemd-run', '--quiet', '--wait', '--collect', '--service-type=exec', '--unit=' + unit, + '-p', 'RuntimeMaxSec=' + str(timeout), '-p', 'TimeoutStopSec=5', '-p', 'WorkingDirectory=' + account.pw_dir, + '-p', 'StandardInput=file:' + str(root / 'request'), + '-p', 'StandardOutput=file:' + str(root / 'response'), + '-p', 'StandardError=file:' + str(root / 'error')] + for key in ('HOME', 'USER', 'LOGNAME', 'PATH', 'XDG_RUNTIME_DIR', 'DBUS_SESSION_BUS_ADDRESS', + 'DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'XDG_SESSION_TYPE', 'GI_TYPELIB_PATH'): + if key in env: + args.append('--setenv=' + key + '=' + env[key]) + identity = f'''import os, fcntl +with open({lease!r}) as current: + fcntl.flock(current, fcntl.LOCK_SH) + if current.read() != {unit!r}: raise SystemExit(125) +os.setgroups({os.getgrouplist(account.pw_name, account.pw_gid)!r}); os.setgid({account.pw_gid}); os.setuid({account.pw_uid}) +''' + python = '/var/lib/snowglobe/computer/bin/snowglobe-python' + args += [python if Path(python).exists() else sys.executable, '-c', identity + source] + result = subprocess.run(args, capture_output=True, timeout=210) + if result.returncode: + print(json.dumps({'error': 'The desktop worker could not start. Check the guest computer installation.'})) + else: + sys.stdout.buffer.write((root / 'response').read_bytes()) diff --git a/guest/computer-windows.cs b/guest/computer-windows.cs new file mode 100644 index 0000000000000000000000000000000000000000..dcf2b2e344d560a8a6fabd713d4b36eacae24219 --- /dev/null +++ b/guest/computer-windows.cs @@ -0,0 +1,249 @@ +using System; +using System.Text; +using System.Diagnostics; +using System.Threading; +using System.Runtime.InteropServices; +public class ComputerProcess { + [DllImport("kernel32.dll", CharSet=CharSet.Unicode)] static extern IntPtr CreateJobObject(IntPtr attributes, string name); + [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern IntPtr OpenJobObject(uint access,bool inherit,string name); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process); + [DllImport("kernel32.dll")] static extern bool TerminateJobObject(IntPtr job, uint code); + [DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr handle); + [StructLayout(LayoutKind.Sequential)] struct SECURITY {public int length;public IntPtr descriptor;public int inherit;} + [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)] struct STARTUP {public int size;public string reserved,desktop,title;public int x,y,width,height,columns,rows,fill,flags;public short show,reservedSize;public IntPtr reservedPointer,input,output,error;} + [StructLayout(LayoutKind.Sequential)] struct CREATED {public IntPtr process,thread;public uint pid,tid;} + [DllImport("kernel32.dll",SetLastError=true)] static extern bool CreatePipe(out IntPtr read,out IntPtr write,ref SECURITY attributes,uint size); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool SetHandleInformation(IntPtr handle,uint mask,uint flags); + [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern IntPtr CreateFile(string path,uint access,uint share,ref SECURITY attributes,uint mode,uint flags,IntPtr template); + [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern bool CreateProcess(string application,StringBuilder command,IntPtr processAttributes,IntPtr threadAttributes,bool inherit,uint flags,IntPtr environment,string directory,ref STARTUP startup,out CREATED created); + [DllImport("kernel32.dll",SetLastError=true)] static extern uint ResumeThread(IntPtr thread); + [DllImport("kernel32.dll")] static extern bool TerminateProcess(IntPtr process,uint code); + [DllImport("kernel32.dll")] static extern uint WaitForSingleObject(IntPtr handle,uint milliseconds); + [DllImport("kernel32.dll",SetLastError=true)] static extern bool GetExitCodeProcess(IntPtr process,out uint code); + [StructLayout(LayoutKind.Sequential)] struct BASICLIMIT { public long perProcess,perJob; public uint flags; public UIntPtr minWorking,maxWorking; public uint active; public UIntPtr affinity; public uint priority,scheduling; } + [StructLayout(LayoutKind.Sequential)] struct COUNTERS { public ulong readOps,writeOps,otherOps,readBytes,writeBytes,otherBytes; } + [StructLayout(LayoutKind.Sequential)] struct LIMIT { public BASICLIMIT basic; public COUNTERS io; public UIntPtr processMemory,jobMemory,peakProcess,peakJob; } + [DllImport("kernel32.dll")] static extern bool SetInformationJobObject(IntPtr job,int type,ref LIMIT info,uint size); + [StructLayout(LayoutKind.Sequential)] struct ACCOUNTING {public long user,kernel,periodUser,periodKernel;public uint faults,total,active,terminated;} + [DllImport("kernel32.dll",SetLastError=true)] static extern bool QueryInformationJobObject(IntPtr job,int type,out ACCOUNTING info,uint size,IntPtr returned); + static string JobName(string request) {return "Global\\SnowglobeComputer-"+System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(request));} + static bool WaitForEmpty(IntPtr job,int timeout) { + var clock=Stopwatch.StartNew(); + do { + ACCOUNTING count; + if(!QueryInformationJobObject(job,1,out count,(uint)Marshal.SizeOf(typeof(ACCOUNTING)),IntPtr.Zero))throw new System.ComponentModel.Win32Exception(); + if(count.active==0)return true; + Thread.Sleep(10); + } while(timeout<0 || clock.ElapsedMilliseconds0){int take=Math.Min(count,bytes.Length-length);Array.Copy(buffer,0,bytes,length,take);length+=take;if(take started=null) { + var stdout=new Drain();var stderr=new Drain(); + Thread outThread=null,errThread=null; + IntPtr output=IntPtr.Zero,outputWrite=IntPtr.Zero,error=IntPtr.Zero,errorWrite=IntPtr.Zero,input=IntPtr.Zero; + var created=new CREATED(); + System.IO.FileStream outputStream=null,errorStream=null; + IntPtr job=CreateJobObject(IntPtr.Zero,JobName(request)); + if(job==IntPtr.Zero)throw new Exception("Cannot create process job"); + try { + var limit=new LIMIT();limit.basic.flags=8192; + if(!SetInformationJobObject(job,9,ref limit,(uint)Marshal.SizeOf(typeof(LIMIT))))throw new Exception("Cannot configure process job"); + var security=new SECURITY {length=Marshal.SizeOf(typeof(SECURITY)),inherit=1}; + if(!CreatePipe(out output,out outputWrite,ref security,0) || !SetHandleInformation(output,1,0) || + !CreatePipe(out error,out errorWrite,ref security,0) || !SetHandleInformation(error,1,0))throw new System.ComponentModel.Win32Exception(); + input=CreateFile("NUL",0x80000000,3,ref security,3,0,IntPtr.Zero); + if(input==new IntPtr(-1))throw new System.ComponentModel.Win32Exception(); + var startup=new STARTUP {size=Marshal.SizeOf(typeof(STARTUP)),flags=256,input=input,output=outputWrite,error=errorWrite}; + // No script instruction may run before the process belongs to its cleanup job. + if(!CreateProcess(null,new StringBuilder("\""+executable+"\" "+arguments),IntPtr.Zero,IntPtr.Zero,true,0x08000004,IntPtr.Zero,directory,ref startup,out created))throw new System.ComponentModel.Win32Exception(); + if(!AssignProcessToJobObject(job,created.process))throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(),"Cannot isolate process job"); + CloseHandle(outputWrite);outputWrite=IntPtr.Zero;CloseHandle(errorWrite);errorWrite=IntPtr.Zero;CloseHandle(input);input=IntPtr.Zero; + outputStream=new System.IO.FileStream(new Microsoft.Win32.SafeHandles.SafeFileHandle(output,true),System.IO.FileAccess.Read);output=IntPtr.Zero; + errorStream=new System.IO.FileStream(new Microsoft.Win32.SafeHandles.SafeFileHandle(error,true),System.IO.FileAccess.Read);error=IntPtr.Zero; + outThread=new Thread(()=>stdout.Read(outputStream));outThread.IsBackground=true;outThread.Start(); + errThread=new Thread(()=>stderr.Read(errorStream));errThread.IsBackground=true;errThread.Start(); + object handle=null; + if(started!=null)using(var owner=Process.GetCurrentProcess())handle=new {pid=(int)created.pid,id=owner.Id+":"+owner.StartTime.ToUniversalTime().Ticks}; + if(!System.IO.File.Exists(request))throw new Exception("Process cleanup failed"); + if(ResumeThread(created.thread)==UInt32.MaxValue)throw new System.ComponentModel.Win32Exception(); + if(started!=null) { + started(handle); + CloseHandle(created.process);created.process=IntPtr.Zero;CloseHandle(created.thread);created.thread=IntPtr.Zero; + WaitForEmpty(job,-1); + return handle; + } + var clock=Stopwatch.StartNew(); + uint waited=WaitForSingleObject(created.process,(uint)timeout); + if(waited!=0 && waited!=258)throw new System.ComponentModel.Win32Exception(); + bool timedOut=waited==258; + if(!timedOut){int remaining=Math.Max(0,timeout-(int)clock.ElapsedMilliseconds);timedOut=!outThread.Join(remaining);remaining=Math.Max(0,timeout-(int)clock.ElapsedMilliseconds);timedOut=!errThread.Join(remaining)||timedOut;} + if(timedOut)TerminateJobObject(job,124); + if(WaitForSingleObject(created.process,5000)!=0)throw new Exception("Process cleanup failed"); + outThread.Join(5000);errThread.Join(5000); + uint code;if(!GetExitCodeProcess(created.process,out code))throw new System.ComponentModel.Win32Exception(); + return new {exit=unchecked((int)code),stdout=stdout.Text(),stderr=stderr.Text(),timed_out=timedOut,stdout_truncated=stdout.truncated,stderr_truncated=stderr.truncated}; + } finally { + TerminateJobObject(job,124); + if(created.process!=IntPtr.Zero)TerminateProcess(created.process,124); + foreach(var handle in new[]{created.process,created.thread,output,outputWrite,error,errorWrite,input})if(handle!=IntPtr.Zero && handle!=new IntPtr(-1))CloseHandle(handle); + WaitForEmpty(job,-1);CloseHandle(job); + if(outputStream!=null)outputStream.Dispose();if(errorStream!=null)errorStream.Dispose(); + } + + } + [StructLayout(LayoutKind.Sequential)] struct KEYBDINPUT {public ushort vk,scan;public uint flags,time;public UIntPtr extra;} + [StructLayout(LayoutKind.Explicit)] struct INPUTUNION {[FieldOffset(0)]public KEYBDINPUT keyboard;[FieldOffset(0)]public MOUSEINPUT mouse;} + [StructLayout(LayoutKind.Sequential)] struct MOUSEINPUT {public int x,y;public uint data,flags,time;public UIntPtr extra;} + [StructLayout(LayoutKind.Sequential)] struct INPUT {public uint type;public INPUTUNION data;} + [DllImport("user32.dll",SetLastError=true)] static extern uint SendInput(uint count, INPUT[] inputs,int size); + public static void Type(string text) { + foreach(char unit in text){var inputs=new INPUT[2];for(int i=0;i<2;i++){inputs[i].type=1;inputs[i].data.keyboard.scan=unit;inputs[i].data.keyboard.flags=(uint)(4+i*2);}if(SendInput(2,inputs,Marshal.SizeOf(typeof(INPUT)))!=2)throw new Exception("Desktop refused text input");} + } +} + +public static class ComputerWorker { + static System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = 4194304 }; + static string requestPath,responsePath; + static System.Collections.Generic.Dictionary Map(object value) { return (System.Collections.Generic.Dictionary)value; } + static object Get(System.Collections.Generic.Dictionary value, string key, object fallback = null) { object found; return value.TryGetValue(key,out found) ? found : fallback; } + static string Text(System.Collections.Generic.Dictionary value, string key, string fallback = "") { return Convert.ToString(Get(value,key,fallback)); } + static int Number(System.Collections.Generic.Dictionary value, string key, int fallback = 0) { return Convert.ToInt32(Get(value,key,fallback)); } + [DllImport("user32.dll")] static extern bool SetProcessDPIAware(); + [STAThread] public static void Main(string[] args) { + if(args.Length != 2) return; + if(args[0]=="--stop") {try {ComputerProcess.Stop(args[1]);}catch {Environment.Exit(1);}return;} + requestPath=args[0];responsePath=args[1]; object result; + try { SetProcessDPIAware(); result=Run(Map(json.DeserializeObject(System.IO.File.ReadAllText(requestPath)))); } + catch(Exception error) { result=new { error=error.Message }; } + Reply(result); + } + static void Reply(object result) {System.IO.File.WriteAllText(responsePath,json.Serialize(result),new UTF8Encoding(false));} + static object Target(System.Windows.Automation.AutomationElement node) { + var current=node.Current; + using(var process=Process.GetProcessById(current.ProcessId)) { + return new {pid=current.ProcessId,started=process.StartTime.ToUniversalTime().Ticks.ToString(),id=String.Join(".",node.GetRuntimeId()),role=current.ControlType.ProgrammaticName,name=current.Name}; + } + } + static System.Windows.Automation.AutomationElement Find(System.Collections.Generic.Dictionary target) { + using(var process=Process.GetProcessById(Number(target,"pid"))) { + if(process.StartTime.ToUniversalTime().Ticks.ToString()!=Text(target,"started")) throw new Exception("The application changed. Read a fresh UI tree."); + } + var roots=System.Windows.Automation.AutomationElement.RootElement.FindAll(System.Windows.Automation.TreeScope.Children,new System.Windows.Automation.PropertyCondition(System.Windows.Automation.AutomationElement.ProcessIdProperty,Number(target,"pid"))); + var queue=new System.Collections.Generic.Queue(); + foreach(System.Windows.Automation.AutomationElement root in roots)queue.Enqueue(root); + var walker=System.Windows.Automation.TreeWalker.ControlViewWalker;var clock=Stopwatch.StartNew();int count=0; + while(queue.Count>0 && count++<5000 && clock.ElapsedMilliseconds<8000) { + var node=queue.Dequeue();var current=node.Current; + if(String.Join(".",node.GetRuntimeId())==Text(target,"id") && current.Name==Text(target,"name") && current.ControlType.ProgrammaticName==Text(target,"role"))return node; + for(var child=walker.GetFirstChild(node);child!=null && queue.Count<5000;child=walker.GetNextSibling(child))queue.Enqueue(child); + } + throw new Exception("The UI element changed. Read a fresh tree."); + } + static object Ui(System.Collections.Generic.Dictionary request) { + var nodes=new System.Collections.Generic.List(); + var queue=new System.Collections.Generic.Queue>(); + queue.Enqueue(Tuple.Create(System.Windows.Automation.AutomationElement.RootElement,0)); + var walker=System.Windows.Automation.TreeWalker.ControlViewWalker;var clock=Stopwatch.StartNew(); + while(queue.Count>0 && nodes.Count<1500 && clock.ElapsedMilliseconds<8000) { + var entry=queue.Dequeue();var node=entry.Item1;int depth=entry.Item2; + try { + var current=node.Current; + if(depth==1 && Number(request,"pid")>0 && current.ProcessId!=Number(request,"pid"))continue; + if(depth>0) { + var actions=new System.Collections.Generic.List();actions.Add("focus");object pattern;string value=null; + if(node.TryGetCurrentPattern(System.Windows.Automation.InvokePattern.Pattern,out pattern))actions.Add("press"); + if(node.TryGetCurrentPattern(System.Windows.Automation.TogglePattern.Pattern,out pattern))actions.Add("toggle"); + if(node.TryGetCurrentPattern(System.Windows.Automation.SelectionItemPattern.Pattern,out pattern))actions.Add("select"); + if(node.TryGetCurrentPattern(System.Windows.Automation.ExpandCollapsePattern.Pattern,out pattern)){actions.Add("expand");actions.Add("collapse");} + if(node.TryGetCurrentPattern(System.Windows.Automation.ValuePattern.Pattern,out pattern)) { + var properties=((System.Windows.Automation.ValuePattern)pattern).Current; + value=properties.Value;if(value.Length>1024)value=value.Substring(0,1024); + if(!properties.IsReadOnly)actions.Add("set_value"); + } else if(node.TryGetCurrentPattern(System.Windows.Automation.TextPattern.Pattern,out pattern)) { + value=((System.Windows.Automation.TextPattern)pattern).DocumentRange.GetText(1024); + } + var r=current.BoundingRectangle; + nodes.Add(new {target=Target(node),role=current.ControlType.ProgrammaticName,name=current.Name,value=value,enabled=current.IsEnabled,focused=current.HasKeyboardFocus,bounds=new[]{r.X,r.Y,r.Width,r.Height},actions=actions}); + } + if(depth<20) for(var child=walker.GetFirstChild(node);child!=null && queue.Count<3000;child=walker.GetNextSibling(child))queue.Enqueue(Tuple.Create(child,depth+1)); + } catch(System.Windows.Automation.ElementNotAvailableException) {} catch(ArgumentException) {} catch(InvalidOperationException) {} + } + return new {nodes=nodes,truncated=queue.Count>0}; + } + static object UiAction(System.Collections.Generic.Dictionary request) { + var node=Find(Map(Get(request,"target"))); + switch(Text(request,"operation")) { + case "focus": node.SetFocus();break; + case "press": ((System.Windows.Automation.InvokePattern)node.GetCurrentPattern(System.Windows.Automation.InvokePattern.Pattern)).Invoke();break; + case "toggle": ((System.Windows.Automation.TogglePattern)node.GetCurrentPattern(System.Windows.Automation.TogglePattern.Pattern)).Toggle();break; + case "select": ((System.Windows.Automation.SelectionItemPattern)node.GetCurrentPattern(System.Windows.Automation.SelectionItemPattern.Pattern)).Select();break; + case "expand": ((System.Windows.Automation.ExpandCollapsePattern)node.GetCurrentPattern(System.Windows.Automation.ExpandCollapsePattern.Pattern)).Expand();break; + case "collapse": ((System.Windows.Automation.ExpandCollapsePattern)node.GetCurrentPattern(System.Windows.Automation.ExpandCollapsePattern.Pattern)).Collapse();break; + case "set_value": ((System.Windows.Automation.ValuePattern)node.GetCurrentPattern(System.Windows.Automation.ValuePattern.Pattern)).SetValue(Text(request,"value"));break; + default: throw new Exception("Choose an action reported by computer_ui."); + } + return new {completed=true}; + } + static object Run(System.Collections.Generic.Dictionary request) { + string action=Text(request,"action");string root=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + if(action=="status") { + var runtimes=new System.Collections.Generic.List(new[]{"powershell","cmd"});if(System.IO.File.Exists(System.IO.Path.Combine(root,"AutoHotkey.exe")))runtimes.Add("ahk"); + return new {platform="windows",hostname=Environment.MachineName,runtimes=runtimes,capabilities=new[]{"shot","input","ui","ui_action","exec","spawn","process","file"},coordinate_units="screenshot_pixels",max_timeout_ms=180000}; + } + if(action=="input") { + var actions=(object[])Get(request,"actions");foreach(var value in actions) {var item=Map(value);if(Text(item,"type")!="text")throw new Exception("Use the VM console for pointer and key actions.");ComputerProcess.Type(Text(item,"text"));}return new {completed=actions.Length}; + } + if(action=="ui")return Ui(request); + if(action=="ui_action")return UiAction(request); + if(action=="exec" || action=="spawn") { + string runtime=Text(request,"runtime","default"),directory=System.IO.Path.GetDirectoryName(requestPath),file,exe,arguments,script=Text(request,"script"); + if(runtime=="default")runtime="powershell"; + switch(runtime) { + case "powershell": file=System.IO.Path.Combine(directory,"script.ps1");exe="powershell.exe";arguments="-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \""+file+"\"";script="[Console]::OutputEncoding=New-Object Text.UTF8Encoding($false)\n"+script;break; + case "cmd": file=System.IO.Path.Combine(directory,"script.cmd");exe="cmd.exe";arguments="/d /s /c \"\""+file+"\"\"";script="@chcp 65001 >nul\r\n"+script;break; + case "ahk": file=System.IO.Path.Combine(directory,"script.ahk");exe=System.IO.Path.Combine(root,"AutoHotkey.exe");arguments="/ErrorStdOut \""+file+"\"";script="#Requires AutoHotkey v2.0\n#SingleInstance Off\n#NoTrayIcon\nFileEncoding 'UTF-8-RAW'\nSendMode 'Input'\nCoordMode 'Mouse', 'Screen'\nCoordMode 'Pixel', 'Screen'\nCoordMode 'ToolTip', 'Screen'\nSetTitleMatchMode 2\nDetectHiddenWindows true\n"+script;break; + default:throw new Exception("Choose a runtime reported by computer_status."); + } + System.IO.File.WriteAllText(file,script,new UTF8Encoding(runtime!="cmd")); + string home=Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + if(action=="exec")return ComputerProcess.Run(exe,arguments,home,Number(request,"timeout_ms"),requestPath); + return ComputerProcess.Run(exe,arguments,home,0,requestPath,Reply); + } + if(action=="process") { + string[] parts=Text(request,"id").Split(':');Process process; + try {process=Process.GetProcessById(Int32.Parse(parts[0]));}catch(ArgumentException){return new {running=false};} + using(process) { + if(parts.Length!=2 || process.StartTime.ToUniversalTime().Ticks.ToString()!=parts[1])return new {running=false}; + if(Text(request,"operation")=="stop") { + var info=new ProcessStartInfo("taskkill.exe","/PID "+process.Id+" /T /F") {UseShellExecute=false,CreateNoWindow=true}; + using(var stop=Process.Start(info)){if(!stop.WaitForExit(10000) || stop.ExitCode!=0)throw new Exception("The process could not stop. Check its status.");}return new {stopping=true}; + } + return new {running=true,pid=process.Id}; + } + } + if(action=="file") { + string path=Text(request,"path"); + if(!System.Text.RegularExpressions.Regex.IsMatch(path,@"^[A-Za-z]:[\\/]") || path.IndexOf('\0')>=0)throw new Exception("Use an absolute guest file path."); + switch(Text(request,"operation")) { + case "stat": if(System.IO.Directory.Exists(path))return new {path=path,size=0,directory=true};var f=new System.IO.FileInfo(path);return new {path=f.FullName,size=f.Length,directory=false,modified=f.LastWriteTimeUtc.ToString("o")}; + case "list": var entries=new System.Collections.Generic.List();foreach(string child in System.IO.Directory.EnumerateFileSystemEntries(path)) {if(entries.Count==1000)return new {entries=entries,truncated=true};entries.Add(new {name=System.IO.Path.GetFileName(child),directory=System.IO.Directory.Exists(child)});}return new {entries=entries,truncated=false}; + case "mkdir": System.IO.Directory.CreateDirectory(path);return new {path=path}; + case "commit": string destination=Text(request,"destination");if(Convert.ToBoolean(Get(request,"overwrite",false)) && System.IO.File.Exists(destination))System.IO.File.Replace(path,destination,null);else System.IO.File.Move(path,destination);return new {path=destination}; + case "remove": System.IO.File.Delete(path);return new {removed=true}; + default: throw new Exception("Choose a supported file operation."); + } + } + throw new Exception("Choose a supported computer operation."); + } +} diff --git a/guest/windows-provision.ps1 b/guest/windows-provision.ps1 index 08155d022ba125dd8ae10f199dd8a257ac7b4497..853b3f4ff3110360225882487865fc0bf475214f 100644 --- a/guest/windows-provision.ps1 +++ b/guest/windows-provision.ps1 @@ -4,6 +4,17 @@ $ProgressPreference = 'SilentlyContinue' $root = 'C:\Snowglobe' $status = Join-Path $root 'setup' $winlogon = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' +$scheduler = New-Object -ComObject Schedule.Service +$scheduler.Connect() +$folder = $scheduler.GetFolder('\') + +function Set-GuestPassword([string]$name, [string]$password) { + $account = [ADSI]("WinNT://" + $env:COMPUTERNAME + "/" + $name + ",user") + $account.SetPassword($password) + $account.UserFlags = ([int]$account.UserFlags.Value -bor 0x10000) -band (-bnot 2) + $account.PasswordExpired = 0 + $account.SetInfo() +} function Set-SetupStatus([string]$stage) { $path = Join-Path $status $stage @@ -15,18 +26,30 @@ function Set-SetupStatus([string]$stage) { try { if ($Prepare) { Remove-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Run' -Name SnowglobeBootstrap -ErrorAction SilentlyContinue - Unregister-ScheduledTask -TaskName SnowglobeFinalize -Confirm:$false -ErrorAction SilentlyContinue + foreach ($task in $folder.GetTasks(1)) { + if ($task.Name -eq 'SnowglobeFinalize') { $task.Stop(0); $folder.DeleteTask($task.Name, 0) } + } & "$root\install-guest.ps1" -Binary "$root\snowglobe-guest.exe" -User sandbox - (Get-LocalUser -Name sandbox).SID.Value | Set-Content "$root\template.sid" -Encoding ASCII - Get-ScheduledTask -TaskName 'SnowglobeGuest-*' | Disable-ScheduledTask | Out-Null + $user = Get-WmiObject Win32_UserAccount -Filter "LocalAccount=True AND Name='sandbox'" + if (!$user) { throw 'The template account is missing.' } + $user.SID | Set-Content "$root\template.sid" -Encoding ASCII + foreach ($task in $folder.GetTasks(1)) { + if ($task.Name -like 'SnowglobeGuest-*') { $task.Enabled = $false } + } Get-Process snowglobe-guest -ErrorAction SilentlyContinue | Stop-Process -Force - $action = New-ScheduledTaskAction -Execute "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument '-NoProfile -ExecutionPolicy Bypass -File C:\Snowglobe\windows-provision.ps1' - $trigger = New-ScheduledTaskTrigger -AtStartup - $principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest - $settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Minutes 5) - Register-ScheduledTask -TaskName SnowglobeProvision -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Force | Out-Null + $task = $scheduler.NewTask(0) + $task.Principal.UserId = 'SYSTEM' + $task.Principal.LogonType = 5 + $task.Principal.RunLevel = 1 + $task.Settings.StartWhenAvailable = $true + $task.Settings.ExecutionTimeLimit = 'PT15M' + $task.Triggers.Create(8) | Out-Null + $action = $task.Actions.Create(0) + $action.Path = "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" + $action.Arguments = '-NoProfile -ExecutionPolicy Bypass -File C:\Snowglobe\windows-provision.ps1' + $folder.RegisterTaskDefinition('SnowglobeProvision', $task, 6, 'SYSTEM', $null, 5, $null) | Out-Null $password = [Guid]::NewGuid().ToString('N') + [Guid]::NewGuid().ToString('N') - Set-LocalUser sandbox -Password (ConvertTo-SecureString $password -AsPlainText -Force) -PasswordNeverExpires $true + Set-GuestPassword sandbox $password Set-ItemProperty $winlogon AutoAdminLogon '1' Set-ItemProperty $winlogon DefaultUserName 'sandbox' Set-ItemProperty $winlogon DefaultDomainName $env:COMPUTERNAME @@ -60,13 +83,13 @@ try { if ($username -notmatch '^[A-Za-z0-9][A-Za-z0-9._-]{0,19}$') { throw 'The VM account name is unsupported.' } if ($password.Length -lt 12 -or $password.Length -gt 128 -or $password -match '[\r\n]') { throw 'The VM password is malformed.' } $sid = Get-Content "$root\template.sid" - $user = Get-LocalUser | Where-Object { $_.SID.Value -eq $sid } + $user = Get-WmiObject Win32_UserAccount -Filter "LocalAccount=True AND SID='$sid'" if (!$user) { throw 'The template account is missing.' } - if (Get-LocalUser | Where-Object { $_.Name -ieq $username -and $_.SID.Value -ne $sid }) { throw 'The VM account name is already in use.' } - $deadline = (Get-Date).AddMinutes(3) + if (Get-WmiObject Win32_UserAccount -Filter "LocalAccount=True AND Name='$username'" | Where-Object { $_.SID -ne $sid }) { throw 'The VM account name is already in use.' } + $deadline = (Get-Date).AddMinutes(10) function Find-Desktop { - Get-CimInstance Win32_Process -Filter "Name='explorer.exe'" | Where-Object { - $_.SessionId -gt 0 -and (Invoke-CimMethod $_ -MethodName GetOwnerSid -ErrorAction SilentlyContinue).Sid -eq $sid + Get-WmiObject Win32_Process -Filter "Name='explorer.exe'" | Where-Object { + $_.SessionId -gt 0 -and $_.GetOwnerSid().Sid -eq $sid } | Select-Object -First 1 } $desktop = Find-Desktop @@ -75,17 +98,17 @@ try { Start-Sleep -Milliseconds 250 $desktop = Find-Desktop } - $user | Set-LocalUser -Password (ConvertTo-SecureString $password -AsPlainText -Force) -PasswordNeverExpires $true - if ($user.Name -ine $username) { $user | Rename-LocalUser -NewName $username } + Set-GuestPassword $user.Name $password + if ($user.Name -ine $username -and $user.Rename($username).ReturnValue -ne 0) { throw 'The template account could not be renamed.' } Set-ItemProperty $winlogon AutoAdminLogon '1' Set-ItemProperty $winlogon DefaultUserName $username Set-ItemProperty $winlogon DefaultDomainName $env:COMPUTERNAME Set-ItemProperty $winlogon DefaultPassword $password Remove-ItemProperty $winlogon AutoLogonCount -ErrorAction SilentlyContinue - $scheduler = New-Object -ComObject Schedule.Service - $scheduler.Connect() - $task = $scheduler.GetFolder('\').GetTask("SnowglobeGuest-$sid") - $task.Enabled = $true + $definition = $folder.GetTask("SnowglobeGuest-$sid").Definition + $definition.Triggers.Item(1).UserId = $sid + $definition.Settings.Enabled = $true + $task = $folder.RegisterTaskDefinition("SnowglobeGuest-$sid", $definition, 6, $sid, $null, 3, $null) Set-ItemProperty $winlogon ForceAutoLogon '1' Set-SetupStatus signingIn & "$env:WINDIR\System32\logoff.exe" $desktop.SessionId @@ -99,8 +122,8 @@ try { do { if ((Get-Date) -gt $deadline) { throw 'Desktop capture did not start.' } Start-Sleep -Milliseconds 250 - $capture = Get-CimInstance Win32_Process -Filter "Name='snowglobe-guest.exe'" | Where-Object { - $_.SessionId -eq $fresh.SessionId -and (Invoke-CimMethod $_ -MethodName GetOwnerSid -ErrorAction SilentlyContinue).Sid -eq $sid + $capture = Get-WmiObject Win32_Process -Filter "Name='snowglobe-guest.exe'" | Where-Object { + $_.SessionId -eq $fresh.SessionId -and $_.GetOwnerSid().Sid -eq $sid } } while (!$capture) Set-SetupStatus ready diff --git a/nixos/computer-ahk.nix b/nixos/computer-ahk.nix new file mode 100644 index 0000000000000000000000000000000000000000..1673d75d52304aa45a9d48bc594e4e3b3a3e2743 --- /dev/null +++ b/nixos/computer-ahk.nix @@ -0,0 +1,11 @@ +{ runCommand, fetchurl, unzip }: +runCommand "snowglobe-autohotkey-2.0.19" { + src = fetchurl { + url = "https://github.com/AutoHotkey/AutoHotkey/releases/download/v2.0.19/AutoHotkey_2.0.19.zip"; + sha256 = "4e0d0e65655066a646a210951320feaef0729a3597177131adaec4066bef5869"; + }; + nativeBuildInputs = [ unzip ]; +} '' + mkdir -p "$out" + unzip -j "$src" AutoHotkey32.exe license.txt -d "$out" +'' diff --git a/nixos/configuration.nix b/nixos/configuration.nix index a0dfb3a2009d157808500d7cb480cbce7c568f71..f90e9f30f60a816b2f17151840e0de61f6bb0d93 100644 --- a/nixos/configuration.nix +++ b/nixos/configuration.nix @@ -6,8 +6,9 @@ let proxyToken = "/var/lib/snowglobe/dashboard-proxy.token"; hostTools = lib.fileset.toSource { root = ../.; - fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../guest/protocol.json ]; + fileset = lib.fileset.unions [ ../tools/dashboard-host.py ../service/copyparty/manage.py ../service/copyparty/copyparty.conf ../tools/shale-labels.py ../tools/shale-access.py ../config/shale.json ../config/site.pkl ../dashboard/src/host-protocol.json ../tools/vms.py ../tools/vm-computer.py ../tools/vm-ai.py ../tools/vm-rfb.py ../tools/vm-screen.py ../tools/mac-computer.py ../guest/computer-windows.cs ../guest/computer-legacy-mac.py ../guest/computer-linux.py ../guest/computer-linux-launch ../guest/computer-linux.nix ../tools/mac-vms.py ../tools/vm-ssh.py ../tools/dashboard-run.py ../tools/release.py ../guest/protocol.json ]; }; + computerAhk = pkgs.callPackage ./computer-ahk.nix { }; nativePkl = pkgs.callPackage ./pkl.nix { }; hostPython = pkgs.callPackage ./host-python.nix { }; screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]); @@ -163,7 +164,7 @@ in SNOWGLOBE_VM_GUEST_PROTOCOL = toString ../guest/protocol.json; }; serviceConfig = { - ExecStart = "${screenPython}/bin/python3 ${../tools/vm-screen.py}"; + ExecStart = "${screenPython}/bin/python3 ${hostTools}/tools/vm-screen.py"; User = "snowglobe-dashboard"; Group = "snowglobe-dashboard"; RuntimeDirectory = "snowglobe-vm-screen"; @@ -184,6 +185,23 @@ in RestartSec = 2; }; }; + systemd.timers.snowglobe-vm-expiry = { + wantedBy = [ "timers.target" ]; + timerConfig = { OnBootSec = "1min"; OnUnitActiveSec = "1min"; }; + }; + systemd.services.snowglobe-vm-expiry = { + path = [ pkgs.python3 pkgs.libvirt ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.python3}/bin/python3 ${hostTools}/tools/vm-ai.py reap"; + UMask = "0077"; + ProtectSystem = "strict"; + ProtectHome = true; + PrivateTmp = true; + NoNewPrivileges = true; + ReadWritePaths = [ "/srv/vm" ]; + }; + }; systemd.services.snowglobe-host = { wantedBy = [ "multi-user.target" ]; wants = [ "podman.socket" ]; @@ -193,6 +211,7 @@ in environment = (lib.filterAttrs (name: _: lib.hasPrefix "SNOWGLOBE_" name) config.environment.variables) // { SNOWGLOBE_POOL = lib.attrByPath [ "SNOWGLOBE_POOL" ] "snowglobe-demo" config.environment.variables; SNOWGLOBE_DASHBOARD_USER = "snowglobe-dashboard"; + SNOWGLOBE_COMPUTER_AHK = toString computerAhk; SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables; }; serviceConfig = { @@ -211,7 +230,7 @@ in PrivateTmp = true; NoNewPrivileges = true; CapabilityBoundingSet = [ "CAP_SYS_ADMIN" "CAP_DAC_READ_SEARCH" "CAP_DAC_OVERRIDE" "CAP_CHOWN" ]; - MemoryMax = "256M"; + MemoryMax = (builtins.fromJSON (builtins.readFile ../dashboard/src/host-protocol.json)).memory; TasksMax = 64; DevicePolicy = "closed"; DeviceAllow = [ "/dev/zfs rw" "char-pts rw" ] ++ lib.optional (builtins.elem "nvidia" config.services.xserver.videoDrivers) "char-nvidia* rw"; diff --git a/nixos/dashboard.nix b/nixos/dashboard.nix index 6bd0dd01cff1e19eae6a1ccf81303fa3fdadc283..b3fb316aaeb5d19fe1686ff7ecc6f4891f6ec17c 100644 --- a/nixos/dashboard.nix +++ b/nixos/dashboard.nix @@ -17,6 +17,7 @@ let fileset = lib.fileset.unions [ ../dashboard/web ../dashboard/vendor ../dashboard/package.json ../dashboard/pnpm-lock.yaml ../dashboard/tsconfig.json ../dashboard/vite.config.ts + ../dashboard/src/claude.svg ../dashboard/src/openai.svg ]; }; sourceRoot = "source/dashboard"; diff --git a/tools/computer-test.py b/tools/computer-test.py new file mode 100644 index 0000000000000000000000000000000000000000..b5b84adbd6d88e757c7dc1fb46d7f3adff190f2c --- /dev/null +++ b/tools/computer-test.py @@ -0,0 +1,298 @@ +#!/usr/bin/env python3 +"""Computer protocol and guest file boundary tests using disposable files.""" +from importlib import import_module +import base64 +import asyncio +import time +import json +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch, AsyncMock, Mock + +computer=import_module('vm-computer') +worker=import_module('mac-computer') +leases=import_module('vm-ai') + + +class Protocol(unittest.TestCase): + def test_rejects_malformed_actions_before_input(self): + for action in [None,[],{'type':[]},{'type':'click','x':True,'y':0}, + {'type':'drag','x':0,'y':0,'to_x':0,'to_y':-1}, + {'type':'key','keys':['Invalid']},{'type':'key','keys':'Enter'}, + {'type':'scroll','x':0,'y':0,'direction':'forward'}, + {'type':'wait','duration_ms':5001},{'type':'text','text':'x'*16385}, + {'type':'click','x':0,'y':0,'command':'id'}]: + with self.subTest(action=action),self.assertRaises(ValueError): + computer.validate({'action':'input','actions':[action],'screenshot':False}) + + def test_rejects_unbounded_files_and_ui_targets(self): + for request in [{'action':'file','operation':'read','path':'/tmp/x','length':65537}, + {'action':'file','operation':'write','path':'/tmp/x','data':'a'*90001}, + {'action':'file','operation':'read','path':'/tmp/x','offset':True}, + {'action':'ui','pid':True}, + {'action':'ui_action','target':{'name':'x'*5000},'operation':'press'}]: + with self.subTest(request=request),self.assertRaises(ValueError):computer.validate(request) + + def test_file_upload_is_exclusive_and_commit_does_not_replace_by_default(self): + with tempfile.TemporaryDirectory() as directory: + temporary=str(Path(directory)/'upload');destination=str(Path(directory)/'Unicode 🦊') + data=b'\0binary\xff\n'*8192 + def call(operation,**values):return worker.execute({'action':'file','operation':operation,'path':temporary,**values}) + call('write',data=base64.b64encode(data[:65536]).decode(),offset=0) + with self.assertRaises(FileExistsError):call('write',data='',offset=0) + with self.assertRaises(ValueError):call('write',data='',offset=42) + call('write',data=base64.b64encode(data[65536:]).decode(),offset=65536) + self.assertEqual(Path(temporary).read_bytes(),data) + Path(destination).write_bytes(b'existing') + with self.assertRaises(FileExistsError):call('commit',destination=destination,overwrite=False) + self.assertEqual(Path(destination).read_bytes(),b'existing') + call('commit',destination=destination,overwrite=True) + self.assertEqual(Path(destination).read_bytes(),data) + self.assertFalse(Path(temporary).exists()) + + def test_upload_refuses_symlink_and_directory(self): + with tempfile.TemporaryDirectory() as directory: + target=Path(directory)/'target';target.write_bytes(b'original') + link=Path(directory)/'link';link.symlink_to(target) + for path in [link,Path(directory)]: + with self.assertRaises(OSError):worker.execute({'action':'file','operation':'write','path':str(path),'data':'','offset':0}) + self.assertEqual(target.read_bytes(),b'original') + + def test_stale_process_handle_cannot_stop_reused_pid(self): + with patch.object(worker,'MAC',True),patch.object(worker,'process_identity',return_value='new-start'),patch.object(worker.os,'killpg') as kill: + self.assertEqual(worker.execute({'action':'process','id':'100:old-start','operation':'stop'}),{'running':False}) + kill.assert_not_called() + + +class GuestCancellation(unittest.TestCase): + def setUp(self): + directory = tempfile.TemporaryDirectory() + self.addCleanup(directory.cleanup) + root = Path(directory.name) + (root / 'fixture').mkdir() + patcher = patch.object(computer.vms, 'DISKS', root) + patcher.start() + self.addCleanup(patcher.stop) + + def test_reused_guest_pid_waits_for_current_process(self): + replies = [{'pid': 2596}, {'exited': True, 'exitcode': 1}, {'exited': False}, + {'exited': True, 'exitcode': 0, 'out-data': base64.b64encode(b'current').decode()}, + ProcessLookupError()] + with patch.object(computer, 'qga', side_effect=replies), patch.object(computer.time, 'sleep'): + self.assertEqual(computer.guest_exec('fixture', 'cmd.exe', []), b'current') + + def test_fast_reused_guest_pid_returns_last_completed_record(self): + replies = [{'pid': 2596}, {'exited': True, 'exitcode': 0, 'out-data': 'b2xk'}, + {'exited': True, 'exitcode': 0, 'out-data': 'bmV3'}, ProcessLookupError()] + with patch.object(computer, 'qga', side_effect=replies): + self.assertEqual(computer.guest_exec('fixture', 'cmd.exe', []), b'new') + + def test_guest_completion_requires_explicit_end_of_records(self): + for error in [ValueError('Disconnected'), TimeoutError()]: + replies = [{'pid': 2596}, {'exited': True, 'exitcode': 0}, error] + with self.subTest(error=type(error)), patch.object(computer, 'qga', side_effect=replies): + with self.assertRaises(type(error)): + computer.guest_exec('fixture', 'cmd.exe', []) + + def test_guest_pid_absence_is_distinct_from_transport_failure(self): + for message in ['PID 2596 does not exist', 'PID ld does not exist', 'PID lld does not exist', "Invalid parameter 'pid'", 'Guest agent is not responding']: + response = Mock(stdout='', stderr='error: ' + message + '\n') + with self.subTest(message=message), patch.object(computer.subprocess, 'run', return_value=response): + with self.assertRaises(ValueError if message == 'Guest agent is not responding' else ProcessLookupError): + computer.qga('fixture', 'guest-exec-status', pid=2596) + + def test_windows_stops_guest_task_after_its_operation_budget_expires(self): + running = [] + def execute(name, script, **kwargs): + if 'RegisterTaskDefinition' in script: + running.append(True) + computer.DEADLINE.set(time.monotonic() - 1) + return b'' + if '$t.Stop(0)' in script: + self.assertGreater(computer.remaining(20), 0) + running.clear() + return b'' + return b'True' + token = computer.DEADLINE.set(time.monotonic() + 30) + try: + with patch.object(computer, 'powershell', execute), patch.object(computer, 'write_file'): + with self.assertRaises(TimeoutError): + computer.worker('fixture', 'windows', {'action': 'input', 'actions': [{'type': 'text', 'text': 'hello'}]}) + self.assertEqual(running, []) + finally: + computer.DEADLINE.reset(token) + + def test_windows_console_remains_available_without_guest_agent(self): + def virsh(action, *args, **kwargs): + return 'running' if action == 'domstate' else 'windows' + with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'qga', side_effect=ValueError('Disconnected')), patch.object(computer, 'worker') as work: + status = computer.handle('fixture', {'action': 'status'}) + self.assertEqual(status['capabilities'], ['shot', 'input']) + self.assertEqual(status['text_encoding'], 'ascii') + actions = [{'type': 'text', 'text': 'recovery console'}] + with patch.object(computer, 'desktop', new_callable=AsyncMock, return_value={'completed': 1}) as desktop: + self.assertEqual(computer.handle('fixture', {'action': 'input', 'actions': actions, 'screenshot': False}), {'completed': 1}) + desktop.assert_awaited_once_with('fixture', actions, 'windows', False, False) + work.assert_not_called() + + def test_failed_cleanup_blocks_later_control_until_reconnection(self): + for platform, identity in [('linux', 'snowglobe-computer-request-' + 'a' * 32), ('windows', 'a' * 32)]: + operation = {'platform': platform, 'id': identity} + computer.record_operation('fixture', operation) + with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError): + computer.finish_operation('fixture', operation) + pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json' + self.assertEqual(json.loads(pending.read_text()), operation) + def virsh(action, *args, **kwargs): + return 'running' if action == 'domstate' else '' + with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work: + with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError): + computer.handle('fixture', {'action': 'status'}) + work.assert_not_called() + with patch.object(computer, 'guest_exec', return_value=b'{}'), patch.object(computer, 'powershell', return_value=b'{}'): + computer.handle('fixture', {'action': 'status'}) + work.assert_called_once() + self.assertFalse(pending.exists()) + + def test_spawn_returns_its_handle_when_cleanup_must_wait_for_reconnection(self): + result = {'id': '123:456', 'running': True} + def execute(name, script, **kwargs): + if 'DeleteTask' in script: + raise TimeoutError() + return b'True' + with patch.object(computer, 'powershell', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()): + self.assertEqual(computer.worker('fixture', 'windows', {'action': 'spawn'}), result) + pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json' + operation = json.loads(pending.read_text()) + self.assertTrue(operation['keep_spawn']) + with patch.object(computer, 'powershell', return_value=b'') as execute: + computer.finish_operation('fixture', operation) + self.assertNotIn('$t.Stop(0)', execute.call_args.args[1]) + self.assertFalse(pending.exists()) + + def test_spawn_is_stopped_if_its_success_cannot_be_persisted(self): + record = computer.record_operation + def persist(name, operation): + if operation.get('keep_spawn'): + raise OSError('Disk full') + record(name, operation) + with patch.object(computer, 'record_operation', persist), patch.object(computer, 'powershell', return_value=b'True') as execute, patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=b'{"id":"123:456"}'): + with self.assertRaises(OSError): + computer.worker('fixture', 'windows', {'action': 'spawn'}) + self.assertIn('$t.Stop(0)', execute.call_args.args[1]) + self.assertFalse((computer.vms.DISKS / 'fixture' / 'computer-operation.json').exists()) + + def test_linux_stops_the_exact_request_unit_with_a_fresh_cleanup_budget(self): + units = [] + def execute(name, executable, arguments, data=None, **kwargs): + request = json.loads(data) + if '_prepare' in request: + return b'{"prepared":true}' + if '_unit' in request: + units.append(request['_unit']) + computer.DEADLINE.set(time.monotonic() - 1) + raise TimeoutError() + self.assertEqual(request, {'_stop': units.pop()}) + self.assertGreater(computer.remaining(20), 0) + return b'{"stopped":true}' + token = computer.DEADLINE.set(time.monotonic() + 30) + try: + with patch.object(computer, 'guest_exec', execute), self.assertRaises(TimeoutError): + computer.worker('fixture', 'linux', {'action': 'status'}) + self.assertEqual(units, []) + finally: + computer.DEADLINE.reset(token) + + +class DesktopBounds(unittest.IsolatedAsyncioTestCase): + async def asyncSetUp(self): + self.writer = Mock(drain=AsyncMock(), wait_closed=AsyncMock()) + self.screen = Mock(width=3840, height=2160, start=AsyncMock()) + async def read(ready, clear): + ready() + await asyncio.Future() + self.screen.read = read + self.patches = [patch.object(computer.vms, 'console_target', return_value={'port': 5900}), + patch.object(computer.asyncio, 'open_connection', AsyncMock(return_value=(None, self.writer))), + patch.object(computer.rfb, 'Desktop', return_value=self.screen)] + for item in self.patches: + item.start() + self.addCleanup(item.stop) + + async def test_input_without_screenshot_never_encodes_or_requests_another_frame(self): + with patch.object(computer.rfb, 'png', side_effect=AssertionError('No capture requested')): + result = await computer.desktop('fixture', [{'type': 'click', 'x': 1, 'y': 1}], 'linux', False) + self.assertEqual(result, {'completed': 1}) + self.screen.request.assert_not_called() + + async def test_cancelled_batch_keeps_capture_slot_until_text_worker_returns(self): + finished = [] + def type_text(*args): + self.assertLess(computer.remaining(999), 181) + time.sleep(.06) + finished.append(True) + wait_for = asyncio.wait_for + with patch.object(computer, 'worker', type_text), patch.object(computer.asyncio, 'wait_for', lambda task, seconds: wait_for(task, .02 if seconds > 170 else seconds)): + with self.assertRaises(asyncio.TimeoutError): + await computer.desktop('fixture', [{'type': 'text', 'text': 'one'}] * 100, 'linux', False) + self.assertEqual(finished, [True]) + self.assertTrue(computer.SCREEN_SLOT.acquire(blocking=False)) + computer.SCREEN_SLOT.release() + + async def test_expired_batch_never_starts_another_guest_command(self): + token = computer.DEADLINE.set(time.monotonic() - 1) + try: + with patch.object(computer.subprocess, 'run') as run, self.assertRaises(TimeoutError): + computer.qga('fixture', 'guest-ping') + run.assert_not_called() + finally: + computer.DEADLINE.reset(token) + + +class TestVmExpiry(unittest.TestCase): + def test_creation_catalog_excludes_unprepared_windows_and_oversized_disks(self): + prepared = {'id': 'ready', 'platform': 'windows', 'setup': 'windows-v4', 'capacity': 40 * 2**30, 'os': 'Windows', 'arch': 'x86_64'} + linux = {**prepared, 'id': 'linux', 'platform': 'linux'} + broken = {**prepared, 'id': 'unprepared', 'setup': None} + oversized = {**linux, 'id': 'oversized', 'capacity': 81 * 2**30} + with patch.object(leases.vms, 'library', return_value={'presets': [prepared, linux, broken, oversized]}), patch.object(leases.vms, 'create') as create: + catalog = leases.manage({'action': 'presets', 'owner': 'fixture'}) + self.assertEqual([p['id'] for p in catalog['presets']], ['ready', 'linux']) + for preset in ['unprepared', 'oversized']: + with self.assertRaisesRegex(ValueError, 'prepared preset'): + leases.create({'action': 'create', 'owner': 'fixture', 'preset': preset, 'name': 'test', 'vcpus': 2, 'memory': 4}) + create.assert_not_called() + + def test_expiry_preserves_personal_vms_and_skips_active_calls(self): + with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.time, 'time', return_value=10000): + for name, expiry in [('personal', 1), ('ai-prefix-only', None), ('ai-expired', 9999), ('ai-live', 10001), ('ai-busy', 9999)]: + root=Path(directory)/name;root.mkdir() + if expiry is not None:(root/'ai.json').write_text(json.dumps({'expiresAt':expiry})) + with leases.vms.computer_lock('ai-busy'), patch.object(leases.vms, 'remove') as remove: + leases.reap() + remove.assert_called_once_with({'name':'ai-expired','disks':True},missing_ok=True) + + def test_activity_renews_only_test_vms_and_cannot_resurrect_expired_vms(self): + with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.time, 'time', return_value=10000): + root=Path(directory)/'ai-fixture';root.mkdir() + path=root/'ai.json';path.write_text(json.dumps({'expiresAt':10001})) + self.assertEqual(leases.lease('ai-fixture'),{'expiresAt':10001}) + self.assertEqual(leases.lease('ai-fixture',3600),{'expiresAt':13600}) + self.assertEqual(leases.lease('ai-fixture',100),{'expiresAt':13600}) + self.assertIsNone(leases.lease('personal',3600)) + path.write_text(json.dumps({'expiresAt':9999})) + with self.assertRaisesRegex(ValueError,'expired'):leases.lease('ai-fixture',86400) + + def test_foreign_owner_and_personal_vm_cannot_be_deleted_or_extended(self): + with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.vms, 'remove') as remove: + for name in ['ai-fixture','personal']:(Path(directory)/name).mkdir() + (Path(directory)/'ai-fixture/ai.json').write_text(json.dumps({'expiresAt':20000})) + with patch.object(leases.vms,'virsh',return_value='owner'): + for name, owner in [('ai-fixture','other'),('personal','owner')]: + for action in ['delete','extend']: + with self.assertRaises(ValueError):leases.manage({'action':action,'name':name,'owner':owner,'seconds':3600}) + remove.assert_not_called() + + +if __name__=='__main__':unittest.main() diff --git a/tools/dashboard-host.py b/tools/dashboard-host.py index 790026b788ed8a4ed8884910fe9da1230ec7b6d6..1cdaeaa660c5b4616f966e65d851bb7bdfa945a4 100644 --- a/tools/dashboard-host.py +++ b/tools/dashboard-host.py @@ -97,6 +97,7 @@ class Host: "host.sample": set(), "host.usage": {"refresh"}, "copyparty.manage": {"action", "payload", "users"}, + "vm.ai": {"payload"}, **dashboard_runs.FIELDS, **{"vm." + action: set() if fields is None else {"payload"} for action, fields in vms.ACTION_FIELDS.items()}, } @@ -179,6 +180,18 @@ class Host: except (OSError, ValueError, StopIteration): self.containers = None return {"at": time.monotonic(), "containers": rows} + if operation == "vm.ai": + if not VM_PREPARATION_SLOT.acquire(blocking=False): + raise Rejected("A test VM operation is running. Wait for it to finish.") + try: + process = subprocess.run(["python3", str(Path(__file__).with_name("vm-ai.py"))], + input=json.dumps(request["payload"]), capture_output=True, text=True, timeout=900) + if process.returncode == 2: + raise Rejected(process.stderr.strip()) + process.check_returncode() + return json.loads(process.stdout) + finally: + VM_PREPARATION_SLOT.release() if operation.startswith("vm."): action = operation[3:] payload = request.get("payload") @@ -186,7 +199,14 @@ class Host: vms.validate(action, payload) except ValueError as error: raise Rejected(str(error)) from error - if action in {"clipboard", "computer"}: + if action == "computer": + try: + return importlib.import_module("vm-computer").handle(payload["name"], payload["request"]) + except BlockingIOError: + raise dashboard_runs.Error(409, "Another computer operation is running. Wait for it to finish.") from None + except ValueError as error: + raise Rejected(str(error)) from error + if action == "clipboard": if not vms.mac_vms.CONFIG or payload["name"] != vms.mac_vms.CONFIG["name"]: raise Rejected("This VM does not support this guest operation.") try: diff --git a/tools/dashboard-stage-test.py b/tools/dashboard-stage-test.py index 421f055846df93eb4e63cdaca138faa8271b11b9..19abf2ae20db7d08342b5f688d2168105928d80f 100644 --- a/tools/dashboard-stage-test.py +++ b/tools/dashboard-stage-test.py @@ -4,6 +4,7 @@ from contextlib import redirect_stdout import io import json import os +import sqlite3 from pathlib import Path import struct import subprocess @@ -41,7 +42,7 @@ class PreviewBoundary(unittest.TestCase): host.handle.assert_not_called() def test_read_uses_the_real_host_handler(self): - for operation in ["host.sample", "vm.node", "vm.domains", "vm.library"]: + for operation in ["host.sample", "vm.node", "vm.domains", "vm.computers", "vm.library"]: with self.subTest(operation=operation): request = {"operation": operation} response, host = self.call(request) @@ -79,6 +80,29 @@ class PreviewBoundary(unittest.TestCase): self.assertEqual(host.handle(request), "real snapshot") handler.assert_called_once_with(request) + def test_preview_vm_creation_and_ownership_stay_in_its_account(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + (root / "data").mkdir() + with sqlite3.connect(root / "data/accounts.sqlite") as accounts: + accounts.execute("CREATE TABLE users (id TEXT, username TEXT)") + accounts.execute("INSERT INTO users VALUES ('preview-id', 'preview')") + host = stage.PreviewHost({"clone": "globe/staging/fixture", "mount": directory, "computer": "mac-fixture,linux-fixture"}) + with patch.object(boundary.Host, "handle", return_value={"owner": "preview-id", "ownerOnly": True}) as call: + for name in ["mac-fixture", "linux-fixture", "ai-new"]: + self.assertEqual(host.handle({"operation": "vm.owner", "payload": {"name": name}})["owner"], "preview-id") + for owner in ["foreign-id", None]: + call.reset_mock() + with self.assertRaises(boundary.dashboard_runs.Error): + host.handle({"operation": "vm.ai", "payload": {"action": "create", "owner": owner}}) + call.assert_not_called() + request = {"operation": "vm.ai", "payload": {"action": "create", "owner": "preview-id"}} + host.handle(request) + call.assert_called_once_with(request) + with patch.object(boundary.Host, "handle", return_value={"owner": "foreign-id", "ownerOnly": True}): + with self.assertRaises(boundary.dashboard_runs.Error): + host.handle({"operation": "vm.computer", "payload": {"name": "ai-foreign"}}) + def test_failed_child_creation_and_repeated_token_revoke_still_clean_parent(self): identity = "dashboard-preview-12345678" dataset = "globe/staging/" + identity diff --git a/tools/dashboard-stage.py b/tools/dashboard-stage.py index 67637b573a137944a914f625f13555e25ca4d9e8..58c08ca7c4030a9c783040f20c318798c5bdc2dc 100644 --- a/tools/dashboard-stage.py +++ b/tools/dashboard-stage.py @@ -21,7 +21,7 @@ import snowglobe OPERATIONS = frozenset({ "shale.access", "host.sample", "host.usage", "storage.mounts", "storage.datasets", "storage.pool", - "storage.snapshots", "vm.node", "vm.domains", "vm.library", "deploy.current", "deploy.main", "deploy.history", + "storage.snapshots", "vm.node", "vm.domains", "vm.computers", "vm.library", "deploy.current", "deploy.main", "deploy.history", "deploy.stages", "deploy.managed", "deploy.release", "deploy.last", "files.snapshot", "files.snapshots", "files.discard", }) @@ -36,10 +36,24 @@ class PreviewHost(boundary.Host): def handle(self, request): operation = request.get("operation") if isinstance(request, dict) else None - if operation in {"vm.computer", "vm.owner"}: + owner = None + if self.stage.get("computer") and self.stage.get("mount") and operation in {"vm.computer", "vm.owner", "vm.domains", "vm.computers", "vm.ai", "vm.act"}: + with sqlite3.connect(f"file:{self.stage['mount']}/data/accounts.sqlite?mode=ro", uri=True) as accounts: + owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone() + if not owner: + raise ValueError("The preview account is unavailable.") + if operation == "vm.ai": payload = request.get("payload") - if not self.stage.get("computer") or not isinstance(payload, dict) or payload.get("name") != self.stage["computer"]: + if not owner or not isinstance(payload, dict) or payload.get("owner") != owner[0]: + raise boundary.dashboard_runs.Error(403, "Test VMs must belong to this preview account.") + if operation in {"vm.computer", "vm.owner", "vm.act"}: + payload = request.get("payload") + if not self.stage.get("computer") or not isinstance(payload, dict): raise boundary.dashboard_runs.Error(403, "Choose the VM explicitly attached to this preview.") + if payload.get("name") not in self.stage["computer"].split(","): + actual = super().handle({"operation": "vm.owner", "payload": {"name": payload.get("name")}}) if owner else {} + if not owner or actual.get("owner") != owner[0] or actual.get("ownerOnly") is not True: + raise boundary.dashboard_runs.Error(403, "Choose a VM attached to or created by this preview.") if operation == "shale.access": service = request.get("service", "") if not re.fullmatch(r"shale-preview-[0-9a-f]{8}", service): @@ -58,7 +72,13 @@ class PreviewHost(boundary.Host): ).stdout) mounted["filesystems"][0]["target"] = "/srv" return mounted - return super().handle(request) + result = super().handle(request) + if owner and operation in {"vm.owner", "vm.domains", "vm.computers"}: + if operation == "vm.owner": + result = {**result, "owner": owner[0], "ownerOnly": True} + else: + result = [{**vm, "owner": owner[0], "ownerOnly": True} if vm["name"] in self.stage["computer"].split(",") else vm for vm in result] + return result def metadata(stage): @@ -91,7 +111,7 @@ def serve(stage): if not slots.acquire(blocking=False): connection.close() continue - operations = OPERATIONS | {"vm.computer", "vm.owner"} if saved.get("computer") else OPERATIONS + operations = OPERATIONS | {"vm.computer", "vm.owner", "vm.ai", "vm.act"} if saved.get("computer") else OPERATIONS work = workers.submit(boundary.serve_connection, connection, host, saved["uid"], operations) work.add_done_callback(lambda _: slots.release()) @@ -121,8 +141,13 @@ def stage(computer=None): if saved.get("computer"): configured = next((value.partition("=")[2] for value in host_environment if value.startswith("SNOWGLOBE_MAC_VM=")), "null") mac = json.loads(configured) - if not mac or mac["name"] != saved["computer"]: - raise ValueError("Choose the configured managed Mac VM for this preview.") + selected = saved["computer"].split(",") + for name in selected: + boundary.vms.ensure_name(name) + if not mac or name != mac["name"]: + snowglobe.command("virsh", "domuuid", name, capture=True) + if mac and mac["name"] not in selected: + mac = None nix = ["nix", "--extra-experimental-features", "nix-command flakes"] package = "path:" + str(snowglobe.REPO) + "#dashboard-image" image = subprocess.run([*nix, "eval", "--raw", package, "--apply", 'image: "${image.imageName}:${image.imageTag}"'], @@ -183,6 +208,9 @@ def stage(computer=None): snowglobe.write_private(root / "access.json", imported.stdout) runtime = "/run/snowglobe-preview/" + identity computer_environment = [] + if saved.get("computer"): + ahk = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#computer-ahk", capture=True).stdout.strip() + computer_environment.append("--setenv=SNOWGLOBE_COMPUTER_AHK=" + ahk) if mac: with sqlite3.connect(f"file:{root / 'data/accounts.sqlite'}?mode=ro", uri=True) as accounts: owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone() @@ -193,7 +221,7 @@ def stage(computer=None): host_python = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip() subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True) snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity, - "--property=Restart=on-failure", "--property=MemoryMax=256M", "--property=TasksMax=32", + "--property=Restart=on-failure", "--property=MemoryMax=" + str(boundary.PROTOCOL["memory"]), "--property=TasksMax=32", "--setenv=" + host_path, "--setenv=SNOWGLOBE_DOMAIN=" + site["domain"], *computer_environment, host_python + "/bin/python3", str(Path(__file__).resolve()), "--serve", identity) diff --git a/tools/dashboard-vms-test.py b/tools/dashboard-vms-test.py index d403aad0ca3e22efd66973c0bb164d52a9ff944b..0ec362badd5edbf294be0f86212588953981046d 100644 --- a/tools/dashboard-vms-test.py +++ b/tools/dashboard-vms-test.py @@ -177,7 +177,7 @@ class VMTests(unittest.TestCase): config = captured['config'] self.assertEqual(config['bootcmd'][0], ['rm', '-f', '/etc/gdm/custom.conf']) self.assertIn('/run/current-system/sw/bin/getent', config['bootcmd'][1][-1]) - self.assertEqual(config['write_files'], [{ + self.assertEqual(config['write_files'][:2], [{ 'path': '/etc/gdm/custom.conf', 'content': '[daemon]\nAutomaticLoginEnable=true\nAutomaticLogin=snow\n', }, { 'path': '/var/lib/snowglobe/hostname', @@ -188,7 +188,7 @@ class VMTests(unittest.TestCase): def test_console_cannot_choose_an_arbitrary_network_target(self): xml = "fixture-vm" - def virsh(*args): + def virsh(*args, **kwargs): return 'running' if args[0] == 'domstate' else xml with patch.object(vms, 'virsh', side_effect=virsh): self.assertEqual(vms.console_target('ubuntu'), {'port': 5901, 'uuid': 'fixture-vm'}) @@ -259,7 +259,7 @@ class VMTests(unittest.TestCase): def test_serial_target_cannot_choose_a_host_file(self): xml = "" - def virsh(*args): + def virsh(*args, **kwargs): return 'running' if args[0] == 'domstate' else xml with patch.object(sys, 'argv', ['vms.py', 'serial', json.dumps({'name': 'ubuntu'})]), patch.object(vms, 'virsh', side_effect=virsh): self.assertEqual(vms.main(), {'path': '/dev/pts/3'}) diff --git a/tools/deploy.py b/tools/deploy.py index def85e8188bf6823b35c6bcd153d91d1e0d07dca..05c1dde02626f00ca24e0a1ad27344c1256f875a 100644 --- a/tools/deploy.py +++ b/tools/deploy.py @@ -160,7 +160,7 @@ def upload(main=False): def main(): parser = argparse.ArgumentParser(description="Preview working changes and deploy main") parser.add_argument("mode", choices=["stage", "publish", "prod", "rollback", "history", "backups", "data-restore", "bootstrap", "allocate", "destroy", "secrets"]) - parser.add_argument("--computer", help="Give a dashboard preview access to this managed Mac VM") + parser.add_argument("--computer", help="Give a dashboard preview access to these comma-separated VM names") parser.add_argument("target", nargs="?") parser.add_argument("--env", action="append", default=[]) parser.add_argument("--file", type=Path) @@ -168,8 +168,8 @@ def main(): parser.add_argument("--backup") parser.add_argument("--discard-writes", action="store_true") args = parser.parse_args() - if args.computer and (args.mode != "stage" or args.target != "dashboard" or not NAME.fullmatch(args.computer)): - parser.error("--computer requires stage dashboard and a VM name") + if args.computer and (args.mode != "stage" or args.target != "dashboard" or not all(NAME.fullmatch(name) for name in args.computer.split(","))): + parser.error("--computer requires stage dashboard and VM names") if args.mode in {"stage", "allocate", "destroy", "secrets", "data-restore"} and (not args.target or not NAME.fullmatch(args.target)): parser.error(f"{args.mode} requires a service or stage ID") if args.mode == "rollback" and args.target and not RELEASE_ID.fullmatch(args.target): diff --git a/tools/mac-agent-gui.swift b/tools/mac-agent-gui.swift index 0cbff14783e64236745de2708619c55c23140dc9..2204cb612f364d8d461559e0b583553c326a850f 100644 --- a/tools/mac-agent-gui.swift +++ b/tools/mac-agent-gui.swift @@ -21,7 +21,9 @@ func attribute(_ element: AXUIElement, _ name: String) -> CFTypeRef? { func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String: Any] { remaining -= 1 - var result: [String: Any] = ["path": path] + var pid: pid_t = 0 + AXUIElementGetPid(element, &pid) + var result: [String: Any] = ["path": path, "target": ["pid": pid, "path": path, "role": attribute(element, "AXRole") as? String ?? "", "name": attribute(element, "AXTitle") as? String ?? ""]] for name in ["AXRole", "AXSubrole", "AXTitle", "AXDescription", "AXValue", "AXEnabled", "AXFocused"] { if let value = attribute(element, name), value is String || value is NSNumber { result[name] = value @@ -43,6 +45,10 @@ func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String: } var actions: CFArray? if AXUIElementCopyActionNames(element, &actions) == .success { result["actions"] = actions } + var settable = DarwinBoolean(false) + if AXUIElementIsAttributeSettable(element, kAXValueAttribute as CFString, &settable) == .success, settable.boolValue { + result["actions"] = (result["actions"] as? [String] ?? []) + ["set_value"] + } if path.count < 16, let children = attribute(element, "AXChildren") as? [AXUIElement] { var nodes: [[String: Any]] = [] for (index, child) in children.enumerated() where remaining > 0 { @@ -54,6 +60,25 @@ func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String: return result } +func typeText(_ text: String, _ eventSource: CGEventSource) { + let units = Array(text.utf16) + var start = 0 + while start < units.count { + var end = min(start + 20, units.count) + if end < units.count && (0xD800...0xDBFF).contains(units[end - 1]) { end -= 1 } + let chunk = Array(units[start.. CGPoint { + guard let px = action[x] as? Double, let py = action[y] as? Double, + px >= 0, py >= 0, px < pixelWidth, py < pixelHeight else { fail("Choose coordinates inside the latest screenshot.") } + return CGPoint(x: bounds.minX + px * bounds.width / pixelWidth, y: bounds.minY + py * bounds.height / pixelHeight) + } + func mouse(_ type: CGEventType, _ at: CGPoint, _ button: CGMouseButton, _ count: Int64 = 1) { + let event = CGEvent(mouseEventSource: eventSource, mouseType: type, mouseCursorPosition: at, mouseButton: button) + event?.setIntegerValueField(.mouseEventClickState, value: count) + event?.flags = [] + event?.post(tap: .cghidEventTap) + usleep(10000) + } + let keys: [String: UInt16] = ["a":0,"s":1,"d":2,"f":3,"h":4,"g":5,"z":6,"x":7,"c":8,"v":9,"b":11,"q":12,"w":13,"e":14,"r":15,"y":16,"t":17,"1":18,"2":19,"3":20,"4":21,"6":22,"5":23,"=":24,"9":25,"7":26,"-":27,"8":28,"0":29, + "]":30,"o":31,"u":32,"[":33,"i":34,"p":35,"Enter":36,"l":37,"j":38,"'":39,"k":40,";":41,"\\":42,",":43,"/":44,"n":45,"m":46,".":47,"Tab":48,"Space":49,"`":50,"Backspace":51,"Escape":53,"Home":115,"PageUp":116,"Delete":117,"End":119,"PageDown":121,"Left":123,"Right":124,"Down":125,"Up":126,"F1":122,"F2":120,"F3":99,"F4":118,"F5":96,"F6":97,"F7":98,"F8":100,"F9":101,"F10":109,"F11":103,"F12":111] + for action in actions { + guard let kind = action["type"] as? String else { fail("Choose a desktop action.") } + switch kind { + case "click", "move", "drag": + let at = point(action) + let button: CGMouseButton = action["button"] as? String == "right" ? .right : (action["button"] as? String == "middle" ? .center : .left) + let down: CGEventType = button == .left ? .leftMouseDown : (button == .right ? .rightMouseDown : .otherMouseDown) + let up: CGEventType = button == .left ? .leftMouseUp : (button == .right ? .rightMouseUp : .otherMouseUp) + mouse(.mouseMoved, at, button) + if kind == "click" { + for count in 1...max(1,min(action["count"] as? Int ?? 1,3)) { mouse(down,at,button,Int64(count)); mouse(up,at,button,Int64(count)) } + } else if kind == "drag" { + let end = point(action,"to_x","to_y") + mouse(down,at,button) + for step in 1...20 { + let p = CGPoint(x:at.x+(end.x-at.x)*Double(step)/20,y:at.y+(end.y-at.y)*Double(step)/20) + mouse(button == .left ? .leftMouseDragged : (button == .right ? .rightMouseDragged : .otherMouseDragged),p,button) + usleep(UInt32(max(1,min(action["duration_ms"] as? Int ?? 400,5000)))*50) + } + mouse(up,end,button) + } + case "scroll": + mouse(.mouseMoved,point(action),.left) + let amount = Int32(max(1,min(action["amount"] as? Int ?? 3,100))) + let direction = action["direction"] as? String ?? "down" + let vertical: Int32 = direction == "up" ? amount : (direction == "down" ? -amount : 0) + let horizontal: Int32 = direction == "left" ? amount : (direction == "right" ? -amount : 0) + CGEvent(scrollWheelEvent2Source:eventSource,units:.line,wheelCount:2,wheel1:vertical,wheel2:horizontal,wheel3:0)?.post(tap:.cghidEventTap) + case "key": + guard let chord = action["keys"] as? [String], (1...8).contains(chord.count) else { fail("Send between 1 and 8 key names.") } + var flags: CGEventFlags = [] + var codes: [UInt16] = [] + for key in chord { + switch key { + case "Meta": flags.insert(.maskCommand) + case "Control": flags.insert(.maskControl) + case "Alt": flags.insert(.maskAlternate) + case "Shift": flags.insert(.maskShift) + default: + guard let code = keys[key] ?? keys[key.lowercased()] else { fail("Choose a supported key name.") } + codes.append(code) + if key.count == 1 && key != key.lowercased() { flags.insert(.maskShift) } + } + } + for down in [true,false] { + for code in down ? codes : Array(codes.reversed()) { + let event = CGEvent(keyboardEventSource:eventSource,virtualKey:code,keyDown:down) + event?.flags = down ? flags : [] + event?.post(tap:.cghidEventTap) + usleep(10000) + } + } + case "text": + guard let text = action["text"] as? String, text.utf8.count <= 16384 else { fail("Type at most 16 KiB at a time.") } + typeText(text, eventSource) + case "wait": usleep(UInt32(max(1,min(action["duration_ms"] as? Int ?? 1,5000)))*1000) + default: fail("Choose a supported desktop action.") + } + } + output(["completed":actions.count]) case "click": guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") } guard arguments.count == 3, let x = Double(arguments[1]), let y = Double(arguments[2]) else { fail("Provide x and y coordinates.") } @@ -145,22 +278,7 @@ case "type": guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") } guard arguments.count == 2 else { fail("Provide text, or a dash to read UTF-8 from stdin.") } let text = arguments[1] == "-" ? String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)! : arguments[1] - let units = Array(text.utf16) - var start = 0 - while start < units.count { - var end = min(start + 20, units.count) - if end < units.count && (0xD800...0xDBFF).contains(units[end - 1]) { end -= 1 } - let chunk = Array(units[start..4096: + raise ValueError('Use an absolute guest file path.') + path=Path(path) + operation=request.get('operation') + if operation in ('stat','list'): + info=path.stat() + result={'path':str(path),'size':info.st_size,'directory':stat.S_ISDIR(info.st_mode),'modified':info.st_mtime} + if operation=='list': + entries=[] + with os.scandir(path) as iterator: + for entry in iterator: + if len(entries)==1000: + result['truncated']=True; break + entries.append({'name':entry.name,'directory':entry.is_dir(follow_symlinks=False)}) + result['entries']=entries + return result + if operation=='mkdir': + path.mkdir(parents=True,exist_ok=True); return {'path':str(path)} + if operation=='read': + offset,length=request.get('offset',0),request.get('length',65536) + if type(offset) is not int or offset<0 or type(length) is not int or not 1<=length<=65536: + raise ValueError('Read at most 64 KiB at a time from a nonnegative offset.') + with path.open('rb') as incoming: + if not stat.S_ISREG(os.fstat(incoming.fileno()).st_mode): + raise ValueError('Choose a regular guest file.') + incoming.seek(offset);data=incoming.read(length) + return {'data':base64.b64encode(data).decode(),'bytes':len(data)} + if operation=='write': + data=base64.b64decode(request.get('data',''),validate=True) + offset=request.get('offset',0) + if len(data)>65536 or type(offset) is not int or not 0<=offset<=64*2**20: + raise ValueError('The transfer chunk exceeds its limit.') + flags=os.O_WRONLY|os.O_NOFOLLOW|(os.O_CREAT|os.O_EXCL if offset==0 else 0) + with os.fdopen(os.open(path,flags,0o600),'r+b') as outgoing: + if not stat.S_ISREG(os.fstat(outgoing.fileno()).st_mode) or os.fstat(outgoing.fileno()).st_size!=offset: + raise ValueError('The transfer offset changed. Start a new upload.') + outgoing.seek(offset);outgoing.write(data) + return {'bytes':len(data)} + if operation=='commit': + destination=request.get('destination') + if not isinstance(destination,str) or not destination.startswith('/') or '\x00' in destination: + raise ValueError('Use an absolute guest destination path.') + if request.get('overwrite',False): os.replace(path,destination) + else: + os.link(path,destination);path.unlink() + return {'path':destination} + if operation=='remove': + path.unlink();return {'removed':True} + raise ValueError('Choose a supported file operation.') + + +def linux_ui(request): + try: + import gi + gi.require_version('Atspi','2.0') + from gi.repository import Atspi, GLib + except (ImportError,ValueError): + raise ValueError('Install the AT-SPI Python bindings in this guest, then retry.') from None + Atspi.set_timeout(1500,3000) + desktop=Atspi.get_desktop(0) + if request['action']=='input': + for item in request['actions']: + if item['type']!='text': raise ValueError('Use the VM console for pointer and key actions.') + pending=[desktop] + focused=None + deadline=time.monotonic()+8 + visited=0 + while pending and visited<1500 and time.monotonic()=1500 or depth>20 or time.monotonic()>deadline: return + try: + pid=node.get_process_id() + if len(path)==1 and request.get('pid') and pid!=request['pid']: return + name=node.get_name() or '' + role=node.get_role_name() + entry={'target':{'path':path,'pid':pid,'started':process_identity(pid) if pid else '', 'role':role,'name':name[:512]},'role':role,'name':name[:512]} + states=node.get_state_set() + entry['focused']=states.contains(Atspi.StateType.FOCUSED) + entry['enabled']=states.contains(Atspi.StateType.ENABLED) + entry['actions']=[] + interfaces=node.get_interfaces() + if 'Component' in interfaces: + if os.environ.get('XDG_SESSION_TYPE')!='wayland' and not os.environ.get('WAYLAND_DISPLAY'): + bounds=node.get_component_iface().get_extents(Atspi.CoordType.SCREEN) + entry['bounds']=[bounds.x,bounds.y,bounds.width,bounds.height] + if states.contains(Atspi.StateType.FOCUSABLE): entry['actions'].append('focus') + if 'Action' in interfaces: + action=node.get_action_iface() + entry['actions'] += [action.get_action_name(i) for i in range(min(action.get_n_actions(),32))] + if 'Text' in interfaces: + entry['value']=Atspi.Text.get_text(node,0,min(Atspi.Text.get_character_count(node),1024)) + if 'EditableText' in interfaces: entry['actions'].append('set_value') + nodes.append(entry) + for index in range(min(node.get_child_count(),1500)): + if len(nodes)>=1500 or time.monotonic()>deadline: break + child=node.get_child_at_index(index) + if child: walk(child,path+[index],depth+1) + except Exception: + return + walk(desktop,[],0) + return {'nodes':nodes,'truncated':len(nodes)>=1500 or time.monotonic()>deadline} def execute(request): @@ -20,24 +216,74 @@ def execute(request): raise ValueError("Send a computer operation.") action = request.get("action") fields = {"status": {"action"}, "shot": {"action"}, "ui": {"action", "pid"}, - "exec": {"action", "script", "timeout_ms", "screenshot"}} - if not isinstance(action, str) or action not in fields or set(request) != fields[action]: + "ui_action": {"action", "target", "operation", "value", "screenshot"}, + "input": {"action", "actions", "screenshot"}, "spawn": {"action", "script", "runtime"}, + "process": {"action", "id", "operation"}, + "file": {"action", "operation", "path", "offset", "data", "length", "destination", "overwrite"}, + "exec": {"action", "script", "runtime", "timeout_ms", "screenshot"}} + if not isinstance(action, str) or action not in fields or set(request) - fields[action]: raise ValueError("Use the fields listed for this computer operation.") - if action == "ui" and (request["pid"] is not None and (type(request["pid"]) is not int or request["pid"] <= 0)): + if action == "file": + return file_operation(request) + if action in ("input", "ui_action"): + if MAC: + result = subprocess.run([GUI, "input" if action == "input" else "action"], + input=json.dumps(request).encode(), capture_output=True, timeout=30) + reply = json.loads(result.stdout) + if result.returncode: + raise ValueError(reply.get("error", "The desktop action failed. Read the UI again.")) + if request.get("screenshot"): + reply.update(execute({"action":"shot"})) + return reply + return linux_ui(request) + if action == "process": + return process_operation(request) + if action == "ui" and (request.get("pid") is not None and (type(request["pid"]) is not int or request["pid"] <= 0)): raise ValueError("Choose a positive application PID, or omit it for the active app.") - if action == "exec": - if not isinstance(request["script"], str) or not 1 <= len(request["script"].encode()) <= 65536: + if action in ("exec", "spawn"): + if not isinstance(request.get("script"), str) or not 1 <= len(request["script"].encode()) <= 65536: raise ValueError("Provide a script of at most 64 KiB.") - if type(request["timeout_ms"]) is not int or not 1 <= request["timeout_ms"] <= 180000: + runtime = request.get("runtime", "default") + runtime = ("zsh" if MAC else "bash") if runtime == "default" else runtime + shell = shutil.which(runtime) if runtime in ("bash", "zsh") else None + if not shell: + raise ValueError("Choose a runtime reported by computer_status.") + if action == "spawn": + if not MAC: + unit="snowglobe-computer-"+uuid.uuid4().hex + subprocess.run(["systemd-run","--user","--quiet","--collect","--service-type=exec","--unit="+unit, + shell,"-c",request["script"]],check=True,timeout=10) + pid=subprocess.run(["systemctl","--user","show",unit,"-p","MainPID","--value"],capture_output=True,text=True,timeout=5).stdout.strip() + return {"id":unit,"pid":int(pid) if pid.isdigit() else None} + process = subprocess.Popen([shell,"-c",request["script"]], stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + return {"id": str(process.pid) + ":" + process_identity(process.pid), "pid":process.pid} + if action == "exec": + if type(request.get("timeout_ms")) is not int or not 1 <= request["timeout_ms"] <= 180000: raise ValueError("Choose a timeout between 1 and 180000 milliseconds.") - if type(request["screenshot"]) is not bool: + if type(request.get("screenshot")) is not bool: raise ValueError("Choose whether to return a screenshot.") if action == "status": - result = subprocess.run([GUI, "status"], capture_output=True, timeout=10, check=True) - return {"permissions": json.loads(result.stdout), "hostname": os.uname().nodename, - "platform": "macos", "script": "zsh", "max_timeout_ms": 180000} + permissions = {} + if MAC: + result = subprocess.run([GUI, "status"], capture_output=True, timeout=10, check=True) + permissions = json.loads(result.stdout) + else: + try: + import gi + gi.require_version("Atspi", "2.0") + from gi.repository import Atspi + permissions["accessibility"] = Atspi.get_desktop(0) is not None + except (ImportError, ValueError): + permissions["accessibility"] = False + return {"permissions": permissions, "hostname": os.uname().nodename, + "platform": "macos" if MAC else "linux", "runtimes": [r for r in ("zsh","bash") if shutil.which(r)], + "capabilities": ["shot","input","exec","spawn","process","file"] + (["ui","ui_action"] if permissions.get("accessibility") else []), + "max_timeout_ms": 180000, "coordinate_units":"screenshot_pixels"} if action == "ui": - args = [GUI, "tree"] + ([] if request["pid"] is None else [str(request["pid"])]) + if not MAC: + return linux_ui(request) + args = [GUI, "tree"] + ([] if request.get("pid") is None else [str(request["pid"])]) result = subprocess.run(args, capture_output=True, timeout=15, check=True) if len(result.stdout) > 2 * 1024 * 1024: raise ValueError("The UI tree is too large. Select an application PID.") @@ -48,7 +294,7 @@ def execute(request): script = Path(directory) / "script" script.write_text(request["script"]) with script.open("rb") as source, selectors.DefaultSelector() as selector: - process = subprocess.Popen(["/bin/zsh", "-s"], stdin=source, + process = subprocess.Popen([shell, "-s"], stdin=source, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True) streams = {"stdout": bytearray(), "stderr": bytearray()} truncated = [] @@ -77,13 +323,14 @@ def execute(request): break finally: if process.poll() is None or reply.get("timed_out"): - try: - cleanup = subprocess.run(["/usr/bin/sudo", "-n", "/bin/kill", "-KILL", "--", "-" + str(process.pid)], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5) - if cleanup.returncode: + if MAC: + try: + cleanup = subprocess.run(["/usr/bin/sudo", "-n", "/bin/kill", "-KILL", "--", "-" + str(process.pid)], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5) + if cleanup.returncode: + reply["cleanup_error"] = "Privileged cleanup failed. Check for remaining child processes." + except (OSError, subprocess.SubprocessError): reply["cleanup_error"] = "Privileged cleanup failed. Check for remaining child processes." - except (OSError, subprocess.SubprocessError): - reply["cleanup_error"] = "Privileged cleanup failed. Check for remaining child processes." try: os.killpg(process.pid, signal.SIGKILL) except (ProcessLookupError, PermissionError): @@ -94,7 +341,7 @@ def execute(request): reply.update(exit=process.returncode, **{k: bytes(v).decode("utf-8", "replace") for k, v in streams.items()}) if truncated: reply["truncated"] = truncated - if action == "shot" or request.get("screenshot"): + if MAC and (action == "shot" or request.get("screenshot")): shot = Path(directory) / "screen.png" try: result = subprocess.run([GUI, "screenshot", str(shot)], capture_output=True, timeout=15, check=True) @@ -116,19 +363,19 @@ def main(): if len(raw) > 524288: raise ValueError("The computer request is too large.") request = json.loads(raw) - lock_path = Path.home() / "Library/Caches/net.paperclover.computer.lock" + lock_path = Path.home() / ("Library/Caches/net.paperclover.computer.lock" if MAC else ".cache/snowglobe-computer.lock") + lock_path.parent.mkdir(parents=True, exist_ok=True) descriptor = os.open(lock_path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600) with os.fdopen(descriptor, "w") as lock: - try: - fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) - except BlockingIOError: - raise ValueError("Another computer operation is running. Wait for it to finish.") + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) return execute(request) if __name__ == "__main__": try: print(json.dumps(main(), ensure_ascii=True)) + except BlockingIOError: + print(json.dumps({"error": "Another computer operation is running. Wait for it to finish.", "status": 409})) except ValueError as error: print(json.dumps({"error": str(error)})) except (OSError, subprocess.SubprocessError): diff --git a/tools/mac-vm-bridge.py b/tools/mac-vm-bridge.py index fc6bcf6d95e5120795cf48709145445e2940cde3..e2b5b76ad29fcfe654db075d35faf65b5a3e5c55 100644 --- a/tools/mac-vm-bridge.py +++ b/tools/mac-vm-bridge.py @@ -77,6 +77,8 @@ def computer(request): if not isinstance(value, dict): raise ValueError("The Mac guest returned an incomplete response.") if "error" in value: + if value.get("status") == 409: + raise BlockingIOError(str(value["error"])) raise ValueError(str(value["error"])) return value finally: @@ -151,6 +153,9 @@ if __name__ == "__main__": signal.signal(signal.SIGTERM, disconnected) try: main() + except BlockingIOError as error: + respond({"error": str(error), "status": 409}) + sys.exit(1) except (ValueError, OSError, subprocess.SubprocessError) as error: print(type(error).__name__, file=sys.stderr) respond({"error": str(error) if isinstance(error, ValueError) else "The Mac VM did not answer. Check its host, then retry."}) diff --git a/tools/mac-vms.py b/tools/mac-vms.py index 3dde6b9e8d8ce6941efa850f46769a13389389d0..ca0aed41c1300b2591e31e85a7b725ebe35405f7 100644 --- a/tools/mac-vms.py +++ b/tools/mac-vms.py @@ -46,10 +46,14 @@ def connect(request, timeout=10): raise ValueError("The Mac response is too large.") reply = json.loads(read(size)) if "error" in reply: + if reply.get("status") == 409: + raise BlockingIOError(reply["error"]) raise ValueError(reply["error"]) if "value" not in reply: raise ValueError("The Mac response is incomplete.") yield reply["value"], parent + except BlockingIOError: + raise except OSError as error: raise ValueError("Mac Studio is unreachable. Check its power and network, then retry.") from error finally: @@ -72,7 +76,7 @@ def domain(): if not CONFIG: return [] value = {"name": CONFIG["name"], **ownership(), "host": "Mac Studio", "arch": "aarch64", - "description": "macOS development", "state": "unreachable", "reason": None, + "state": "unreachable", "reason": None, "os": "macOS", "vcpus": None, "memory": None, "balloon": None, "pinned": None, "autostart": False, "startedAt": None, "agent": None, "setup": None, "pendingAction": None, "disks": [], "media": [], "firmware": "uefi", "platform": "macos", "console": True, diff --git a/tools/release.py b/tools/release.py index 5e85befaeacfe08046e1a0a024720473a6689269..77d6e811df952738d8b143eadf8de3384ff22ca4 100644 --- a/tools/release.py +++ b/tools/release.py @@ -60,7 +60,7 @@ def host_digest(root): digest = hashlib.sha256() paths = sorted(path for path, relative in files(root) if relative.parts[0] in {"nixos", "dashboard", "config", "service"} - or str(relative) in {"flake.nix", "flake.lock", "tools/router.py", "tools/dashboard-host.py", "tools/shale-access.py", "tools/shale-labels.py", "tools/dashboard-run.py", "tools/release.py", "tools/vms.py", "tools/mac-vms.py", "tools/vm-screen.py", "guest/protocol.json"}) + or str(relative) in {"flake.nix", "flake.lock", "tools/router.py", "tools/dashboard-host.py", "tools/shale-access.py", "tools/shale-labels.py", "tools/dashboard-run.py", "tools/release.py", "tools/vms.py", "tools/mac-vms.py", "tools/vm-screen.py", "tools/vm-rfb.py", "tools/vm-computer.py", "tools/vm-ai.py", "tools/mac-computer.py", "guest/computer-windows.cs", "guest/computer-legacy-mac.py", "guest/computer-linux.py", "guest/computer-linux-launch", "guest/computer-linux.nix", "guest/protocol.json"}) for path in paths: digest.update(str(path.relative_to(root)).encode()) _hash_contents(digest, path) diff --git a/tools/vm-ai.py b/tools/vm-ai.py new file mode 100644 index 0000000000000000000000000000000000000000..d73672b79911ea203d37fe77a52ddee735a08540 --- /dev/null +++ b/tools/vm-ai.py @@ -0,0 +1,132 @@ +"""Owner-scoped disposable VMs and idle expiry.""" +import fcntl +import json +import os +from pathlib import Path +import re +import shutil +import sys +import time +import uuid +import xml.etree.ElementTree as ET + +import vms + +LIMITS = {"per_user": 2, "host_total": 8, "vcpus": 4, "memory_gib": 8, + "idle_seconds": 3600, "extension_seconds": 86400} + + +def presets(): + return [p for p in vms.library()["presets"] if p["capacity"] <= 80 * 2**30 and + (p["platform"] == "linux" or p.get("setup") in {"windows-v3", "windows-v4"})] + + +def lease(name, seconds=None): + vms.ensure_name(name) + path = vms.DISKS / name / "ai.json" + try: + with os.fdopen(vms.open_regular(path)) as incoming: + value = json.load(incoming) + except FileNotFoundError: + return None + if not name.startswith("ai-") or type(value.get("expiresAt")) is not int: + raise ValueError("The test VM's expiry record needs repair.") + if seconds is not None: + now = int(time.time()) + if value["expiresAt"] <= now: + raise ValueError("This test VM expired. Create another VM.") + value["expiresAt"] = max(value["expiresAt"], now + seconds) + temporary = path.with_suffix(".new") + with open(temporary, "w", opener=lambda p, flags: os.open(p, flags | os.O_NOFOLLOW, 0o600)) as outgoing: + json.dump(value, outgoing) + os.replace(temporary, path) + return value + + +def create(request): + if set(request) != {"action", "owner", "preset", "name", "vcpus", "memory"}: + raise ValueError("Use the fields listed for creating a test VM.") + if not isinstance(request["owner"], str) or not re.fullmatch(r"[a-zA-Z0-9_-]{1,128}", request["owner"]): + raise ValueError("Choose a signed-in account to own this VM.") + if not isinstance(request["name"], str) or not re.fullmatch(r"[a-z0-9][a-z0-9-]{0,35}", request["name"]): + raise ValueError("Use a name of 1–36 lowercase letters, numbers, or hyphens.") + if type(request["vcpus"]) is not int or not 1 <= request["vcpus"] <= LIMITS["vcpus"]: + raise ValueError("Choose between 1 and 4 vCPUs.") + if type(request["memory"]) is not int or not 1 <= request["memory"] <= LIMITS["memory_gib"]: + raise ValueError("Choose between 1 and 8 GiB of memory.") + preset = next((p for p in presets() if p["id"] == request["preset"]), None) + if preset is None: + raise ValueError("Choose a prepared preset from computer_presets.") + with open(vms.DISKS / "ai-create.lock", "w", opener=lambda p, flags: os.open(p, flags | os.O_NOFOLLOW, 0o600)) as lock: + fcntl.flock(lock, fcntl.LOCK_EX) + owned = total = 0 + for name in vms.virsh("list", "--all", "--name").splitlines(): + if not lease(name): + continue + total += 1 + root = ET.fromstring(vms.virsh("dumpxml", name)) + owned += root.findtext(f"metadata/{vms.NS}vm/{vms.NS}owner") == request["owner"] + if owned >= LIMITS["per_user"] or total >= LIMITS["host_total"]: + raise ValueError("The test VM limit is reached: 2 per account and 8 on this host. Delete an unused test VM or wait for expiry.") + if shutil.disk_usage(vms.DISKS).free < preset["capacity"] + 10 * 2**30: + raise ValueError("There is not enough free space for another test VM. Delete an unused test VM.") + name = "ai-" + request["name"].removeprefix("ai-") + "-" + uuid.uuid4().hex[:8] + spec = {"name": name, "description": "", "image": preset["id"], + "vcpus": request["vcpus"], "memory": request["memory"] * 2**30, + "disk": preset["capacity"], "autostart": False, "start": True, + "mode": "preset", "firmware": preset["firmware"], "platform": preset["platform"], + "owner": request["owner"], "username": "sandbox"} + vms.validate("create", spec) + vms.create(spec, ai=True) + return {"id": name, "name": name, "platform": preset["platform"], "os": preset["os"], + "arch": preset["arch"], "aiOwned": True, **lease(name)} + + +def manage(request): + if request.get("action") == "presets" and set(request) == {"action", "owner"}: + return {"presets": [{"id": p["id"], "os": p["os"], "platform": p["platform"], + "arch": p["arch"], "disk_bytes": p["capacity"]} for p in presets()], "limits": LIMITS} + if request.get("action") == "create": + return create(request) + if set(request) != {"action", "name", "owner", "seconds"} or request["action"] not in {"extend", "delete"}: + raise ValueError("Choose a supported test VM operation.") + name = request["name"] + vms.ensure_name(name) + if type(request["seconds"]) is not int or not 1 <= request["seconds"] <= LIMITS["extension_seconds"]: + raise ValueError("Extend a test VM by up to 24 hours from now.") + with vms.computer_lock(name): + root = ET.fromstring(vms.virsh("dumpxml", name)) + if root.findtext(f"metadata/{vms.NS}vm/{vms.NS}owner") != request["owner"] or not lease(name): + raise ValueError("Choose a test VM owned by your account.") + if request["action"] == "extend": + return {"id": name, "aiOwned": True, **lease(name, request["seconds"])} + vms.remove({"name": name, "disks": True}) + return {"id": name, "deleted": True} + + +def reap(): + for directory in vms.DISKS.glob("ai-*"): + if directory.is_symlink() or not directory.is_dir(): + continue + try: + with vms.computer_lock(directory.name): + value = lease(directory.name) + if value and value["expiresAt"] <= time.time(): + vms.remove({"name": directory.name, "disks": True}, missing_ok=True) + print("Expired test VM:", directory.name, flush=True) + except BlockingIOError: + continue + except FileNotFoundError: + if directory.exists(): + raise + + +if __name__ == "__main__": + try: + if sys.argv[1:] == ["reap"]: + reap() + else: + print(json.dumps(manage(json.load(sys.stdin)))) + except (ValueError, OSError) as error: + print(str(error), file=sys.stderr) + sys.exit(2) diff --git a/tools/vm-computer.py b/tools/vm-computer.py new file mode 100644 index 0000000000000000000000000000000000000000..c80cd29019bf46b6b21ab09ae543b256f74015f7 --- /dev/null +++ b/tools/vm-computer.py @@ -0,0 +1,556 @@ +"""Bounded computer operations over the existing private VM transports.""" +import asyncio +import base64 +import contextlib +from contextvars import ContextVar +import hashlib +from importlib import import_module +import json +import os +from pathlib import Path +import shlex +import subprocess +import sys +import threading +import time +import uuid +import xml.etree.ElementTree as ET + +import vms + +rfb = import_module('vm-rfb') +SCREEN_SLOT = threading.BoundedSemaphore(1) +DEADLINE = ContextVar('computer_deadline', default=float('inf')) + + +def remaining(limit): + seconds = min(limit, DEADLINE.get() - time.monotonic()) + if seconds <= 0: + raise TimeoutError('The input batch timed out. Check the desktop before continuing.') + return seconds + + +def qga(name, operation, **arguments): + payload = json.dumps({'execute': operation, 'arguments': arguments}) + # Virsh reads stdin so guest scripts and file contents never enter host argv. + command = 'qemu-agent-command ' + shlex.quote(name) + ' --timeout 10 ' + shlex.quote(payload) + '\n' + result = subprocess.run(['virsh', '--quiet', '--connect', 'qemu:///system'], input=command, + capture_output=True, text=True, timeout=remaining(15)) + for line in result.stdout.splitlines(): + if line.startswith('{'): + value = json.loads(line) + if 'return' in value: + return value['return'] + if operation == 'guest-exec-status' and result.stderr.rstrip().endswith(( + f"PID {arguments['pid']} does not exist", 'PID ld does not exist', 'PID lld does not exist', "Invalid parameter 'pid'")): + raise ProcessLookupError('The guest process result is unavailable. Check the VM before retrying.') + raise ValueError('The guest agent did not answer. Wait for the VM to finish starting, then retry.') + + +def guest_exec(name, path, args, data=None, timeout=20): + arguments = {'path': path, 'arg': args, 'capture-output': True} + if data is not None: + arguments['input-data'] = base64.b64encode(data).decode() + pid = qga(name, 'guest-exec', **arguments)['pid'] + deadline = time.monotonic() + remaining(timeout) + finished = None + while time.monotonic() < deadline: + try: + result = qga(name, 'guest-exec-status', pid=pid) + except ProcessLookupError: + if finished is None: + raise + if finished.get('exitcode') != 0: + raise ValueError('The guest worker could not run. Check the desktop session and installed computer tools.') + if finished.get('out-truncated') or finished.get('err-truncated'): + raise ValueError('The guest response was truncated. Narrow the request.') + return base64.b64decode(finished.get('out-data', ''), validate=True) + # QGA retains completed records across PID reuse and returns the oldest first. + if result.get('exited'): + finished = result + continue + finished = None + time.sleep(.15) + raise ValueError('The guest operation is still running. Check its status before retrying.') + + +def read_file(name, path, offset=0, length=65536): + handle = qga(name, 'guest-file-open', path=path, mode='rb') + try: + qga(name, 'guest-file-seek', handle=handle, offset=offset, whence=0) + value = qga(name, 'guest-file-read', handle=handle, count=length) + return base64.b64decode(value['buf-b64'], validate=True) + finally: + qga(name, 'guest-file-close', handle=handle) + + +def write_file(name, path, data, offset=0): + handle = qga(name, 'guest-file-open', path=path, mode='r+b' if offset else 'wb') + try: + if offset: + end = qga(name, 'guest-file-seek', handle=handle, offset=0, whence=2) + if end['position'] != offset: + raise ValueError('The transfer offset changed. Start a new upload.') + for start in range(0, len(data), 49152): + chunk = data[start:start + 49152] + result = qga(name, 'guest-file-write', handle=handle, **{'buf-b64': base64.b64encode(chunk).decode()}) + if result['count'] != len(chunk): + raise ValueError('The guest disk could not accept the file. Check free space.') + finally: + qga(name, 'guest-file-close', handle=handle) + + +def powershell(name, script, timeout=20): + """Windows 7 PowerShell needs a hidden console when launched by QGA.""" + stem = 'C:\\Windows\\Temp\\snowglobe-computer-' + uuid.uuid4().hex + command = 'cmd.exe /d /c powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File ' + stem + '.ps1' + command += ' > ' + stem + '.out 2> ' + stem + '.err' + launcher = '''Set shell = CreateObject("WScript.Shell") +code = shell.Run("COMMAND", 0, True) +Set stream = CreateObject("ADODB.Stream") +stream.Type = 1 +stream.Open +stream.LoadFromFile "STEM.out" +Set xml = CreateObject("Msxml2.DOMDocument.6.0") +Set node = xml.createElement("output") +node.DataType = "bin.base64" +node.nodeTypedValue = stream.Read +WScript.StdOut.Write Replace(Replace(node.Text, vbCr, ""), vbLf, "") +stream.Close +Set files = CreateObject("Scripting.FileSystemObject") +files.DeleteFile "STEM.out" +files.DeleteFile "STEM.err" +files.DeleteFile "STEM.ps1" +files.DeleteFile WScript.ScriptFullName +WScript.Quit code +'''.replace('COMMAND', command).replace('STEM', stem) + write_file(name, stem + '.ps1', b'\xff\xfe' + script.encode('utf-16le')) + write_file(name, stem + '.vbs', launcher.encode('ascii')) + return base64.b64decode(guest_exec(name, 'cscript.exe', ['//nologo', stem + '.vbs'], timeout=timeout), validate=True) + + +async def desktop(name, actions=None, platform=None, screenshot=True, native_text=True): + if not SCREEN_SLOT.acquire(blocking=False): + raise ValueError('Another screen capture is running. Retry in a moment.') + token = DEADLINE.set(min(DEADLINE.get(), time.monotonic() + 180)) + try: + return await asyncio.wait_for(capture(name, actions, platform, screenshot, native_text), remaining(180)) + finally: + DEADLINE.reset(token) + SCREEN_SLOT.release() + + +async def capture(name, actions, platform, screenshot=True, native_text=True): + target = vms.console_target(name, timeout=remaining(10)) + reader, writer = await asyncio.wait_for(asyncio.open_connection('127.0.0.1', target['port']), 10) + screen = rfb.Desktop(reader, writer, lambda _: None) + task = None + try: + await asyncio.wait_for(screen.start(), 10) + ready = asyncio.Event() + task = asyncio.create_task(screen.read(ready.set, ready.clear)) + await asyncio.wait_for(ready.wait(), 10) + if actions: + for action in actions: + kind = action['type'] + if kind in ('move', 'click', 'drag', 'scroll'): + x, y = action['x'], action['y'] + if not 0 <= x < screen.width or not 0 <= y < screen.height: + raise ValueError('Choose coordinates inside the latest screenshot.') + screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0}) + button = {'left': 1, 'middle': 2, 'right': 4}.get(action.get('button', 'left')) + if kind == 'click': + for _ in range(action.get('count', 1)): + screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': button}) + await asyncio.sleep(.04) + screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0}) + await asyncio.sleep(.06) + elif kind == 'drag': + end_x, end_y = action['to_x'], action['to_y'] + if not 0 <= end_x < screen.width or not 0 <= end_y < screen.height: + raise ValueError('Choose drag coordinates inside the latest screenshot.') + screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': button}) + for step in range(1, 21): + screen.input({'type': 'pointer', 'x': round(x+(end_x-x)*step/20), + 'y': round(y+(end_y-y)*step/20), 'buttons': button}) + await asyncio.sleep(action.get('duration_ms', 400)/20000) + screen.input({'type': 'release'}) + elif kind == 'scroll': + wheel = {'up': 8, 'down': 16, 'left': 32, 'right': 64}[action['direction']] + for _ in range(action.get('amount', 3)): + screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': wheel}) + screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0}) + await asyncio.sleep(.02) + elif kind == 'key': + keys = [keycode(key) for key in action['keys']] + for key in keys: + screen.input({'type': 'key', 'key': key, 'down': True}) + await asyncio.sleep(.04) + for key in reversed(keys): + screen.input({'type': 'key', 'key': key, 'down': False}) + elif kind == 'text': + if native_text: + pending = asyncio.create_task(asyncio.to_thread(worker,name,platform,{'action':'input','actions':[action],'screenshot':False})) + try: + await asyncio.shield(pending) + finally: + # Cancellation cannot release the VM lock while its worker still runs. + with contextlib.suppress(Exception): + await pending + else: + text = action['text'] + if any(not (32 <= ord(char) <= 126 or char in '\n\r\t') for char in text): + raise ValueError('This legacy guest supports ASCII text only. Use key actions for other keyboard input.') + shifted = dict(zip('~!@#$%^&*()_+{}|:"<>?', '`1234567890-=[]\\;\',./')) + for char in text.replace('\r\n', '\n'): + shift = char in shifted + base = shifted.get(char, char) + key = {'\n': 0xff0d, '\r': 0xff0d, '\t': 0xff09}.get(base, ord(base)) + if shift: + screen.input({'type': 'key', 'key': 0xffe1, 'down': True}) + screen.input({'type': 'key', 'key': key, 'down': True}) + await writer.drain() + await asyncio.sleep(.01) + screen.input({'type': 'key', 'key': key, 'down': False}) + if shift: + screen.input({'type': 'key', 'key': 0xffe1, 'down': False}) + await writer.drain() + await asyncio.sleep(.01) + elif kind == 'wait': + await asyncio.sleep(action['duration_ms']/1000) + await writer.drain() + screen.input({'type': 'release'}) + await asyncio.wait_for(writer.drain(), 5) + if not screenshot: + return {'completed': len(actions)} + await asyncio.sleep(.2) + # Force a full update even when the desktop has stopped painting. + ready.clear() + screen.request(False) + await writer.drain() + await asyncio.wait_for(ready.wait(), 10) + task.cancel() + with contextlib.suppress(asyncio.CancelledError): + await task + task = None + return {'width': screen.width, 'height': screen.height, 'bounds': [0,0,screen.width,screen.height], + 'scale': [1,1], 'png': rfb.png(screen.width, screen.height, screen.pixels, bgra=True).split(',',1)[1]} + finally: + with contextlib.suppress(Exception): + screen.input({'type': 'release'}) + await asyncio.wait_for(writer.drain(), 5) + if task: + task.cancel() + with contextlib.suppress(asyncio.CancelledError, Exception): + await task + writer.close() + await asyncio.wait_for(writer.wait_closed(), 5) + + +def keycode(key): + names = {'Enter': 0xff0d, 'Tab': 0xff09, 'Escape': 0xff1b, 'Backspace': 0xff08, + 'Delete': 0xffff, 'Insert': 0xff63, 'Home': 0xff50, 'End': 0xff57, + 'PageUp': 0xff55, 'PageDown': 0xff56, 'Left': 0xff51, 'Up': 0xff52, + 'Right': 0xff53, 'Down': 0xff54, 'Shift': 0xffe1, 'Control': 0xffe3, + 'Alt': 0xffe9, 'Meta': 0xffeb, 'Space': 0x20} + names.update({'F'+str(i): 0xffbd+i for i in range(1,25)}) + if key in names: + return names[key] + if len(key) == 1 and 32 <= ord(key) <= 126: + return ord(key) + raise ValueError('Use a named key or one ASCII character.') + + +def validate(request): + if not isinstance(request, dict): + raise ValueError('Send a computer operation.') + action = request.get('action') + fields = {'status': set(), 'shot': set(), 'ui': {'pid'}, 'ui_action': {'target','operation','value','screenshot'}, + 'exec': {'script','runtime','timeout_ms','screenshot'}, 'input': {'actions','screenshot'}, + 'spawn': {'script','runtime'}, 'process': {'id','operation'}, + 'file': {'operation','path','offset','data','length','destination','overwrite'}} + if not isinstance(action,str) or action not in fields or set(request)-{'action'}-fields[action]: + raise ValueError('Use the fields listed for this computer operation.') + if action in ('exec','spawn'): + if not isinstance(request.get('script'),str) or not 1 <= len(request['script'].encode()) <= 65536: + raise ValueError('Provide a script of at most 64 KiB.') + if request.get('runtime') not in (None,'default','bash','zsh','powershell','cmd','ahk'): + raise ValueError('Choose a runtime reported by computer_status.') + if action == 'exec' and (type(request.get('timeout_ms')) is not int or not 1 <= request['timeout_ms'] <= 180000): + raise ValueError('Choose a timeout between 1 and 180000 milliseconds.') + if 'screenshot' in request and type(request['screenshot']) is not bool: + raise ValueError('Choose whether to return a screenshot.') + if action == 'ui' and request.get('pid') is not None and (type(request['pid']) is not int or request['pid']<=0): + raise ValueError('Choose a positive application PID.') + if action == 'ui_action': + if not isinstance(request.get('target'),dict) or len(json.dumps(request['target']))>4096 or not isinstance(request.get('operation'),str) or len(request['operation'])>64: + raise ValueError('Use a target and action returned by computer_ui.') + if 'value' in request and (not isinstance(request['value'],str) or len(request['value'].encode())>16384): + raise ValueError('Set a value of at most 16 KiB.') + if action == 'file': + path=request.get('path') + if not isinstance(path,str) or not 1<=len(path)<=4096 or '\x00' in path: + raise ValueError('Use an absolute guest file path.') + operation=request.get('operation') + if operation not in ('stat','list','mkdir','remove','read','write','commit'): + raise ValueError('Choose a supported file operation.') + if operation in ('read','write'): + offset=request.get('offset',0) + if type(offset) is not int or not 0<=offset<=64*2**20: + raise ValueError('Choose a transfer offset within 64 MiB.') + if operation=='read': + length=request.get('length',65536) + if type(length) is not int or not 1<=length<=65536: raise ValueError('Read at most 64 KiB at a time.') + else: + if not isinstance(request.get('data'),str) or len(request['data'])>90000 or len(base64.b64decode(request['data'],validate=True))>65536: + raise ValueError('Write at most 64 KiB at a time.') + if operation=='commit' and (not isinstance(request.get('destination'),str) or not 1<=len(request['destination'])<=4096 or '\x00' in request['destination'] or type(request.get('overwrite',False)) is not bool): + raise ValueError('Choose an absolute guest destination and whether to replace it.') + if action == 'process' and (not isinstance(request.get('id'),str) or len(request['id'])>128 or request.get('operation') not in ('status','stop')): + raise ValueError('Use the process handle returned by computer_spawn.') + if action == 'input': + actions = request.get('actions') + if not isinstance(actions,list) or not 1 <= len(actions) <= 100: + raise ValueError('Send between 1 and 100 desktop actions.') + for item in actions: + if not isinstance(item,dict): + raise ValueError('Send a desktop action object.') + kind = item.get('type') + required = {'move': {'x','y'}, 'click': {'x','y'}, 'drag': {'x','y','to_x','to_y'}, + 'scroll': {'x','y','direction'}, 'key': {'keys'}, 'text': {'text'}, 'wait': {'duration_ms'}} + optional = {'click': {'button','count'}, 'drag': {'button','duration_ms'}, 'scroll': {'amount'}} + if not isinstance(kind,str) or kind not in required or not required[kind] <= item.keys() or set(item)-{'type'}-required[kind]-optional.get(kind,set()): + raise ValueError('Use the fields listed for this desktop action.') + for key in ('x','y','to_x','to_y'): + if key in item and (type(item[key]) is not int or not 0 <= item[key] <= 16384): + raise ValueError('Use nonnegative screenshot pixel coordinates.') + if 'button' in item and item['button'] not in ('left','right','middle'): + raise ValueError('Choose the left, right, or middle mouse button.') + for key,maximum in [('count',3),('amount',100),('duration_ms',5000)]: + if key in item and (type(item[key]) is not int or not 1 <= item[key] <= maximum): + raise ValueError('The desktop action exceeds its limit.') + if kind == 'scroll' and item['direction'] not in ('up','down','left','right'): + raise ValueError('Choose a scroll direction.') + if kind == 'key': + if not isinstance(item['keys'],list) or not 1 <= len(item['keys']) <= 8 or any(not isinstance(k,str) for k in item['keys']): + raise ValueError('Send between 1 and 8 key names.') + for key in item['keys']: keycode(key) + if kind == 'text' and (not isinstance(item['text'],str) or len(item['text'].encode()) > 16384): + raise ValueError('Type at most 16 KiB of text at a time.') + if sum(a.get('duration_ms',0) for a in actions) > 15000: + raise ValueError('Keep total waits and drags under 15 seconds.') + + +def record_operation(name, operation): + path = vms.DISKS / name / 'computer-operation.json' + temporary = path.with_suffix('.new') + with open(temporary, 'w', opener=lambda p, flags: os.open(p, flags | os.O_NOFOLLOW, 0o600)) as outgoing: + json.dump(operation, outgoing) + os.replace(temporary, path) + + +def finish_operation(name, operation): + token = DEADLINE.set(time.monotonic() + 20) + try: + if operation['platform'] == 'linux': + guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [], + json.dumps({'_stop': operation['id']}).encode(), timeout=18) + else: + run = operation['id'] + directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run + cleanup = "$ErrorActionPreference='Stop';$d='"+directory+"';" + cleanup += "if(Test-Path -LiteralPath ($d+'\\request.json')){Remove-Item -LiteralPath ($d+'\\request.json') -Force};" + cleanup += "$s=New-Object -ComObject Schedule.Service;$s.Connect();$f=$s.GetFolder('\\');" + cleanup += "$t=$f.GetTasks(1) | Where-Object {$_.Name -eq 'SnowglobeComputer-"+run+"'};if($t){" + if not operation.get('keep_spawn'): + cleanup += "$workers=@(Get-WmiObject Win32_Process | Where-Object {$_.CommandLine -like ('*'+$d+'\\request.json*')} | ForEach-Object {$p=Get-Process -Id $_.ProcessId -ErrorAction SilentlyContinue;if($p){$null=$p.Handle;$p}});" + cleanup += "$info=New-Object Diagnostics.ProcessStartInfo;$info.FileName=$t.Definition.Actions.Item(1).Path;$info.Arguments='--stop \"'+$d+'\\request.json\"';$info.UseShellExecute=$false;$info.CreateNoWindow=$true;" + cleanup += "$stop=[Diagnostics.Process]::Start($info);if(!$stop.WaitForExit(12000) -or $stop.ExitCode -ne 0){throw 'Process cleanup failed'};$stop.Dispose();" + cleanup += "$t.Stop(0);while($t.State -eq 4){Start-Sleep -Milliseconds 100};" + cleanup += "foreach($p in $workers){if(!$p.WaitForExit(10000)){throw 'Process cleanup failed'};$p.Dispose()};" + cleanup += "$f.DeleteTask('SnowglobeComputer-"+run+"',0)};" + if not operation.get('keep_spawn'): + cleanup += "if(Test-Path -LiteralPath $d){Remove-Item -LiteralPath $d -Recurse -Force}" + powershell(name, cleanup, timeout=18) + (vms.DISKS / name / 'computer-operation.json').unlink() + except Exception as error: + raise BlockingIOError('The previous operation has not stopped. Check its screenshot, then retry when the guest agent reconnects.') from error + finally: + DEADLINE.reset(token) + + +def worker(name, platform, request): + if platform == 'macos': + source = Path(__file__).resolve().parent.parent / 'guest/computer-legacy-mac.py' + bootstrap = "import json,sys; request=json.load(sys.stdin); exec(compile(request.pop('_worker'),'','exec'))" + environment = dict(os.environ, SSH_ORIGINAL_COMMAND='/usr/bin/python -c ' + shlex.quote(bootstrap)) + result = subprocess.run([sys.executable, str(Path(__file__).with_name('vm-ssh.py')), name], + input=json.dumps({**request, '_worker':source.read_text()}).encode(), + capture_output=True, timeout=remaining(200), env=environment) + if result.returncode: + raise ValueError('The private guest connection failed. Wait for desktop setup to finish, then retry.') + reply = json.loads(result.stdout) + if 'error' in reply: raise ValueError(reply['error']) + return reply + if platform == 'linux': + source = Path(__file__).with_name('mac-computer.py').read_text() + unit = 'snowglobe-computer-request-' + uuid.uuid4().hex + guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [], + json.dumps({'_prepare': unit}).encode()) + operation = {'platform': platform, 'id': unit} + record_operation(name, operation) + try: + raw = guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [], + json.dumps({**request, '_worker': source, '_unit': unit, + '_timeout': max(1, int(remaining(200)) - 5)}).encode(), timeout=220) + except BaseException: + finish_operation(name, operation) + raise + (vms.DISKS / name / 'computer-operation.json').unlink() + result = json.loads(raw) + if 'error' in result: + if result.get('status') == 409: + raise BlockingIOError(result['error']) + raise ValueError(result['error']) + return result + if platform == 'windows': + run = uuid.uuid4().hex + directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run + setup = "$d='" + directory + "'; New-Item -ItemType Directory -Force -Path $d | Out-Null; Test-Path C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe" + installed=powershell(name,setup).strip()==b'True' + ahk=os.environ.get('SNOWGLOBE_COMPUTER_AHK') + if not installed and ahk: + write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe',(Path(ahk)/'AutoHotkey32.exe').read_bytes()) + write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey-license.txt',(Path(ahk)/'license.txt').read_bytes()) + write_file(name,directory+'\\request.json',json.dumps(request).encode()) + source = Path(__file__).resolve().parent.parent / 'guest/computer-windows.cs' + content = source.read_bytes() + digest = hashlib.sha256(content).hexdigest()[:16] + installed_worker = 'C:\\ProgramData\\SnowglobeComputer\\worker-' + digest + '.exe' + exists = powershell(name, "Test-Path -LiteralPath '" + installed_worker + "'").strip() == b'True' + if not exists: + source_path = directory + '\\worker.cs' + write_file(name, source_path, content) + compile_script = r""" +$ErrorActionPreference='Stop' +$lock=New-Object Threading.Mutex($false,'Global\SnowglobeComputerCompile-HASH') +if(!$lock.WaitOne(0)){$lock.Dispose();Write-Output 'pending';return} +try { +if(Test-Path -LiteralPath 'WORKER'){return} +$references=@('System.dll','System.Core.dll') +foreach($assembly in @('System.Web.Extensions','UIAutomationClient','UIAutomationTypes','WindowsBase')) { + $references += [Reflection.Assembly]::LoadWithPartialName($assembly).Location +} +Remove-Item -LiteralPath 'WORKER.new.exe' -Force -ErrorAction SilentlyContinue +Add-Type -TypeDefinition ([IO.File]::ReadAllText('SOURCE')) -ReferencedAssemblies $references -OutputAssembly 'WORKER.new.exe' -OutputType WindowsApplication +Move-Item -LiteralPath 'WORKER.new.exe' -Destination 'WORKER' +} finally {$lock.ReleaseMutex();$lock.Dispose()} +""".replace('SOURCE', source_path).replace('WORKER', installed_worker).replace('HASH',digest) + if powershell(name, compile_script, timeout=60).strip() == b'pending': + raise ValueError('Computer tools are still installing. Retry in a moment.') + launch = """ +$ErrorActionPreference='Stop' +$d='DIRECTORY' +$user=(Get-WmiObject Win32_ComputerSystem).UserName +if(!$user){throw 'No interactive user'} +$service=New-Object -ComObject Schedule.Service +$service.Connect() +$task=$service.NewTask(0) +$task.Principal.UserId=$user +$task.Principal.LogonType=3 +$task.Principal.RunLevel=1 +$task.Settings.ExecutionTimeLimit='TASKLIMIT' +$task.Settings.DisallowStartIfOnBatteries=$false +$task.Settings.StopIfGoingOnBatteries=$false +$action=$task.Actions.Create(0) +$action.Path='WORKER' +$action.Arguments='"'+$d+'\\request.json" "'+$d+'\\response.json"' +$registered=$service.GetFolder('\\').RegisterTaskDefinition('SnowglobeComputer-RUN',$task,6,$user,$null,3) +$registered.Run($null) | Out-Null +""".replace('DIRECTORY',directory).replace('RUN',run).replace('WORKER',installed_worker).replace('TASKLIMIT','PT0S' if request['action'] == 'spawn' else 'PT210S') + operation = {'platform': platform, 'id': run} + record_operation(name, operation) + try: + powershell(name,launch) + deadline=time.monotonic()+remaining(200) + while time.monotonic() 8 * 1024 * 1024: + raise ValueError("The screenshot is too large. Lower the guest display resolution, then retry.") + compressed.extend(compressor.flush()) + data = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack("!IIBBBBB", width, height, 8, 6, 0, 0, 0)) + chunk(b"IDAT", compressed) + chunk(b"IEND", b"") + return "data:image/png;base64," + base64.b64encode(data).decode() + + + +class Desktop: + def __init__(self, reader, writer, notify): + self.reader, self.writer, self.notify = reader, writer, notify + self.keys = set() + self.pointer = (0, 0) + self.buttons = 0 + self.pixels = bytearray() + self.cursor = None + + def resize(self, width, height): + if not 0 < width <= 4096 or not 0 < height <= 4096 or width * height > MAX_PIXELS: + raise ValueError("The VM screen exceeds the supported 4K display size.") + self.width, self.height = width, height + self.pixels = bytearray(width * height * 4) + + async def start(self): + if not (await self.reader.readexactly(12)).startswith(b"RFB 003."): + raise ValueError("The VM screen did not respond. Restart the VM and reconnect.") + self.writer.write(b"RFB 003.008\n") + count = (await self.reader.readexactly(1))[0] + security = await self.reader.readexactly(count) + if 1 not in security: + raise ValueError("Remove the VM's VNC password in its display settings, then reconnect.") + self.writer.write(b"\x01") + if await self.reader.readexactly(4) != b"\0\0\0\0": + raise ValueError("The VM rejected the screen connection. Restart it and reconnect.") + self.writer.write(b"\x01") + width, height, _, length = struct.unpack("!HH16sI", await self.reader.readexactly(24)) + if length > 65536: + raise ValueError("The VM screen sent an unsupported desktop name.") + await self.reader.readexactly(length) + self.resize(width, height) + self.writer.write(struct.pack("!B3xBBBBHHHBBB3x", 0, 32, 24, 0, 1, 255, 255, 255, 16, 8, 0)) + encodings = (0, 1, -223, -224, -239) + self.writer.write(struct.pack("!BBH" + "i" * len(encodings), 2, 0, len(encodings), *encodings)) + self.request(False) + + def request(self, incremental=True): + self.writer.write(struct.pack("!BBHHHH", 3, incremental, 0, 0, self.width, self.height)) + + def input(self, message): + kind = message.get("type") + if kind == "key": + key, down = message.get("key"), message.get("down") + if type(key) is not int or not 0 <= key <= 0xFFFFFFFF or type(down) is not bool: + raise ValueError("Unsupported key event.") + if down: + self.keys.add(key) + else: + self.keys.discard(key) + self.writer.write(struct.pack("!BB2xI", 4, down, key)) + elif kind == "pointer": + x, y, buttons = message.get("x"), message.get("y"), message.get("buttons") + if any(type(value) is not int for value in (x, y, buttons)) or not 0 <= buttons <= 255: + raise ValueError("Unsupported pointer event.") + self.pointer = (max(0, min(x, self.width - 1)), max(0, min(y, self.height - 1))) + self.buttons = buttons + self.writer.write(struct.pack("!BBHH", 5, buttons, *self.pointer)) + elif kind == "release": + for key in self.keys: + self.writer.write(struct.pack("!BB2xI", 4, 0, key)) + self.keys.clear() + self.buttons = 0 + self.writer.write(struct.pack("!BBHH", 5, 0, *self.pointer)) + elif kind == "clipboard": + text = message.get("text") + if not isinstance(text, str) or len(text) > 65536: + raise ValueError("Shorten the clipboard text, then send it again.") + try: + data = text.encode("latin1") + except UnicodeEncodeError: + raise ValueError("Some clipboard characters aren't supported by this guest.") from None + self.writer.write(struct.pack("!B3xI", 6, len(data)) + data) + else: + raise ValueError("Unsupported input event.") + + async def read(self, present, resized): + while True: + kind = (await self.reader.readexactly(1))[0] + if kind == 0: + _, count = struct.unpack("!BH", await self.reader.readexactly(3)) + dirty = False + for _ in range(count): + x, y, width, height, encoding = struct.unpack("!HHHHi", await self.reader.readexactly(12)) + if encoding == -224: + break + if encoding == -223: + self.resize(width, height) + resized() + continue + if encoding == -239: + if width * height > 256 * 256: + raise ValueError("The VM sent an unsupported cursor size.") + pixels = bytearray(await self.reader.readexactly(width * height * 4)) + mask = await self.reader.readexactly(((width + 7) // 8) * height) + for row in range(height): + for col in range(width): + offset = (row * width + col) * 4 + pixels[offset], pixels[offset + 2] = pixels[offset + 2], pixels[offset] + pixels[offset + 3] = 255 if mask[row * ((width + 7) // 8) + col // 8] & (128 >> (col % 8)) else 0 + self.cursor = {"type": "cursor", "source": "qemu", "width": width, "height": height, "x": x, "y": y, + "frame": 0, "frames": 1, "duration": 100, + "image": png(width, height, pixels) if width and height else None} + self.notify(self.cursor) + continue + if x + width > self.width or y + height > self.height: + raise ValueError("The VM sent a screen update outside its display.") + if encoding == 0: + data = await self.reader.readexactly(width * height * 4) + elif encoding == 1: + source_x, source_y = struct.unpack("!HH", await self.reader.readexactly(4)) + if source_x + width > self.width or source_y + height > self.height: + raise ValueError("The VM sent a screen copy outside its display.") + data = b"".join(self.pixels[((source_y + row) * self.width + source_x) * 4: + ((source_y + row) * self.width + source_x + width) * 4] for row in range(height)) + else: + raise ValueError("The VM sent an unsupported screen encoding.") + for row in range(height): + start = ((y + row) * self.width + x) * 4 + self.pixels[start:start + width * 4] = data[row * width * 4:(row + 1) * width * 4] + dirty = dirty or bool(width and height) + if dirty: + present() + self.request() + await self.writer.drain() + elif kind == 2: + pass + elif kind == 3: + _, length = struct.unpack("!3sI", await self.reader.readexactly(7)) + if length > 65536: + raise ValueError("The VM sent too much clipboard text.") + self.notify({"type": "clipboard", "text": (await self.reader.readexactly(length)).decode("latin1")}) + else: + raise ValueError("The VM sent an unsupported screen message.") + diff --git a/tools/vm-screen.py b/tools/vm-screen.py index 4d4344cd5db45eaf906829b95a790daf52676530..0a0981ac9e778b6db8d4911115fffadd0a093e9c 100644 --- a/tools/vm-screen.py +++ b/tools/vm-screen.py @@ -23,7 +23,10 @@ gi.require_version("GstRtp", "1.0") from gi.repository import GLib, Gst, GstRtp, GstSdp, GstWebRTC -MAX_PIXELS = 3840 * 2160 +from importlib import import_module + +rfb = import_module("vm-rfb") +Desktop, png = rfb.Desktop, rfb.png STUN = os.environ.get("SNOWGLOBE_VM_STUN_SERVER", "stun://stun.cloudflare.com:3478") SLOTS = asyncio.Semaphore(4) GUEST_PROTOCOL = json.loads(Path(os.environ.get("SNOWGLOBE_VM_GUEST_PROTOCOL", Path(__file__).resolve().parent.parent / "guest/protocol.json")).read_text()) @@ -93,14 +96,6 @@ class WindowDesktop: self.copy(window, x, y, w, h, data) -def png(width, height, pixels): - def chunk(kind, data): - return struct.pack("!I", len(data)) + kind + data + struct.pack("!I", zlib.crc32(kind + data)) - rows = b"".join(b"\0" + pixels[row * width * 4:(row + 1) * width * 4] for row in range(height)) - data = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack("!IIBBBBB", width, height, 8, 6, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(rows)) + chunk(b"IEND", b"") - return "data:image/png;base64," + base64.b64encode(data).decode() - - class GuestDesktop: def __init__(self, name): self.name = name @@ -342,142 +337,6 @@ class GuestDesktop: await asyncio.sleep(2) -class Desktop: - def __init__(self, reader, writer, notify): - self.reader, self.writer, self.notify = reader, writer, notify - self.keys = set() - self.pointer = (0, 0) - self.buttons = 0 - self.pixels = bytearray() - self.cursor = None - - def resize(self, width, height): - if not 0 < width <= 4096 or not 0 < height <= 4096 or width * height > MAX_PIXELS: - raise ValueError("The VM screen exceeds the supported 4K display size.") - self.width, self.height = width, height - self.pixels = bytearray(width * height * 4) - - async def start(self): - if not (await self.reader.readexactly(12)).startswith(b"RFB 003."): - raise ValueError("The VM screen did not respond. Restart the VM and reconnect.") - self.writer.write(b"RFB 003.008\n") - count = (await self.reader.readexactly(1))[0] - security = await self.reader.readexactly(count) - if 1 not in security: - raise ValueError("Remove the VM's VNC password in its display settings, then reconnect.") - self.writer.write(b"\x01") - if await self.reader.readexactly(4) != b"\0\0\0\0": - raise ValueError("The VM rejected the screen connection. Restart it and reconnect.") - self.writer.write(b"\x01") - width, height, _, length = struct.unpack("!HH16sI", await self.reader.readexactly(24)) - if length > 65536: - raise ValueError("The VM screen sent an unsupported desktop name.") - await self.reader.readexactly(length) - self.resize(width, height) - self.writer.write(struct.pack("!B3xBBBBHHHBBB3x", 0, 32, 24, 0, 1, 255, 255, 255, 16, 8, 0)) - encodings = (0, 1, -223, -224, -239) - self.writer.write(struct.pack("!BBH" + "i" * len(encodings), 2, 0, len(encodings), *encodings)) - self.request(False) - - def request(self, incremental=True): - self.writer.write(struct.pack("!BBHHHH", 3, incremental, 0, 0, self.width, self.height)) - - def input(self, message): - kind = message.get("type") - if kind == "key": - key, down = message.get("key"), message.get("down") - if type(key) is not int or not 0 <= key <= 0xFFFFFFFF or type(down) is not bool: - raise ValueError("Unsupported key event.") - if down: - self.keys.add(key) - else: - self.keys.discard(key) - self.writer.write(struct.pack("!BB2xI", 4, down, key)) - elif kind == "pointer": - x, y, buttons = message.get("x"), message.get("y"), message.get("buttons") - if any(type(value) is not int for value in (x, y, buttons)) or not 0 <= buttons <= 255: - raise ValueError("Unsupported pointer event.") - self.pointer = (max(0, min(x, self.width - 1)), max(0, min(y, self.height - 1))) - self.buttons = buttons - self.writer.write(struct.pack("!BBHH", 5, buttons, *self.pointer)) - elif kind == "release": - for key in self.keys: - self.writer.write(struct.pack("!BB2xI", 4, 0, key)) - self.keys.clear() - self.buttons = 0 - self.writer.write(struct.pack("!BBHH", 5, 0, *self.pointer)) - elif kind == "clipboard": - text = message.get("text") - if not isinstance(text, str) or len(text) > 65536: - raise ValueError("Shorten the clipboard text, then send it again.") - try: - data = text.encode("latin1") - except UnicodeEncodeError: - raise ValueError("Some clipboard characters aren't supported by this guest.") from None - self.writer.write(struct.pack("!B3xI", 6, len(data)) + data) - else: - raise ValueError("Unsupported input event.") - - async def read(self, present, resized): - while True: - kind = (await self.reader.readexactly(1))[0] - if kind == 0: - _, count = struct.unpack("!BH", await self.reader.readexactly(3)) - dirty = False - for _ in range(count): - x, y, width, height, encoding = struct.unpack("!HHHHi", await self.reader.readexactly(12)) - if encoding == -224: - break - if encoding == -223: - self.resize(width, height) - resized() - continue - if encoding == -239: - if width * height > 256 * 256: - raise ValueError("The VM sent an unsupported cursor size.") - pixels = bytearray(await self.reader.readexactly(width * height * 4)) - mask = await self.reader.readexactly(((width + 7) // 8) * height) - for row in range(height): - for col in range(width): - offset = (row * width + col) * 4 - pixels[offset], pixels[offset + 2] = pixels[offset + 2], pixels[offset] - pixels[offset + 3] = 255 if mask[row * ((width + 7) // 8) + col // 8] & (128 >> (col % 8)) else 0 - self.cursor = {"type": "cursor", "source": "qemu", "width": width, "height": height, "x": x, "y": y, - "frame": 0, "frames": 1, "duration": 100, - "image": png(width, height, pixels) if width and height else None} - self.notify(self.cursor) - continue - if x + width > self.width or y + height > self.height: - raise ValueError("The VM sent a screen update outside its display.") - if encoding == 0: - data = await self.reader.readexactly(width * height * 4) - elif encoding == 1: - source_x, source_y = struct.unpack("!HH", await self.reader.readexactly(4)) - if source_x + width > self.width or source_y + height > self.height: - raise ValueError("The VM sent a screen copy outside its display.") - data = b"".join(self.pixels[((source_y + row) * self.width + source_x) * 4: - ((source_y + row) * self.width + source_x + width) * 4] for row in range(height)) - else: - raise ValueError("The VM sent an unsupported screen encoding.") - for row in range(height): - start = ((y + row) * self.width + x) * 4 - self.pixels[start:start + width * 4] = data[row * width * 4:(row + 1) * width * 4] - dirty = dirty or bool(width and height) - if dirty: - present() - self.request() - await self.writer.drain() - elif kind == 2: - pass - elif kind == 3: - _, length = struct.unpack("!3sI", await self.reader.readexactly(7)) - if length > 65536: - raise ValueError("The VM sent too much clipboard text.") - self.notify({"type": "clipboard", "text": (await self.reader.readexactly(length)).decode("latin1")}) - else: - raise ValueError("The VM sent an unsupported screen message.") - - class Screen: def __init__(self, writer, desktop): self.loop = asyncio.get_running_loop() diff --git a/tools/vms.py b/tools/vms.py index 31d9d3623f866c18e34a307dc34d00b488c1bbec..8cd78b73d8f6ccc3b899ab80fb57bd0a2fade1b8 100644 --- a/tools/vms.py +++ b/tools/vms.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 import json +import contextlib +import fcntl from importlib import import_module import os from pathlib import Path @@ -70,7 +72,7 @@ PROFILES = { }, } ACTION_FIELDS = { - "node": None, "domains": None, "stats": None, + "node": None, "domains": None, "computers": None, "stats": None, "create": {"name", "description", "image", "vcpus", "memory", "disk", "autostart", "start", "mode", "firmware", "platform", "owner", "username"}, "act": {"name", "action"}, "update": {"name", "description", "autostart"}, "remove": {"name", "disks"}, @@ -205,12 +207,24 @@ def validate(action, payload): raise ValueError("The VM disk directory is a symbolic link. Remove the link before continuing.") -def command(*args, pass_fds=()): - return subprocess.run(args, check=True, text=True, capture_output=True, pass_fds=pass_fds).stdout.strip() +@contextlib.contextmanager +def computer_lock(name): + ensure_name(name) + directory = DISKS / name + if directory.is_symlink(): + raise ValueError("The VM disk directory is a symbolic link.") + descriptor = os.open(directory / "computer.lock", os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600) + with os.fdopen(descriptor, "w") as lock: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + yield -def virsh(*args): - return command("virsh", "-c", "qemu:///system", *args) +def command(*args, pass_fds=(), timeout=None): + return subprocess.run(args, check=True, text=True, capture_output=True, pass_fds=pass_fds, timeout=timeout).stdout.strip() + + +def virsh(*args, timeout=None): + return command("virsh", "-c", "qemu:///system", *args, timeout=timeout) def info(file): @@ -242,7 +256,7 @@ def image(file): "disk": (64 if windows else 32) * 2**30}} -def domains(): +def domains(*, computers=False): result = [] for name in virsh("list", "--all", "--name").splitlines(): if not name: @@ -251,6 +265,19 @@ def domains(): state_line = virsh("domstate", name, "--reason").splitlines()[0].lower() state = next((value for value in ("running", "blocked", "paused", "shutdown", "crashed", "pmsuspended") if state_line.startswith(value)), "shutoff") reason = state_line.split("(", 1)[1].rstrip(")") if state in {"paused", "crashed"} and "(" in state_line else None + ai = import_module("vm-ai").lease(name) + entry = { + "name": name, "owner": root.findtext(f"metadata/{NS}vm/{NS}owner"), + "platform": root.findtext(f"metadata/{NS}vm/{NS}platform", "linux"), + "os": root.findtext(f"metadata/{NS}vm/{NS}os", root.findtext(f"metadata/{NS}os", "Other")), + "arch": root.find("./os/type").get("arch"), "state": state, + "aiOwned": ai is not None, "ownerOnly": ai is not None, + "expiresAt": ai["expiresAt"] if ai else None, + "capabilities": ["start", "shutdown", "reboot", "destroy", "resume", "suspend", "edit", "delete", "media", "preset", "computer"], + } + if computers: + result.append(entry) + continue memory = int(root.findtext("memory", "0")) * 1024 balloon = int(root.findtext("currentMemory", str(memory // 1024))) * 1024 disks = [] @@ -298,16 +325,11 @@ def domains(): agent = root.find("./devices/channel/target[@name='org.qemu.guest_agent.0']") setup = root.findtext(f"metadata/{NS}vm/{NS}setup") result.append({ - "name": name, + **entry, "host": os.uname().nodename, - "arch": root.find("./os/type").get("arch"), "pendingAction": None, - "capabilities": ["start", "shutdown", "reboot", "destroy", "resume", "suspend", "edit", "delete", "media", "preset"], - "owner": root.findtext(f"metadata/{NS}vm/{NS}owner"), "description": root.findtext("description", ""), - "state": state, "reason": reason, - "os": root.findtext(f"metadata/{NS}vm/{NS}os", root.findtext(f"metadata/{NS}os", "Other")), "vcpus": int(root.findtext("vcpu", "1")), "pinned": None, "memory": memory, @@ -319,7 +341,6 @@ def domains(): "disks": disks, "media": media, "firmware": "uefi" if root.find("./os/loader") is not None or root.find("os").get("firmware") == "efi" else "bios", - "platform": root.findtext(f"metadata/{NS}vm/{NS}platform", "linux"), "console": root.find("./devices/graphics[@type='vnc']") is not None, "serial": root.find("./devices/console[@type='pty']/target[@type='serial']") is not None, "interfaces": interfaces, @@ -588,10 +609,10 @@ def save_preset(spec): shutil.rmtree(temporary) -def console_target(name): - root = ET.fromstring(virsh("dumpxml", name)) +def console_target(name, *, timeout=None): + root = ET.fromstring(virsh("dumpxml", name, timeout=timeout)) graphics = root.find("./devices/graphics[@type='vnc']") - if virsh("domstate", name).strip() not in {"running", "paused", "blocked"} or graphics is None: + if virsh("domstate", name, timeout=timeout).strip() not in {"running", "paused", "blocked"} or graphics is None: raise ValueError("Start this VM before opening its screen.") # Only libvirt's loopback VNC listener; callers cannot provide a host or port. if graphics.get("listen") != "127.0.0.1" or graphics.get("socket"): @@ -642,7 +663,11 @@ old=$({account_tool}getent passwd 1000 | cut -d: -f1) test -n "$old" if [ "$old" != {username} ]; then ! {account_tool}getent passwd {username} - {account_tool}usermod -l {username} -c {username} -d /home/{username} -m "$old" + if [ -e /home/{username} ]; then + {account_tool}usermod -l {username} -c {username} "$old" + else + {account_tool}usermod -l {username} -c {username} -d /home/{username} -m "$old" + fi if [ "$({account_tool}getent group 1000 | cut -d: -f1)" = "$old" ]; then {account_tool}groupmod -n {username} "$old"; fi fi ''' @@ -654,13 +679,26 @@ fi files.insert(0, {"path": "/etc/ssh/sshd_config.d/00-snowglobe.conf", "permissions": "0600", "content": "PasswordAuthentication yes\nPermitRootLogin yes\n"}) else: files.append({"path": "/var/lib/snowglobe/hostname", "content": hostname + "\n"}) + guest = Path(__file__).resolve().parent.parent / "guest" + files.extend([ + {"path": "/usr/local/libexec/snowglobe-computer.py", "permissions": "0755", "content": (guest / "computer-linux.py").read_text()}, + {"path": "/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer", "permissions": "0755", "content": (guest / "computer-linux-launch").read_text()}, + ]) + computer_setup = [] + if fedora: + computer_setup = [["setsebool", "-P", "virt_qemu_ga_run_unconfined", "on"], ["restorecon", "-R", "/etc/qemu-ga/fsfreeze-hook.d"]] + if nixos: + files.append({"path": "/var/lib/snowglobe/computer.nix", "content": (guest / "computer-linux.nix").read_text()}) + computer_setup = [["/run/current-system/sw/bin/bash", "-lc", "nix-env --profile /var/lib/snowglobe/computer -if /var/lib/snowglobe/computer.nix"]] config = { "users": [], "disable_root": False, "ssh_pwauth": True, "ssh_deletekeys": True, "bootcmd": ([["rm", "-f", gdm_config]] if nixos else []) + [["sh", "-c", rename]], "chpasswd": {"expire": False, "users": [{"name": user, "password": hashed, "type": "hash"} for user in [username, "root"]]}, "write_files": files, - "runcmd": [["systemctl", "enable", "--now", "sshd" if fedora or nixos else "ssh"], ["systemctl", "enable", "--now", f"serial-getty@{serial}.service"]], + "runcmd": [["systemctl", "enable", "--now", "sshd" if fedora or nixos else "ssh"], ["systemctl", "enable", "--now", f"serial-getty@{serial}.service"]] + computer_setup, } + if not nixos: + config["packages"] = ["python3", "python3-gobject", "at-spi2-core"] if fedora else ["python3", "python3-gi", "gir1.2-atspi-2.0"] with tempfile.TemporaryDirectory(prefix=".seed-", dir=directory) as temporary: files = Path(temporary) (files / "user-data").write_text("#cloud-config\n" + json.dumps(config)) @@ -696,7 +734,7 @@ def macos_seed(directory, username): (directory / "seed.iso").chmod(0o600) -def create(spec): +def create(spec, *, ai=False): name = spec["name"] ensure_name(name) if name in virsh("list", "--all", "--name").splitlines(): @@ -730,6 +768,8 @@ def create(spec): directory = DISKS / name directory.mkdir(mode=0o700, parents=True, exist_ok=False) drive = directory / "disk.qcow2" + if ai: + (directory / "ai.json").write_text(json.dumps({"expiresAt": int(time.time()) + import_module("vm-ai").LIMITS["idle_seconds"]})) def expired(_signum, _frame): raise TimeoutError("VM image preparation timed out.") @@ -839,8 +879,11 @@ def create(spec): ET.SubElement(cpu, "feature", policy="require", name="hypervisor") elif root.get("type") == "qemu": ET.SubElement(root, "cpu", mode="maximum") - if profile_name == "windows-q35-uefi": - ET.SubElement(root.find("cpu"), "topology", sockets="1", cores=str(spec["vcpus"]), threads="1") + if spec["platform"] == "windows": + cpu = root.find("cpu") + if cpu is None: + cpu = ET.SubElement(root, "cpu") + ET.SubElement(cpu, "topology", sockets="1", cores=str(spec["vcpus"]), threads="1") clock = ET.SubElement(root, "clock", offset=details_profile["clock"]) if profile_name == "windows-q35-uefi": ET.SubElement(clock, "timer", name="hypervclock", present="yes") @@ -942,14 +985,32 @@ def create(spec): signal.signal(signal.SIGALRM, previous) +def remove(spec, *, missing_ok=False): + name = spec["name"] + ensure_name(name) + exists = name in virsh("list", "--all", "--name").splitlines() + if not exists and not missing_ok: + raise ValueError("VM does not exist") + if exists: + if virsh("domstate", name).strip() != "shut off": + virsh("destroy", name) + root = ET.fromstring(virsh("dumpxml", name)) + virsh("undefine", name, "--managed-save", *(["--nvram"] if root.find("./os/nvram") is not None else []), *(["--tpm"] if root.find("./devices/tpm") is not None else [])) + directory = DISKS / name + if spec["disks"] and directory.is_dir(): + for file in directory.iterdir(): + file.unlink() + directory.rmdir() + + def main(): action = sys.argv[1] payload = json.loads(sys.argv[2]) if len(sys.argv) > 2 else None validate(action, payload) if action == "node": return node() - if action == "domains": - return domains() + mac_vms.domain() + if action in {"domains", "computers"}: + return domains(computers=action == "computers") + mac_vms.domain() if action == "stats": result = {} for name in virsh("list", "--name").splitlines(): @@ -987,7 +1048,7 @@ def main(): raise ValueError("This VM has no serial console. Add one in its hardware settings.") return {"path": path} if action == "owner": - return {"owner": ET.fromstring(virsh("dumpxml", payload["name"])).findtext(f"metadata/{NS}vm/{NS}owner")} + return {"owner": ET.fromstring(virsh("dumpxml", payload["name"])).findtext(f"metadata/{NS}vm/{NS}owner"), "ownerOnly": import_module("vm-ai").lease(payload["name"]) is not None} if action == "access": try: with os.fdopen(open_regular(DISKS / payload["name"] / "access.json")) as incoming: @@ -1003,11 +1064,14 @@ def main(): elif action == "act": name = payload["name"] ensure_name(name) - if payload["action"] == "start": - root = ET.fromstring(virsh("dumpxml", name, "--inactive")) - if root.find("./devices/interface/source[@network='default']") is not None: - ensure_network() - virsh({"start": "start", "shutdown": "shutdown", "reboot": "reboot", "destroy": "destroy", "resume": "resume", "suspend": "suspend"}[payload["action"]], name) + with computer_lock(name): + if payload["action"] == "start": + root = ET.fromstring(virsh("dumpxml", name, "--inactive")) + if root.find("./devices/interface/source[@network='default']") is not None: + ensure_network() + ai = import_module("vm-ai") + ai.lease(name, ai.LIMITS["idle_seconds"]) + virsh({"start": "start", "shutdown": "shutdown", "reboot": "reboot", "destroy": "destroy", "resume": "resume", "suspend": "suspend"}[payload["action"]], name) elif action == "update": name = payload["name"] ensure_name(name) @@ -1019,19 +1083,8 @@ def main(): flags.append("--live") virsh("desc", name, *flags, "--", payload["description"]) elif action == "remove": - name = payload["name"] - ensure_name(name) - if name not in virsh("list", "--all", "--name").splitlines(): - raise ValueError("VM does not exist") - if virsh("domstate", name).strip() != "shut off": - virsh("destroy", name) - root = ET.fromstring(virsh("dumpxml", name)) - virsh("undefine", name, "--managed-save", *(["--nvram"] if root.find("./os/nvram") is not None else []), *(["--tpm"] if root.find("./devices/tpm") is not None else [])) - directory = DISKS / name - if payload["disks"] and directory.is_dir(): - for file in directory.iterdir(): - file.unlink() - directory.rmdir() + with computer_lock(payload["name"]): + remove(payload) else: raise ValueError("unsupported VM action") return None -- 2.54.1