authoravatarclover caruso<git@paperclover.net>2026-10-22 13:14:53-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-24 19:01:23-07:00
log756c156486e0d69ac87e351d8429af457369d2f1
tree0b70e8ac90f859a22905a16e006c9a2328e97c46
parent6baed795fd800aa9f5dc8a1a5cae8ddea135bea7
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

41 files changed, 3759 insertions(+), 483 deletions(-)

dashboard/src/computer-tools.json created+631
...@@ -0,0 +1,631 @@
1[
2 {
3 "name": "computer_list",
4 "description": "List VMs granted to this connection, with their operating systems and current power state.",
5 "inputSchema": {
6 "type": "object",
7 "properties": {},
8 "required": [],
9 "additionalProperties": false
10 },
11 "annotations": {
12 "readOnlyHint": true
13 }
14 },
15 {
16 "name": "computer_status",
17 "description": "Report the selected VM’s available runtimes, native UI support, permissions and desktop capabilities.",
18 "inputSchema": {
19 "type": "object",
20 "properties": {
21 "vm": {
22 "type": "string",
23 "description": "VM name from computer_list."
24 }
25 },
26 "required": [
27 "vm"
28 ],
29 "additionalProperties": false
30 },
31 "annotations": {
32 "readOnlyHint": true
33 }
34 },
35 {
36 "name": "computer_shot",
37 "description": "Capture the VM desktop as an image. Input coordinates are pixels in this screenshot, with (0,0) at its top left.",
38 "inputSchema": {
39 "type": "object",
40 "properties": {
41 "vm": {
42 "type": "string",
43 "description": "VM name from computer_list."
44 }
45 },
46 "required": [
47 "vm"
48 ],
49 "additionalProperties": false
50 },
51 "annotations": {
52 "readOnlyHint": true
53 }
54 },
55 {
56 "name": "computer_input",
57 "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.",
58 "inputSchema": {
59 "type": "object",
60 "properties": {
61 "vm": {
62 "type": "string",
63 "description": "VM name from computer_list."
64 },
65 "actions": {
66 "type": "array",
67 "items": {
68 "oneOf": [
69 {
70 "type": "object",
71 "properties": {
72 "type": {
73 "const": "click"
74 },
75 "x": {
76 "type": "integer",
77 "minimum": 0
78 },
79 "y": {
80 "type": "integer",
81 "minimum": 0
82 },
83 "button": {
84 "enum": [
85 "left",
86 "right",
87 "middle"
88 ]
89 },
90 "count": {
91 "type": "integer",
92 "minimum": 1,
93 "maximum": 3
94 }
95 },
96 "required": [
97 "type",
98 "x",
99 "y"
100 ],
101 "additionalProperties": false
102 },
103 {
104 "type": "object",
105 "properties": {
106 "type": {
107 "const": "move"
108 },
109 "x": {
110 "type": "integer",
111 "minimum": 0
112 },
113 "y": {
114 "type": "integer",
115 "minimum": 0
116 }
117 },
118 "required": [
119 "type",
120 "x",
121 "y"
122 ],
123 "additionalProperties": false
124 },
125 {
126 "type": "object",
127 "properties": {
128 "type": {
129 "const": "drag"
130 },
131 "x": {
132 "type": "integer",
133 "minimum": 0
134 },
135 "y": {
136 "type": "integer",
137 "minimum": 0
138 },
139 "to_x": {
140 "type": "integer",
141 "minimum": 0
142 },
143 "to_y": {
144 "type": "integer",
145 "minimum": 0
146 },
147 "button": {
148 "enum": [
149 "left",
150 "right",
151 "middle"
152 ]
153 },
154 "duration_ms": {
155 "type": "integer",
156 "minimum": 1,
157 "maximum": 5000
158 }
159 },
160 "required": [
161 "type",
162 "x",
163 "y",
164 "to_x",
165 "to_y"
166 ],
167 "additionalProperties": false
168 },
169 {
170 "type": "object",
171 "properties": {
172 "type": {
173 "const": "scroll"
174 },
175 "x": {
176 "type": "integer",
177 "minimum": 0
178 },
179 "y": {
180 "type": "integer",
181 "minimum": 0
182 },
183 "direction": {
184 "enum": [
185 "up",
186 "down",
187 "left",
188 "right"
189 ]
190 },
191 "amount": {
192 "type": "integer",
193 "minimum": 1,
194 "maximum": 100
195 }
196 },
197 "required": [
198 "type",
199 "x",
200 "y",
201 "direction"
202 ],
203 "additionalProperties": false
204 },
205 {
206 "type": "object",
207 "properties": {
208 "type": {
209 "const": "key"
210 },
211 "keys": {
212 "type": "array",
213 "items": {
214 "type": "string"
215 },
216 "minItems": 1,
217 "maxItems": 8
218 }
219 },
220 "required": [
221 "type",
222 "keys"
223 ],
224 "additionalProperties": false
225 },
226 {
227 "type": "object",
228 "properties": {
229 "type": {
230 "const": "text"
231 },
232 "text": {
233 "type": "string",
234 "maxLength": 16384
235 }
236 },
237 "required": [
238 "type",
239 "text"
240 ],
241 "additionalProperties": false
242 },
243 {
244 "type": "object",
245 "properties": {
246 "type": {
247 "const": "wait"
248 },
249 "duration_ms": {
250 "type": "integer",
251 "minimum": 1,
252 "maximum": 5000
253 }
254 },
255 "required": [
256 "type",
257 "duration_ms"
258 ],
259 "additionalProperties": false
260 }
261 ]
262 },
263 "minItems": 1,
264 "maxItems": 100
265 },
266 "screenshot": {
267 "type": "boolean",
268 "default": true
269 }
270 },
271 "required": [
272 "vm",
273 "actions"
274 ],
275 "additionalProperties": false
276 },
277 "annotations": {
278 "readOnlyHint": false
279 }
280 },
281 {
282 "name": "computer_ui",
283 "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.",
284 "inputSchema": {
285 "type": "object",
286 "properties": {
287 "vm": {
288 "type": "string",
289 "description": "VM name from computer_list."
290 },
291 "pid": {
292 "type": "integer",
293 "minimum": 1
294 }
295 },
296 "required": [
297 "vm"
298 ],
299 "additionalProperties": false
300 },
301 "annotations": {
302 "readOnlyHint": true
303 }
304 },
305 {
306 "name": "computer_ui_action",
307 "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.",
308 "inputSchema": {
309 "type": "object",
310 "properties": {
311 "vm": {
312 "type": "string",
313 "description": "VM name from computer_list."
314 },
315 "target": {
316 "type": "object"
317 },
318 "operation": {
319 "type": "string"
320 },
321 "value": {
322 "type": "string"
323 },
324 "screenshot": {
325 "type": "boolean",
326 "default": true
327 }
328 },
329 "required": [
330 "vm",
331 "target",
332 "operation"
333 ],
334 "additionalProperties": false
335 },
336 "annotations": {
337 "readOnlyHint": false
338 }
339 },
340 {
341 "name": "computer_exec",
342 "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.",
343 "inputSchema": {
344 "type": "object",
345 "properties": {
346 "vm": {
347 "type": "string",
348 "description": "VM name from computer_list."
349 },
350 "script": {
351 "type": "string",
352 "maxLength": 65536
353 },
354 "runtime": {
355 "type": "string",
356 "enum": [
357 "default",
358 "zsh",
359 "bash",
360 "powershell",
361 "cmd",
362 "ahk"
363 ],
364 "default": "default"
365 },
366 "timeout_ms": {
367 "type": "integer",
368 "minimum": 1,
369 "maximum": 180000,
370 "default": 60000
371 },
372 "screenshot": {
373 "type": "boolean",
374 "default": true
375 }
376 },
377 "required": [
378 "vm",
379 "script"
380 ],
381 "additionalProperties": false
382 },
383 "annotations": {
384 "readOnlyHint": false
385 }
386 },
387 {
388 "name": "computer_spawn",
389 "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.",
390 "inputSchema": {
391 "type": "object",
392 "properties": {
393 "vm": {
394 "type": "string",
395 "description": "VM name from computer_list."
396 },
397 "script": {
398 "type": "string",
399 "maxLength": 65536
400 },
401 "runtime": {
402 "type": "string",
403 "enum": [
404 "default",
405 "zsh",
406 "bash",
407 "powershell",
408 "cmd",
409 "ahk"
410 ],
411 "default": "default"
412 }
413 },
414 "required": [
415 "vm",
416 "script"
417 ],
418 "additionalProperties": false
419 },
420 "annotations": {
421 "readOnlyHint": false
422 }
423 },
424 {
425 "name": "computer_process",
426 "description": "Inspect or stop a process launched by computer_spawn. Use its returned id; handles reject PID reuse.",
427 "inputSchema": {
428 "type": "object",
429 "properties": {
430 "vm": {
431 "type": "string",
432 "description": "VM name from computer_list."
433 },
434 "id": {
435 "type": "string"
436 },
437 "operation": {
438 "enum": [
439 "status",
440 "stop"
441 ]
442 }
443 },
444 "required": [
445 "vm",
446 "id",
447 "operation"
448 ],
449 "additionalProperties": false
450 },
451 "annotations": {
452 "readOnlyHint": false
453 }
454 },
455 {
456 "name": "computer_file",
457 "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.",
458 "inputSchema": {
459 "type": "object",
460 "properties": {
461 "vm": {
462 "type": "string",
463 "description": "VM name from computer_list."
464 },
465 "path": {
466 "type": "string"
467 },
468 "operation": {
469 "enum": [
470 "stat",
471 "list",
472 "mkdir",
473 "remove"
474 ]
475 }
476 },
477 "required": [
478 "vm",
479 "path",
480 "operation"
481 ],
482 "additionalProperties": false
483 },
484 "annotations": {
485 "readOnlyHint": false
486 }
487 },
488 {
489 "name": "computer_upload",
490 "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.",
491 "inputSchema": {
492 "type": "object",
493 "properties": {
494 "vm": {
495 "type": "string",
496 "description": "VM name from computer_list."
497 },
498 "path": {
499 "type": "string"
500 },
501 "size": {
502 "type": "integer",
503 "minimum": 0,
504 "maximum": 67108864
505 },
506 "overwrite": {
507 "type": "boolean",
508 "default": false
509 }
510 },
511 "required": [
512 "vm",
513 "path",
514 "size"
515 ],
516 "additionalProperties": false
517 },
518 "annotations": {
519 "readOnlyHint": false
520 }
521 },
522 {
523 "name": "computer_download",
524 "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.",
525 "inputSchema": {
526 "type": "object",
527 "properties": {
528 "vm": {
529 "type": "string",
530 "description": "VM name from computer_list."
531 },
532 "path": {
533 "type": "string"
534 }
535 },
536 "required": [
537 "vm",
538 "path"
539 ],
540 "additionalProperties": false
541 },
542 "annotations": {
543 "readOnlyHint": true
544 }
545 },
546 {
547 "name": "computer_presets",
548 "description": "List approved Linux and Windows presets and enforced test-VM limits. Requires computer:create permission.",
549 "inputSchema": {
550 "type": "object",
551 "properties": {},
552 "required": [],
553 "additionalProperties": false
554 },
555 "annotations": {
556 "readOnlyHint": true
557 }
558 },
559 {
560 "name": "computer_create",
561 "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.",
562 "inputSchema": {
563 "type": "object",
564 "properties": {
565 "preset": {
566 "type": "string"
567 },
568 "name": {
569 "type": "string",
570 "pattern": "^[a-z0-9][a-z0-9-]{0,35}$"
571 },
572 "vcpus": {
573 "type": "integer",
574 "minimum": 1,
575 "maximum": 4,
576 "default": 2
577 },
578 "memory_gib": {
579 "type": "integer",
580 "minimum": 1,
581 "maximum": 8,
582 "default": 4
583 }
584 },
585 "required": [
586 "preset",
587 "name"
588 ],
589 "additionalProperties": false
590 },
591 "annotations": {
592 "readOnlyHint": false
593 }
594 },
595 {
596 "name": "computer_vm",
597 "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.",
598 "inputSchema": {
599 "type": "object",
600 "properties": {
601 "vm": {
602 "type": "string"
603 },
604 "operation": {
605 "type": "string",
606 "enum": [
607 "start",
608 "shutdown",
609 "reboot",
610 "extend",
611 "delete"
612 ]
613 },
614 "seconds": {
615 "type": "integer",
616 "minimum": 1,
617 "maximum": 86400,
618 "default": 3600
619 }
620 },
621 "required": [
622 "vm",
623 "operation"
624 ],
625 "additionalProperties": false
626 },
627 "annotations": {
628 "readOnlyHint": false
629 }
630 }
631]
dashboard/src/computer.rs+310-29
...@@ -4,19 +4,18 @@ use rmcp::{...@@ -4,19 +4,18 @@ use rmcp::{
4 model::{4 model::{
5 CacheScope, CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock,5 CacheScope, CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock,
6 ListToolsResult, PaginatedRequestParams, ServerCapabilities, ServerConfig, Tool,6 ListToolsResult, PaginatedRequestParams, ServerCapabilities, ServerConfig, Tool,
7 ToolAnnotations,
8 },7 },
9 service::RequestContext,8 service::RequestContext,
10};9};
1110
12pub async fn resources(owner: &Value) -> Result<Vec<Value>> {11pub async fn resources(owner: &Value) -> Result<Vec<Value>> {
13 let domains = host::call(json!({"operation":"vm.domains"})).await?;12 let domains = host::call(json!({"operation":"vm.computers"})).await?;
14 Ok(array(&domains)13 Ok(array(&domains)
15 .iter()14 .iter()
16 .filter(|vm| {15 .filter(|vm| {
17 vm["owner"] == owner["id"] && array(&vm["capabilities"]).iter().any(|c| c == "computer")16 vm["owner"] == owner["id"] && array(&vm["capabilities"]).iter().any(|c| c == "computer")
18 })17 })
19 .map(|vm| json!({"id":vm["name"],"name":vm["name"],"description":vm["description"]}))18 .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"]}))
20 .collect())19 .collect())
21}20}
2221
...@@ -34,8 +33,68 @@ fn permitted(grant: &Value, vm: &str, scope: &str, owner: &Value) -> Result<()>...@@ -34,8 +33,68 @@ fn permitted(grant: &Value, vm: &str, scope: &str, owner: &Value) -> Result<()>
34 Ok(())33 Ok(())
35}34}
3635
36struct Transfer {
37 headers: HeaderMap,
38 vm: String,
39 path: String,
40 size: u64,
41 upload: bool,
42 overwrite: bool,
43 expires: std::time::Instant,
44}
45
46struct UploadCleanup {
47 vm: String,
48 path: Option<String>,
49}
50
51impl Drop for UploadCleanup {
52 fn drop(&mut self) {
53 if let Some(path) = self.path.take() {
54 let vm = self.vm.clone();
55 tokio::spawn(async move {
56 let deadline = tokio::time::Instant::now() + Duration::from_secs(240);
57 loop {
58 match dispatch(&vm, json!({"action":"file","operation":"remove","path":path})).await {
59 Err(error) if error.status == 409 && tokio::time::Instant::now() < deadline => {
60 tokio::time::sleep(Duration::from_secs(1)).await;
61 }
62 _ => break,
63 }
64 }
65 });
66 }
67 }
68}
69
37#[derive(Clone)]70#[derive(Clone)]
38struct Computer;71struct Computer {
72 app: Arc<App>,
73 transfers: Arc<Mutex<HashMap<String, Transfer>>>,
74 transfer_slots: Arc<Semaphore>,
75}
76
77fn scope(action: &str, arguments: &serde_json::Map<String, Value>) -> &'static str {
78 if matches!(action, "status" | "shot" | "ui")
79 || (action == "process"
80 && arguments.get("operation").and_then(Value::as_str) == Some("status"))
81 {
82 "computer:read"
83 } else {
84 "computer:control"
85 }
86}
87
88async fn dispatch(vm: &str, operation: Value) -> Result<Value> {
89 let value =
90 host::call(json!({"operation":"vm.computer","payload":{"name":vm,"request":operation}}))
91 .await?;
92 if let Some(error) = value["error"].as_str() {
93 return Err(Error::new(400, error));
94 }
95 Ok(value)
96}
97
39impl ServerHandler for Computer {98impl ServerHandler for Computer {
40 fn get_info(&self) -> ServerConfig {99 fn get_info(&self) -> ServerConfig {
41 ServerConfig::new(ServerCapabilities::builder().enable_tools().build())100 ServerConfig::new(ServerCapabilities::builder().enable_tools().build())
...@@ -45,16 +104,8 @@ impl ServerHandler for Computer {...@@ -45,16 +104,8 @@ impl ServerHandler for Computer {
45 _: Option<PaginatedRequestParams>,104 _: Option<PaginatedRequestParams>,
46 _: RequestContext<RoleServer>,105 _: RequestContext<RoleServer>,
47 ) -> std::result::Result<ListToolsResult, ErrorData> {106 ) -> std::result::Result<ListToolsResult, ErrorData> {
48 let tools = [107 let tools: Vec<Tool> = serde_json::from_str(include_str!("computer-tools.json"))
49 ("computer_list", "List granted VMs with computer-use support.", json!({}), json!([])),108 .expect("computer tool schema");
50 ("computer_status", "Check a VM's desktop permissions and scripting capabilities.", json!({"vm":{"type":"string"}}), json!(["vm"])),
51 ("computer_shot", "Capture the selected VM's current desktop as an image.", json!({"vm":{"type":"string"}}), json!(["vm"])),
52 ("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"])),
53 ("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"])),
54 ].into_iter().map(|(name, description, properties, required)| {
55 Tool::new(name, description, json!({"type":"object","properties":properties,"required":required,"additionalProperties":false}).as_object().unwrap().clone())
56 .with_annotations(ToolAnnotations::new().read_only(name != "computer_exec"))
57 }).collect();
58 Ok(ListToolsResult {109 Ok(ListToolsResult {
59 tools,110 tools,
60 ttl_ms: Some(0),111 ttl_ms: Some(0),
...@@ -68,10 +119,39 @@ impl ServerHandler for Computer {...@@ -68,10 +119,39 @@ impl ServerHandler for Computer {
68 context: RequestContext<RoleServer>,119 context: RequestContext<RoleServer>,
69 ) -> std::result::Result<CallToolResponse, ErrorData> {120 ) -> std::result::Result<CallToolResponse, ErrorData> {
70 let result: Result<Value> = async {121 let result: Result<Value> = async {
71 let grant = &context.extensions.get::<axum::http::request::Parts>()122 let parts = context.extensions.get::<axum::http::request::Parts>()
72 .and_then(|p| p.extensions.get::<mcp::Grant>())123 .ok_or_else(|| Error::new(401, "This connection expired. Connect again."))?;
124 let grant = &parts.extensions.get::<mcp::Grant>()
73 .ok_or_else(|| Error::new(401, "This connection expired. Connect again."))?.0;125 .ok_or_else(|| Error::new(401, "This connection expired. Connect again."))?.0;
74 let mut arguments = request.arguments.unwrap_or_default();126 let mut arguments = request.arguments.unwrap_or_default();
127 if matches!(request.name.as_ref(), "computer_presets" | "computer_create") {
128 if !array(&grant["scopes"]).iter().any(|scope| scope == "computer:create") {
129 return Err(Error::new(403, "Reconnect with permission to create test VMs."));
130 }
131 if request.name == "computer_presets" {
132 if !arguments.is_empty() { return Err(Error::new(400, "This tool takes no arguments.")); }
133 return host::call(json!({"operation":"vm.ai","payload":{"action":"presets","owner":grant["user"]}})).await;
134 }
135 if arguments.keys().any(|key| !["preset","name","vcpus","memory_gib"].contains(&key.as_str())) {
136 return Err(Error::new(400, "Use the fields listed for creating a test VM."));
137 }
138 let created = host::call(json!({"operation":"vm.ai","payload":{"action":"create","owner":grant["user"],
139 "preset":arguments.get("preset"),"name":arguments.get("name"),
140 "vcpus":arguments.get("vcpus").unwrap_or(&json!(2)),"memory":arguments.get("memory_gib").unwrap_or(&json!(4))}})).await?;
141 let mut db = self.app.mcp.db.lock().unwrap();
142 let tx = db.transaction()?;
143 let key = format!("grant:{}",string(&grant["id"]));
144 let mut current = mcp::get(&tx,&key)?;
145 if current["user"] != grant["user"] || !array(&current["scopes"]).iter().any(|scope| scope == "computer:create") {
146 return Err(Error::new(403, "The connection changed while the VM was starting. The test VM will expire automatically."));
147 }
148 let mut selected = array(&current["resources"]).to_vec();
149 selected.push(created["id"].clone());
150 current["resources"] = json!(selected);
151 mcp::put(&tx,&key,&current,0)?;
152 tx.commit()?;
153 return Ok(created);
154 }
75 if request.name == "computer_list" {155 if request.name == "computer_list" {
76 if !arguments.is_empty() { return Err(Error::new(400, "This tool takes no arguments.")); }156 if !arguments.is_empty() { return Err(Error::new(400, "This tool takes no arguments.")); }
77 return Ok(json!({"computers":resources(&json!({"id":grant["user"]})).await?.into_iter()157 return Ok(json!({"computers":resources(&json!({"id":grant["user"]})).await?.into_iter()
...@@ -83,20 +163,73 @@ impl ServerHandler for Computer {...@@ -83,20 +163,73 @@ impl ServerHandler for Computer {
83 if !array(&grant["resources"]).iter().any(|id| id == &vm) {163 if !array(&grant["resources"]).iter().any(|id| id == &vm) {
84 return Err(Error::new(403, "Choose a VM granted to this connection."));164 return Err(Error::new(403, "Choose a VM granted to this connection."));
85 }165 }
86 let scope = if request.name == "computer_exec" { "computer:control" } else { "computer:read" };166 let action = request.name.strip_prefix("computer_").ok_or_else(|| Error::new(404,"No tool with that name."))?;
87 let owner = host::call(json!({"operation":"vm.owner","payload":{"name":vm}})).await?;167 let owner = host::call(json!({"operation":"vm.owner","payload":{"name":vm}})).await?;
88 permitted(grant, &vm, scope, &owner)?;168 permitted(grant, &vm, scope(action,&arguments), &owner)?;
89 let operation = match request.name.as_ref() {169 if action == "vm" {
90 "computer_status" => json!({"action":"status"}),170 if arguments.keys().any(|key| !["operation","seconds"].contains(&key.as_str())) {
91 "computer_shot" => json!({"action":"shot"}),171 return Err(Error::new(400,"Use the fields listed for this VM operation."));
92 "computer_ui" => json!({"action":"ui","pid":arguments.remove("pid")}),172 }
93 "computer_exec" => json!({"action":"exec", "script":arguments.remove("script"),173 return match arguments.get("operation").and_then(Value::as_str) {
94 "timeout_ms":arguments.remove("timeout_ms").unwrap_or(json!(60000)),174 Some(operation @ ("extend" | "delete")) => host::call(json!({"operation":"vm.ai","payload":{
95 "screenshot":arguments.remove("screenshot").unwrap_or(json!(true))}),175 "action":operation,"name":vm,"owner":grant["user"],"seconds":arguments.get("seconds").unwrap_or(&json!(3600))}})).await,
96 _ => return Err(Error::new(404, "No tool with that name.")),176 Some(operation @ ("start" | "shutdown" | "reboot")) if !arguments.contains_key("seconds") => {
177 host::call(json!({"operation":"vm.act","payload":{"name":vm,"action":operation}})).await?;
178 Ok(json!({"id":vm,"requested":operation}))
179 },
180 _ => Err(Error::new(400,"Choose start, shutdown, reboot, extend, or delete.")),
181 };
182 }
183 if matches!(action,"upload"|"download") {
184 let path = arguments.remove("path").and_then(|v| v.as_str().map(str::to_owned))
185 .filter(|p| !p.is_empty() && p.len() <= 4096 && !p.contains('\0'))
186 .ok_or_else(|| Error::new(400,"Choose an absolute guest file path."))?;
187 let upload = action == "upload";
188 let overwrite = arguments.remove("overwrite").unwrap_or(json!(false));
189 if !overwrite.is_boolean() { return Err(Error::new(400,"Choose whether to replace the destination.")); }
190 let size = if upload {
191 arguments.remove("size").and_then(|v|v.as_u64()).ok_or_else(||Error::new(400,"Provide the upload size in bytes."))?
192 } else {
193 let info = dispatch(&vm,json!({"action":"file","operation":"stat","path":path})).await?;
194 if info["directory"] == true { return Err(Error::new(400,"Choose a file to download.")); }
195 info["size"].as_u64().ok_or_else(||Error::new(400,"The guest did not report a file size."))?
196 };
197 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.")); }
198 if upload {
199 let parent = path.rfind(['/', '\\']).filter(|index| index + 1 < path.len())
200 .map(|index| &path[..=index]).ok_or_else(|| Error::new(400,"Choose an absolute guest file path."))?;
201 let info = dispatch(&vm,json!({"action":"file","operation":"stat","path":parent})).await?;
202 if info["directory"] != true { return Err(Error::new(400,"Choose an existing destination folder.")); }
203 }
204 let now = std::time::Instant::now();
205 let mut transfers = self.transfers.lock().unwrap();
206 transfers.retain(|_,t|t.expires>now);
207 if transfers.len()>=128 { return Err(Error::new(429,"Too many pending transfers. Finish one or wait for it to expire.")); }
208 let id = format!("{}{}",uuid::Uuid::new_v4().simple(),uuid::Uuid::new_v4().simple());
209 let mut headers = HeaderMap::new();
210 if let Some(value) = parts.headers.get("authorization") { headers.insert("authorization",value.clone()); }
211 transfers.insert(id.clone(),Transfer{headers,vm,path,size,upload,overwrite:overwrite==true,expires:now+Duration::from_secs(600)});
212 return Ok(json!({"url":format!("{}mcp/computer/files/{id}",self.app.mcp.origin),"method":if upload {"PUT"} else {"GET"},"bytes":size,"expires_in":600}));
213 }
214 let fields: &[&str] = match action {
215 "status"|"shot" => &[], "ui" => &["pid"],
216 "input" => &["actions","screenshot"],
217 "ui_action" => &["target","operation","value","screenshot"],
218 "exec" => &["script","runtime","timeout_ms","screenshot"],
219 "spawn" => &["script","runtime"], "process" => &["id","operation"],
220 "file" => &["path","operation"],
221 _ => return Err(Error::new(404,"No tool with that name.")),
97 };222 };
98 if !arguments.is_empty() { return Err(Error::new(400, "Use the fields listed for this tool.")); }223 if arguments.keys().any(|k|!fields.contains(&k.as_str())) { return Err(Error::new(400,"Use the fields listed for this tool.")); }
99 host::call(json!({"operation":"vm.computer","payload":{"name":vm,"request":operation}})).await224 if action == "file" && !matches!(arguments.get("operation").and_then(Value::as_str),Some("stat"|"list"|"mkdir"|"remove")) {
225 return Err(Error::new(400,"Choose stat, list, mkdir or remove."));
226 }
227 if action == "exec" { arguments.entry("timeout_ms").or_insert(json!(60000)); }
228 if matches!(action,"exec"|"input"|"ui_action") { arguments.entry("screenshot").or_insert(json!(true)); }
229 if matches!(action,"exec"|"spawn") { arguments.entry("runtime").or_insert(json!("default")); }
230 arguments.insert("action".into(),json!(action));
231 dispatch(&vm,Value::Object(arguments)).await
232
100 }.await;233 }.await;
101 Ok(match result {234 Ok(match result {
102 Ok(mut value) => {235 Ok(mut value) => {
...@@ -113,8 +246,144 @@ impl ServerHandler for Computer {...@@ -113,8 +246,144 @@ impl ServerHandler for Computer {
113 }246 }
114}247}
115248
249async fn transfer_access(computer: &Computer, transfer: &Transfer) -> Result<()> {
250 if transfer.expires <= std::time::Instant::now() {
251 return Err(Error::new(410, "This transfer expired. Request a new URL."));
252 }
253 let grant = computer
254 .app
255 .mcp
256 .authenticate(&transfer.headers, &computer.app.mcp.resource("computer"))?;
257 if !mcp::active_owner(&computer.app, &grant)? {
258 return Err(Error::new(401, "This connection expired. Connect again."));
259 }
260 let owner = host::call(json!({"operation":"vm.owner","payload":{"name":transfer.vm}})).await?;
261 permitted(&grant, &transfer.vm, "computer:control", &owner)
262}
263
264async fn transfer(
265 State(computer): State<Computer>,
266 axum::extract::Path(id): axum::extract::Path<String>,
267 request: Request,
268) -> Result<Response> {
269 use base64::Engine;
270 let transfer = computer
271 .transfers
272 .lock()
273 .unwrap()
274 .remove(&id)
275 .ok_or_else(|| {
276 Error::new(
277 410,
278 "This transfer expired or was already used. Request a new URL.",
279 )
280 })?;
281 transfer_access(&computer, &transfer).await?;
282 let permit = computer
283 .transfer_slots
284 .clone()
285 .try_acquire_owned()
286 .map_err(|_| {
287 Error::new(
288 429,
289 "Four file transfers are running. Finish one, then request a new URL.",
290 )
291 })?;
292 if request.method()
293 != if transfer.upload {
294 Method::PUT
295 } else {
296 Method::GET
297 }
298 {
299 return Err(Error::new(
300 405,
301 "Use the HTTP method returned with this URL.",
302 ));
303 }
304 if transfer.upload {
305 let temporary = format!(
306 "{}.snowglobe-{}",
307 transfer.path,
308 uuid::Uuid::new_v4().simple()
309 );
310 let mut cleanup = UploadCleanup {
311 vm: transfer.vm.clone(),
312 path: Some(temporary.clone()),
313 };
314 let result: Result<Value> = tokio::time::timeout_at(transfer.expires.into(), async {
315 let mut stream=request.into_body().into_data_stream();
316 let mut offset=0_u64;
317 while let Some(chunk)=stream.next().await {
318 let chunk=chunk.map_err(|_|Error::new(400,"The upload ended early. Request a new URL and retry."))?;
319 for bytes in chunk.chunks(65536) {
320 if offset+bytes.len() as u64>transfer.size { return Err(Error::new(413,"The upload exceeds its declared size.")); }
321 transfer_access(&computer,&transfer).await?;
322 dispatch(&transfer.vm,json!({"action":"file","operation":"write","path":temporary,"offset":offset,"data":base64::engine::general_purpose::STANDARD.encode(bytes)})).await?;
323 offset+=bytes.len() as u64;
324 }
325 }
326 if offset!=transfer.size { return Err(Error::new(400,"The upload ended early. Request a new URL and retry.")); }
327 if offset==0 { dispatch(&transfer.vm,json!({"action":"file","operation":"write","path":temporary,"offset":0,"data":""})).await?; }
328 transfer_access(&computer,&transfer).await?;
329 dispatch(&transfer.vm,json!({"action":"file","operation":"commit","path":temporary,"destination":transfer.path,"overwrite":transfer.overwrite})).await?;
330 Ok(json!({"path":transfer.path,"bytes":offset}))
331 }).await.map_err(|_|Error::new(410,"This transfer expired. Request a new URL."))?;
332 if result.is_ok() {
333 cleanup.path = None;
334 }
335 Ok(axum::Json(result?).into_response())
336 } else {
337 let size = transfer.size;
338 let (sender, receiver) = tokio::sync::mpsc::channel(1);
339 tokio::spawn(async move {
340 let _permit = permit;
341 let result: Result<()> = tokio::time::timeout_at(transfer.expires.into(), async {
342 let mut offset = 0;
343 while offset < transfer.size {
344 if sender.is_closed() { return Ok(()); }
345 transfer_access(&computer, &transfer).await?;
346 let length = (transfer.size - offset).min(65536);
347 let reply = dispatch(&transfer.vm, json!({"action":"file","operation":"read","path":transfer.path,"offset":offset,"length":length})).await?;
348 let bytes = base64::engine::general_purpose::STANDARD.decode(string(&reply["data"]))
349 .map_err(|_| Error::new(502, "The guest returned an incomplete file."))?;
350 if bytes.len() as u64 != length {
351 return Err(Error::new(409, "The guest file changed during the download."));
352 }
353 if sender.send(Ok(Bytes::from(bytes))).await.is_err() { return Ok(()); }
354 offset += length;
355 }
356 Ok(())
357 }).await.unwrap_or_else(|_| Err(Error::new(410, "This transfer expired. Request a new URL.")));
358 if let Err(error) = result {
359 let _ = sender.try_send(Err(std::io::Error::other(error.message)));
360 }
361 });
362 let stream = futures::stream::unfold(receiver, |mut receiver| async {
363 receiver.recv().await.map(|chunk| (chunk, receiver))
364 });
365 Ok(Response::builder()
366 .header("content-type", "application/octet-stream")
367 .header("content-length", size)
368 .header("cache-control", "no-store")
369 .header("content-disposition", "attachment")
370 .body(axum::body::Body::from_stream(stream))
371 .unwrap())
372 }
373}
374
116pub fn router(app: Arc<App>) -> Router {375pub fn router(app: Arc<App>) -> Router {
117 mcp::router(app, "computer", || Ok(Computer))376 let computer = Computer {
377 app: app.clone(),
378 transfers: Arc::new(Mutex::new(HashMap::new())),
379 transfer_slots: Arc::new(Semaphore::new(4)),
380 };
381 let handler = computer.clone();
382 mcp::router(app, "computer", move || Ok(handler.clone())).merge(
383 Router::new()
384 .route("/mcp/computer/files/{id}", any(transfer))
385 .with_state(computer),
386 )
118}387}
119388
120#[cfg(test)]389#[cfg(test)]
...@@ -138,4 +407,16 @@ mod tests {...@@ -138,4 +407,16 @@ mod tests {
138 );407 );
139 assert!(permitted(&grant, "m3-devbox", "computer:read", &Value::Null).is_err());408 assert!(permitted(&grant, "m3-devbox", "computer:read", &Value::Null).is_err());
140 }409 }
410
411 #[test]
412 fn screen_access_does_not_grant_files_commands_or_lifecycle_changes() {
413 let args = serde_json::Map::new();
414 for action in ["file", "upload", "download", "exec", "spawn", "input", "ui_action", "vm"] {
415 assert_eq!(scope(action, &args), "computer:control");
416 }
417 let status = json!({"operation":"status"});
418 let stop = json!({"operation":"stop"});
419 assert_eq!(scope("process", status.as_object().unwrap()), "computer:read");
420 assert_eq!(scope("process", stop.as_object().unwrap()), "computer:control");
421 }
141}422}
dashboard/src/host-protocol.json+1-1
...@@ -1 +1 @@...@@ -1 +1 @@
1{"request":2097152,"response":16777216}1{"request":2097152,"response":16777216,"memory":536870912}
dashboard/src/host.rs+1-1
...@@ -31,7 +31,7 @@ pub async fn call(request: Value) -> Result<Value> {...@@ -31,7 +31,7 @@ pub async fn call(request: Value) -> Result<Value> {
31pub async fn open(request: Value) -> Result<(Value, tokio::net::UnixStream)> {31pub async fn open(request: Value) -> Result<(Value, tokio::net::UnixStream)> {
32 let timeout = if matches!(32 let timeout = if matches!(
33 request["operation"].as_str(),33 request["operation"].as_str(),
34 Some("vm.create" | "vm.media" | "vm.preset")34 Some("vm.create" | "vm.media" | "vm.preset" | "vm.ai")
35 ) {35 ) {
36 91036 910
37 } else if matches!(request["operation"].as_str(), Some("vm.act" | "vm.computer")) {37 } else if matches!(request["operation"].as_str(), Some("vm.act" | "vm.computer")) {
dashboard/src/mcp.rs+11-9
...@@ -8,7 +8,7 @@ use sha2::{Digest, Sha256};...@@ -8,7 +8,7 @@ use sha2::{Digest, Sha256};
8use std::os::unix::fs::{DirBuilderExt, PermissionsExt};8use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
99
10const CATALOGS: &[(&str, &str, &[&str])] = &[10const CATALOGS: &[(&str, &str, &[&str])] = &[
11 ("computer", "Computers", &["computer:read", "computer:control", "offline_access"]),11 ("computer", "Computers", &["computer:read", "computer:control", "computer:create", "offline_access"]),
12 (12 (
13 "observability",13 "observability",
14 "Logs and traces",14 "Logs and traces",
...@@ -669,13 +669,13 @@ pub async fn oauth(State(app): State<Arc<App>>, request: Request) -> Response {...@@ -669,13 +669,13 @@ pub async fn oauth(State(app): State<Arc<App>>, request: Request) -> Response {
669 response669 response
670}670}
671671
672fn chosen_resources(body: &Value, resources: &[Value], shale: bool) -> Result<Value> {672fn chosen_resources(body: &Value, resources: &[Value], shale: bool, allow_empty: bool) -> Result<Value> {
673 if shale && body["resources"] == "all" {673 if shale && body["resources"] == "all" {
674 return Ok(json!("all"));674 return Ok(json!("all"));
675 }675 }
676 let chosen = body["resources"]676 let chosen = body["resources"]
677 .as_array()677 .as_array()
678 .filter(|items| !items.is_empty() && items.len() <= resources.len())678 .filter(|items| (allow_empty || !items.is_empty()) && items.len() <= resources.len())
679 .ok_or_else(|| Error::new(400, "Choose each available resource once."))?;679 .ok_or_else(|| Error::new(400, "Choose each available resource once."))?;
680 if chosen.iter().enumerate().any(|(index, item)| {680 if chosen.iter().enumerate().any(|(index, item)| {
681 !resources.iter().any(|resource| item == &resource["id"]) || chosen[..index].contains(item)681 !resources.iter().any(|resource| item == &resource["id"]) || chosen[..index].contains(item)
...@@ -861,7 +861,7 @@ pub async fn manage(...@@ -861,7 +861,7 @@ pub async fn manage(
861 "Link your Shale account before allowing repository access.",861 "Link your Shale account before allowing repository access.",
862 ));862 ));
863 }863 }
864 let chosen = chosen_resources(&body, &resources, shale_consent)?;864 let chosen = chosen_resources(&body, &resources, shale_consent, array(&pending["scopes"]).iter().any(|s| s == "computer:create"))?;
865 if list(&tx, "grant:")?865 if list(&tx, "grant:")?
866 .iter()866 .iter()
867 .filter(|g| g["user"] == owner_id)867 .filter(|g| g["user"] == owner_id)
...@@ -911,7 +911,7 @@ pub async fn manage(...@@ -911,7 +911,7 @@ pub async fn manage(
911 "Link your Shale account before allowing repository access.",911 "Link your Shale account before allowing repository access.",
912 ));912 ));
913 }913 }
914 let chosen = chosen_resources(&body, &resources, shale_consent)?;914 let chosen = chosen_resources(&body, &resources, shale_consent, array(&grant["scopes"]).iter().any(|s| s == "computer:create"))?;
915 grant["resources"] = json!(chosen);915 grant["resources"] = json!(chosen);
916 tx.execute(916 tx.execute(
917 "UPDATE records SET value=? WHERE key=?",917 "UPDATE records SET value=? WHERE key=?",
...@@ -936,7 +936,7 @@ mod tests {...@@ -936,7 +936,7 @@ mod tests {
936 fn resource_updates_reject_empty_duplicates_and_foreign_choices() {936 fn resource_updates_reject_empty_duplicates_and_foreign_choices() {
937 let resources = vec![json!({"id":"alpha"}), json!({"id":"beta"})];937 let resources = vec![json!({"id":"alpha"}), json!({"id":"beta"})];
938 assert_eq!(938 assert_eq!(
939 chosen_resources(&json!({"resources":["beta"]}), &resources, false).unwrap(),939 chosen_resources(&json!({"resources":["beta"]}), &resources, false, false).unwrap(),
940 json!(["beta"])940 json!(["beta"])
941 );941 );
942 for selected in [942 for selected in [
...@@ -945,13 +945,15 @@ mod tests {...@@ -945,13 +945,15 @@ mod tests {
945 json!(["foreign"]),945 json!(["foreign"]),
946 json!([null]),946 json!([null]),
947 ] {947 ] {
948 assert!(chosen_resources(&json!({"resources":selected}), &resources, false).is_err());948 assert!(chosen_resources(&json!({"resources":selected}), &resources, false, false).is_err());
949 }949 }
950 assert_eq!(950 assert_eq!(
951 chosen_resources(&json!({"resources":"all"}), &[], true).unwrap(),951 chosen_resources(&json!({"resources":"all"}), &[], true, false).unwrap(),
952 json!("all")952 json!("all")
953 );953 );
954 assert!(chosen_resources(&json!({"resources":"all"}), &resources, false).is_err());954 assert!(chosen_resources(&json!({"resources":"all"}), &resources, false, false).is_err());
955 assert_eq!(chosen_resources(&json!({"resources":[]}), &resources, false, true).unwrap(), json!([]));
956 assert!(chosen_resources(&json!({"resources":["foreign"]}), &resources, false, true).is_err());
955 }957 }
956958
957 use super::*;959 use super::*;
dashboard/web/components/TabBar.tsx+10
...@@ -9,15 +9,25 @@ export function TabBar(props: { label: string; children: JSX.Element }) {...@@ -9,15 +9,25 @@ export function TabBar(props: { label: string; children: JSX.Element }) {
9 let root!: HTMLDivElement;9 let root!: HTMLDivElement;
10 let pill!: HTMLSpanElement;10 let pill!: HTMLSpanElement;
11 let ink!: HTMLDivElement;11 let ink!: HTMLDivElement;
12 let shown = false;
12 const place = () => {13 const place = () => {
13 ink.replaceChildren(...[...root.children].filter((el) => el !== pill && el !== ink).map((el) => el.cloneNode(true)));14 ink.replaceChildren(...[...root.children].filter((el) => el !== pill && el !== ink).map((el) => el.cloneNode(true)));
14 const chosen = root.querySelector<HTMLElement>(':scope > :is([aria-pressed="true"], [aria-current="page"])');15 const chosen = root.querySelector<HTMLElement>(':scope > :is([aria-pressed="true"], [aria-current="page"])');
15 pill.hidden = ink.hidden = !chosen;16 pill.hidden = ink.hidden = !chosen;
16 if (!chosen) return;17 if (!chosen) return;
18 // Back from display: none it jumps; a slide would start from stale geometry and can stall in a throttled tab.
19 const jump = !shown;
20 shown = pill.getClientRects().length > 0;
21 if (!shown) return;
22 if (jump) pill.style.transition = ink.style.transition = "none";
17 pill.style.left = `${chosen.offsetLeft}px`;23 pill.style.left = `${chosen.offsetLeft}px`;
18 pill.style.width = `${chosen.offsetWidth}px`;24 pill.style.width = `${chosen.offsetWidth}px`;
19 const right = root.clientWidth - chosen.offsetLeft - chosen.offsetWidth;25 const right = root.clientWidth - chosen.offsetLeft - chosen.offsetWidth;
20 ink.style.clipPath = `inset(2px ${right}px 2px ${chosen.offsetLeft}px round 7px)`;26 ink.style.clipPath = `inset(2px ${right}px 2px ${chosen.offsetLeft}px round 7px)`;
27 if (jump) {
28 pill.getBoundingClientRect();
29 pill.style.transition = ink.style.transition = "";
30 }
21 };31 };
22 onMount(() => {32 onMount(() => {
23 place();33 place();
dashboard/web/pages/AI.tsx+55-28
...@@ -1,47 +1,74 @@...@@ -1,47 +1,74 @@
1import { parseResponse } from "hono/client";1import { parseResponse } from "hono/client";
2import CopyIcon from "lucide-solid/icons/copy";
3import Download from "lucide-solid/icons/download";
4import EyeOff from "lucide-solid/icons/eye-off";
5import KeyRound from "lucide-solid/icons/key-round";
2import { createResource, createSignal, For, Show } from "solid-js";6import { createResource, createSignal, For, Show } from "solid-js";
3import { api, reason } from "../api.ts";7import { api, reason } from "../api.ts";
4import { Copy } from "../components/Copy.tsx";8import { Copy } from "../components/Copy.tsx";
5import { Loaded } from "../components/Loaded.tsx";9import { Loaded } from "../components/Loaded.tsx";
6import { toast } from "../components/Toast.tsx";10import claude from "../../src/claude.svg?raw";
11import openai from "../../src/openai.svg?raw";
12
13export function CopyField(props: { value: string; label: string }) {
14 return <div class="mcp-field"><Copy value={props.value} label={props.label}><span class="value">{props.value}</span><CopyIcon size={14} class="icon" /></Copy></div>;
15}
16
17export function ClientMark(props: { name: string }) {
18 const logo = () => /claude/i.test(props.name) ? claude : /codex|openai|chatgpt/i.test(props.name) ? openai : "";
19 return <span class="mcp-client-mark" aria-hidden="true">
20 <Show when={logo()} fallback={<span class="monogram">{props.name.trim()[0]?.toUpperCase()}</span>}>{(svg) => <span class="logo" innerHTML={svg()} />}</Show>
21 </span>;
22}
23
24const WRAPPERS = [["claude", "Claude Code"], ["codex", "Codex"]] as const;
725
8export function AI(props: { viewing: boolean }) {26export function AI(props: { viewing: boolean }) {
9 const [info, { refetch }] = createResource(() => parseResponse(api.ai.$get()));27 const [info, { refetch }] = createResource(() => parseResponse(api.ai.$get()));
10 const [key, setKey] = createSignal("");28 const [key, setKey] = createSignal("");
11 const [busy, setBusy] = createSignal(false);29 const [busy, setBusy] = createSignal(false);
30 const [error, setError] = createSignal("");
31 let show!: HTMLButtonElement;
32 let hide!: HTMLButtonElement;
12 return <Loaded data={info} what="Local AI" retry={refetch}>{(data) => <>33 return <Loaded data={info} what="Local AI" retry={refetch}>{(data) => <>
13 <section class="mcp-endpoint">34 <section class="mcp-section">
14 <p>{data().model} on Snow Globe · {Math.round(data().context / 1024)}K context</p>35 <div class="stats"><span class="stat"><b>{data().model}</b></span><span class="stat"><b>{Math.round(data().context / 1024)}K</b> context</span></div>
15 <div class="mcp-section-heading"><h2>API endpoint</h2><Copy value={data().endpoint} label="API endpoint" /></div>36 <h2 class="mcp-label">API endpoint</h2>
16 <p class="muted">OpenAI Responses and Chat Completions use /v1. Anthropic Messages uses this base URL.</p>37 <CopyField value={data().endpoint} label="API endpoint" />
17 </section>38 <p class="hint">OpenAI Responses and Chat Completions use /v1. Anthropic Messages uses this base URL</p>
18 <section class="mcp-panel">39 <h2 class="mcp-label">API key</h2>
19 <h2>API key</h2>40 <div class="mcp-key">
20 <p>Use this key with the endpoint or enter it when a Snow Globe wrapper first starts.</p>41 <Show when={key()} fallback={<>
21 <div class="mcp-actions">42 <span class="mcp-field masked" aria-hidden="true"><span class="value">••••••••••••••••••••••••••••••••</span></span>
22 <Show when={key()} fallback={<button class="button" disabled={busy() || props.viewing}43 <button ref={show} class="button" disabled={busy() || props.viewing} aria-busy={busy()}
23 title={props.viewing ? "Switch back to your account to access the key" : undefined} onClick={async () => {44 data-tip={props.viewing ? "Switch back to your account to see the key" : undefined} onClick={async () => {
24 setBusy(true);45 setBusy(true); setError("");
25 try { setKey((await parseResponse(api.ai.key.$post())).key); }46 try { setKey((await parseResponse(api.ai.key.$post())).key); hide.focus(); }
26 catch (error) { toast(reason(error)); }47 catch (failure) { setError(reason(failure)); }
27 finally { setBusy(false); }48 finally { setBusy(false); }
28 }}>Access API key</button>}>49 }}><KeyRound size={14} />Show key</button>
29 <Copy value={key()} label="API key">Copy API key</Copy>50 </>}>
30 <button class="button" onClick={() => setKey("")}>Hide key</button>51 <CopyField value={key()} label="API key" />
52 <button ref={hide} class="button" onClick={() => { setKey(""); show.focus(); }}><EyeOff size={14} />Hide</button>
31 </Show>53 </Show>
32 </div>54 </div>
33 <Show when={key()}><details class="mcp-install"><summary>Show key value</summary><code class="ai-key">{key()}</code></details></Show>55 <Show when={error()}><p class="error" role="alert">Couldn't show the key. {error()}</p></Show>
56 <p class="hint">Use it with the endpoint, or enter it when a wrapper first starts</p>
34 </section>57 </section>
35 <section class="mcp-panel">58 <section class="mcp-section">
36 <h2>Coding clients</h2>59 <h2>Coding clients</h2>
37 <p>Shell wrappers for installed Codex and Claude Code. Both start with automatic approval and use the local model.</p>60 <p>Bash wrappers for macOS and Linux that run your installed Claude Code or Codex on the local model. Both start with automatic approval.</p>
38 <p>Download a wrapper, then run its install command. Codex or Claude Code must already be installed.</p>61 <div class="mcp-wrappers"><For each={WRAPPERS}>{([id, name]) => <div class="mcp-wrapper">
39 <div class="ai-downloads"><For each={["codex", "claude"]}>{(client) => <div class="mcp-actions">62 <ClientMark name={name} />
40 <a class="button" href={`/api/ai/snow-${client}`} download={`snow-${client}`}>Download snow-{client}</a>63 <div class="mcp-wrapper-main">
41 <Copy value={`mkdir -p ~/.local/bin && install -m 755 ~/Downloads/snow-${client} ~/.local/bin/snow-${client}`} label={`install snow-${client}`} />64 <div class="mcp-wrapper-head"><b>{name}</b>
65 <a class="button small" href={`/api/ai/snow-${id}`} download={`snow-${id}`}><Download size={14} />Download snow-{id}</a></div>
66 <span class="mcp-label">Install from a macOS or Linux terminal</span>
67 <CopyField value={`mkdir -p ~/.local/bin && install -m 755 ~/Downloads/snow-${id} ~/.local/bin/snow-${id}`} label={`macOS and Linux install command for snow-${id}`} />
68 </div>
42 </div>}</For></div>69 </div>}</For></div>
43 <p>Run snow-codex or snow-claude from a project. The first run asks for your API key.</p>70 <p class="hint">Keep ~/.local/bin on your PATH, then run snow-claude or snow-codex from a project. The first run asks for your API key</p>
44 <p class="muted">API waits allow eight hours. Stock Codex approval reviews retain their 90-second deadline.</p>71 <p class="hint">API waits allow eight hours. Stock Codex approval reviews keep their 90-second deadline</p>
45 </section>72 </section>
46 </>}</Loaded>;73 </>}</Loaded>;
47}74}
dashboard/web/pages/MCP.css+169-54
...@@ -1,59 +1,174 @@...@@ -1,59 +1,174 @@
1/* Settings use the dashboard's controls and flat, divided sections. */1.mcp-page { max-width: 960px; }
2.mcp-page { max-width: 1000px; }2.mcp-page .page-head { margin-bottom: 12px; }
3.mcp-page h2 { font-size: 14px; font-weight: 600; margin: 0 0 8px; color: var(--text); }
4.mcp-page h3 { font-size: 13px; margin: 0; }
5.mcp-page p { color: var(--text-2); margin: 8px 0; }
6.mcp-page > .segmented { margin-bottom: 8px; }
7.mcp-panel, .mcp-endpoint { padding: 20px 0; border-bottom: 1px solid var(--line); }
8.mcp-section-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
9.mcp-section-heading h2 { margin: 0; flex: none; }
10.mcp-endpoint > p:first-child { margin: 0 0 16px; }
11.mcp-endpoint .copy { max-width: 100%; min-width: 0; overflow-wrap: anywhere; white-space: normal; text-align: left; }
12.mcp-endpoint .muted { font-size: 12px; }
13.mcp-repositories, .mcp-install { margin: 12px 0; }
14.mcp-repositories summary, .mcp-install summary { cursor: pointer; color: var(--text-2); }
15.mcp-page .mcp-access-modes > label { padding: 6px 0; border: 0; background: none; }
16.mcp-actions { display: flex; flex-wrap: wrap; gap: 8px; }
17.mcp-access { padding: 0; margin: 20px 0; border: 0; min-width: 0; }
18.mcp-access legend { font-weight: 600; margin-bottom: 12px; }
19.mcp-access-modes { display: grid; gap: 10px; }
20.mcp-access-modes > label { display: flex; align-items: start; gap: 12px; padding: 14px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
21.mcp-access-modes > label:has(input:checked) { border-color: var(--accent); background: var(--accent-wash); }
22.mcp-access-modes input { margin: 4px 0 0; accent-color: var(--accent); }
23.mcp-access-modes span span { display: block; font-size: 12px; margin-top: 4px; }
24.mcp-resources { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin: 16px 0; }
25.mcp-resources .checkbox { align-items: start; overflow-wrap: anywhere; }
26.mcp-resources small { display: block; margin-top: 4px; }
27.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; }
28.mcp-client { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 16px; padding: 18px 0; border-top: 1px solid var(--line); }
29.mcp-client > .mcp-actions { align-self: start; }
30.mcp-client p { margin: 6px 0 0; overflow-wrap: anywhere; }
31.mcp-edit-access { grid-column: 1 / -1; }
323
4.mcp-section { padding: 22px 0 6px; }
5.mcp-section + .mcp-section { border-top: 1px solid var(--line); margin-top: 16px; }
6.mcp-section h2 { margin: 0; color: var(--text); font-size: 15px; }
7.mcp-section > p { margin: 6px 0; color: var(--text-2); max-width: 70ch; }
8.mcp-section > p:first-child { margin-top: 0; }
9.mcp-heading { display: flex; align-items: center; gap: 10px; min-height: 26px; flex-wrap: wrap; }
10.mcp-heading .mcp-actions { margin-left: auto; }
11.mcp-section h2.mcp-label { margin: 16px 0 6px; color: var(--text-2); font-size: 12px; font-weight: 600; }
12.mcp-page .hint, .mcp-approval .hint { margin: 6px 0 0; color: var(--muted); font-size: 12px; }
13.mcp-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
14
15/* A value you copy: the whole well is the Copy button. */
16.mcp-field > .copy, .mcp-field.masked {
17 display: flex;
18 align-items: center;
19 gap: 10px;
20 width: 100%;
21 min-height: 34px;
22 padding: 6px 10px;
23 border: 2px solid var(--line);
24 border-radius: var(--radius);
25 background: var(--well);
26 color: var(--text);
27 font: 12.5px/1.45 var(--mono);
28 text-align: left;
29 white-space: normal;
30 cursor: copy;
31 transition: border-color 150ms, background-color 150ms;
32}
33.mcp-field { flex: 1; min-width: 0; }
34.mcp-field .value { flex: 1; min-width: 0; overflow-wrap: anywhere; }
35.mcp-field .icon { flex: none; color: var(--muted); transition: color 150ms; }
36.mcp-field > .copy:hover { border-color: var(--axis); background: var(--accent-wash); }
37.mcp-field > .copy:hover .icon { color: var(--accent); }
38.mcp-field.masked { box-sizing: border-box; cursor: default; color: var(--muted); letter-spacing: 0.1em; overflow: hidden; }
39.mcp-field.masked .value { overflow-wrap: normal; white-space: nowrap; }
40
41.mcp-key { display: flex; gap: 8px; align-items: stretch; }
42.mcp-key .button { height: auto; min-height: 34px; }
43.mcp-section > .mcp-key + .error { margin: 6px 0 0; color: color-mix(in srgb, var(--critical) 80%, var(--text)); font-size: 13px; }
44
45.mcp-wrappers { display: grid; gap: 14px; margin: 14px 0 10px; }
46.mcp-wrapper { display: grid; grid-template-columns: 28px minmax(0, 1fr); gap: 12px; align-items: start; }
47.mcp-wrapper-main { display: grid; gap: 8px; min-width: 0; }
48.mcp-wrapper-main > .mcp-label { margin: 2px 0 -4px; color: var(--text-2); font-size: 12px; font-weight: 600; }
49.mcp-wrapper-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 26px; }
50
51.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; }
52.mcp-client-mark .logo { display: grid; width: 16px; height: 16px; color: var(--text); }
53.mcp-client-mark .logo svg { width: 100%; height: 100%; }
54.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; }
55
56/* Shale account */
57.mcp-repositories { margin: 10px 0 0; }
58.mcp-repositories summary { display: inline-flex; align-items: center; gap: 4px; color: var(--text-2); cursor: pointer; list-style: none; border-radius: 4px; }
59.mcp-repositories summary::-webkit-details-marker { display: none; }
60.mcp-repositories[open] summary .chevron { transform: rotate(90deg); }
61.mcp-repository-list { margin: 8px 0 0; padding: 0; list-style: none; columns: 2 280px; column-gap: 24px; }
62.mcp-repository-list li { display: flex; gap: 10px; padding: 4px 0; break-inside: avoid; font-size: 13px; overflow-wrap: anywhere; }
63.mcp-repository-list li span:first-child { font-weight: 550; }
64.mcp-link { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; margin-top: 6px; }
65.mcp-link p { margin: 0; color: var(--text-2); }
66
67/* Connected clients: an edge-to-edge list, as the dashboard's tables are. */
68.mcp-section.flush { padding-bottom: 0; }
69.mcp-clients { margin: 10px calc(-1 * var(--gutter)) 0; padding: 0; list-style: none; }
70.mcp-client {
71 display: grid;
72 grid-template-columns: 28px minmax(0, 1fr) auto auto;
73 align-items: center;
74 column-gap: 12px;
75 padding: 8px 0 8px var(--gutter);
76 border-top: 1px solid var(--grid);
77 transition: background-color 150ms;
78}
79.mcp-client:last-child { border-bottom: 1px solid var(--grid); }
80.mcp-client:hover, .mcp-client.editing { background: color-mix(in srgb, var(--hover) 60%, transparent); }
81.mcp-client-main { min-width: 0; display: grid; gap: 4px; }
82.mcp-client h3 { margin: 0; font-size: 13.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
83.mcp-page .chips, .mcp-resources .chips { gap: 4px 6px; }
84.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); }
85.chip.fact { padding: 0 6px; font-size: 11px; line-height: 16px; color: var(--muted); }
86.chip.ability.strong { border-color: color-mix(in srgb, var(--warning) 45%, var(--line)); color: color-mix(in srgb, var(--warning) 60%, var(--text)); }
87.mcp-client-when { color: var(--muted); font-size: 12px; white-space: nowrap; }
88/* td.actions buttons, filling the row's height. */
89.mcp-row-actions { display: flex; align-self: stretch; }
90.mcp-row-actions button {
91 min-height: 36px;
92 padding: 0 14px;
93 border: 0;
94 background: none;
95 color: var(--accent);
96 font-size: 13px;
97 font-weight: 500;
98 white-space: nowrap;
99 cursor: pointer;
100 transition: background-color 150ms;
101}
102.mcp-row-actions button:hover:not(:disabled) { background: var(--raised); }
103.mcp-row-actions button.danger { color: color-mix(in srgb, var(--critical) 80%, var(--text)); }
104.mcp-row-actions button.danger:hover:not(:disabled) { background: color-mix(in srgb, var(--critical) 12%, transparent); }
105.mcp-row-actions button:disabled { opacity: 0.45; cursor: default; }
106.mcp-row-actions button:focus-visible { outline-offset: -2px; }
107.mcp-row-actions button[aria-expanded="true"] { background: var(--accent-wash); }
108
109.mcp-editor { grid-column: 2 / -1; padding: 6px var(--gutter) 10px 0; }
110.mcp-editor > p:first-child { margin: 6px 0 10px; color: var(--text-2); }
111.mcp-access { margin: 6px 0 0; padding: 0; border: 0; min-width: 0; }
112.mcp-access legend { padding: 0; margin-bottom: 8px; color: var(--text-2); font-size: 12px; font-weight: 600; }
113.mcp-resources { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px 16px; margin: 10px 0; }
114.mcp-resources .checkbox { align-items: start; overflow-wrap: anywhere; color: var(--text); }
115.mcp-resources .checkbox .box { margin-top: 2px; }
116.mcp-resources .checkbox > span:last-child { display: grid; gap: 4px; min-width: 0; }
117.mcp-resources small { display: block; font-size: 12px; }
118.mcp-fixed { display: flex; gap: 6px; align-items: start; margin: 8px 0 12px; color: var(--muted); font-size: 12px; }
119.mcp-fixed .icon { flex: none; margin-top: 2px; }
120.mcp-editor .error { margin: 0 0 10px; font-size: 13px; }
121.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; }
122
123.mcp-empty { display: grid; justify-items: start; gap: 4px; padding: 14px 0 8px; }
124.mcp-empty p { margin: 0; color: var(--muted); }
125.mcp-empty .mcp-empty-title { color: var(--text-2); font-weight: 550; }
126
127@media (max-width: 760px) {
128 /* Every tab stays in view as a grid; the chosen one is filled directly, since the sliding highlight assumes one row. */
129 .mcp-tabs .segmented { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
130 .mcp-tabs .segmented :is(.pill, .ink) { display: none; }
131 .mcp-tabs .segmented > a { justify-content: center; padding-block: 6px; }
132 .mcp-tabs .segmented > a[aria-current="page"] { background: var(--accent); color: var(--on-accent); }
133 .mcp-client { grid-template-columns: 28px minmax(0, 1fr); row-gap: 4px; padding-right: var(--gutter); align-items: start; }
134 .mcp-client h3 { white-space: normal; }
135 .mcp-client-when { grid-column: 2; }
136 .mcp-row-actions { grid-column: 2; margin-left: -14px; }
137 .mcp-row-actions button { min-height: 32px; }
138 .mcp-editor { grid-column: 1 / -1; padding-right: 0; }
139 .mcp-wrapper-head { flex-wrap: wrap; }
140}
141
142/* Consent: a full-screen route, no shell. */
33.mcp-authorization { min-height: 100%; display: grid; place-items: center; padding: 40px 24px; box-sizing: border-box; }143.mcp-authorization { min-height: 100%; display: grid; place-items: center; padding: 40px 24px; box-sizing: border-box; }
34.mcp-approval { width: min(100%, 600px); padding: 32px; box-sizing: border-box; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; }144.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); }
35.mcp-approval-brand { color: var(--accent); font-size: 13px; font-weight: 600; margin-bottom: 28px; }145.mcp-approval-brand { display: flex; align-items: center; gap: 8px; margin-bottom: 22px; color: var(--accent); font-weight: 700; font-size: 15px; }
36.mcp-approval h1 { font-size: 28px; line-height: 1.2; margin: 0; overflow-wrap: anywhere; }146.mcp-approval-brand img { width: 26px; height: 26px; }
37.mcp-approval h2 { font-size: 15px; margin: 0 0 12px; }147.mcp-approval h1 { margin: 0; font-size: 26px; line-height: 1.2; letter-spacing: -0.01em; overflow-wrap: anywhere; }
38.mcp-approval-intro { font-size: 16px; color: var(--text-2); margin: 12px 0 24px; }148.mcp-approval h2 { margin: 0 0 10px; color: var(--text); font-size: 14px; }
39.mcp-request-identity { padding: 16px 0; border-block: 1px solid var(--line); margin: 0; }149.mcp-approval-intro { margin: 8px 0 18px; color: var(--text-2); font-size: 15px; }
40.mcp-request-identity > div { display: grid; grid-template-columns: 100px minmax(0, 1fr); gap: 16px; margin: 6px 0; }150.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; }
151.mcp-request-identity > div { display: contents; }
41.mcp-request-identity dt { color: var(--muted); }152.mcp-request-identity dt { color: var(--muted); }
42.mcp-request-identity dd { margin: 0; overflow-wrap: anywhere; }153.mcp-request-identity dd { margin: 0; overflow-wrap: anywhere; font-weight: 550; }
43.mcp-permissions, .mcp-link-step { padding: 24px 0; border-bottom: 1px solid var(--line); }154.mcp-permissions { padding: 20px 0 4px; }
44.mcp-permissions p { margin: 8px 0; }155.mcp-permissions ul { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }
45.mcp-approval-actions { display: flex; justify-content: space-between; gap: 16px; padding-top: 24px; border-top: 1px solid var(--line); margin-top: 24px; }156.mcp-permissions li { display: flex; gap: 10px; align-items: start; color: var(--text-2); }
46.mcp-close { background: none; border: 0; color: var(--muted); cursor: pointer; margin-top: 20px; padding: 0; text-decoration: underline; }157.mcp-permissions li svg { flex: none; margin-top: 1px; color: var(--accent); }
158.mcp-permissions li.strong { color: var(--text); font-weight: 550; }
159.mcp-permissions li small { display: block; margin-top: 2px; color: var(--muted); font-size: 12px; font-weight: 400; }
160.mcp-permissions li.strong svg { color: color-mix(in srgb, var(--warning) 70%, var(--text)); }
161.mcp-approval .mcp-access { margin-top: 16px; }
162.mcp-link-step { margin-top: 18px; padding: 14px; border: 1px solid var(--line); border-radius: var(--radius); }
163.mcp-link-step p { margin: 0 0 12px; color: var(--text-2); }
164.mcp-approval > .error { margin: 16px 0 0; }
165.mcp-approval > .mcp-actions { margin-top: 16px; }
166.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); }
167.mcp-approval-actions .spacer { flex: 1; }
168.mcp-approval-actions .hint { margin: 0; }
47@media (max-width: 760px) {169@media (max-width: 760px) {
48 .mcp-client { grid-template-columns: 1fr; }170 .mcp-authorization { padding: 12px; align-items: start; }
49 .mcp-section-heading { flex-wrap: wrap; }171 .mcp-approval { padding: 20px 18px 18px; }
50 .mcp-endpoint .copy { width: 100%; }172 .mcp-approval h1 { font-size: 22px; }
51 .mcp-repository-list > div { grid-template-columns: 1fr; gap: 4px; }173 .mcp-approval-actions .button { flex: 1; }
52 .mcp-authorization { padding: 20px 12px; align-items: start; }
53 .mcp-approval { padding: 24px 20px; }
54}174}
55
56.ai-downloads { display: grid; gap: 12px; }
57.ai-downloads .copy { white-space: normal; overflow-wrap: anywhere; text-align: left; }
58
59.ai-key { display: block; margin-top: 8px; overflow-wrap: anywhere; user-select: text; }
dashboard/web/pages/MCP.tsx+128-55
...@@ -1,35 +1,55 @@...@@ -1,35 +1,55 @@
1import { A, useNavigate, useParams } from "@solidjs/router";1import { A, useNavigate, useParams } from "@solidjs/router";
2import { parseResponse } from "hono/client";2import { parseResponse } from "hono/client";
3import { createEffect, createResource, createSignal, For, Show } from "solid-js";3import ChevronRight from "lucide-solid/icons/chevron-right";
4import Eye from "lucide-solid/icons/eye";
5import GitBranch from "lucide-solid/icons/git-branch";
6import Lock from "lucide-solid/icons/lock";
7import MonitorCheck from "lucide-solid/icons/monitor-check";
8import MousePointer2 from "lucide-solid/icons/mouse-pointer-2";
9import RefreshCw from "lucide-solid/icons/refresh-cw";
10import { createEffect, createResource, createSignal, For, type JSX, Show } from "solid-js";
4import { api, reason } from "../api.ts";11import { api, reason } from "../api.ts";
5import { Ago } from "../components/Ago.tsx";12import { Ago } from "../components/Ago.tsx";
6import { Copy } from "../components/Copy.tsx";
7import { showConfirmDialog } from "../components/Dialog.tsx";13import { showConfirmDialog } from "../components/Dialog.tsx";
8import { Loaded } from "../components/Loaded.tsx";14import { Loaded } from "../components/Loaded.tsx";
15import { StatusLabel } from "../components/Status.tsx";
9import { TabBar } from "../components/TabBar.tsx";16import { TabBar } from "../components/TabBar.tsx";
10import { toast } from "../components/Toast.tsx";17import { toast } from "../components/Toast.tsx";
11import { AI } from "./AI.tsx";18import { AI, ClientMark, CopyField } from "./AI.tsx";
12import { MCPAccess } from "./MCPAccess.tsx";19import { MCPAccess } from "./MCPAccess.tsx";
13import type { Me } from "../types/model.ts";20import type { Me } from "../types/model.ts";
14import type { Access, Catalog } from "../types/mcp.ts";21import type { Access, Catalog, Connections } from "../types/mcp.ts";
15import "./MCP.css";22import "./MCP.css";
1623
17const descriptions: Record<Catalog, string> = {24const descriptions: Record<Catalog, string> = {
18 computer: "Inspect and control selected virtual machines, run commands, and install guest software.",25 computer: "Inspect and control selected virtual machines, run commands, transfer files, and create disposable test VMs.",
19 shale: "Read and edit issues across your Shale repositories.",26 shale: "Read and edit issues across your Shale repositories.",
20 observability: "Read logs and traces from your services.",27 observability: "Read logs and traces from your services.",
21};28};
29const VIEWING = "Switch back to your account to change access";
30
31/** What a connection's scopes let it do, strongest first; scopes come from the client's request, never from settings. */
32function abilities(client: Connections["connections"][number]) {
33 const has = (scope: string) => client.scopes.includes(scope);
34 const out: [JSX.Element, string, boolean][] = [];
35 if (client.catalog === "computer") out.push(has("computer:control") ? [<MousePointer2 size={12} />, "Control, commands, and files", true] : [<Eye size={12} />, "View only", false]);
36 if (has("computer:create")) out.push([<MonitorCheck size={12} />, "Creates test VMs", true]);
37 if (client.catalog === "shale") out.push(has("shale:write") ? [<GitBranch size={12} />, "Edits issues", false] : [<Eye size={12} />, "Read only", false]);
38 if (client.catalog === "observability") out.push([<Eye size={12} />, "Read only", false]);
39 if (has("offline_access")) out.push([<RefreshCw size={12} />, "Stays signed in", false]);
40 return out;
41}
2242
23export function MCP(props: { me: Me }) {43export function MCP(props: { me: Me }) {
24 const params = useParams<{ catalog?: string }>();44 const params = useParams<{ catalog?: string }>();
25 const navigate = useNavigate();45 const navigate = useNavigate();
26 const ai = () => props.me.sections.includes("ai");46 createEffect(() => { if (!params.catalog) navigate("/mcp/settings/ai", { replace: true }); });
27 createEffect(() => { if (!params.catalog || params.catalog === "ai" && !ai()) navigate(`/mcp/settings/${ai() ? "ai" : "observability"}`, { replace: true }); });
28 const [overview, { refetch }] = createResource(() => parseResponse(api.mcp.$get()));47 const [overview, { refetch }] = createResource(() => parseResponse(api.mcp.$get()));
29 const [shale, { refetch: retryShale }] = createResource(() => params.catalog === "shale",48 const [shale, { refetch: retryShale }] = createResource(() => params.catalog === "shale",
30 () => parseResponse(api.mcp.shale.$get()));49 () => parseResponse(api.mcp.shale.$get()));
31 const [editing, setEditing] = createSignal("");50 const [editing, setEditing] = createSignal("");
32 const [selection, setSelection] = createSignal<Access>([]);51 const [selection, setSelection] = createSignal<Access>([]);
52 const [saveError, setSaveError] = createSignal("");
33 const [connection, { refetch: retryConnection, mutate: clearConnection }] = createResource(() => editing() || false, async (id) => {53 const [connection, { refetch: retryConnection, mutate: clearConnection }] = createResource(() => editing() || false, async (id) => {
34 clearConnection(undefined);54 clearConnection(undefined);
35 const result = await parseResponse(api.mcp.connections[":id"].$get({ param: { id } }));55 const result = await parseResponse(api.mcp.connections[":id"].$get({ param: { id } }));
...@@ -38,76 +58,129 @@ export function MCP(props: { me: Me }) {...@@ -38,76 +58,129 @@ export function MCP(props: { me: Me }) {
38 });58 });
39 const [busy, setBusy] = createSignal(false);59 const [busy, setBusy] = createSignal(false);
40 createEffect(() => { params.catalog; setEditing(""); });60 createEffect(() => { params.catalog; setEditing(""); });
61 const locked = () => busy() || props.me.viewing;
41 const linkShale = async () => {62 const linkShale = async () => {
42 setBusy(true);63 setBusy(true);
43 try { const result = await parseResponse(api.mcp.shale.$post({ json: {} })); window.location.assign(result.redirect); }64 try { const result = await parseResponse(api.mcp.shale.$post({ json: {} })); window.location.assign(result.redirect); }
44 catch (error) { toast(reason(error)); setBusy(false); }65 catch (error) { toast(reason(error)); setBusy(false); }
45 };66 };
46 return <div class="page mcp-page">67 return <div class="page mcp-page">
68 <header class="page-head"><h1>AI / MCP</h1></header>
47 <Loaded data={overview} what="AI / MCP settings" retry={refetch}>69 <Loaded data={overview} what="AI / MCP settings" retry={refetch}>
48 {(data) => {70 {(data) => {
49 const catalog = () => data().catalogs.find((catalog) => catalog.id === params.catalog);71 const catalog = () => data().catalogs.find((catalog) => catalog.id === params.catalog);
50 const connections = () => data().connections.filter((connection) => connection.catalog === catalog()?.id);72 const connections = (id: Catalog) => data().connections.filter((connection) => connection.catalog === id);
51 return <>73 return <>
52 <header class="page-head"><h1>AI / MCP</h1></header>74 <div class="mcp-tabs"><TabBar label="AI / MCP settings">
53 <TabBar label="AI / MCP settings">75 <A href="/mcp/settings/ai" end>Local AI</A>
54 <Show when={ai()}><A href="/mcp/settings/ai" end>AI</A></Show>76 <For each={data().catalogs}>{(catalog) => <A href={`/mcp/settings/${catalog.id}`} end>
55 <For each={data().catalogs}>{(catalog) => <A href={`/mcp/settings/${catalog.id}`} end>{catalog.name}</A>}</For>77 {catalog.name}<Show when={connections(catalog.id).length}>{(count) => <> <span class="count">{count()}</span></>}</Show>
56 </TabBar>78 </A>}</For>
79 </TabBar></div>
80 <Show when={params.catalog === "ai"}><AI viewing={props.me.viewing} /></Show>
57 <Show when={params.catalog && params.catalog !== "ai" && !catalog()}><p class="empty">No connector here.</p></Show>81 <Show when={params.catalog && params.catalog !== "ai" && !catalog()}><p class="empty">No connector here.</p></Show>
58 <Show when={ai() && params.catalog === "ai"}><AI viewing={props.me.viewing} /></Show>
59 <Show when={catalog()}>{(current) => <>82 <Show when={catalog()}>{(current) => <>
60 <section class="mcp-endpoint">83 <section class="mcp-section">
61 <p>{descriptions[current().id]}</p>84 <p>{descriptions[current().id]}</p>
62 <div class="mcp-section-heading"><h2>Connector URL</h2><Copy value={current().endpoint} label="connector URL" /></div>85 <h2 class="mcp-label">Connector URL</h2>
63 <p class="muted">Add this URL to your AI client’s MCP settings, then approve access.</p>86 <CopyField value={current().endpoint} label="connector URL" />
87 <p class="hint">Add this URL to your AI client’s MCP settings, then approve access</p>
64 </section>88 </section>
65 <Show when={current().id === "shale"}>89 <Show when={current().id === "shale"}>
66 <section class="mcp-panel"><div class="mcp-section-heading"><h2>Shale account</h2><div class="mcp-actions">90 <section class="mcp-section">
67 <button class="button" disabled={busy()} onClick={linkShale}>{data().shale ? "Relink account" : "Link account"}</button>91 <div class="mcp-heading"><h2>Shale account</h2>
68 <Show when={data().shale}><button class="button" disabled={busy()} onClick={() => showConfirmDialog({ title: "Unlink Shale?", description: "Every Shale client connection will lose access.", confirmLabel: "Unlink", destructive: true, onConfirm: async () => {92 <Show when={data().shale}>{(linked) => <span class="stat"><StatusLabel health="healthy">Linked <Ago t={linked().linkedAt} /></StatusLabel></span>}</Show>
69 await parseResponse(api.mcp.shale.$delete()); await refetch(); await retryShale();93 <Show when={data().shale}><div class="mcp-actions">
70 } })}>Unlink</button></Show>94 <button class="button small" disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={linkShale}>Relink</button>
71 </div></div>95 <button class="button small danger" disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={(event) => showConfirmDialog({
96 title: "Unlink Shale?", description: "Every Shale client connection will lose access.", confirmLabel: "Unlink", destructive: true, returnFocus: event.currentTarget,
97 onConfirm: async () => { await parseResponse(api.mcp.shale.$delete()); await refetch(); await retryShale(); },
98 })}>Unlink</button>
99 </div></Show>
100 </div>
72 <Loaded data={shale} what="Shale repositories" retry={retryShale}>101 <Loaded data={shale} what="Shale repositories" retry={retryShale}>
73 {(account) => <Show when={account().linked} fallback={<p class="muted">Link your Shale account to connect clients.</p>}>102 {(account) => <Show when={account().linked} fallback={<div class="mcp-link">
74 <p class="muted">Account linked<Show when={data().shale}> <Ago t={data().shale!.linkedAt} /></Show></p>103 <p>Link your Shale account to connect clients.</p>
75 <details class="mcp-repositories"><summary>{account().resources.length} repositories available</summary><div class="mcp-repository-list"><For each={account().resources}>{(resource) => <div><span>{resource.name}</span><span class="muted">{resource.description}</span></div>}</For></div></details>104 <button class="button primary" disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={linkShale}>Link account</button>
76 <Show when={!account().resources.length}><p class="muted">Your account has no repositories yet.</p></Show>105 </div>}>
106 <Show when={account().resources.length} fallback={<p class="hint">Your account has no repositories yet</p>}>
107 <details class="mcp-repositories"><summary><ChevronRight class="chevron" />{account().resources.length} repositories available</summary>
108 <ul class="mcp-repository-list"><For each={account().resources}>{(resource) => <li><span>{resource.name}</span><span class="muted">{resource.description}</span></li>}</For></ul>
109 </details>
110 </Show>
77 </Show>}111 </Show>}
78 </Loaded>112 </Loaded>
79 </section>113 </section>
80 </Show>114 </Show>
81 <section class="mcp-panel"><h2>Connected clients</h2>115 <section class="mcp-section flush">
82 <Show when={connections().length} fallback={<p class="muted">No clients connected. Add the connector URL to your AI client to get started.</p>}>116 <h2>Connected clients</h2>
83 <div class="mcp-client-list"><For each={connections()}>{(client) => {117 <Show when={connections(current().id).length} fallback={<div class="mcp-empty">
118 <p class="mcp-empty-title">No clients connected yet.</p>
119 <p>Clients appear here after you approve them.</p>
120 </div>}>
121 <ul class="mcp-clients"><For each={connections(current().id)}>{(client) => {
84 let editButton!: HTMLButtonElement;122 let editButton!: HTMLButtonElement;
85 const cancel = () => { setEditing(""); editButton.focus(); };123 let editor!: HTMLDivElement;
86 return <article class="mcp-client" onKeyDown={(event) => {124 const open = () => editing() === client.id;
87 if (event.key === "Escape" && editing() === client.id && !busy()) {125 const create = client.scopes.includes("computer:create");
88 event.preventDefault(); event.stopPropagation(); cancel();126 const close = () => { setEditing(""); editButton.focus(); };
89 }127 const save = async () => {
128 setBusy(true); setSaveError("");
129 try {
130 await parseResponse(api.mcp.connections[":id"].$post({ param: { id: client.id }, json: { resources: selection() } }));
131 await refetch(); close();
132 } catch (error) { setSaveError(reason(error)); }
133 finally { setBusy(false); }
134 };
135 const empty = () => selection() !== "all" && !selection().length && !create;
136 return <li class="mcp-client" classList={{ editing: open() }} onKeyDown={(event) => {
137 if (event.key === "Escape" && open() && !busy()) { event.preventDefault(); event.stopPropagation(); close(); }
90 }}>138 }}>
91 <div><h3>{client.name}</h3><p>{client.resources === "all" ? "All Repositories" : client.resources.join(", ")}</p><p class="muted">{client.scopes.includes("computer:control") ? "VM control and commands" : client.scopes.includes("shale:write") ? "Issue editing" : "Read only"} · Connected <Ago t={client.createdAt} /></p></div>139 <ClientMark name={client.name} />
92 <div class="mcp-actions"><button ref={editButton} class="button small" aria-expanded={editing() === client.id} disabled={busy()} onClick={() => setEditing(editing() === client.id ? "" : client.id)}>Edit access</button>140 <div class="mcp-client-main">
93 <button class="button small" disabled={busy()} onClick={() => showConfirmDialog({ title: "Revoke this connection?", description: `${client.name} will lose access immediately.`, confirmLabel: "Revoke", destructive: true, onConfirm: async () => {141 <h3>{client.name}</h3>
94 await parseResponse(api.mcp.connections[":id"].$delete({ param: { id: client.id } })); if (editing() === client.id) setEditing(""); await refetch(); toast("Connection revoked");142 <div class="chips">
95 } })}>Revoke</button></div>143 <Show when={client.resources === "all"} fallback={<For each={client.resources as string[]} fallback={<span class="chip">{client.catalog === "computer" ? "No VMs yet" : "Nothing selected"}</span>}>{(name) => <span class="chip">{name}</span>}</For>}>
96 <Show when={editing() === client.id}><div class="mcp-edit-access">144 <span class="chip">All repositories</span>
97 <Loaded data={connection} what="connection access" retry={retryConnection}>145 </Show>
98 {(details) => <Show when={details().linked} fallback={<><p>Link your Shale account to change repository access.</p><button class="button" disabled={busy()} onClick={linkShale}>Link account</button></>}>146 <For each={abilities(client)}>{([icon, label, strong]) => <span class="chip ability" classList={{ strong }}>{icon}{label}</span>}</For>
99 <MCPAccess catalog={current().id} resources={details().resources} value={selection()} onChange={setSelection} disabled={busy()} />147 </div>
100 <Show when={details().resourceError}><p class="error" role="alert">{details().resourceError}</p></Show>148 </div>
101 <div class="mcp-actions"><button class="button primary" disabled={busy() || (selection() !== "all" && !selection().length)} onClick={async () => {149 <span class="mcp-client-when">Connected <Ago t={client.createdAt} /></span>
102 setBusy(true);150 <div class="mcp-row-actions">
103 try { await parseResponse(api.mcp.connections[":id"].$post({ param: { id: client.id }, json: { resources: selection() } })); setEditing(""); await refetch(); }151 <button ref={editButton} aria-expanded={open()} disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={() => {
104 catch (error) { toast(reason(error)); }152 if (open()) return close();
105 finally { setBusy(false); }153 setSaveError(""); setEditing(client.id);
106 }}>Save access</button><button class="button" disabled={busy()} onClick={cancel}>Cancel</button></div>154 }}>Edit access</button>
107 </Show>}155 <button class="danger" disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={(event) => showConfirmDialog({
108 </Loaded>156 title: `Revoke ${client.name}?`, description: `${client.name} loses access immediately.`, confirmLabel: "Revoke", destructive: true, returnFocus: event.currentTarget,
109 </div></Show>157 onConfirm: async () => {
110 </article>; }}</For></div>158 await parseResponse(api.mcp.connections[":id"].$delete({ param: { id: client.id } }));
159 if (open()) setEditing("");
160 await refetch(); toast("Connection revoked");
161 },
162 })}>Revoke</button>
163 </div>
164 <Show when={open()}><div ref={editor} class="mcp-editor">
165 <Loaded data={connection} what="connection access" retry={retryConnection}>
166 {(details) => { queueMicrotask(() => editor.querySelector<HTMLElement>("input:not(:disabled), .segmented button, .button")?.focus()); return <Show when={details().linked} fallback={<>
167 <p>Link your Shale account to change repository access.</p>
168 <div class="mcp-actions"><button class="button primary" disabled={busy()} onClick={linkShale}>Link account</button><button class="button" disabled={busy()} onClick={close}>Cancel</button></div>
169 </>}>
170 <MCPAccess catalog={current().id} resources={details().resources} value={selection()} onChange={setSelection} disabled={busy()} create={create} />
171 <Show when={details().resourceError}><p class="error" role="alert">{details().resourceError}</p></Show>
172 <p class="mcp-fixed"><Lock size={13} class="icon" /><span>{client.name} keeps the abilities it asked for. To change them, revoke it and connect again.</span></p>
173 <Show when={saveError()}><p class="error" role="alert">Couldn't save access. {saveError()}</p></Show>
174 <div class="mcp-actions">
175 <button class="button primary" disabled={busy() || empty()} aria-busy={busy()} onClick={save}>Save access</button>
176 <button class="button" disabled={busy()} onClick={close}>Cancel</button>
177 <Show when={empty()}><span class="hint">Choose at least one {current().id === "computer" ? "VM" : current().id === "shale" ? "repository" : "service"}</span></Show>
178 </div>
179 </Show>; }}
180 </Loaded>
181 </div></Show>
182 </li>;
183 }}</For></ul>
111 </Show>184 </Show>
112 </section>185 </section>
113 </>}</Show>186 </>}</Show>
dashboard/web/pages/MCPAccess.tsx+35-15
...@@ -1,34 +1,54 @@...@@ -1,34 +1,54 @@
1import { For, Show } from "solid-js";1import { For, Show } from "solid-js";
2import { Checkbox } from "../components/Checkbox.tsx";2import { Checkbox } from "../components/Checkbox.tsx";
3import { TabBar } from "../components/TabBar.tsx";
4import { datetime, until } from "../format.ts";
3import type { Access, Catalog, Resources } from "../types/mcp.ts";5import type { Access, Catalog, Resources } from "../types/mcp.ts";
46
7const PLATFORMS: Record<string, string> = { linux: "Linux", windows: "Windows", macos: "macOS" };
8
9function facts(resource: Resources[number]) {
10 const platform = resource.platform && (PLATFORMS[resource.platform] ?? resource.platform);
11 return [platform && !resource.os?.toLowerCase().includes(platform.toLowerCase()) ? platform : null, resource.os, resource.arch].filter(Boolean);
12}
13
5export function MCPAccess(props: {14export function MCPAccess(props: {
6 catalog: Catalog;15 catalog: Catalog;
7 resources: Resources;16 resources: Resources;
8 value: Access;17 value: Access;
9 onChange: (value: Access) => void;18 onChange: (value: Access) => void;
10 disabled: boolean;19 disabled: boolean;
20 create: boolean;
11}) {21}) {
12 return <fieldset class="mcp-access" disabled={props.disabled}>22 return <fieldset class="mcp-access" disabled={props.disabled}>
13 <legend>{props.catalog === "computer" ? "Virtual machines" : props.catalog === "shale" ? "Repositories" : "Services"}</legend>23 <legend>{props.catalog === "computer" ? "Virtual machines" : props.catalog === "shale" ? "Repositories" : "Services"}</legend>
14 <Show when={props.catalog === "shale"}>24 <Show when={props.catalog === "shale"}>
15 <div class="mcp-access-modes">25 <TabBar label="Repository access">
16 <label><input type="radio" name="repository-access" checked={props.value === "all"} onChange={() => props.onChange("all")} />26 <button type="button" aria-pressed={props.value === "all"} onClick={() => props.onChange("all")}>All repositories</button>
17 <span><strong>All Repositories</strong><span class="muted">Includes repositories you can access now and in the future.</span></span>27 <button type="button" aria-pressed={props.value !== "all"} onClick={() => props.value === "all" && props.onChange([])}>Selected repositories</button>
18 </label>28 </TabBar>
19 <label><input type="radio" name="repository-access" checked={props.value !== "all"} onChange={() => props.onChange([])} />29 <p class="hint">{props.value === "all" ? "Includes repositories you can access later" : "Only the repositories you choose"}</p>
20 <span><strong>Selected repositories</strong><span class="muted">Limit this connection to the repositories you choose.</span></span>
21 </label>
22 </div>
23 </Show>30 </Show>
24 <Show when={props.value !== "all"}>31 <Show when={props.value !== "all"}>
25 <div class="mcp-resources"><For each={props.resources}>{(resource) =>32 <Show when={props.resources.length} fallback={<p class={props.catalog === "computer" && !props.create ? "mcp-none" : "hint"}>
26 <Checkbox checked={props.value !== "all" && props.value.includes(resource.id)} disabled={props.disabled} onChange={(checked) => {33 {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."
27 const selected = props.value === "all" ? [] : props.value;34 : props.catalog === "shale" ? "No repositories available to select." : "No services available to your account."}
28 props.onChange(checked ? [...selected, resource.id] : selected.filter((id) => id !== resource.id));35 </p>}>
29 }}><span>{resource.name}<Show when={resource.description}><small class="muted">{resource.description}</small></Show></span></Checkbox>36 <div class="mcp-resources"><For each={props.resources}>{(resource) =>
30 }</For></div>37 <Checkbox checked={props.value !== "all" && props.value.includes(resource.id)} disabled={props.disabled} onChange={(checked) => {
31 <Show when={!props.resources.length}><p class="muted">{props.catalog === "computer" ? "No supported VMs belong to your account." : props.catalog === "shale" ? "No repositories available to select." : "No services available to your account."}</p></Show>38 const selected = props.value === "all" ? [] : props.value;
39 props.onChange(checked ? [...selected, resource.id] : selected.filter((id) => id !== resource.id));
40 }}><span>{resource.name}
41 <Show when={props.catalog === "computer"} fallback={<Show when={resource.description}><small class="muted">{resource.description}</small></Show>}>
42 <span class="chips">
43 <For each={facts(resource)}>{(fact) => <span class="chip fact">{fact}</span>}</For>
44 <Show when={resource.state}><span class="chip fact">{resource.state}</span></Show>
45 <Show when={resource.aiOwned}><span class="chip fact">Test VM</span></Show>
46 <Show when={resource.expiresAt}>{(t) => <span class="chip fact" data-tip={datetime(t())}>Expires {until(t())}</span>}</Show>
47 </span>
48 </Show>
49 </span></Checkbox>
50 }</For></div>
51 </Show>
32 </Show>52 </Show>
33 </fieldset>;53 </fieldset>;
34}54}
dashboard/web/pages/MCPConsent.tsx+35-17
...@@ -1,12 +1,31 @@...@@ -1,12 +1,31 @@
1import { useBeforeLeave, useLocation, useParams } from "@solidjs/router";1import { useBeforeLeave, useLocation, useParams } from "@solidjs/router";
2import { parseResponse } from "hono/client";2import { parseResponse } from "hono/client";
3import { createEffect, createResource, createSignal, on, onCleanup, onMount, Show } from "solid-js";3import Eye from "lucide-solid/icons/eye";
4import GitBranch from "lucide-solid/icons/git-branch";
5import MonitorCheck from "lucide-solid/icons/monitor-check";
6import MousePointer2 from "lucide-solid/icons/mouse-pointer-2";
7import RefreshCw from "lucide-solid/icons/refresh-cw";
8import { createEffect, createResource, createSignal, For, type JSX, on, onCleanup, onMount, Show } from "solid-js";
4import { api, reason } from "../api.ts";9import { api, reason } from "../api.ts";
5import { showConfirmDialog } from "../components/Dialog.tsx";10import { showConfirmDialog } from "../components/Dialog.tsx";
11import snowflake from "../snowflake.svg";
6import { MCPAccess } from "./MCPAccess.tsx";12import { MCPAccess } from "./MCPAccess.tsx";
7import type { Access } from "../types/mcp.ts";13import type { Access, Consent } from "../types/mcp.ts";
8import "./MCP.css";14import "./MCP.css";
915
16/** Each requested ability as one line; `strong` ones act on the VMs rather than reading them. */
17function abilities(data: Consent) {
18 const has = (scope: string) => data.scopes.includes(scope);
19 const out: [JSX.Element, JSX.Element, boolean][] = [[<Eye size={16} />,
20 data.catalog === "computer" ? "See VM status, screenshots, and on-screen controls" : data.catalog === "shale" ? "Read issues and comments" : "Read logs and traces", false]];
21 if (has("computer:control")) out.push([<MousePointer2 size={16} />, "Control the desktop, run commands, and transfer files in the VMs you choose", true]);
22 if (has("computer:create")) out.push([<MonitorCheck size={16} />, <>Create up to 2 Linux or Windows test VMs
23 <small>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</small></>, true]);
24 if (has("shale:write")) out.push([<GitBranch size={16} />, "Create issues, comment, and edit titles and status", false]);
25 if (has("offline_access")) out.push([<RefreshCw size={16} />, "Stay connected without signing in again", false]);
26 return out;
27}
28
10export function MCPConsent() {29export function MCPConsent() {
11 const params = useParams<{ id: string }>();30 const params = useParams<{ id: string }>();
12 const location = useLocation();31 const location = useLocation();
...@@ -46,39 +65,38 @@ export function MCPConsent() {...@@ -46,39 +65,38 @@ export function MCPConsent() {
46 redirect(result.redirect);65 redirect(result.redirect);
47 } catch (error) { setError(reason(error)); setBusy(false); }66 } catch (error) { setError(reason(error)); setBusy(false); }
48 };67 };
68 const create = () => !!details()?.scopes.includes("computer:create");
69 const empty = () => access() !== "all" && !access().length && !create();
49 return <main class="mcp-authorization">70 return <main class="mcp-authorization">
50 <section class="mcp-approval" aria-labelledby="connection-title">71 <section class="mcp-approval" aria-labelledby="connection-title">
51 <div class="mcp-approval-brand">Snow Globe <span class="muted">· MCP connection</span></div>72 <div class="mcp-approval-brand"><img src={snowflake} alt="" />the snow globe</div>
52 <Show when={details()} fallback={<>73 <Show when={details()} fallback={<>
53 <h1 id="connection-title">{consent.state === "errored" ? "Connection unavailable" : "Loading connection…"}</h1>74 <h1 id="connection-title">{consent.state === "errored" ? "Connection unavailable" : "Loading connection…"}</h1>
54 <Show when={consent.state === "errored"} fallback={<div class="skeleton" style={{ height: "120px" }} aria-label="Loading connection" />}>75 <Show when={consent.state === "errored"} fallback={<div class="skeleton" style={{ height: "120px" }} aria-label="Loading connection" />}>
55 <p class="error" role="alert">{reason(consent.error)}</p>76 <p class="error" role="alert">{reason(consent.error)}</p>
56 <button class="button" onClick={() => refetch()}>Retry</button>77 <div class="mcp-actions"><button class="button" onClick={() => refetch()}>Retry</button><button class="button" onClick={() => redirect("/mcp")}>Close request</button></div>
57 </Show>78 </Show>
58 </>}>79 </>}>
59 {(data) => <>80 {(data) => <>
60 <h1 id="connection-title">Connect {data().client}</h1>81 <h1 id="connection-title">Connect {data().client}</h1>
61 <p class="mcp-approval-intro">{data().catalog === "computer" ? "Grant access to selected virtual machines." : data().catalog === "shale" ? "Grant access to Shale issues." : "Grant access to logs and traces."}</p>82 <p class="mcp-approval-intro">{data().catalog === "computer" ? "Grant access to your virtual machines." : data().catalog === "shale" ? "Grant access to Shale issues." : "Grant access to logs and traces."}</p>
62 <dl class="mcp-request-identity"><div><dt>Signed in as</dt><dd>{data().account}</dd></div><div><dt>Return to</dt><dd>{data().redirectHost}</dd></div></dl>83 <dl class="mcp-request-identity"><div><dt>Signed in as</dt><dd>{data().account}</dd></div><div><dt>Returns to</dt><dd>{data().redirectHost}</dd></div></dl>
63 <div class="mcp-permissions"><h2>Requested access</h2>84 <div class="mcp-permissions"><h2>{data().client} will be able to</h2>
64 <p>{data().catalog === "computer" ? "Read VM status, screenshots, and application controls" : data().catalog === "shale" ? "Read issues and comments" : "Read logs and traces"}</p>85 <ul><For each={abilities(data())}>{([icon, text, strong]) => <li classList={{ strong }}>{icon}<span>{text}</span></li>}</For></ul>
65 <Show when={data().scopes.includes("computer:control")}><p>Control the desktop, run commands, and install software inside the selected VMs</p></Show>
66 <Show when={data().scopes.includes("shale:write")}><p>Create issues, comment, and edit issue titles and status</p></Show>
67 <Show when={data().scopes.includes("offline_access")}><p>Stay connected without signing in again</p></Show>
68 </div>86 </div>
69 <Show when={data().linked} fallback={<div class="mcp-link-step"><h2>Link your Shale account</h2><p>Sign in to Shale, then return here to approve access.</p><button class="button primary" disabled={busy()} onClick={linkShale}>Link account</button></div>}>87 <Show when={data().linked} fallback={<div class="mcp-link-step"><h2>Link your Shale account</h2><p>Sign in to Shale, then return here to approve access.</p><button class="button primary" disabled={busy()} onClick={linkShale}>Link account</button></div>}>
70 <MCPAccess catalog={data().catalog} resources={data().resources} value={access()} onChange={setSelection} disabled={busy()} />88 <MCPAccess catalog={data().catalog} resources={data().resources} value={access()} onChange={setSelection} disabled={busy()} create={create()} />
71 <Show when={data().resourceError}><p class="error" role="alert">{data().resourceError} <button class="button small" disabled={busy()} onClick={() => refetch()}>Retry</button></p></Show>89 <Show when={data().resourceError}><p class="error" role="alert">{data().resourceError} <button class="button small" disabled={busy()} onClick={() => refetch()}>Retry</button></p></Show>
72 <p class="muted">You can change this connection’s access later in MCP settings.</p>90 <Show when={data().resources.length || create() || data().catalog === "shale"}><p class="hint">You can change this later in AI / MCP settings</p></Show>
73 </Show>91 </Show>
74 </>}92 </>}
75 </Show>93 </Show>
76 <Show when={error()}><p class="error" role="alert">{error()}</p></Show>94 <Show when={error()}><p class="error" role="alert">{error()}</p></Show>
77 <div class="mcp-approval-actions">95 <Show when={details()}><div class="mcp-approval-actions">
78 <button class="button" disabled={busy()} onClick={() => answer(true).catch(() => {})}>Decline</button>96 <button class="button" disabled={busy()} onClick={() => answer(true).catch(() => {})}>Decline</button>
79 <Show when={details()?.linked}><button class="button primary" disabled={busy() || (access() !== "all" && !access().length)} onClick={() => answer(false).catch(() => {})}>Allow access</button></Show>97 <Show when={details()?.linked}><span class="spacer" /><Show when={empty() && details()?.resources.length}><span class="hint">Choose at least one {details()?.catalog === "computer" ? "VM" : details()?.catalog === "shale" ? "repository" : "service"}</span></Show>
80 </div>98 <button class="button primary" disabled={busy() || empty()} aria-busy={busy()} onClick={() => answer(false).catch(() => {})}>Allow access</button></Show>
81 <Show when={consent.state === "errored"}><button class="mcp-close" onClick={() => redirect("/mcp")}>Close request</button></Show>99 </div></Show>
82 </section>100 </section>
83 </main>;101 </main>;
84}102}
dashboard/web/styles.css+13
...@@ -289,6 +289,19 @@ button { font: inherit; color: inherit; }...@@ -289,6 +289,19 @@ button { font: inherit; color: inherit; }
289.view-as .button { justify-self: start; }289.view-as .button { justify-self: start; }
290@keyframes menu-in { from { opacity: 0; transform: translateY(6px); } }290@keyframes menu-in { from { opacity: 0; transform: translateY(6px); } }
291291
292@media (max-width: 760px) {
293 body:has(.mcp-page) { --sidebar: 56px; }
294 body:has(.mcp-page) .sidebar > .brand { justify-content: center; padding: 12px 0; font-size: 0; }
295 body:has(.mcp-page) .sidebar > .brand img { width: 30px; height: 30px; }
296 body:has(.mcp-page) .sidebar > .nav { padding-inline: 6px; }
297 body:has(.mcp-page) .sidebar > .nav :is(.nav-item, .nav-row .nav-item) { justify-content: center; padding: 0; }
298 body:has(.mcp-page) .sidebar > .nav :is(.nav-item .badge, .nav-item .chevron, .nav-toggle, .nav-group) { display: none; }
299 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; }
300 body:has(.mcp-page) .whoami-button { justify-content: center; padding: 0; }
301 body:has(.mcp-page) .account-menu { width: 240px; }
302}
303
304
292/* status ------------------------------------------------------------------ */305/* status ------------------------------------------------------------------ */
293306
294.status { width: 10px; height: 10px; display: block; transition: color 300ms; }307.status { width: 10px; height: 10px; display: block; transition: color 300ms; }
dashboard/web/types/mcp.ts+1-1
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1export type Catalog = "shale" | "observability" | "computer";1export type Catalog = "shale" | "observability" | "computer";
2export type Access = "all" | string[];2export type Access = "all" | string[];
3export type Resources = { id: string; name: string; description?: string }[];3export type Resources = { id: string; name: string; description?: string; platform?: string; os?: string | null; arch?: string; state?: string; aiOwned?: boolean; expiresAt?: number | null }[];
44
5export interface Connections {5export interface Connections {
6 catalogs: { id: Catalog; name: string; endpoint: string }[];6 catalogs: { id: Catalog; name: string; endpoint: string }[];
flake.nix+1
...@@ -6,6 +6,7 @@...@@ -6,6 +6,7 @@
6 outputs = { self, nixpkgs, ... }: {6 outputs = { self, nixpkgs, ... }: {
7 packages.x86_64-linux.dashboard = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/dashboard.nix { };7 packages.x86_64-linux.dashboard = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/dashboard.nix { };
8 packages.x86_64-linux.host-python = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/host-python.nix { };8 packages.x86_64-linux.host-python = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/host-python.nix { };
9 packages.x86_64-linux.computer-ahk = nixpkgs.legacyPackages.x86_64-linux.callPackage ./nixos/computer-ahk.nix { };
9 packages.x86_64-linux.dashboard-image = self.packages.x86_64-linux.dashboard.image;10 packages.x86_64-linux.dashboard-image = self.packages.x86_64-linux.dashboard.image;
10 packages.x86_64-linux.installer = self.nixosConfigurations.installer.config.system.build.isoImage;11 packages.x86_64-linux.installer = self.nixosConfigurations.installer.config.system.build.isoImage;
1112
guest/DESIGN.md+1-1
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1# Guest display stream1# Guest display stream
22
3The 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.3The 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.
44
5Build 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.5Build 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.
66
guest/computer-legacy-mac.py created+170
...@@ -0,0 +1,170 @@
1#!/usr/bin/python
2"""Bounded console and files over the legacy guest's pinned private SSH connection."""
3import base64
4import ctypes
5import json
6import os
7import select
8import signal
9import stat
10import subprocess
11import sys
12import tempfile
13import time
14
15
16def identity(pid):
17 value = subprocess.Popen(['ps', '-p', str(pid), '-o', 'lstart='], stdout=subprocess.PIPE).communicate()[0]
18 return value.decode().strip().replace(':', '-')
19
20
21def run(request):
22 action = request['action']
23 if action == 'status':
24 return {'platform': 'macos', 'runtimes': ['bash', 'zsh'],
25 'capabilities': ['shot', 'input', 'exec', 'spawn', 'process', 'file'],
26 'max_timeout_ms': 180000, 'coordinate_units': 'screenshot_pixels'}
27 if action == 'input':
28 quartz = ctypes.CDLL('/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices')
29 core = ctypes.CDLL('/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation')
30 quartz.CGEventCreateKeyboardEvent.argtypes = [ctypes.c_void_p, ctypes.c_ushort, ctypes.c_bool]
31 quartz.CGEventCreateKeyboardEvent.restype = ctypes.c_void_p
32 quartz.CGEventKeyboardSetUnicodeString.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_void_p]
33 quartz.CGEventPost.argtypes = [ctypes.c_uint, ctypes.c_void_p]
34 core.CFRelease.argtypes = [ctypes.c_void_p]
35 for item in request['actions']:
36 if item['type'] != 'text':
37 raise ValueError('Use the VM console for pointer and key actions.')
38 text = item['text']
39 for start in range(0, len(text), 8):
40 data = text[start:start + 8].encode('utf-16-le')
41 buffer = ctypes.create_string_buffer(data)
42 for down in (True, False):
43 event = quartz.CGEventCreateKeyboardEvent(None, 0, down)
44 if not event:
45 raise ValueError('The desktop refused keyboard input. Sign in and retry.')
46 try:
47 quartz.CGEventKeyboardSetUnicodeString(event, len(data) // 2, buffer)
48 quartz.CGEventPost(0, event)
49 finally:
50 core.CFRelease(event)
51 time.sleep(.015)
52 return {'completed': len(request['actions'])}
53 if action == 'process':
54 pid, separator, started = request['id'].partition(':')
55 if not separator or not pid.isdigit() or int(pid) <= 1:
56 raise ValueError('Use the process handle returned by computer_spawn.')
57 pid = int(pid)
58 if identity(pid) != started:
59 return {'running': False}
60 if request['operation'] == 'stop':
61 if os.getpgid(pid) != pid:
62 raise ValueError('The process is not an isolated computer task.')
63 os.killpg(pid, signal.SIGTERM)
64 return {'stopping': True}
65 return {'running': True, 'pid': pid}
66 if action == 'file':
67 path = request['path']
68 if not path.startswith('/') or '\x00' in path:
69 raise ValueError('Use an absolute guest file path.')
70 operation = request['operation']
71 if operation in ('stat', 'list'):
72 info = os.stat(path)
73 result = {'path': path, 'size': info.st_size, 'directory': stat.S_ISDIR(info.st_mode), 'modified': info.st_mtime}
74 if operation == 'list':
75 names = os.listdir(path)
76 result['entries'] = [{'name': name, 'directory': stat.S_ISDIR(os.lstat(os.path.join(path, name)).st_mode)} for name in names[:1000]]
77 result['truncated'] = len(names) > 1000
78 return result
79 if operation == 'mkdir':
80 if not os.path.isdir(path):
81 os.makedirs(path)
82 return {'path': path}
83 if operation == 'remove':
84 os.unlink(path)
85 return {'removed': True}
86 if operation == 'read':
87 with open(path, 'rb') as incoming:
88 if not stat.S_ISREG(os.fstat(incoming.fileno()).st_mode):
89 raise ValueError('Choose a regular guest file.')
90 incoming.seek(request.get('offset', 0))
91 data = incoming.read(request.get('length', 65536))
92 return {'data': base64.b64encode(data).decode(), 'bytes': len(data)}
93 if operation == 'write':
94 offset = request.get('offset', 0)
95 flags = os.O_WRONLY | os.O_NOFOLLOW | (os.O_CREAT | os.O_EXCL if offset == 0 else 0)
96 with os.fdopen(os.open(path, flags, 0o600), 'wb') as outgoing:
97 info = os.fstat(outgoing.fileno())
98 if not stat.S_ISREG(info.st_mode) or info.st_size != offset:
99 raise ValueError('The transfer offset changed. Start a new upload.')
100 outgoing.seek(offset)
101 data = base64.b64decode(request['data'])
102 outgoing.write(data)
103 return {'bytes': len(data)}
104 if operation == 'commit':
105 destination = request['destination']
106 if not destination.startswith('/') or '\x00' in destination:
107 raise ValueError('Use an absolute guest destination path.')
108 if request.get('overwrite', False):
109 os.rename(path, destination)
110 else:
111 os.link(path, destination)
112 os.unlink(path)
113 return {'path': destination}
114 if action not in ('exec', 'spawn'):
115 raise ValueError('This legacy guest supports screenshot, keyboard, mouse, console, and file tools.')
116 runtime = request.get('runtime', 'default')
117 if runtime == 'default':
118 runtime = 'bash'
119 if runtime not in ('bash', 'zsh'):
120 raise ValueError('Choose bash or zsh for this guest.')
121 descriptor, path = tempfile.mkstemp(prefix='snowglobe-computer-')
122 try:
123 with os.fdopen(descriptor, 'wb') as outgoing:
124 outgoing.write(request['script'].encode('utf-8'))
125 with open('/dev/null', 'rb') as incoming:
126 with open('/dev/null', 'wb') as quiet:
127 process = subprocess.Popen(['/bin/' + runtime, path], stdin=incoming,
128 stdout=quiet if action == 'spawn' else subprocess.PIPE,
129 stderr=quiet if action == 'spawn' else subprocess.PIPE, preexec_fn=os.setsid)
130 if action == 'spawn':
131 return {'id': str(process.pid) + ':' + identity(process.pid), 'pid': process.pid}
132 streams = {process.stdout: 'stdout', process.stderr: 'stderr'}
133 output = {'stdout': b'', 'stderr': b''}
134 truncated = set()
135 deadline = time.time() + request['timeout_ms'] / 1000.0
136 timed_out = False
137 try:
138 while streams or process.poll() is None:
139 if time.time() >= deadline:
140 timed_out = True
141 break
142 ready = select.select(list(streams), [], [], min(.1, max(0, deadline - time.time())))[0]
143 for stream in ready:
144 name = streams[stream]
145 data = os.read(stream.fileno(), 8192)
146 if not data:
147 del streams[stream]
148 continue
149 available = 65536 - len(output[name])
150 output[name] += data[:available]
151 if len(data) > available:
152 truncated.add(name)
153 finally:
154 try:
155 os.killpg(process.pid, signal.SIGKILL)
156 except OSError:
157 pass
158 process.wait()
159 return {'exit': process.returncode, 'stdout': output['stdout'].decode('utf-8', 'replace'),
160 'stderr': output['stderr'].decode('utf-8', 'replace'), 'timed_out': timed_out,
161 'stdout_truncated': 'stdout' in truncated, 'stderr_truncated': 'stderr' in truncated}
162 finally:
163 if action != 'spawn':
164 os.unlink(path)
165
166
167try:
168 print(json.dumps(run(request)))
169except (ValueError, OSError, KeyError) as error:
170 print(json.dumps({'error': str(error)}))
guest/computer-linux-launch created+7
...@@ -0,0 +1,7 @@
1#!/bin/sh
2[ "$#" -eq 0 ] || exit 0
3export PATH=/run/current-system/sw/bin:/usr/local/bin:/usr/bin:/bin
4if [ -x /var/lib/snowglobe/computer/bin/snowglobe-python ]; then
5 exec /var/lib/snowglobe/computer/bin/snowglobe-python /usr/local/libexec/snowglobe-computer.py
6fi
7exec python3 /usr/local/libexec/snowglobe-computer.py
guest/computer-linux.nix created+7
...@@ -0,0 +1,7 @@
1{ pkgs ? import <nixpkgs> { } }:
2let
3 python = pkgs.python3.withPackages (p: [ p.pygobject3 ]);
4in pkgs.writeShellScriptBin "snowglobe-python" ''
5 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}"
6 exec ${python}/bin/python3 "$@"
7''
guest/computer-linux.py created+91
...@@ -0,0 +1,91 @@
1#!/usr/bin/env python3
2import json
3import fcntl
4import os
5from pathlib import Path
6import pwd
7import re
8import subprocess
9import sys
10import tempfile
11
12request = json.load(sys.stdin)
13prepare = request.pop('_prepare', None)
14stop = request.pop('_stop', None)
15unit = prepare or stop or request.pop('_unit')
16if not re.fullmatch(r'snowglobe-computer-request-[0-9a-f]{32}', unit):
17 raise ValueError('Invalid computer task.')
18lease = '/run/snowglobe-computer-request'
19if prepare or stop:
20 with os.fdopen(os.open(lease, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600), 'r+') as current:
21 fcntl.flock(current, fcntl.LOCK_EX)
22 if prepare or current.read() == unit:
23 current.seek(0)
24 current.truncate()
25 current.write(unit if prepare else '')
26 current.flush()
27 if prepare:
28 print(json.dumps({'prepared': True}))
29 sys.exit(0)
30if stop:
31 subprocess.run(['systemctl', 'stop', unit], timeout=12, capture_output=True)
32 state = subprocess.check_output(['systemctl', 'show', unit, '-p', 'ActiveState', '--value'], text=True, timeout=3).strip()
33 if state not in ('inactive', 'failed'):
34 raise RuntimeError('The computer task did not stop.')
35 print(json.dumps({'stopped': True}))
36 sys.exit(0)
37timeout = max(1, min(200, int(request.pop('_timeout'))))
38sessions = subprocess.check_output(['loginctl', 'list-sessions', '--no-legend'], text=True).splitlines()
39for line in sessions:
40 props = dict(item.split('=', 1) for item in subprocess.check_output([
41 'loginctl', 'show-session', line.split()[0], '-p', 'Active', '-p', 'Remote', '-p', 'Type', '-p', 'User',
42 ], text=True).splitlines())
43 if props.get('Active') == 'yes' and props.get('Remote') == 'no' and props.get('Type') in ('x11', 'wayland'):
44 account = pwd.getpwuid(int(props['User']))
45 break
46else:
47 print(json.dumps({'error': 'Sign in to the VM desktop before using this tool.'}))
48 sys.exit(0)
49env = dict(os.environ, HOME=account.pw_dir, USER=account.pw_name, LOGNAME=account.pw_name,
50 XDG_RUNTIME_DIR='/run/user/' + str(account.pw_uid),
51 DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/' + str(account.pw_uid) + '/bus')
52for path in Path('/proc').glob('[0-9]*/environ'):
53 try:
54 if path.stat().st_uid != account.pw_uid:
55 continue
56 entries = dict(p.split(b'=', 1) for p in path.read_bytes().split(b'\0') if b'=' in p)
57 if b'WAYLAND_DISPLAY' in entries or b'DISPLAY' in entries:
58 for key in ('DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'XDG_SESSION_TYPE', 'GI_TYPELIB_PATH'):
59 if key.encode() in entries:
60 env[key] = entries[key.encode()].decode()
61 break
62 except (OSError, ValueError):
63 pass
64source = request.pop('_worker')
65requests = Path('/var/lib/snowglobe/computer-requests')
66requests.mkdir(mode=0o700, parents=True, exist_ok=True)
67with tempfile.TemporaryDirectory(dir=requests) as directory:
68 root = Path(directory)
69 (root / 'request').write_text(json.dumps(request))
70 args = ['systemd-run', '--quiet', '--wait', '--collect', '--service-type=exec', '--unit=' + unit,
71 '-p', 'RuntimeMaxSec=' + str(timeout), '-p', 'TimeoutStopSec=5', '-p', 'WorkingDirectory=' + account.pw_dir,
72 '-p', 'StandardInput=file:' + str(root / 'request'),
73 '-p', 'StandardOutput=file:' + str(root / 'response'),
74 '-p', 'StandardError=file:' + str(root / 'error')]
75 for key in ('HOME', 'USER', 'LOGNAME', 'PATH', 'XDG_RUNTIME_DIR', 'DBUS_SESSION_BUS_ADDRESS',
76 'DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'XDG_SESSION_TYPE', 'GI_TYPELIB_PATH'):
77 if key in env:
78 args.append('--setenv=' + key + '=' + env[key])
79 identity = f'''import os, fcntl
80with open({lease!r}) as current:
81 fcntl.flock(current, fcntl.LOCK_SH)
82 if current.read() != {unit!r}: raise SystemExit(125)
83os.setgroups({os.getgrouplist(account.pw_name, account.pw_gid)!r}); os.setgid({account.pw_gid}); os.setuid({account.pw_uid})
84'''
85 python = '/var/lib/snowglobe/computer/bin/snowglobe-python'
86 args += [python if Path(python).exists() else sys.executable, '-c', identity + source]
87 result = subprocess.run(args, capture_output=True, timeout=210)
88 if result.returncode:
89 print(json.dumps({'error': 'The desktop worker could not start. Check the guest computer installation.'}))
90 else:
91 sys.stdout.buffer.write((root / 'response').read_bytes())
guest/computer-windows.cs created+249
...@@ -0,0 +1,249 @@
1using System;
2using System.Text;
3using System.Diagnostics;
4using System.Threading;
5using System.Runtime.InteropServices;
6public class ComputerProcess {
7 [DllImport("kernel32.dll", CharSet=CharSet.Unicode)] static extern IntPtr CreateJobObject(IntPtr attributes, string name);
8 [DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)] static extern IntPtr OpenJobObject(uint access,bool inherit,string name);
9 [DllImport("kernel32.dll",SetLastError=true)] static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
10 [DllImport("kernel32.dll")] static extern bool TerminateJobObject(IntPtr job, uint code);
11 [DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr handle);
12 [StructLayout(LayoutKind.Sequential)] struct SECURITY {public int length;public IntPtr descriptor;public int inherit;}
13 [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;}
14 [StructLayout(LayoutKind.Sequential)] struct CREATED {public IntPtr process,thread;public uint pid,tid;}
15 [DllImport("kernel32.dll",SetLastError=true)] static extern bool CreatePipe(out IntPtr read,out IntPtr write,ref SECURITY attributes,uint size);
16 [DllImport("kernel32.dll",SetLastError=true)] static extern bool SetHandleInformation(IntPtr handle,uint mask,uint flags);
17 [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);
18 [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);
19 [DllImport("kernel32.dll",SetLastError=true)] static extern uint ResumeThread(IntPtr thread);
20 [DllImport("kernel32.dll")] static extern bool TerminateProcess(IntPtr process,uint code);
21 [DllImport("kernel32.dll")] static extern uint WaitForSingleObject(IntPtr handle,uint milliseconds);
22 [DllImport("kernel32.dll",SetLastError=true)] static extern bool GetExitCodeProcess(IntPtr process,out uint code);
23 [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; }
24 [StructLayout(LayoutKind.Sequential)] struct COUNTERS { public ulong readOps,writeOps,otherOps,readBytes,writeBytes,otherBytes; }
25 [StructLayout(LayoutKind.Sequential)] struct LIMIT { public BASICLIMIT basic; public COUNTERS io; public UIntPtr processMemory,jobMemory,peakProcess,peakJob; }
26 [DllImport("kernel32.dll")] static extern bool SetInformationJobObject(IntPtr job,int type,ref LIMIT info,uint size);
27 [StructLayout(LayoutKind.Sequential)] struct ACCOUNTING {public long user,kernel,periodUser,periodKernel;public uint faults,total,active,terminated;}
28 [DllImport("kernel32.dll",SetLastError=true)] static extern bool QueryInformationJobObject(IntPtr job,int type,out ACCOUNTING info,uint size,IntPtr returned);
29 static string JobName(string request) {return "Global\\SnowglobeComputer-"+System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(request));}
30 static bool WaitForEmpty(IntPtr job,int timeout) {
31 var clock=Stopwatch.StartNew();
32 do {
33 ACCOUNTING count;
34 if(!QueryInformationJobObject(job,1,out count,(uint)Marshal.SizeOf(typeof(ACCOUNTING)),IntPtr.Zero))throw new System.ComponentModel.Win32Exception();
35 if(count.active==0)return true;
36 Thread.Sleep(10);
37 } while(timeout<0 || clock.ElapsedMilliseconds<timeout);
38 return false;
39 }
40 public static void Stop(string request) {
41 IntPtr job=OpenJobObject(12,false,JobName(request));
42 if(job==IntPtr.Zero) {if(Marshal.GetLastWin32Error()==2)return;throw new System.ComponentModel.Win32Exception();}
43 try {if(!TerminateJobObject(job,124) || !WaitForEmpty(job,10000))throw new Exception("Process cleanup failed");}
44 finally {CloseHandle(job);}
45 }
46 class Drain {
47 public byte[] bytes=new byte[65536]; public int length; public bool truncated;
48 public void Read(System.IO.Stream stream){try{var buffer=new byte[4096];int count;while((count=stream.Read(buffer,0,buffer.Length))>0){int take=Math.Min(count,bytes.Length-length);Array.Copy(buffer,0,bytes,length,take);length+=take;if(take<count)truncated=true;}}catch(System.IO.IOException){truncated=true;}catch(ObjectDisposedException){truncated=true;}}
49 public string Text(){return Encoding.UTF8.GetString(bytes,0,length);}
50 }
51 public static object Run(string executable, string arguments, string directory, int timeout, string request, Action<object> started=null) {
52 var stdout=new Drain();var stderr=new Drain();
53 Thread outThread=null,errThread=null;
54 IntPtr output=IntPtr.Zero,outputWrite=IntPtr.Zero,error=IntPtr.Zero,errorWrite=IntPtr.Zero,input=IntPtr.Zero;
55 var created=new CREATED();
56 System.IO.FileStream outputStream=null,errorStream=null;
57 IntPtr job=CreateJobObject(IntPtr.Zero,JobName(request));
58 if(job==IntPtr.Zero)throw new Exception("Cannot create process job");
59 try {
60 var limit=new LIMIT();limit.basic.flags=8192;
61 if(!SetInformationJobObject(job,9,ref limit,(uint)Marshal.SizeOf(typeof(LIMIT))))throw new Exception("Cannot configure process job");
62 var security=new SECURITY {length=Marshal.SizeOf(typeof(SECURITY)),inherit=1};
63 if(!CreatePipe(out output,out outputWrite,ref security,0) || !SetHandleInformation(output,1,0) ||
64 !CreatePipe(out error,out errorWrite,ref security,0) || !SetHandleInformation(error,1,0))throw new System.ComponentModel.Win32Exception();
65 input=CreateFile("NUL",0x80000000,3,ref security,3,0,IntPtr.Zero);
66 if(input==new IntPtr(-1))throw new System.ComponentModel.Win32Exception();
67 var startup=new STARTUP {size=Marshal.SizeOf(typeof(STARTUP)),flags=256,input=input,output=outputWrite,error=errorWrite};
68 // No script instruction may run before the process belongs to its cleanup job.
69 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();
70 if(!AssignProcessToJobObject(job,created.process))throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(),"Cannot isolate process job");
71 CloseHandle(outputWrite);outputWrite=IntPtr.Zero;CloseHandle(errorWrite);errorWrite=IntPtr.Zero;CloseHandle(input);input=IntPtr.Zero;
72 outputStream=new System.IO.FileStream(new Microsoft.Win32.SafeHandles.SafeFileHandle(output,true),System.IO.FileAccess.Read);output=IntPtr.Zero;
73 errorStream=new System.IO.FileStream(new Microsoft.Win32.SafeHandles.SafeFileHandle(error,true),System.IO.FileAccess.Read);error=IntPtr.Zero;
74 outThread=new Thread(()=>stdout.Read(outputStream));outThread.IsBackground=true;outThread.Start();
75 errThread=new Thread(()=>stderr.Read(errorStream));errThread.IsBackground=true;errThread.Start();
76 object handle=null;
77 if(started!=null)using(var owner=Process.GetCurrentProcess())handle=new {pid=(int)created.pid,id=owner.Id+":"+owner.StartTime.ToUniversalTime().Ticks};
78 if(!System.IO.File.Exists(request))throw new Exception("Process cleanup failed");
79 if(ResumeThread(created.thread)==UInt32.MaxValue)throw new System.ComponentModel.Win32Exception();
80 if(started!=null) {
81 started(handle);
82 CloseHandle(created.process);created.process=IntPtr.Zero;CloseHandle(created.thread);created.thread=IntPtr.Zero;
83 WaitForEmpty(job,-1);
84 return handle;
85 }
86 var clock=Stopwatch.StartNew();
87 uint waited=WaitForSingleObject(created.process,(uint)timeout);
88 if(waited!=0 && waited!=258)throw new System.ComponentModel.Win32Exception();
89 bool timedOut=waited==258;
90 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;}
91 if(timedOut)TerminateJobObject(job,124);
92 if(WaitForSingleObject(created.process,5000)!=0)throw new Exception("Process cleanup failed");
93 outThread.Join(5000);errThread.Join(5000);
94 uint code;if(!GetExitCodeProcess(created.process,out code))throw new System.ComponentModel.Win32Exception();
95 return new {exit=unchecked((int)code),stdout=stdout.Text(),stderr=stderr.Text(),timed_out=timedOut,stdout_truncated=stdout.truncated,stderr_truncated=stderr.truncated};
96 } finally {
97 TerminateJobObject(job,124);
98 if(created.process!=IntPtr.Zero)TerminateProcess(created.process,124);
99 foreach(var handle in new[]{created.process,created.thread,output,outputWrite,error,errorWrite,input})if(handle!=IntPtr.Zero && handle!=new IntPtr(-1))CloseHandle(handle);
100 WaitForEmpty(job,-1);CloseHandle(job);
101 if(outputStream!=null)outputStream.Dispose();if(errorStream!=null)errorStream.Dispose();
102 }
103
104 }
105 [StructLayout(LayoutKind.Sequential)] struct KEYBDINPUT {public ushort vk,scan;public uint flags,time;public UIntPtr extra;}
106 [StructLayout(LayoutKind.Explicit)] struct INPUTUNION {[FieldOffset(0)]public KEYBDINPUT keyboard;[FieldOffset(0)]public MOUSEINPUT mouse;}
107 [StructLayout(LayoutKind.Sequential)] struct MOUSEINPUT {public int x,y;public uint data,flags,time;public UIntPtr extra;}
108 [StructLayout(LayoutKind.Sequential)] struct INPUT {public uint type;public INPUTUNION data;}
109 [DllImport("user32.dll",SetLastError=true)] static extern uint SendInput(uint count, INPUT[] inputs,int size);
110 public static void Type(string text) {
111 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");}
112 }
113}
114
115public static class ComputerWorker {
116 static System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = 4194304 };
117 static string requestPath,responsePath;
118 static System.Collections.Generic.Dictionary<string,object> Map(object value) { return (System.Collections.Generic.Dictionary<string,object>)value; }
119 static object Get(System.Collections.Generic.Dictionary<string,object> value, string key, object fallback = null) { object found; return value.TryGetValue(key,out found) ? found : fallback; }
120 static string Text(System.Collections.Generic.Dictionary<string,object> value, string key, string fallback = "") { return Convert.ToString(Get(value,key,fallback)); }
121 static int Number(System.Collections.Generic.Dictionary<string,object> value, string key, int fallback = 0) { return Convert.ToInt32(Get(value,key,fallback)); }
122 [DllImport("user32.dll")] static extern bool SetProcessDPIAware();
123 [STAThread] public static void Main(string[] args) {
124 if(args.Length != 2) return;
125 if(args[0]=="--stop") {try {ComputerProcess.Stop(args[1]);}catch {Environment.Exit(1);}return;}
126 requestPath=args[0];responsePath=args[1]; object result;
127 try { SetProcessDPIAware(); result=Run(Map(json.DeserializeObject(System.IO.File.ReadAllText(requestPath)))); }
128 catch(Exception error) { result=new { error=error.Message }; }
129 Reply(result);
130 }
131 static void Reply(object result) {System.IO.File.WriteAllText(responsePath,json.Serialize(result),new UTF8Encoding(false));}
132 static object Target(System.Windows.Automation.AutomationElement node) {
133 var current=node.Current;
134 using(var process=Process.GetProcessById(current.ProcessId)) {
135 return new {pid=current.ProcessId,started=process.StartTime.ToUniversalTime().Ticks.ToString(),id=String.Join(".",node.GetRuntimeId()),role=current.ControlType.ProgrammaticName,name=current.Name};
136 }
137 }
138 static System.Windows.Automation.AutomationElement Find(System.Collections.Generic.Dictionary<string,object> target) {
139 using(var process=Process.GetProcessById(Number(target,"pid"))) {
140 if(process.StartTime.ToUniversalTime().Ticks.ToString()!=Text(target,"started")) throw new Exception("The application changed. Read a fresh UI tree.");
141 }
142 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")));
143 var queue=new System.Collections.Generic.Queue<System.Windows.Automation.AutomationElement>();
144 foreach(System.Windows.Automation.AutomationElement root in roots)queue.Enqueue(root);
145 var walker=System.Windows.Automation.TreeWalker.ControlViewWalker;var clock=Stopwatch.StartNew();int count=0;
146 while(queue.Count>0 && count++<5000 && clock.ElapsedMilliseconds<8000) {
147 var node=queue.Dequeue();var current=node.Current;
148 if(String.Join(".",node.GetRuntimeId())==Text(target,"id") && current.Name==Text(target,"name") && current.ControlType.ProgrammaticName==Text(target,"role"))return node;
149 for(var child=walker.GetFirstChild(node);child!=null && queue.Count<5000;child=walker.GetNextSibling(child))queue.Enqueue(child);
150 }
151 throw new Exception("The UI element changed. Read a fresh tree.");
152 }
153 static object Ui(System.Collections.Generic.Dictionary<string,object> request) {
154 var nodes=new System.Collections.Generic.List<object>();
155 var queue=new System.Collections.Generic.Queue<Tuple<System.Windows.Automation.AutomationElement,int>>();
156 queue.Enqueue(Tuple.Create(System.Windows.Automation.AutomationElement.RootElement,0));
157 var walker=System.Windows.Automation.TreeWalker.ControlViewWalker;var clock=Stopwatch.StartNew();
158 while(queue.Count>0 && nodes.Count<1500 && clock.ElapsedMilliseconds<8000) {
159 var entry=queue.Dequeue();var node=entry.Item1;int depth=entry.Item2;
160 try {
161 var current=node.Current;
162 if(depth==1 && Number(request,"pid")>0 && current.ProcessId!=Number(request,"pid"))continue;
163 if(depth>0) {
164 var actions=new System.Collections.Generic.List<string>();actions.Add("focus");object pattern;string value=null;
165 if(node.TryGetCurrentPattern(System.Windows.Automation.InvokePattern.Pattern,out pattern))actions.Add("press");
166 if(node.TryGetCurrentPattern(System.Windows.Automation.TogglePattern.Pattern,out pattern))actions.Add("toggle");
167 if(node.TryGetCurrentPattern(System.Windows.Automation.SelectionItemPattern.Pattern,out pattern))actions.Add("select");
168 if(node.TryGetCurrentPattern(System.Windows.Automation.ExpandCollapsePattern.Pattern,out pattern)){actions.Add("expand");actions.Add("collapse");}
169 if(node.TryGetCurrentPattern(System.Windows.Automation.ValuePattern.Pattern,out pattern)) {
170 var properties=((System.Windows.Automation.ValuePattern)pattern).Current;
171 value=properties.Value;if(value.Length>1024)value=value.Substring(0,1024);
172 if(!properties.IsReadOnly)actions.Add("set_value");
173 } else if(node.TryGetCurrentPattern(System.Windows.Automation.TextPattern.Pattern,out pattern)) {
174 value=((System.Windows.Automation.TextPattern)pattern).DocumentRange.GetText(1024);
175 }
176 var r=current.BoundingRectangle;
177 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});
178 }
179 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));
180 } catch(System.Windows.Automation.ElementNotAvailableException) {} catch(ArgumentException) {} catch(InvalidOperationException) {}
181 }
182 return new {nodes=nodes,truncated=queue.Count>0};
183 }
184 static object UiAction(System.Collections.Generic.Dictionary<string,object> request) {
185 var node=Find(Map(Get(request,"target")));
186 switch(Text(request,"operation")) {
187 case "focus": node.SetFocus();break;
188 case "press": ((System.Windows.Automation.InvokePattern)node.GetCurrentPattern(System.Windows.Automation.InvokePattern.Pattern)).Invoke();break;
189 case "toggle": ((System.Windows.Automation.TogglePattern)node.GetCurrentPattern(System.Windows.Automation.TogglePattern.Pattern)).Toggle();break;
190 case "select": ((System.Windows.Automation.SelectionItemPattern)node.GetCurrentPattern(System.Windows.Automation.SelectionItemPattern.Pattern)).Select();break;
191 case "expand": ((System.Windows.Automation.ExpandCollapsePattern)node.GetCurrentPattern(System.Windows.Automation.ExpandCollapsePattern.Pattern)).Expand();break;
192 case "collapse": ((System.Windows.Automation.ExpandCollapsePattern)node.GetCurrentPattern(System.Windows.Automation.ExpandCollapsePattern.Pattern)).Collapse();break;
193 case "set_value": ((System.Windows.Automation.ValuePattern)node.GetCurrentPattern(System.Windows.Automation.ValuePattern.Pattern)).SetValue(Text(request,"value"));break;
194 default: throw new Exception("Choose an action reported by computer_ui.");
195 }
196 return new {completed=true};
197 }
198 static object Run(System.Collections.Generic.Dictionary<string,object> request) {
199 string action=Text(request,"action");string root=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
200 if(action=="status") {
201 var runtimes=new System.Collections.Generic.List<string>(new[]{"powershell","cmd"});if(System.IO.File.Exists(System.IO.Path.Combine(root,"AutoHotkey.exe")))runtimes.Add("ahk");
202 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};
203 }
204 if(action=="input") {
205 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};
206 }
207 if(action=="ui")return Ui(request);
208 if(action=="ui_action")return UiAction(request);
209 if(action=="exec" || action=="spawn") {
210 string runtime=Text(request,"runtime","default"),directory=System.IO.Path.GetDirectoryName(requestPath),file,exe,arguments,script=Text(request,"script");
211 if(runtime=="default")runtime="powershell";
212 switch(runtime) {
213 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;
214 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;
215 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;
216 default:throw new Exception("Choose a runtime reported by computer_status.");
217 }
218 System.IO.File.WriteAllText(file,script,new UTF8Encoding(runtime!="cmd"));
219 string home=Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
220 if(action=="exec")return ComputerProcess.Run(exe,arguments,home,Number(request,"timeout_ms"),requestPath);
221 return ComputerProcess.Run(exe,arguments,home,0,requestPath,Reply);
222 }
223 if(action=="process") {
224 string[] parts=Text(request,"id").Split(':');Process process;
225 try {process=Process.GetProcessById(Int32.Parse(parts[0]));}catch(ArgumentException){return new {running=false};}
226 using(process) {
227 if(parts.Length!=2 || process.StartTime.ToUniversalTime().Ticks.ToString()!=parts[1])return new {running=false};
228 if(Text(request,"operation")=="stop") {
229 var info=new ProcessStartInfo("taskkill.exe","/PID "+process.Id+" /T /F") {UseShellExecute=false,CreateNoWindow=true};
230 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};
231 }
232 return new {running=true,pid=process.Id};
233 }
234 }
235 if(action=="file") {
236 string path=Text(request,"path");
237 if(!System.Text.RegularExpressions.Regex.IsMatch(path,@"^[A-Za-z]:[\\/]") || path.IndexOf('\0')>=0)throw new Exception("Use an absolute guest file path.");
238 switch(Text(request,"operation")) {
239 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")};
240 case "list": var entries=new System.Collections.Generic.List<object>();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};
241 case "mkdir": System.IO.Directory.CreateDirectory(path);return new {path=path};
242 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};
243 case "remove": System.IO.File.Delete(path);return new {removed=true};
244 default: throw new Exception("Choose a supported file operation.");
245 }
246 }
247 throw new Exception("Choose a supported computer operation.");
248 }
249}
guest/windows-provision.ps1+45-22
...@@ -4,6 +4,17 @@ $ProgressPreference = 'SilentlyContinue'...@@ -4,6 +4,17 @@ $ProgressPreference = 'SilentlyContinue'
4$root = 'C:\Snowglobe'4$root = 'C:\Snowglobe'
5$status = Join-Path $root 'setup'5$status = Join-Path $root 'setup'
6$winlogon = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'6$winlogon = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
7$scheduler = New-Object -ComObject Schedule.Service
8$scheduler.Connect()
9$folder = $scheduler.GetFolder('\')
10
11function Set-GuestPassword([string]$name, [string]$password) {
12 $account = [ADSI]("WinNT://" + $env:COMPUTERNAME + "/" + $name + ",user")
13 $account.SetPassword($password)
14 $account.UserFlags = ([int]$account.UserFlags.Value -bor 0x10000) -band (-bnot 2)
15 $account.PasswordExpired = 0
16 $account.SetInfo()
17}
718
8function Set-SetupStatus([string]$stage) {19function Set-SetupStatus([string]$stage) {
9 $path = Join-Path $status $stage20 $path = Join-Path $status $stage
...@@ -15,18 +26,30 @@ function Set-SetupStatus([string]$stage) {...@@ -15,18 +26,30 @@ function Set-SetupStatus([string]$stage) {
15try {26try {
16 if ($Prepare) {27 if ($Prepare) {
17 Remove-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Run' -Name SnowglobeBootstrap -ErrorAction SilentlyContinue28 Remove-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Run' -Name SnowglobeBootstrap -ErrorAction SilentlyContinue
18 Unregister-ScheduledTask -TaskName SnowglobeFinalize -Confirm:$false -ErrorAction SilentlyContinue29 foreach ($task in $folder.GetTasks(1)) {
30 if ($task.Name -eq 'SnowglobeFinalize') { $task.Stop(0); $folder.DeleteTask($task.Name, 0) }
31 }
19 & "$root\install-guest.ps1" -Binary "$root\snowglobe-guest.exe" -User sandbox32 & "$root\install-guest.ps1" -Binary "$root\snowglobe-guest.exe" -User sandbox
20 (Get-LocalUser -Name sandbox).SID.Value | Set-Content "$root\template.sid" -Encoding ASCII33 $user = Get-WmiObject Win32_UserAccount -Filter "LocalAccount=True AND Name='sandbox'"
21 Get-ScheduledTask -TaskName 'SnowglobeGuest-*' | Disable-ScheduledTask | Out-Null34 if (!$user) { throw 'The template account is missing.' }
35 $user.SID | Set-Content "$root\template.sid" -Encoding ASCII
36 foreach ($task in $folder.GetTasks(1)) {
37 if ($task.Name -like 'SnowglobeGuest-*') { $task.Enabled = $false }
38 }
22 Get-Process snowglobe-guest -ErrorAction SilentlyContinue | Stop-Process -Force39 Get-Process snowglobe-guest -ErrorAction SilentlyContinue | Stop-Process -Force
23 $action = New-ScheduledTaskAction -Execute "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument '-NoProfile -ExecutionPolicy Bypass -File C:\Snowglobe\windows-provision.ps1'40 $task = $scheduler.NewTask(0)
24 $trigger = New-ScheduledTaskTrigger -AtStartup41 $task.Principal.UserId = 'SYSTEM'
25 $principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest42 $task.Principal.LogonType = 5
26 $settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Minutes 5)43 $task.Principal.RunLevel = 1
27 Register-ScheduledTask -TaskName SnowglobeProvision -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Force | Out-Null44 $task.Settings.StartWhenAvailable = $true
45 $task.Settings.ExecutionTimeLimit = 'PT15M'
46 $task.Triggers.Create(8) | Out-Null
47 $action = $task.Actions.Create(0)
48 $action.Path = "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe"
49 $action.Arguments = '-NoProfile -ExecutionPolicy Bypass -File C:\Snowglobe\windows-provision.ps1'
50 $folder.RegisterTaskDefinition('SnowglobeProvision', $task, 6, 'SYSTEM', $null, 5, $null) | Out-Null
28 $password = [Guid]::NewGuid().ToString('N') + [Guid]::NewGuid().ToString('N')51 $password = [Guid]::NewGuid().ToString('N') + [Guid]::NewGuid().ToString('N')
29 Set-LocalUser sandbox -Password (ConvertTo-SecureString $password -AsPlainText -Force) -PasswordNeverExpires $true52 Set-GuestPassword sandbox $password
30 Set-ItemProperty $winlogon AutoAdminLogon '1'53 Set-ItemProperty $winlogon AutoAdminLogon '1'
31 Set-ItemProperty $winlogon DefaultUserName 'sandbox'54 Set-ItemProperty $winlogon DefaultUserName 'sandbox'
32 Set-ItemProperty $winlogon DefaultDomainName $env:COMPUTERNAME55 Set-ItemProperty $winlogon DefaultDomainName $env:COMPUTERNAME
...@@ -60,13 +83,13 @@ try {...@@ -60,13 +83,13 @@ try {
60 if ($username -notmatch '^[A-Za-z0-9][A-Za-z0-9._-]{0,19}$') { throw 'The VM account name is unsupported.' }83 if ($username -notmatch '^[A-Za-z0-9][A-Za-z0-9._-]{0,19}$') { throw 'The VM account name is unsupported.' }
61 if ($password.Length -lt 12 -or $password.Length -gt 128 -or $password -match '[\r\n]') { throw 'The VM password is malformed.' }84 if ($password.Length -lt 12 -or $password.Length -gt 128 -or $password -match '[\r\n]') { throw 'The VM password is malformed.' }
62 $sid = Get-Content "$root\template.sid"85 $sid = Get-Content "$root\template.sid"
63 $user = Get-LocalUser | Where-Object { $_.SID.Value -eq $sid }86 $user = Get-WmiObject Win32_UserAccount -Filter "LocalAccount=True AND SID='$sid'"
64 if (!$user) { throw 'The template account is missing.' }87 if (!$user) { throw 'The template account is missing.' }
65 if (Get-LocalUser | Where-Object { $_.Name -ieq $username -and $_.SID.Value -ne $sid }) { throw 'The VM account name is already in use.' }88 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.' }
66 $deadline = (Get-Date).AddMinutes(3)89 $deadline = (Get-Date).AddMinutes(10)
67 function Find-Desktop {90 function Find-Desktop {
68 Get-CimInstance Win32_Process -Filter "Name='explorer.exe'" | Where-Object {91 Get-WmiObject Win32_Process -Filter "Name='explorer.exe'" | Where-Object {
69 $_.SessionId -gt 0 -and (Invoke-CimMethod $_ -MethodName GetOwnerSid -ErrorAction SilentlyContinue).Sid -eq $sid92 $_.SessionId -gt 0 -and $_.GetOwnerSid().Sid -eq $sid
70 } | Select-Object -First 193 } | Select-Object -First 1
71 }94 }
72 $desktop = Find-Desktop95 $desktop = Find-Desktop
...@@ -75,17 +98,17 @@ try {...@@ -75,17 +98,17 @@ try {
75 Start-Sleep -Milliseconds 25098 Start-Sleep -Milliseconds 250
76 $desktop = Find-Desktop99 $desktop = Find-Desktop
77 }100 }
78 $user | Set-LocalUser -Password (ConvertTo-SecureString $password -AsPlainText -Force) -PasswordNeverExpires $true101 Set-GuestPassword $user.Name $password
79 if ($user.Name -ine $username) { $user | Rename-LocalUser -NewName $username }102 if ($user.Name -ine $username -and $user.Rename($username).ReturnValue -ne 0) { throw 'The template account could not be renamed.' }
80 Set-ItemProperty $winlogon AutoAdminLogon '1'103 Set-ItemProperty $winlogon AutoAdminLogon '1'
81 Set-ItemProperty $winlogon DefaultUserName $username104 Set-ItemProperty $winlogon DefaultUserName $username
82 Set-ItemProperty $winlogon DefaultDomainName $env:COMPUTERNAME105 Set-ItemProperty $winlogon DefaultDomainName $env:COMPUTERNAME
83 Set-ItemProperty $winlogon DefaultPassword $password106 Set-ItemProperty $winlogon DefaultPassword $password
84 Remove-ItemProperty $winlogon AutoLogonCount -ErrorAction SilentlyContinue107 Remove-ItemProperty $winlogon AutoLogonCount -ErrorAction SilentlyContinue
85 $scheduler = New-Object -ComObject Schedule.Service108 $definition = $folder.GetTask("SnowglobeGuest-$sid").Definition
86 $scheduler.Connect()109 $definition.Triggers.Item(1).UserId = $sid
87 $task = $scheduler.GetFolder('\').GetTask("SnowglobeGuest-$sid")110 $definition.Settings.Enabled = $true
88 $task.Enabled = $true111 $task = $folder.RegisterTaskDefinition("SnowglobeGuest-$sid", $definition, 6, $sid, $null, 3, $null)
89 Set-ItemProperty $winlogon ForceAutoLogon '1'112 Set-ItemProperty $winlogon ForceAutoLogon '1'
90 Set-SetupStatus signingIn113 Set-SetupStatus signingIn
91 & "$env:WINDIR\System32\logoff.exe" $desktop.SessionId114 & "$env:WINDIR\System32\logoff.exe" $desktop.SessionId
...@@ -99,8 +122,8 @@ try {...@@ -99,8 +122,8 @@ try {
99 do {122 do {
100 if ((Get-Date) -gt $deadline) { throw 'Desktop capture did not start.' }123 if ((Get-Date) -gt $deadline) { throw 'Desktop capture did not start.' }
101 Start-Sleep -Milliseconds 250124 Start-Sleep -Milliseconds 250
102 $capture = Get-CimInstance Win32_Process -Filter "Name='snowglobe-guest.exe'" | Where-Object {125 $capture = Get-WmiObject Win32_Process -Filter "Name='snowglobe-guest.exe'" | Where-Object {
103 $_.SessionId -eq $fresh.SessionId -and (Invoke-CimMethod $_ -MethodName GetOwnerSid -ErrorAction SilentlyContinue).Sid -eq $sid126 $_.SessionId -eq $fresh.SessionId -and $_.GetOwnerSid().Sid -eq $sid
104 }127 }
105 } while (!$capture)128 } while (!$capture)
106 Set-SetupStatus ready129 Set-SetupStatus ready
nixos/computer-ahk.nix created+11
...@@ -0,0 +1,11 @@
1{ runCommand, fetchurl, unzip }:
2runCommand "snowglobe-autohotkey-2.0.19" {
3 src = fetchurl {
4 url = "https://github.com/AutoHotkey/AutoHotkey/releases/download/v2.0.19/AutoHotkey_2.0.19.zip";
5 sha256 = "4e0d0e65655066a646a210951320feaef0729a3597177131adaec4066bef5869";
6 };
7 nativeBuildInputs = [ unzip ];
8} ''
9 mkdir -p "$out"
10 unzip -j "$src" AutoHotkey32.exe license.txt -d "$out"
11''
nixos/configuration.nix+22-3
...@@ -6,8 +6,9 @@ let...@@ -6,8 +6,9 @@ let
6 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";6 proxyToken = "/var/lib/snowglobe/dashboard-proxy.token";
7 hostTools = lib.fileset.toSource {7 hostTools = lib.fileset.toSource {
8 root = ../.;8 root = ../.;
9 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 ];9 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 ];
10 };10 };
11 computerAhk = pkgs.callPackage ./computer-ahk.nix { };
11 nativePkl = pkgs.callPackage ./pkl.nix { };12 nativePkl = pkgs.callPackage ./pkl.nix { };
12 hostPython = pkgs.callPackage ./host-python.nix { };13 hostPython = pkgs.callPackage ./host-python.nix { };
13 screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);14 screenPython = pkgs.python3.withPackages (packages: [ packages.pygobject3 packages.gst-python ]);
...@@ -163,7 +164,7 @@ in...@@ -163,7 +164,7 @@ in
163 SNOWGLOBE_VM_GUEST_PROTOCOL = toString ../guest/protocol.json;164 SNOWGLOBE_VM_GUEST_PROTOCOL = toString ../guest/protocol.json;
164 };165 };
165 serviceConfig = {166 serviceConfig = {
166 ExecStart = "${screenPython}/bin/python3 ${../tools/vm-screen.py}";167 ExecStart = "${screenPython}/bin/python3 ${hostTools}/tools/vm-screen.py";
167 User = "snowglobe-dashboard";168 User = "snowglobe-dashboard";
168 Group = "snowglobe-dashboard";169 Group = "snowglobe-dashboard";
169 RuntimeDirectory = "snowglobe-vm-screen";170 RuntimeDirectory = "snowglobe-vm-screen";
...@@ -184,6 +185,23 @@ in...@@ -184,6 +185,23 @@ in
184 RestartSec = 2;185 RestartSec = 2;
185 };186 };
186 };187 };
188 systemd.timers.snowglobe-vm-expiry = {
189 wantedBy = [ "timers.target" ];
190 timerConfig = { OnBootSec = "1min"; OnUnitActiveSec = "1min"; };
191 };
192 systemd.services.snowglobe-vm-expiry = {
193 path = [ pkgs.python3 pkgs.libvirt ];
194 serviceConfig = {
195 Type = "oneshot";
196 ExecStart = "${pkgs.python3}/bin/python3 ${hostTools}/tools/vm-ai.py reap";
197 UMask = "0077";
198 ProtectSystem = "strict";
199 ProtectHome = true;
200 PrivateTmp = true;
201 NoNewPrivileges = true;
202 ReadWritePaths = [ "/srv/vm" ];
203 };
204 };
187 systemd.services.snowglobe-host = {205 systemd.services.snowglobe-host = {
188 wantedBy = [ "multi-user.target" ];206 wantedBy = [ "multi-user.target" ];
189 wants = [ "podman.socket" ];207 wants = [ "podman.socket" ];
...@@ -193,6 +211,7 @@ in...@@ -193,6 +211,7 @@ in
193 environment = (lib.filterAttrs (name: _: lib.hasPrefix "SNOWGLOBE_" name) config.environment.variables) // {211 environment = (lib.filterAttrs (name: _: lib.hasPrefix "SNOWGLOBE_" name) config.environment.variables) // {
194 SNOWGLOBE_POOL = lib.attrByPath [ "SNOWGLOBE_POOL" ] "snowglobe-demo" config.environment.variables;212 SNOWGLOBE_POOL = lib.attrByPath [ "SNOWGLOBE_POOL" ] "snowglobe-demo" config.environment.variables;
195 SNOWGLOBE_DASHBOARD_USER = "snowglobe-dashboard";213 SNOWGLOBE_DASHBOARD_USER = "snowglobe-dashboard";
214 SNOWGLOBE_COMPUTER_AHK = toString computerAhk;
196 SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables;215 SNOWGLOBE_VM_ACCEL = lib.attrByPath [ "SNOWGLOBE_VM_ACCEL" ] "kvm" config.environment.variables;
197 };216 };
198 serviceConfig = {217 serviceConfig = {
...@@ -211,7 +230,7 @@ in...@@ -211,7 +230,7 @@ in
211 PrivateTmp = true;230 PrivateTmp = true;
212 NoNewPrivileges = true;231 NoNewPrivileges = true;
213 CapabilityBoundingSet = [ "CAP_SYS_ADMIN" "CAP_DAC_READ_SEARCH" "CAP_DAC_OVERRIDE" "CAP_CHOWN" ];232 CapabilityBoundingSet = [ "CAP_SYS_ADMIN" "CAP_DAC_READ_SEARCH" "CAP_DAC_OVERRIDE" "CAP_CHOWN" ];
214 MemoryMax = "256M";233 MemoryMax = (builtins.fromJSON (builtins.readFile ../dashboard/src/host-protocol.json)).memory;
215 TasksMax = 64;234 TasksMax = 64;
216 DevicePolicy = "closed";235 DevicePolicy = "closed";
217 DeviceAllow = [ "/dev/zfs rw" "char-pts rw" ] ++ lib.optional (builtins.elem "nvidia" config.services.xserver.videoDrivers) "char-nvidia* rw";236 DeviceAllow = [ "/dev/zfs rw" "char-pts rw" ] ++ lib.optional (builtins.elem "nvidia" config.services.xserver.videoDrivers) "char-nvidia* rw";
nixos/dashboard.nix+1
...@@ -17,6 +17,7 @@ let...@@ -17,6 +17,7 @@ let
17 fileset = lib.fileset.unions [17 fileset = lib.fileset.unions [
18 ../dashboard/web ../dashboard/vendor ../dashboard/package.json ../dashboard/pnpm-lock.yaml18 ../dashboard/web ../dashboard/vendor ../dashboard/package.json ../dashboard/pnpm-lock.yaml
19 ../dashboard/tsconfig.json ../dashboard/vite.config.ts19 ../dashboard/tsconfig.json ../dashboard/vite.config.ts
20 ../dashboard/src/claude.svg ../dashboard/src/openai.svg
20 ];21 ];
21 };22 };
22 sourceRoot = "source/dashboard";23 sourceRoot = "source/dashboard";
tools/computer-test.py created+298
...@@ -0,0 +1,298 @@
1#!/usr/bin/env python3
2"""Computer protocol and guest file boundary tests using disposable files."""
3from importlib import import_module
4import base64
5import asyncio
6import time
7import json
8import tempfile
9import unittest
10from pathlib import Path
11from unittest.mock import patch, AsyncMock, Mock
12
13computer=import_module('vm-computer')
14worker=import_module('mac-computer')
15leases=import_module('vm-ai')
16
17
18class Protocol(unittest.TestCase):
19 def test_rejects_malformed_actions_before_input(self):
20 for action in [None,[],{'type':[]},{'type':'click','x':True,'y':0},
21 {'type':'drag','x':0,'y':0,'to_x':0,'to_y':-1},
22 {'type':'key','keys':['Invalid']},{'type':'key','keys':'Enter'},
23 {'type':'scroll','x':0,'y':0,'direction':'forward'},
24 {'type':'wait','duration_ms':5001},{'type':'text','text':'x'*16385},
25 {'type':'click','x':0,'y':0,'command':'id'}]:
26 with self.subTest(action=action),self.assertRaises(ValueError):
27 computer.validate({'action':'input','actions':[action],'screenshot':False})
28
29 def test_rejects_unbounded_files_and_ui_targets(self):
30 for request in [{'action':'file','operation':'read','path':'/tmp/x','length':65537},
31 {'action':'file','operation':'write','path':'/tmp/x','data':'a'*90001},
32 {'action':'file','operation':'read','path':'/tmp/x','offset':True},
33 {'action':'ui','pid':True},
34 {'action':'ui_action','target':{'name':'x'*5000},'operation':'press'}]:
35 with self.subTest(request=request),self.assertRaises(ValueError):computer.validate(request)
36
37 def test_file_upload_is_exclusive_and_commit_does_not_replace_by_default(self):
38 with tempfile.TemporaryDirectory() as directory:
39 temporary=str(Path(directory)/'upload');destination=str(Path(directory)/'Unicode 🦊')
40 data=b'\0binary\xff\n'*8192
41 def call(operation,**values):return worker.execute({'action':'file','operation':operation,'path':temporary,**values})
42 call('write',data=base64.b64encode(data[:65536]).decode(),offset=0)
43 with self.assertRaises(FileExistsError):call('write',data='',offset=0)
44 with self.assertRaises(ValueError):call('write',data='',offset=42)
45 call('write',data=base64.b64encode(data[65536:]).decode(),offset=65536)
46 self.assertEqual(Path(temporary).read_bytes(),data)
47 Path(destination).write_bytes(b'existing')
48 with self.assertRaises(FileExistsError):call('commit',destination=destination,overwrite=False)
49 self.assertEqual(Path(destination).read_bytes(),b'existing')
50 call('commit',destination=destination,overwrite=True)
51 self.assertEqual(Path(destination).read_bytes(),data)
52 self.assertFalse(Path(temporary).exists())
53
54 def test_upload_refuses_symlink_and_directory(self):
55 with tempfile.TemporaryDirectory() as directory:
56 target=Path(directory)/'target';target.write_bytes(b'original')
57 link=Path(directory)/'link';link.symlink_to(target)
58 for path in [link,Path(directory)]:
59 with self.assertRaises(OSError):worker.execute({'action':'file','operation':'write','path':str(path),'data':'','offset':0})
60 self.assertEqual(target.read_bytes(),b'original')
61
62 def test_stale_process_handle_cannot_stop_reused_pid(self):
63 with patch.object(worker,'MAC',True),patch.object(worker,'process_identity',return_value='new-start'),patch.object(worker.os,'killpg') as kill:
64 self.assertEqual(worker.execute({'action':'process','id':'100:old-start','operation':'stop'}),{'running':False})
65 kill.assert_not_called()
66
67
68class GuestCancellation(unittest.TestCase):
69 def setUp(self):
70 directory = tempfile.TemporaryDirectory()
71 self.addCleanup(directory.cleanup)
72 root = Path(directory.name)
73 (root / 'fixture').mkdir()
74 patcher = patch.object(computer.vms, 'DISKS', root)
75 patcher.start()
76 self.addCleanup(patcher.stop)
77
78 def test_reused_guest_pid_waits_for_current_process(self):
79 replies = [{'pid': 2596}, {'exited': True, 'exitcode': 1}, {'exited': False},
80 {'exited': True, 'exitcode': 0, 'out-data': base64.b64encode(b'current').decode()},
81 ProcessLookupError()]
82 with patch.object(computer, 'qga', side_effect=replies), patch.object(computer.time, 'sleep'):
83 self.assertEqual(computer.guest_exec('fixture', 'cmd.exe', []), b'current')
84
85 def test_fast_reused_guest_pid_returns_last_completed_record(self):
86 replies = [{'pid': 2596}, {'exited': True, 'exitcode': 0, 'out-data': 'b2xk'},
87 {'exited': True, 'exitcode': 0, 'out-data': 'bmV3'}, ProcessLookupError()]
88 with patch.object(computer, 'qga', side_effect=replies):
89 self.assertEqual(computer.guest_exec('fixture', 'cmd.exe', []), b'new')
90
91 def test_guest_completion_requires_explicit_end_of_records(self):
92 for error in [ValueError('Disconnected'), TimeoutError()]:
93 replies = [{'pid': 2596}, {'exited': True, 'exitcode': 0}, error]
94 with self.subTest(error=type(error)), patch.object(computer, 'qga', side_effect=replies):
95 with self.assertRaises(type(error)):
96 computer.guest_exec('fixture', 'cmd.exe', [])
97
98 def test_guest_pid_absence_is_distinct_from_transport_failure(self):
99 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']:
100 response = Mock(stdout='', stderr='error: ' + message + '\n')
101 with self.subTest(message=message), patch.object(computer.subprocess, 'run', return_value=response):
102 with self.assertRaises(ValueError if message == 'Guest agent is not responding' else ProcessLookupError):
103 computer.qga('fixture', 'guest-exec-status', pid=2596)
104
105 def test_windows_stops_guest_task_after_its_operation_budget_expires(self):
106 running = []
107 def execute(name, script, **kwargs):
108 if 'RegisterTaskDefinition' in script:
109 running.append(True)
110 computer.DEADLINE.set(time.monotonic() - 1)
111 return b''
112 if '$t.Stop(0)' in script:
113 self.assertGreater(computer.remaining(20), 0)
114 running.clear()
115 return b''
116 return b'True'
117 token = computer.DEADLINE.set(time.monotonic() + 30)
118 try:
119 with patch.object(computer, 'powershell', execute), patch.object(computer, 'write_file'):
120 with self.assertRaises(TimeoutError):
121 computer.worker('fixture', 'windows', {'action': 'input', 'actions': [{'type': 'text', 'text': 'hello'}]})
122 self.assertEqual(running, [])
123 finally:
124 computer.DEADLINE.reset(token)
125
126 def test_windows_console_remains_available_without_guest_agent(self):
127 def virsh(action, *args, **kwargs):
128 return 'running' if action == 'domstate' else '<domain><metadata><vm xmlns="https://paperclover.net/snowglobe"><platform>windows</platform></vm></metadata></domain>'
129 with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'qga', side_effect=ValueError('Disconnected')), patch.object(computer, 'worker') as work:
130 status = computer.handle('fixture', {'action': 'status'})
131 self.assertEqual(status['capabilities'], ['shot', 'input'])
132 self.assertEqual(status['text_encoding'], 'ascii')
133 actions = [{'type': 'text', 'text': 'recovery console'}]
134 with patch.object(computer, 'desktop', new_callable=AsyncMock, return_value={'completed': 1}) as desktop:
135 self.assertEqual(computer.handle('fixture', {'action': 'input', 'actions': actions, 'screenshot': False}), {'completed': 1})
136 desktop.assert_awaited_once_with('fixture', actions, 'windows', False, False)
137 work.assert_not_called()
138
139 def test_failed_cleanup_blocks_later_control_until_reconnection(self):
140 for platform, identity in [('linux', 'snowglobe-computer-request-' + 'a' * 32), ('windows', 'a' * 32)]:
141 operation = {'platform': platform, 'id': identity}
142 computer.record_operation('fixture', operation)
143 with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError):
144 computer.finish_operation('fixture', operation)
145 pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json'
146 self.assertEqual(json.loads(pending.read_text()), operation)
147 def virsh(action, *args, **kwargs):
148 return 'running' if action == 'domstate' else '<domain/>'
149 with patch.object(computer.vms, 'virsh', virsh), patch.object(computer, 'worker') as work:
150 with patch.object(computer, 'guest_exec', side_effect=TimeoutError()), patch.object(computer, 'powershell', side_effect=TimeoutError()), self.assertRaises(BlockingIOError):
151 computer.handle('fixture', {'action': 'status'})
152 work.assert_not_called()
153 with patch.object(computer, 'guest_exec', return_value=b'{}'), patch.object(computer, 'powershell', return_value=b'{}'):
154 computer.handle('fixture', {'action': 'status'})
155 work.assert_called_once()
156 self.assertFalse(pending.exists())
157
158 def test_spawn_returns_its_handle_when_cleanup_must_wait_for_reconnection(self):
159 result = {'id': '123:456', 'running': True}
160 def execute(name, script, **kwargs):
161 if 'DeleteTask' in script:
162 raise TimeoutError()
163 return b'True'
164 with patch.object(computer, 'powershell', execute), patch.object(computer, 'write_file'), patch.object(computer, 'read_file', return_value=json.dumps(result).encode()):
165 self.assertEqual(computer.worker('fixture', 'windows', {'action': 'spawn'}), result)
166 pending = computer.vms.DISKS / 'fixture' / 'computer-operation.json'
167 operation = json.loads(pending.read_text())
168 self.assertTrue(operation['keep_spawn'])
169 with patch.object(computer, 'powershell', return_value=b'') as execute:
170 computer.finish_operation('fixture', operation)
171 self.assertNotIn('$t.Stop(0)', execute.call_args.args[1])
172 self.assertFalse(pending.exists())
173
174 def test_spawn_is_stopped_if_its_success_cannot_be_persisted(self):
175 record = computer.record_operation
176 def persist(name, operation):
177 if operation.get('keep_spawn'):
178 raise OSError('Disk full')
179 record(name, operation)
180 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"}'):
181 with self.assertRaises(OSError):
182 computer.worker('fixture', 'windows', {'action': 'spawn'})
183 self.assertIn('$t.Stop(0)', execute.call_args.args[1])
184 self.assertFalse((computer.vms.DISKS / 'fixture' / 'computer-operation.json').exists())
185
186 def test_linux_stops_the_exact_request_unit_with_a_fresh_cleanup_budget(self):
187 units = []
188 def execute(name, executable, arguments, data=None, **kwargs):
189 request = json.loads(data)
190 if '_prepare' in request:
191 return b'{"prepared":true}'
192 if '_unit' in request:
193 units.append(request['_unit'])
194 computer.DEADLINE.set(time.monotonic() - 1)
195 raise TimeoutError()
196 self.assertEqual(request, {'_stop': units.pop()})
197 self.assertGreater(computer.remaining(20), 0)
198 return b'{"stopped":true}'
199 token = computer.DEADLINE.set(time.monotonic() + 30)
200 try:
201 with patch.object(computer, 'guest_exec', execute), self.assertRaises(TimeoutError):
202 computer.worker('fixture', 'linux', {'action': 'status'})
203 self.assertEqual(units, [])
204 finally:
205 computer.DEADLINE.reset(token)
206
207
208class DesktopBounds(unittest.IsolatedAsyncioTestCase):
209 async def asyncSetUp(self):
210 self.writer = Mock(drain=AsyncMock(), wait_closed=AsyncMock())
211 self.screen = Mock(width=3840, height=2160, start=AsyncMock())
212 async def read(ready, clear):
213 ready()
214 await asyncio.Future()
215 self.screen.read = read
216 self.patches = [patch.object(computer.vms, 'console_target', return_value={'port': 5900}),
217 patch.object(computer.asyncio, 'open_connection', AsyncMock(return_value=(None, self.writer))),
218 patch.object(computer.rfb, 'Desktop', return_value=self.screen)]
219 for item in self.patches:
220 item.start()
221 self.addCleanup(item.stop)
222
223 async def test_input_without_screenshot_never_encodes_or_requests_another_frame(self):
224 with patch.object(computer.rfb, 'png', side_effect=AssertionError('No capture requested')):
225 result = await computer.desktop('fixture', [{'type': 'click', 'x': 1, 'y': 1}], 'linux', False)
226 self.assertEqual(result, {'completed': 1})
227 self.screen.request.assert_not_called()
228
229 async def test_cancelled_batch_keeps_capture_slot_until_text_worker_returns(self):
230 finished = []
231 def type_text(*args):
232 self.assertLess(computer.remaining(999), 181)
233 time.sleep(.06)
234 finished.append(True)
235 wait_for = asyncio.wait_for
236 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)):
237 with self.assertRaises(asyncio.TimeoutError):
238 await computer.desktop('fixture', [{'type': 'text', 'text': 'one'}] * 100, 'linux', False)
239 self.assertEqual(finished, [True])
240 self.assertTrue(computer.SCREEN_SLOT.acquire(blocking=False))
241 computer.SCREEN_SLOT.release()
242
243 async def test_expired_batch_never_starts_another_guest_command(self):
244 token = computer.DEADLINE.set(time.monotonic() - 1)
245 try:
246 with patch.object(computer.subprocess, 'run') as run, self.assertRaises(TimeoutError):
247 computer.qga('fixture', 'guest-ping')
248 run.assert_not_called()
249 finally:
250 computer.DEADLINE.reset(token)
251
252
253class TestVmExpiry(unittest.TestCase):
254 def test_creation_catalog_excludes_unprepared_windows_and_oversized_disks(self):
255 prepared = {'id': 'ready', 'platform': 'windows', 'setup': 'windows-v4', 'capacity': 40 * 2**30, 'os': 'Windows', 'arch': 'x86_64'}
256 linux = {**prepared, 'id': 'linux', 'platform': 'linux'}
257 broken = {**prepared, 'id': 'unprepared', 'setup': None}
258 oversized = {**linux, 'id': 'oversized', 'capacity': 81 * 2**30}
259 with patch.object(leases.vms, 'library', return_value={'presets': [prepared, linux, broken, oversized]}), patch.object(leases.vms, 'create') as create:
260 catalog = leases.manage({'action': 'presets', 'owner': 'fixture'})
261 self.assertEqual([p['id'] for p in catalog['presets']], ['ready', 'linux'])
262 for preset in ['unprepared', 'oversized']:
263 with self.assertRaisesRegex(ValueError, 'prepared preset'):
264 leases.create({'action': 'create', 'owner': 'fixture', 'preset': preset, 'name': 'test', 'vcpus': 2, 'memory': 4})
265 create.assert_not_called()
266
267 def test_expiry_preserves_personal_vms_and_skips_active_calls(self):
268 with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.time, 'time', return_value=10000):
269 for name, expiry in [('personal', 1), ('ai-prefix-only', None), ('ai-expired', 9999), ('ai-live', 10001), ('ai-busy', 9999)]:
270 root=Path(directory)/name;root.mkdir()
271 if expiry is not None:(root/'ai.json').write_text(json.dumps({'expiresAt':expiry}))
272 with leases.vms.computer_lock('ai-busy'), patch.object(leases.vms, 'remove') as remove:
273 leases.reap()
274 remove.assert_called_once_with({'name':'ai-expired','disks':True},missing_ok=True)
275
276 def test_activity_renews_only_test_vms_and_cannot_resurrect_expired_vms(self):
277 with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.time, 'time', return_value=10000):
278 root=Path(directory)/'ai-fixture';root.mkdir()
279 path=root/'ai.json';path.write_text(json.dumps({'expiresAt':10001}))
280 self.assertEqual(leases.lease('ai-fixture'),{'expiresAt':10001})
281 self.assertEqual(leases.lease('ai-fixture',3600),{'expiresAt':13600})
282 self.assertEqual(leases.lease('ai-fixture',100),{'expiresAt':13600})
283 self.assertIsNone(leases.lease('personal',3600))
284 path.write_text(json.dumps({'expiresAt':9999}))
285 with self.assertRaisesRegex(ValueError,'expired'):leases.lease('ai-fixture',86400)
286
287 def test_foreign_owner_and_personal_vm_cannot_be_deleted_or_extended(self):
288 with tempfile.TemporaryDirectory() as directory, patch.object(leases.vms, 'DISKS', Path(directory)), patch.object(leases.vms, 'remove') as remove:
289 for name in ['ai-fixture','personal']:(Path(directory)/name).mkdir()
290 (Path(directory)/'ai-fixture/ai.json').write_text(json.dumps({'expiresAt':20000}))
291 with patch.object(leases.vms,'virsh',return_value='<domain><metadata xmlns:s="https://paperclover.net/snowglobe"><s:vm><s:owner>owner</s:owner></s:vm></metadata></domain>'):
292 for name, owner in [('ai-fixture','other'),('personal','owner')]:
293 for action in ['delete','extend']:
294 with self.assertRaises(ValueError):leases.manage({'action':action,'name':name,'owner':owner,'seconds':3600})
295 remove.assert_not_called()
296
297
298if __name__=='__main__':unittest.main()
tools/dashboard-host.py+21-1
...@@ -97,6 +97,7 @@ class Host:...@@ -97,6 +97,7 @@ class Host:
97 "host.sample": set(),97 "host.sample": set(),
98 "host.usage": {"refresh"},98 "host.usage": {"refresh"},
99 "copyparty.manage": {"action", "payload", "users"},99 "copyparty.manage": {"action", "payload", "users"},
100 "vm.ai": {"payload"},
100 **dashboard_runs.FIELDS,101 **dashboard_runs.FIELDS,
101 **{"vm." + action: set() if fields is None else {"payload"} for action, fields in vms.ACTION_FIELDS.items()},102 **{"vm." + action: set() if fields is None else {"payload"} for action, fields in vms.ACTION_FIELDS.items()},
102 }103 }
...@@ -179,6 +180,18 @@ class Host:...@@ -179,6 +180,18 @@ class Host:
179 except (OSError, ValueError, StopIteration):180 except (OSError, ValueError, StopIteration):
180 self.containers = None181 self.containers = None
181 return {"at": time.monotonic(), "containers": rows}182 return {"at": time.monotonic(), "containers": rows}
183 if operation == "vm.ai":
184 if not VM_PREPARATION_SLOT.acquire(blocking=False):
185 raise Rejected("A test VM operation is running. Wait for it to finish.")
186 try:
187 process = subprocess.run(["python3", str(Path(__file__).with_name("vm-ai.py"))],
188 input=json.dumps(request["payload"]), capture_output=True, text=True, timeout=900)
189 if process.returncode == 2:
190 raise Rejected(process.stderr.strip())
191 process.check_returncode()
192 return json.loads(process.stdout)
193 finally:
194 VM_PREPARATION_SLOT.release()
182 if operation.startswith("vm."):195 if operation.startswith("vm."):
183 action = operation[3:]196 action = operation[3:]
184 payload = request.get("payload")197 payload = request.get("payload")
...@@ -186,7 +199,14 @@ class Host:...@@ -186,7 +199,14 @@ class Host:
186 vms.validate(action, payload)199 vms.validate(action, payload)
187 except ValueError as error:200 except ValueError as error:
188 raise Rejected(str(error)) from error201 raise Rejected(str(error)) from error
189 if action in {"clipboard", "computer"}:202 if action == "computer":
203 try:
204 return importlib.import_module("vm-computer").handle(payload["name"], payload["request"])
205 except BlockingIOError:
206 raise dashboard_runs.Error(409, "Another computer operation is running. Wait for it to finish.") from None
207 except ValueError as error:
208 raise Rejected(str(error)) from error
209 if action == "clipboard":
190 if not vms.mac_vms.CONFIG or payload["name"] != vms.mac_vms.CONFIG["name"]:210 if not vms.mac_vms.CONFIG or payload["name"] != vms.mac_vms.CONFIG["name"]:
191 raise Rejected("This VM does not support this guest operation.")211 raise Rejected("This VM does not support this guest operation.")
192 try:212 try:
tools/dashboard-stage-test.py+25-1
...@@ -4,6 +4,7 @@ from contextlib import redirect_stdout...@@ -4,6 +4,7 @@ from contextlib import redirect_stdout
4import io4import io
5import json5import json
6import os6import os
7import sqlite3
7from pathlib import Path8from pathlib import Path
8import struct9import struct
9import subprocess10import subprocess
...@@ -41,7 +42,7 @@ class PreviewBoundary(unittest.TestCase):...@@ -41,7 +42,7 @@ class PreviewBoundary(unittest.TestCase):
41 host.handle.assert_not_called()42 host.handle.assert_not_called()
4243
43 def test_read_uses_the_real_host_handler(self):44 def test_read_uses_the_real_host_handler(self):
44 for operation in ["host.sample", "vm.node", "vm.domains", "vm.library"]:45 for operation in ["host.sample", "vm.node", "vm.domains", "vm.computers", "vm.library"]:
45 with self.subTest(operation=operation):46 with self.subTest(operation=operation):
46 request = {"operation": operation}47 request = {"operation": operation}
47 response, host = self.call(request)48 response, host = self.call(request)
...@@ -79,6 +80,29 @@ class PreviewBoundary(unittest.TestCase):...@@ -79,6 +80,29 @@ class PreviewBoundary(unittest.TestCase):
79 self.assertEqual(host.handle(request), "real snapshot")80 self.assertEqual(host.handle(request), "real snapshot")
80 handler.assert_called_once_with(request)81 handler.assert_called_once_with(request)
8182
83 def test_preview_vm_creation_and_ownership_stay_in_its_account(self):
84 with tempfile.TemporaryDirectory() as directory:
85 root = Path(directory)
86 (root / "data").mkdir()
87 with sqlite3.connect(root / "data/accounts.sqlite") as accounts:
88 accounts.execute("CREATE TABLE users (id TEXT, username TEXT)")
89 accounts.execute("INSERT INTO users VALUES ('preview-id', 'preview')")
90 host = stage.PreviewHost({"clone": "globe/staging/fixture", "mount": directory, "computer": "mac-fixture,linux-fixture"})
91 with patch.object(boundary.Host, "handle", return_value={"owner": "preview-id", "ownerOnly": True}) as call:
92 for name in ["mac-fixture", "linux-fixture", "ai-new"]:
93 self.assertEqual(host.handle({"operation": "vm.owner", "payload": {"name": name}})["owner"], "preview-id")
94 for owner in ["foreign-id", None]:
95 call.reset_mock()
96 with self.assertRaises(boundary.dashboard_runs.Error):
97 host.handle({"operation": "vm.ai", "payload": {"action": "create", "owner": owner}})
98 call.assert_not_called()
99 request = {"operation": "vm.ai", "payload": {"action": "create", "owner": "preview-id"}}
100 host.handle(request)
101 call.assert_called_once_with(request)
102 with patch.object(boundary.Host, "handle", return_value={"owner": "foreign-id", "ownerOnly": True}):
103 with self.assertRaises(boundary.dashboard_runs.Error):
104 host.handle({"operation": "vm.computer", "payload": {"name": "ai-foreign"}})
105
82 def test_failed_child_creation_and_repeated_token_revoke_still_clean_parent(self):106 def test_failed_child_creation_and_repeated_token_revoke_still_clean_parent(self):
83 identity = "dashboard-preview-12345678"107 identity = "dashboard-preview-12345678"
84 dataset = "globe/staging/" + identity108 dataset = "globe/staging/" + identity
tools/dashboard-stage.py+36-8
...@@ -21,7 +21,7 @@ import snowglobe...@@ -21,7 +21,7 @@ import snowglobe
21OPERATIONS = frozenset({21OPERATIONS = frozenset({
22 "shale.access",22 "shale.access",
23 "host.sample", "host.usage", "storage.mounts", "storage.datasets", "storage.pool",23 "host.sample", "host.usage", "storage.mounts", "storage.datasets", "storage.pool",
24 "storage.snapshots", "vm.node", "vm.domains", "vm.library", "deploy.current", "deploy.main", "deploy.history",24 "storage.snapshots", "vm.node", "vm.domains", "vm.computers", "vm.library", "deploy.current", "deploy.main", "deploy.history",
25 "deploy.stages", "deploy.managed", "deploy.release", "deploy.last",25 "deploy.stages", "deploy.managed", "deploy.release", "deploy.last",
26 "files.snapshot", "files.snapshots", "files.discard",26 "files.snapshot", "files.snapshots", "files.discard",
27})27})
...@@ -36,10 +36,24 @@ class PreviewHost(boundary.Host):...@@ -36,10 +36,24 @@ class PreviewHost(boundary.Host):
3636
37 def handle(self, request):37 def handle(self, request):
38 operation = request.get("operation") if isinstance(request, dict) else None38 operation = request.get("operation") if isinstance(request, dict) else None
39 if operation in {"vm.computer", "vm.owner"}:39 owner = None
40 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"}:
41 with sqlite3.connect(f"file:{self.stage['mount']}/data/accounts.sqlite?mode=ro", uri=True) as accounts:
42 owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone()
43 if not owner:
44 raise ValueError("The preview account is unavailable.")
45 if operation == "vm.ai":
46 payload = request.get("payload")
47 if not owner or not isinstance(payload, dict) or payload.get("owner") != owner[0]:
48 raise boundary.dashboard_runs.Error(403, "Test VMs must belong to this preview account.")
49 if operation in {"vm.computer", "vm.owner", "vm.act"}:
40 payload = request.get("payload")50 payload = request.get("payload")
41 if not self.stage.get("computer") or not isinstance(payload, dict) or payload.get("name") != self.stage["computer"]:51 if not self.stage.get("computer") or not isinstance(payload, dict):
42 raise boundary.dashboard_runs.Error(403, "Choose the VM explicitly attached to this preview.")52 raise boundary.dashboard_runs.Error(403, "Choose the VM explicitly attached to this preview.")
53 if payload.get("name") not in self.stage["computer"].split(","):
54 actual = super().handle({"operation": "vm.owner", "payload": {"name": payload.get("name")}}) if owner else {}
55 if not owner or actual.get("owner") != owner[0] or actual.get("ownerOnly") is not True:
56 raise boundary.dashboard_runs.Error(403, "Choose a VM attached to or created by this preview.")
43 if operation == "shale.access":57 if operation == "shale.access":
44 service = request.get("service", "")58 service = request.get("service", "")
45 if not re.fullmatch(r"shale-preview-[0-9a-f]{8}", service):59 if not re.fullmatch(r"shale-preview-[0-9a-f]{8}", service):
...@@ -58,7 +72,13 @@ class PreviewHost(boundary.Host):...@@ -58,7 +72,13 @@ class PreviewHost(boundary.Host):
58 ).stdout)72 ).stdout)
59 mounted["filesystems"][0]["target"] = "/srv"73 mounted["filesystems"][0]["target"] = "/srv"
60 return mounted74 return mounted
61 return super().handle(request)75 result = super().handle(request)
76 if owner and operation in {"vm.owner", "vm.domains", "vm.computers"}:
77 if operation == "vm.owner":
78 result = {**result, "owner": owner[0], "ownerOnly": True}
79 else:
80 result = [{**vm, "owner": owner[0], "ownerOnly": True} if vm["name"] in self.stage["computer"].split(",") else vm for vm in result]
81 return result
6282
6383
64def metadata(stage):84def metadata(stage):
...@@ -91,7 +111,7 @@ def serve(stage):...@@ -91,7 +111,7 @@ def serve(stage):
91 if not slots.acquire(blocking=False):111 if not slots.acquire(blocking=False):
92 connection.close()112 connection.close()
93 continue113 continue
94 operations = OPERATIONS | {"vm.computer", "vm.owner"} if saved.get("computer") else OPERATIONS114 operations = OPERATIONS | {"vm.computer", "vm.owner", "vm.ai", "vm.act"} if saved.get("computer") else OPERATIONS
95 work = workers.submit(boundary.serve_connection, connection, host, saved["uid"], operations)115 work = workers.submit(boundary.serve_connection, connection, host, saved["uid"], operations)
96 work.add_done_callback(lambda _: slots.release())116 work.add_done_callback(lambda _: slots.release())
97117
...@@ -121,8 +141,13 @@ def stage(computer=None):...@@ -121,8 +141,13 @@ def stage(computer=None):
121 if saved.get("computer"):141 if saved.get("computer"):
122 configured = next((value.partition("=")[2] for value in host_environment if value.startswith("SNOWGLOBE_MAC_VM=")), "null")142 configured = next((value.partition("=")[2] for value in host_environment if value.startswith("SNOWGLOBE_MAC_VM=")), "null")
123 mac = json.loads(configured)143 mac = json.loads(configured)
124 if not mac or mac["name"] != saved["computer"]:144 selected = saved["computer"].split(",")
125 raise ValueError("Choose the configured managed Mac VM for this preview.")145 for name in selected:
146 boundary.vms.ensure_name(name)
147 if not mac or name != mac["name"]:
148 snowglobe.command("virsh", "domuuid", name, capture=True)
149 if mac and mac["name"] not in selected:
150 mac = None
126 nix = ["nix", "--extra-experimental-features", "nix-command flakes"]151 nix = ["nix", "--extra-experimental-features", "nix-command flakes"]
127 package = "path:" + str(snowglobe.REPO) + "#dashboard-image"152 package = "path:" + str(snowglobe.REPO) + "#dashboard-image"
128 image = subprocess.run([*nix, "eval", "--raw", package, "--apply", 'image: "${image.imageName}:${image.imageTag}"'],153 image = subprocess.run([*nix, "eval", "--raw", package, "--apply", 'image: "${image.imageName}:${image.imageTag}"'],
...@@ -183,6 +208,9 @@ def stage(computer=None):...@@ -183,6 +208,9 @@ def stage(computer=None):
183 snowglobe.write_private(root / "access.json", imported.stdout)208 snowglobe.write_private(root / "access.json", imported.stdout)
184 runtime = "/run/snowglobe-preview/" + identity209 runtime = "/run/snowglobe-preview/" + identity
185 computer_environment = []210 computer_environment = []
211 if saved.get("computer"):
212 ahk = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#computer-ahk", capture=True).stdout.strip()
213 computer_environment.append("--setenv=SNOWGLOBE_COMPUTER_AHK=" + ahk)
186 if mac:214 if mac:
187 with sqlite3.connect(f"file:{root / 'data/accounts.sqlite'}?mode=ro", uri=True) as accounts:215 with sqlite3.connect(f"file:{root / 'data/accounts.sqlite'}?mode=ro", uri=True) as accounts:
188 owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone()216 owner = accounts.execute("SELECT id FROM users WHERE username='preview'").fetchone()
...@@ -193,7 +221,7 @@ def stage(computer=None):...@@ -193,7 +221,7 @@ def stage(computer=None):
193 host_python = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip()221 host_python = snowglobe.command(*nix, "build", "--no-link", "--print-out-paths", "path:" + str(snowglobe.REPO) + "#host-python", capture=True).stdout.strip()
194 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)222 subprocess.run(["systemctl", "stop", "snowglobe-preview-" + identity], capture_output=True)
195 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity,223 snowglobe.command("systemd-run", "--collect", "--quiet", "--unit=snowglobe-preview-" + identity,
196 "--property=Restart=on-failure", "--property=MemoryMax=256M", "--property=TasksMax=32",224 "--property=Restart=on-failure", "--property=MemoryMax=" + str(boundary.PROTOCOL["memory"]), "--property=TasksMax=32",
197 "--setenv=" + host_path, "--setenv=SNOWGLOBE_DOMAIN=" + site["domain"],225 "--setenv=" + host_path, "--setenv=SNOWGLOBE_DOMAIN=" + site["domain"],
198 *computer_environment,226 *computer_environment,
199 host_python + "/bin/python3", str(Path(__file__).resolve()), "--serve", identity)227 host_python + "/bin/python3", str(Path(__file__).resolve()), "--serve", identity)
tools/dashboard-vms-test.py+3-3
...@@ -177,7 +177,7 @@ class VMTests(unittest.TestCase):...@@ -177,7 +177,7 @@ class VMTests(unittest.TestCase):
177 config = captured['config']177 config = captured['config']
178 self.assertEqual(config['bootcmd'][0], ['rm', '-f', '/etc/gdm/custom.conf'])178 self.assertEqual(config['bootcmd'][0], ['rm', '-f', '/etc/gdm/custom.conf'])
179 self.assertIn('/run/current-system/sw/bin/getent', config['bootcmd'][1][-1])179 self.assertIn('/run/current-system/sw/bin/getent', config['bootcmd'][1][-1])
180 self.assertEqual(config['write_files'], [{180 self.assertEqual(config['write_files'][:2], [{
181 'path': '/etc/gdm/custom.conf', 'content': '[daemon]\nAutomaticLoginEnable=true\nAutomaticLogin=snow\n',181 'path': '/etc/gdm/custom.conf', 'content': '[daemon]\nAutomaticLoginEnable=true\nAutomaticLogin=snow\n',
182 }, {182 }, {
183 'path': '/var/lib/snowglobe/hostname',183 'path': '/var/lib/snowglobe/hostname',
...@@ -188,7 +188,7 @@ class VMTests(unittest.TestCase):...@@ -188,7 +188,7 @@ class VMTests(unittest.TestCase):
188188
189 def test_console_cannot_choose_an_arbitrary_network_target(self):189 def test_console_cannot_choose_an_arbitrary_network_target(self):
190 xml = "<domain><uuid>fixture-vm</uuid><devices><graphics type='vnc' listen='127.0.0.1' port='5901'/></devices></domain>"190 xml = "<domain><uuid>fixture-vm</uuid><devices><graphics type='vnc' listen='127.0.0.1' port='5901'/></devices></domain>"
191 def virsh(*args):191 def virsh(*args, **kwargs):
192 return 'running' if args[0] == 'domstate' else xml192 return 'running' if args[0] == 'domstate' else xml
193 with patch.object(vms, 'virsh', side_effect=virsh):193 with patch.object(vms, 'virsh', side_effect=virsh):
194 self.assertEqual(vms.console_target('ubuntu'), {'port': 5901, 'uuid': 'fixture-vm'})194 self.assertEqual(vms.console_target('ubuntu'), {'port': 5901, 'uuid': 'fixture-vm'})
...@@ -259,7 +259,7 @@ class VMTests(unittest.TestCase):...@@ -259,7 +259,7 @@ class VMTests(unittest.TestCase):
259259
260 def test_serial_target_cannot_choose_a_host_file(self):260 def test_serial_target_cannot_choose_a_host_file(self):
261 xml = "<domain><devices><console type='pty'><source path='/dev/pts/3'/><target type='serial'/></console></devices></domain>"261 xml = "<domain><devices><console type='pty'><source path='/dev/pts/3'/><target type='serial'/></console></devices></domain>"
262 def virsh(*args):262 def virsh(*args, **kwargs):
263 return 'running' if args[0] == 'domstate' else xml263 return 'running' if args[0] == 'domstate' else xml
264 with patch.object(sys, 'argv', ['vms.py', 'serial', json.dumps({'name': 'ubuntu'})]), patch.object(vms, 'virsh', side_effect=virsh):264 with patch.object(sys, 'argv', ['vms.py', 'serial', json.dumps({'name': 'ubuntu'})]), patch.object(vms, 'virsh', side_effect=virsh):
265 self.assertEqual(vms.main(), {'path': '/dev/pts/3'})265 self.assertEqual(vms.main(), {'path': '/dev/pts/3'})
tools/deploy.py+3-3
...@@ -160,7 +160,7 @@ def upload(main=False):...@@ -160,7 +160,7 @@ def upload(main=False):
160def main():160def main():
161 parser = argparse.ArgumentParser(description="Preview working changes and deploy main")161 parser = argparse.ArgumentParser(description="Preview working changes and deploy main")
162 parser.add_argument("mode", choices=["stage", "publish", "prod", "rollback", "history", "backups", "data-restore", "bootstrap", "allocate", "destroy", "secrets"])162 parser.add_argument("mode", choices=["stage", "publish", "prod", "rollback", "history", "backups", "data-restore", "bootstrap", "allocate", "destroy", "secrets"])
163 parser.add_argument("--computer", help="Give a dashboard preview access to this managed Mac VM")163 parser.add_argument("--computer", help="Give a dashboard preview access to these comma-separated VM names")
164 parser.add_argument("target", nargs="?")164 parser.add_argument("target", nargs="?")
165 parser.add_argument("--env", action="append", default=[])165 parser.add_argument("--env", action="append", default=[])
166 parser.add_argument("--file", type=Path)166 parser.add_argument("--file", type=Path)
...@@ -168,8 +168,8 @@ def main():...@@ -168,8 +168,8 @@ def main():
168 parser.add_argument("--backup")168 parser.add_argument("--backup")
169 parser.add_argument("--discard-writes", action="store_true")169 parser.add_argument("--discard-writes", action="store_true")
170 args = parser.parse_args()170 args = parser.parse_args()
171 if args.computer and (args.mode != "stage" or args.target != "dashboard" or not NAME.fullmatch(args.computer)):171 if args.computer and (args.mode != "stage" or args.target != "dashboard" or not all(NAME.fullmatch(name) for name in args.computer.split(","))):
172 parser.error("--computer requires stage dashboard and a VM name")172 parser.error("--computer requires stage dashboard and VM names")
173 if args.mode in {"stage", "allocate", "destroy", "secrets", "data-restore"} and (not args.target or not NAME.fullmatch(args.target)):173 if args.mode in {"stage", "allocate", "destroy", "secrets", "data-restore"} and (not args.target or not NAME.fullmatch(args.target)):
174 parser.error(f"{args.mode} requires a service or stage ID")174 parser.error(f"{args.mode} requires a service or stage ID")
175 if args.mode == "rollback" and args.target and not RELEASE_ID.fullmatch(args.target):175 if args.mode == "rollback" and args.target and not RELEASE_ID.fullmatch(args.target):
tools/mac-agent-gui.swift+135-17
...@@ -21,7 +21,9 @@ func attribute(_ element: AXUIElement, _ name: String) -> CFTypeRef? {...@@ -21,7 +21,9 @@ func attribute(_ element: AXUIElement, _ name: String) -> CFTypeRef? {
2121
22func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String: Any] {22func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String: Any] {
23 remaining -= 123 remaining -= 1
24 var result: [String: Any] = ["path": path]24 var pid: pid_t = 0
25 AXUIElementGetPid(element, &pid)
26 var result: [String: Any] = ["path": path, "target": ["pid": pid, "path": path, "role": attribute(element, "AXRole") as? String ?? "", "name": attribute(element, "AXTitle") as? String ?? ""]]
25 for name in ["AXRole", "AXSubrole", "AXTitle", "AXDescription", "AXValue", "AXEnabled", "AXFocused"] {27 for name in ["AXRole", "AXSubrole", "AXTitle", "AXDescription", "AXValue", "AXEnabled", "AXFocused"] {
26 if let value = attribute(element, name), value is String || value is NSNumber {28 if let value = attribute(element, name), value is String || value is NSNumber {
27 result[name] = value29 result[name] = value
...@@ -43,6 +45,10 @@ func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String:...@@ -43,6 +45,10 @@ func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String:
43 }45 }
44 var actions: CFArray?46 var actions: CFArray?
45 if AXUIElementCopyActionNames(element, &actions) == .success { result["actions"] = actions }47 if AXUIElementCopyActionNames(element, &actions) == .success { result["actions"] = actions }
48 var settable = DarwinBoolean(false)
49 if AXUIElementIsAttributeSettable(element, kAXValueAttribute as CFString, &settable) == .success, settable.boolValue {
50 result["actions"] = (result["actions"] as? [String] ?? []) + ["set_value"]
51 }
46 if path.count < 16, let children = attribute(element, "AXChildren") as? [AXUIElement] {52 if path.count < 16, let children = attribute(element, "AXChildren") as? [AXUIElement] {
47 var nodes: [[String: Any]] = []53 var nodes: [[String: Any]] = []
48 for (index, child) in children.enumerated() where remaining > 0 {54 for (index, child) in children.enumerated() where remaining > 0 {
...@@ -54,6 +60,25 @@ func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String:...@@ -54,6 +60,25 @@ func tree(_ element: AXUIElement, path: [Int], remaining: inout Int) -> [String:
54 return result60 return result
55}61}
5662
63func typeText(_ text: String, _ eventSource: CGEventSource) {
64 let units = Array(text.utf16)
65 var start = 0
66 while start < units.count {
67 var end = min(start + 20, units.count)
68 if end < units.count && (0xD800...0xDBFF).contains(units[end - 1]) { end -= 1 }
69 let chunk = Array(units[start..<end])
70 for down in [true, false] {
71 let event = CGEvent(keyboardEventSource: eventSource, virtualKey: 0, keyDown: down)
72 event?.flags = []
73 event?.keyboardSetUnicodeString(stringLength: chunk.count, unicodeString: chunk)
74 event?.post(tap: .cghidEventTap)
75 usleep(10000)
76 }
77 start = end
78 }
79 usleep(50000)
80}
81
57let arguments = Array(CommandLine.arguments.dropFirst())82let arguments = Array(CommandLine.arguments.dropFirst())
58guard let command = arguments.first else { fail("Use status, request-permissions, screenshot, tree, press, click, key, or type.") }83guard let command = arguments.first else { fail("Use status, request-permissions, screenshot, tree, press, click, key, or type.") }
59guard let eventSource = CGEventSource(stateID: .privateState) else { fail("Could not create a private input event source.") }84guard let eventSource = CGEventSource(stateID: .privateState) else { fail("Could not create a private input event source.") }
...@@ -110,6 +135,114 @@ case "tree", "press":...@@ -110,6 +135,114 @@ case "tree", "press":
110 guard result == .success else { fail("AXPress failed: \(result.rawValue)") }135 guard result == .success else { fail("AXPress failed: \(result.rawValue)") }
111 output(["pressed": true])136 output(["pressed": true])
112 }137 }
138case "action":
139 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }
140 guard let request = try? JSONSerialization.jsonObject(with: FileHandle.standardInput.readDataToEndOfFile()) as? [String: Any],
141 let target = request["target"] as? [String: Any], let pid = target["pid"] as? Int32,
142 let path = target["path"] as? [Int], path.count <= 16,
143 let operation = request["operation"] as? String else { fail("Use a target returned by computer_ui.") }
144 var element = AXUIElementCreateApplication(pid)
145 for index in path {
146 guard let children = attribute(element, "AXChildren") as? [AXUIElement], children.indices.contains(index)
147 else { fail("The UI element changed. Read a fresh tree.") }
148 element = children[index]
149 }
150 guard (attribute(element, "AXRole") as? String ?? "") == (target["role"] as? String ?? ""),
151 (attribute(element, "AXTitle") as? String ?? "") == (target["name"] as? String ?? "")
152 else { fail("The UI element changed. Read a fresh tree.") }
153 let result: AXError
154 if operation == "set_value" {
155 guard let value = request["value"] as? String else { fail("Provide the new value.") }
156 result = AXUIElementSetAttributeValue(element, kAXValueAttribute as CFString, value as CFString)
157 } else if operation == "focus" {
158 result = AXUIElementSetAttributeValue(element, kAXFocusedAttribute as CFString, kCFBooleanTrue)
159 } else {
160 result = AXUIElementPerformAction(element, operation as CFString)
161 }
162 guard result == .success else { fail("The application refused the UI action. Read a fresh tree.") }
163 output(["completed": true])
164case "input":
165 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }
166 guard let request = try? JSONSerialization.jsonObject(with: FileHandle.standardInput.readDataToEndOfFile()) as? [String: Any],
167 let actions = request["actions"] as? [[String: Any]], (1...100).contains(actions.count)
168 else { fail("Send between 1 and 100 desktop actions.") }
169 let bounds = CGDisplayBounds(CGMainDisplayID())
170 let pixelWidth = Double(CGDisplayPixelsWide(CGMainDisplayID()))
171 let pixelHeight = Double(CGDisplayPixelsHigh(CGMainDisplayID()))
172 func point(_ action: [String: Any], _ x: String = "x", _ y: String = "y") -> CGPoint {
173 guard let px = action[x] as? Double, let py = action[y] as? Double,
174 px >= 0, py >= 0, px < pixelWidth, py < pixelHeight else { fail("Choose coordinates inside the latest screenshot.") }
175 return CGPoint(x: bounds.minX + px * bounds.width / pixelWidth, y: bounds.minY + py * bounds.height / pixelHeight)
176 }
177 func mouse(_ type: CGEventType, _ at: CGPoint, _ button: CGMouseButton, _ count: Int64 = 1) {
178 let event = CGEvent(mouseEventSource: eventSource, mouseType: type, mouseCursorPosition: at, mouseButton: button)
179 event?.setIntegerValueField(.mouseEventClickState, value: count)
180 event?.flags = []
181 event?.post(tap: .cghidEventTap)
182 usleep(10000)
183 }
184 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,
185 "]":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]
186 for action in actions {
187 guard let kind = action["type"] as? String else { fail("Choose a desktop action.") }
188 switch kind {
189 case "click", "move", "drag":
190 let at = point(action)
191 let button: CGMouseButton = action["button"] as? String == "right" ? .right : (action["button"] as? String == "middle" ? .center : .left)
192 let down: CGEventType = button == .left ? .leftMouseDown : (button == .right ? .rightMouseDown : .otherMouseDown)
193 let up: CGEventType = button == .left ? .leftMouseUp : (button == .right ? .rightMouseUp : .otherMouseUp)
194 mouse(.mouseMoved, at, button)
195 if kind == "click" {
196 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)) }
197 } else if kind == "drag" {
198 let end = point(action,"to_x","to_y")
199 mouse(down,at,button)
200 for step in 1...20 {
201 let p = CGPoint(x:at.x+(end.x-at.x)*Double(step)/20,y:at.y+(end.y-at.y)*Double(step)/20)
202 mouse(button == .left ? .leftMouseDragged : (button == .right ? .rightMouseDragged : .otherMouseDragged),p,button)
203 usleep(UInt32(max(1,min(action["duration_ms"] as? Int ?? 400,5000)))*50)
204 }
205 mouse(up,end,button)
206 }
207 case "scroll":
208 mouse(.mouseMoved,point(action),.left)
209 let amount = Int32(max(1,min(action["amount"] as? Int ?? 3,100)))
210 let direction = action["direction"] as? String ?? "down"
211 let vertical: Int32 = direction == "up" ? amount : (direction == "down" ? -amount : 0)
212 let horizontal: Int32 = direction == "left" ? amount : (direction == "right" ? -amount : 0)
213 CGEvent(scrollWheelEvent2Source:eventSource,units:.line,wheelCount:2,wheel1:vertical,wheel2:horizontal,wheel3:0)?.post(tap:.cghidEventTap)
214 case "key":
215 guard let chord = action["keys"] as? [String], (1...8).contains(chord.count) else { fail("Send between 1 and 8 key names.") }
216 var flags: CGEventFlags = []
217 var codes: [UInt16] = []
218 for key in chord {
219 switch key {
220 case "Meta": flags.insert(.maskCommand)
221 case "Control": flags.insert(.maskControl)
222 case "Alt": flags.insert(.maskAlternate)
223 case "Shift": flags.insert(.maskShift)
224 default:
225 guard let code = keys[key] ?? keys[key.lowercased()] else { fail("Choose a supported key name.") }
226 codes.append(code)
227 if key.count == 1 && key != key.lowercased() { flags.insert(.maskShift) }
228 }
229 }
230 for down in [true,false] {
231 for code in down ? codes : Array(codes.reversed()) {
232 let event = CGEvent(keyboardEventSource:eventSource,virtualKey:code,keyDown:down)
233 event?.flags = down ? flags : []
234 event?.post(tap:.cghidEventTap)
235 usleep(10000)
236 }
237 }
238 case "text":
239 guard let text = action["text"] as? String, text.utf8.count <= 16384 else { fail("Type at most 16 KiB at a time.") }
240 typeText(text, eventSource)
241 case "wait": usleep(UInt32(max(1,min(action["duration_ms"] as? Int ?? 1,5000)))*1000)
242 default: fail("Choose a supported desktop action.")
243 }
244 }
245 output(["completed":actions.count])
113case "click":246case "click":
114 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }247 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }
115 guard arguments.count == 3, let x = Double(arguments[1]), let y = Double(arguments[2]) else { fail("Provide x and y coordinates.") }248 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":...@@ -145,22 +278,7 @@ case "type":
145 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }278 guard AXIsProcessTrusted() else { fail("Accessibility permission is required.") }
146 guard arguments.count == 2 else { fail("Provide text, or a dash to read UTF-8 from stdin.") }279 guard arguments.count == 2 else { fail("Provide text, or a dash to read UTF-8 from stdin.") }
147 let text = arguments[1] == "-" ? String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)! : arguments[1]280 let text = arguments[1] == "-" ? String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)! : arguments[1]
148 let units = Array(text.utf16)281 typeText(text, eventSource)
149 var start = 0
150 while start < units.count {
151 var end = min(start + 20, units.count)
152 if end < units.count && (0xD800...0xDBFF).contains(units[end - 1]) { end -= 1 }
153 let chunk = Array(units[start..<end])
154 for down in [true, false] {
155 let event = CGEvent(keyboardEventSource: eventSource, virtualKey: 0, keyDown: down)
156 event?.flags = []
157 event?.keyboardSetUnicodeString(stringLength: chunk.count, unicodeString: chunk)
158 event?.post(tap: .cghidEventTap)
159 usleep(10000)
160 }
161 start = end
162 }
163 usleep(50000)
164 output(["typed": true])282 output(["typed": true])
165default:283default:
166 fail("Unknown command.")284 fail("Unknown command.")
tools/mac-agent-vm.md+3
...@@ -231,6 +231,9 @@ future apps and request-only categories can still prompt. SIP remains enabled....@@ -231,6 +231,9 @@ future apps and request-only categories can still prompt. SIP remains enabled.
231FSEvents and durable disk synchronization remain enabled. Apply OS updates231FSEvents and durable disk synchronization remain enabled. Apply OS updates
232deliberately between test runs, then recheck GUI permissions and boot.232deliberately between test runs, then recheck GUI permissions and boot.
233233
234Shared `jj`, `pnpm`, and workspace command routing is maintained in
235[project tools](project-store.md).
236
234Initial provisioning installed Rust/Cargo 1.99.1, Node 24.22.0 LTS, npm 11.19.1,237Initial provisioning installed Rust/Cargo 1.99.1, Node 24.22.0 LTS, npm 11.19.1,
235pnpm 12.14.0, Jujutsu 0.42.0, and Apple's CLT with Clang 17 and the macOS SDK.238pnpm 12.14.0, Jujutsu 0.42.0, and Apple's CLT with Clang 17 and the macOS SDK.
236Subsequent guest setup manages toolchain versions and paths in239Subsequent guest setup manages toolchain versions and paths in
tools/mac-computer.py+272-25
...@@ -1,8 +1,12 @@...@@ -1,8 +1,12 @@
1#!/usr/bin/env python31#!/usr/bin/env python3
2"""Computer operations in the logged-in Mac guest, over stdin/stdout."""2"""Computer operations in a logged-in Unix desktop, over stdin/stdout."""
3import base643import base64
4import fcntl4import fcntl
5import json5import json
6import hashlib
7import shutil
8import stat
9import uuid
6import os10import os
7from pathlib import Path11from pathlib import Path
8import selectors12import selectors
...@@ -13,6 +17,198 @@ import tempfile...@@ -13,6 +17,198 @@ import tempfile
13import time17import time
1418
15GUI = "/usr/local/bin/agent-gui"19GUI = "/usr/local/bin/agent-gui"
20MAC = sys.platform == "darwin"
21
22
23def process_identity(pid):
24 if not MAC:
25 return Path('/proc/' + str(pid) + '/stat').read_text().rsplit(')',1)[1].split()[19]
26 return subprocess.check_output(['ps','-p',str(pid),'-o','lstart='],text=True).strip().replace(':','-')
27
28
29def process_operation(request):
30 identity = request.get('id','')
31 if not MAC:
32 import re
33 if not re.fullmatch('snowglobe-computer-[0-9a-f]{32}',identity) or request.get('operation') not in ('status','stop'):
34 raise ValueError('Use the process ID returned by computer_spawn.')
35 if request['operation']=='stop':
36 subprocess.run(['systemctl','--user','stop',identity],check=True,timeout=15)
37 return {'stopping':True}
38 result=subprocess.run(['systemctl','--user','show',identity,'-p','ActiveState','-p','MainPID'],capture_output=True,text=True,timeout=5)
39 state=dict(line.split('=',1) for line in result.stdout.splitlines() if '=' in line)
40 return {'running':state.get('ActiveState')=='active','pid':int(state.get('MainPID','0'))}
41 pid_text,separator,started = identity.partition(':')
42 if not separator or not pid_text.isdigit() or int(pid_text) <= 1 or request.get('operation') not in ('status','stop'):
43 raise ValueError('Use the process ID returned by computer_spawn.')
44 pid=int(pid_text)
45 try:
46 current = process_identity(pid)
47 except (OSError,subprocess.SubprocessError):
48 return {'running':False}
49 if not current or current != started:
50 return {'running':False}
51 if request['operation']=='stop':
52 if os.getpgid(pid) != pid:
53 raise ValueError('The process is not an isolated computer task.')
54 os.killpg(pid,signal.SIGTERM)
55 return {'stopping':True}
56 return {'running':True,'pid':pid}
57
58
59def file_operation(request):
60 path = request.get('path')
61 if not isinstance(path,str) or not path.startswith('/') or '\x00' in path or len(path)>4096:
62 raise ValueError('Use an absolute guest file path.')
63 path=Path(path)
64 operation=request.get('operation')
65 if operation in ('stat','list'):
66 info=path.stat()
67 result={'path':str(path),'size':info.st_size,'directory':stat.S_ISDIR(info.st_mode),'modified':info.st_mtime}
68 if operation=='list':
69 entries=[]
70 with os.scandir(path) as iterator:
71 for entry in iterator:
72 if len(entries)==1000:
73 result['truncated']=True; break
74 entries.append({'name':entry.name,'directory':entry.is_dir(follow_symlinks=False)})
75 result['entries']=entries
76 return result
77 if operation=='mkdir':
78 path.mkdir(parents=True,exist_ok=True); return {'path':str(path)}
79 if operation=='read':
80 offset,length=request.get('offset',0),request.get('length',65536)
81 if type(offset) is not int or offset<0 or type(length) is not int or not 1<=length<=65536:
82 raise ValueError('Read at most 64 KiB at a time from a nonnegative offset.')
83 with path.open('rb') as incoming:
84 if not stat.S_ISREG(os.fstat(incoming.fileno()).st_mode):
85 raise ValueError('Choose a regular guest file.')
86 incoming.seek(offset);data=incoming.read(length)
87 return {'data':base64.b64encode(data).decode(),'bytes':len(data)}
88 if operation=='write':
89 data=base64.b64decode(request.get('data',''),validate=True)
90 offset=request.get('offset',0)
91 if len(data)>65536 or type(offset) is not int or not 0<=offset<=64*2**20:
92 raise ValueError('The transfer chunk exceeds its limit.')
93 flags=os.O_WRONLY|os.O_NOFOLLOW|(os.O_CREAT|os.O_EXCL if offset==0 else 0)
94 with os.fdopen(os.open(path,flags,0o600),'r+b') as outgoing:
95 if not stat.S_ISREG(os.fstat(outgoing.fileno()).st_mode) or os.fstat(outgoing.fileno()).st_size!=offset:
96 raise ValueError('The transfer offset changed. Start a new upload.')
97 outgoing.seek(offset);outgoing.write(data)
98 return {'bytes':len(data)}
99 if operation=='commit':
100 destination=request.get('destination')
101 if not isinstance(destination,str) or not destination.startswith('/') or '\x00' in destination:
102 raise ValueError('Use an absolute guest destination path.')
103 if request.get('overwrite',False): os.replace(path,destination)
104 else:
105 os.link(path,destination);path.unlink()
106 return {'path':destination}
107 if operation=='remove':
108 path.unlink();return {'removed':True}
109 raise ValueError('Choose a supported file operation.')
110
111
112def linux_ui(request):
113 try:
114 import gi
115 gi.require_version('Atspi','2.0')
116 from gi.repository import Atspi, GLib
117 except (ImportError,ValueError):
118 raise ValueError('Install the AT-SPI Python bindings in this guest, then retry.') from None
119 Atspi.set_timeout(1500,3000)
120 desktop=Atspi.get_desktop(0)
121 if request['action']=='input':
122 for item in request['actions']:
123 if item['type']!='text': raise ValueError('Use the VM console for pointer and key actions.')
124 pending=[desktop]
125 focused=None
126 deadline=time.monotonic()+8
127 visited=0
128 while pending and visited<1500 and time.monotonic()<deadline:
129 node=pending.pop()
130 visited+=1
131 try:
132 if node.get_state_set().contains(Atspi.StateType.FOCUSED) and 'EditableText' in node.get_interfaces():
133 focused=node
134 break
135 pending.extend(node.get_child_at_index(i) for i in range(min(node.get_child_count(),1500-visited)))
136 except (GLib.Error,AttributeError):
137 continue
138 if focused is not None:
139 try:
140 position=Atspi.Text.get_caret_offset(focused)
141 if Atspi.Text.get_n_selections(focused):
142 selection=Atspi.Text.get_selection(focused,0)
143 position=selection.start_offset
144 if not Atspi.EditableText.delete_text(focused,position,selection.end_offset):
145 raise ValueError('The application refused to replace the selected text.')
146 value=item['text']
147 if not Atspi.EditableText.insert_text(focused,position,value,len(value.encode())):
148 raise ValueError('The application refused text input.')
149 Atspi.Text.set_caret_offset(focused,position+len(value))
150 except GLib.Error:
151 raise ValueError('The text field is unavailable. Focus it again, then retry.') from None
152 elif os.environ.get('XDG_SESSION_TYPE')!='wayland' and not os.environ.get('WAYLAND_DISPLAY'):
153 if not Atspi.generate_keyboard_event(0,item['text'],Atspi.KeySynthType.STRING):
154 raise ValueError('This desktop refused text input. Use a UI set_value action or a guest script.')
155 else:
156 raise ValueError('Focus an editable text field, or use a guest script to enter this text.')
157 return {'completed':len(request['actions'])}
158 if request['action']=='ui_action':
159 target=request.get('target',{})
160 node=desktop
161 try:
162 for index in target['path']: node=node.get_child_at_index(index)
163 if node.get_process_id()!=target['pid'] or process_identity(target['pid'])!=target['started'] or node.get_role_name()!=target['role'] or (node.get_name() or '')[:512]!=target['name']:
164 raise ValueError('The UI element changed. Read a fresh tree.')
165 operation=request['operation']
166 if operation=='focus': success=node.get_component_iface().grab_focus()
167 elif operation=='set_value': success=Atspi.EditableText.set_text_contents(node,request.get('value',''))
168 else:
169 actions=node.get_action_iface()
170 indices=[i for i in range(actions.get_n_actions()) if actions.get_action_name(i)==operation]
171 if not indices: raise ValueError('Choose an action reported by computer_ui.')
172 success=actions.do_action(indices[0])
173 if not success: raise ValueError('The application refused the UI action. Read a fresh tree.')
174 return {'completed':True}
175 except (KeyError,AttributeError,TypeError,GLib.Error):
176 raise ValueError('The UI element is unavailable. Read a fresh tree.') from None
177 nodes=[]
178 deadline=time.monotonic()+8
179 def walk(node,path,depth):
180 if len(nodes)>=1500 or depth>20 or time.monotonic()>deadline: return
181 try:
182 pid=node.get_process_id()
183 if len(path)==1 and request.get('pid') and pid!=request['pid']: return
184 name=node.get_name() or ''
185 role=node.get_role_name()
186 entry={'target':{'path':path,'pid':pid,'started':process_identity(pid) if pid else '', 'role':role,'name':name[:512]},'role':role,'name':name[:512]}
187 states=node.get_state_set()
188 entry['focused']=states.contains(Atspi.StateType.FOCUSED)
189 entry['enabled']=states.contains(Atspi.StateType.ENABLED)
190 entry['actions']=[]
191 interfaces=node.get_interfaces()
192 if 'Component' in interfaces:
193 if os.environ.get('XDG_SESSION_TYPE')!='wayland' and not os.environ.get('WAYLAND_DISPLAY'):
194 bounds=node.get_component_iface().get_extents(Atspi.CoordType.SCREEN)
195 entry['bounds']=[bounds.x,bounds.y,bounds.width,bounds.height]
196 if states.contains(Atspi.StateType.FOCUSABLE): entry['actions'].append('focus')
197 if 'Action' in interfaces:
198 action=node.get_action_iface()
199 entry['actions'] += [action.get_action_name(i) for i in range(min(action.get_n_actions(),32))]
200 if 'Text' in interfaces:
201 entry['value']=Atspi.Text.get_text(node,0,min(Atspi.Text.get_character_count(node),1024))
202 if 'EditableText' in interfaces: entry['actions'].append('set_value')
203 nodes.append(entry)
204 for index in range(min(node.get_child_count(),1500)):
205 if len(nodes)>=1500 or time.monotonic()>deadline: break
206 child=node.get_child_at_index(index)
207 if child: walk(child,path+[index],depth+1)
208 except Exception:
209 return
210 walk(desktop,[],0)
211 return {'nodes':nodes,'truncated':len(nodes)>=1500 or time.monotonic()>deadline}
16212
17213
18def execute(request):214def execute(request):
...@@ -20,24 +216,74 @@ def execute(request):...@@ -20,24 +216,74 @@ def execute(request):
20 raise ValueError("Send a computer operation.")216 raise ValueError("Send a computer operation.")
21 action = request.get("action")217 action = request.get("action")
22 fields = {"status": {"action"}, "shot": {"action"}, "ui": {"action", "pid"},218 fields = {"status": {"action"}, "shot": {"action"}, "ui": {"action", "pid"},
23 "exec": {"action", "script", "timeout_ms", "screenshot"}}219 "ui_action": {"action", "target", "operation", "value", "screenshot"},
24 if not isinstance(action, str) or action not in fields or set(request) != fields[action]:220 "input": {"action", "actions", "screenshot"}, "spawn": {"action", "script", "runtime"},
221 "process": {"action", "id", "operation"},
222 "file": {"action", "operation", "path", "offset", "data", "length", "destination", "overwrite"},
223 "exec": {"action", "script", "runtime", "timeout_ms", "screenshot"}}
224 if not isinstance(action, str) or action not in fields or set(request) - fields[action]:
25 raise ValueError("Use the fields listed for this computer operation.")225 raise ValueError("Use the fields listed for this computer operation.")
26 if action == "ui" and (request["pid"] is not None and (type(request["pid"]) is not int or request["pid"] <= 0)):226 if action == "file":
227 return file_operation(request)
228 if action in ("input", "ui_action"):
229 if MAC:
230 result = subprocess.run([GUI, "input" if action == "input" else "action"],
231 input=json.dumps(request).encode(), capture_output=True, timeout=30)
232 reply = json.loads(result.stdout)
233 if result.returncode:
234 raise ValueError(reply.get("error", "The desktop action failed. Read the UI again."))
235 if request.get("screenshot"):
236 reply.update(execute({"action":"shot"}))
237 return reply
238 return linux_ui(request)
239 if action == "process":
240 return process_operation(request)
241 if action == "ui" and (request.get("pid") is not None and (type(request["pid"]) is not int or request["pid"] <= 0)):
27 raise ValueError("Choose a positive application PID, or omit it for the active app.")242 raise ValueError("Choose a positive application PID, or omit it for the active app.")
28 if action == "exec":243 if action in ("exec", "spawn"):
29 if not isinstance(request["script"], str) or not 1 <= len(request["script"].encode()) <= 65536:244 if not isinstance(request.get("script"), str) or not 1 <= len(request["script"].encode()) <= 65536:
30 raise ValueError("Provide a script of at most 64 KiB.")245 raise ValueError("Provide a script of at most 64 KiB.")
31 if type(request["timeout_ms"]) is not int or not 1 <= request["timeout_ms"] <= 180000:246 runtime = request.get("runtime", "default")
247 runtime = ("zsh" if MAC else "bash") if runtime == "default" else runtime
248 shell = shutil.which(runtime) if runtime in ("bash", "zsh") else None
249 if not shell:
250 raise ValueError("Choose a runtime reported by computer_status.")
251 if action == "spawn":
252 if not MAC:
253 unit="snowglobe-computer-"+uuid.uuid4().hex
254 subprocess.run(["systemd-run","--user","--quiet","--collect","--service-type=exec","--unit="+unit,
255 shell,"-c",request["script"]],check=True,timeout=10)
256 pid=subprocess.run(["systemctl","--user","show",unit,"-p","MainPID","--value"],capture_output=True,text=True,timeout=5).stdout.strip()
257 return {"id":unit,"pid":int(pid) if pid.isdigit() else None}
258 process = subprocess.Popen([shell,"-c",request["script"]], stdin=subprocess.DEVNULL,
259 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True)
260 return {"id": str(process.pid) + ":" + process_identity(process.pid), "pid":process.pid}
261 if action == "exec":
262 if type(request.get("timeout_ms")) is not int or not 1 <= request["timeout_ms"] <= 180000:
32 raise ValueError("Choose a timeout between 1 and 180000 milliseconds.")263 raise ValueError("Choose a timeout between 1 and 180000 milliseconds.")
33 if type(request["screenshot"]) is not bool:264 if type(request.get("screenshot")) is not bool:
34 raise ValueError("Choose whether to return a screenshot.")265 raise ValueError("Choose whether to return a screenshot.")
35 if action == "status":266 if action == "status":
36 result = subprocess.run([GUI, "status"], capture_output=True, timeout=10, check=True)267 permissions = {}
37 return {"permissions": json.loads(result.stdout), "hostname": os.uname().nodename,268 if MAC:
38 "platform": "macos", "script": "zsh", "max_timeout_ms": 180000}269 result = subprocess.run([GUI, "status"], capture_output=True, timeout=10, check=True)
270 permissions = json.loads(result.stdout)
271 else:
272 try:
273 import gi
274 gi.require_version("Atspi", "2.0")
275 from gi.repository import Atspi
276 permissions["accessibility"] = Atspi.get_desktop(0) is not None
277 except (ImportError, ValueError):
278 permissions["accessibility"] = False
279 return {"permissions": permissions, "hostname": os.uname().nodename,
280 "platform": "macos" if MAC else "linux", "runtimes": [r for r in ("zsh","bash") if shutil.which(r)],
281 "capabilities": ["shot","input","exec","spawn","process","file"] + (["ui","ui_action"] if permissions.get("accessibility") else []),
282 "max_timeout_ms": 180000, "coordinate_units":"screenshot_pixels"}
39 if action == "ui":283 if action == "ui":
40 args = [GUI, "tree"] + ([] if request["pid"] is None else [str(request["pid"])])284 if not MAC:
285 return linux_ui(request)
286 args = [GUI, "tree"] + ([] if request.get("pid") is None else [str(request["pid"])])
41 result = subprocess.run(args, capture_output=True, timeout=15, check=True)287 result = subprocess.run(args, capture_output=True, timeout=15, check=True)
42 if len(result.stdout) > 2 * 1024 * 1024:288 if len(result.stdout) > 2 * 1024 * 1024:
43 raise ValueError("The UI tree is too large. Select an application PID.")289 raise ValueError("The UI tree is too large. Select an application PID.")
...@@ -48,7 +294,7 @@ def execute(request):...@@ -48,7 +294,7 @@ def execute(request):
48 script = Path(directory) / "script"294 script = Path(directory) / "script"
49 script.write_text(request["script"])295 script.write_text(request["script"])
50 with script.open("rb") as source, selectors.DefaultSelector() as selector:296 with script.open("rb") as source, selectors.DefaultSelector() as selector:
51 process = subprocess.Popen(["/bin/zsh", "-s"], stdin=source,297 process = subprocess.Popen([shell, "-s"], stdin=source,
52 stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)298 stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)
53 streams = {"stdout": bytearray(), "stderr": bytearray()}299 streams = {"stdout": bytearray(), "stderr": bytearray()}
54 truncated = []300 truncated = []
...@@ -77,13 +323,14 @@ def execute(request):...@@ -77,13 +323,14 @@ def execute(request):
77 break323 break
78 finally:324 finally:
79 if process.poll() is None or reply.get("timed_out"):325 if process.poll() is None or reply.get("timed_out"):
80 try:326 if MAC:
81 cleanup = subprocess.run(["/usr/bin/sudo", "-n", "/bin/kill", "-KILL", "--", "-" + str(process.pid)],327 try:
82 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5)328 cleanup = subprocess.run(["/usr/bin/sudo", "-n", "/bin/kill", "-KILL", "--", "-" + str(process.pid)],
83 if cleanup.returncode:329 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5)
330 if cleanup.returncode:
331 reply["cleanup_error"] = "Privileged cleanup failed. Check for remaining child processes."
332 except (OSError, subprocess.SubprocessError):
84 reply["cleanup_error"] = "Privileged cleanup failed. Check for remaining child processes."333 reply["cleanup_error"] = "Privileged cleanup failed. Check for remaining child processes."
85 except (OSError, subprocess.SubprocessError):
86 reply["cleanup_error"] = "Privileged cleanup failed. Check for remaining child processes."
87 try:334 try:
88 os.killpg(process.pid, signal.SIGKILL)335 os.killpg(process.pid, signal.SIGKILL)
89 except (ProcessLookupError, PermissionError):336 except (ProcessLookupError, PermissionError):
...@@ -94,7 +341,7 @@ def execute(request):...@@ -94,7 +341,7 @@ def execute(request):
94 reply.update(exit=process.returncode, **{k: bytes(v).decode("utf-8", "replace") for k, v in streams.items()})341 reply.update(exit=process.returncode, **{k: bytes(v).decode("utf-8", "replace") for k, v in streams.items()})
95 if truncated:342 if truncated:
96 reply["truncated"] = truncated343 reply["truncated"] = truncated
97 if action == "shot" or request.get("screenshot"):344 if MAC and (action == "shot" or request.get("screenshot")):
98 shot = Path(directory) / "screen.png"345 shot = Path(directory) / "screen.png"
99 try:346 try:
100 result = subprocess.run([GUI, "screenshot", str(shot)], capture_output=True, timeout=15, check=True)347 result = subprocess.run([GUI, "screenshot", str(shot)], capture_output=True, timeout=15, check=True)
...@@ -116,19 +363,19 @@ def main():...@@ -116,19 +363,19 @@ def main():
116 if len(raw) > 524288:363 if len(raw) > 524288:
117 raise ValueError("The computer request is too large.")364 raise ValueError("The computer request is too large.")
118 request = json.loads(raw)365 request = json.loads(raw)
119 lock_path = Path.home() / "Library/Caches/net.paperclover.computer.lock"366 lock_path = Path.home() / ("Library/Caches/net.paperclover.computer.lock" if MAC else ".cache/snowglobe-computer.lock")
367 lock_path.parent.mkdir(parents=True, exist_ok=True)
120 descriptor = os.open(lock_path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)368 descriptor = os.open(lock_path, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
121 with os.fdopen(descriptor, "w") as lock:369 with os.fdopen(descriptor, "w") as lock:
122 try:370 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
123 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
124 except BlockingIOError:
125 raise ValueError("Another computer operation is running. Wait for it to finish.")
126 return execute(request)371 return execute(request)
127372
128373
129if __name__ == "__main__":374if __name__ == "__main__":
130 try:375 try:
131 print(json.dumps(main(), ensure_ascii=True))376 print(json.dumps(main(), ensure_ascii=True))
377 except BlockingIOError:
378 print(json.dumps({"error": "Another computer operation is running. Wait for it to finish.", "status": 409}))
132 except ValueError as error:379 except ValueError as error:
133 print(json.dumps({"error": str(error)}))380 print(json.dumps({"error": str(error)}))
134 except (OSError, subprocess.SubprocessError):381 except (OSError, subprocess.SubprocessError):
tools/mac-vm-bridge.py+5
...@@ -77,6 +77,8 @@ def computer(request):...@@ -77,6 +77,8 @@ def computer(request):
77 if not isinstance(value, dict):77 if not isinstance(value, dict):
78 raise ValueError("The Mac guest returned an incomplete response.")78 raise ValueError("The Mac guest returned an incomplete response.")
79 if "error" in value:79 if "error" in value:
80 if value.get("status") == 409:
81 raise BlockingIOError(str(value["error"]))
80 raise ValueError(str(value["error"]))82 raise ValueError(str(value["error"]))
81 return value83 return value
82 finally:84 finally:
...@@ -151,6 +153,9 @@ if __name__ == "__main__":...@@ -151,6 +153,9 @@ if __name__ == "__main__":
151 signal.signal(signal.SIGTERM, disconnected)153 signal.signal(signal.SIGTERM, disconnected)
152 try:154 try:
153 main()155 main()
156 except BlockingIOError as error:
157 respond({"error": str(error), "status": 409})
158 sys.exit(1)
154 except (ValueError, OSError, subprocess.SubprocessError) as error:159 except (ValueError, OSError, subprocess.SubprocessError) as error:
155 print(type(error).__name__, file=sys.stderr)160 print(type(error).__name__, file=sys.stderr)
156 respond({"error": str(error) if isinstance(error, ValueError) else "The Mac VM did not answer. Check its host, then retry."})161 respond({"error": str(error) if isinstance(error, ValueError) else "The Mac VM did not answer. Check its host, then retry."})
tools/mac-vms.py+5-1
...@@ -46,10 +46,14 @@ def connect(request, timeout=10):...@@ -46,10 +46,14 @@ def connect(request, timeout=10):
46 raise ValueError("The Mac response is too large.")46 raise ValueError("The Mac response is too large.")
47 reply = json.loads(read(size))47 reply = json.loads(read(size))
48 if "error" in reply:48 if "error" in reply:
49 if reply.get("status") == 409:
50 raise BlockingIOError(reply["error"])
49 raise ValueError(reply["error"])51 raise ValueError(reply["error"])
50 if "value" not in reply:52 if "value" not in reply:
51 raise ValueError("The Mac response is incomplete.")53 raise ValueError("The Mac response is incomplete.")
52 yield reply["value"], parent54 yield reply["value"], parent
55 except BlockingIOError:
56 raise
53 except OSError as error:57 except OSError as error:
54 raise ValueError("Mac Studio is unreachable. Check its power and network, then retry.") from error58 raise ValueError("Mac Studio is unreachable. Check its power and network, then retry.") from error
55 finally:59 finally:
...@@ -72,7 +76,7 @@ def domain():...@@ -72,7 +76,7 @@ def domain():
72 if not CONFIG:76 if not CONFIG:
73 return []77 return []
74 value = {"name": CONFIG["name"], **ownership(), "host": "Mac Studio", "arch": "aarch64",78 value = {"name": CONFIG["name"], **ownership(), "host": "Mac Studio", "arch": "aarch64",
75 "description": "macOS development", "state": "unreachable", "reason": None,79 "state": "unreachable", "reason": None,
76 "os": "macOS", "vcpus": None, "memory": None, "balloon": None, "pinned": None,80 "os": "macOS", "vcpus": None, "memory": None, "balloon": None, "pinned": None,
77 "autostart": False, "startedAt": None, "agent": None, "setup": None, "pendingAction": None,81 "autostart": False, "startedAt": None, "agent": None, "setup": None, "pendingAction": None,
78 "disks": [], "media": [], "firmware": "uefi", "platform": "macos", "console": True,82 "disks": [], "media": [], "firmware": "uefi", "platform": "macos", "console": True,
tools/release.py+1-1
...@@ -60,7 +60,7 @@ def host_digest(root):...@@ -60,7 +60,7 @@ def host_digest(root):
60 digest = hashlib.sha256()60 digest = hashlib.sha256()
61 paths = sorted(path for path, relative in files(root)61 paths = sorted(path for path, relative in files(root)
62 if relative.parts[0] in {"nixos", "dashboard", "config", "service"}62 if relative.parts[0] in {"nixos", "dashboard", "config", "service"}
63 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"})63 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"})
64 for path in paths:64 for path in paths:
65 digest.update(str(path.relative_to(root)).encode())65 digest.update(str(path.relative_to(root)).encode())
66 _hash_contents(digest, path)66 _hash_contents(digest, path)
tools/vm-ai.py created+132
...@@ -0,0 +1,132 @@
1"""Owner-scoped disposable VMs and idle expiry."""
2import fcntl
3import json
4import os
5from pathlib import Path
6import re
7import shutil
8import sys
9import time
10import uuid
11import xml.etree.ElementTree as ET
12
13import vms
14
15LIMITS = {"per_user": 2, "host_total": 8, "vcpus": 4, "memory_gib": 8,
16 "idle_seconds": 3600, "extension_seconds": 86400}
17
18
19def presets():
20 return [p for p in vms.library()["presets"] if p["capacity"] <= 80 * 2**30 and
21 (p["platform"] == "linux" or p.get("setup") in {"windows-v3", "windows-v4"})]
22
23
24def lease(name, seconds=None):
25 vms.ensure_name(name)
26 path = vms.DISKS / name / "ai.json"
27 try:
28 with os.fdopen(vms.open_regular(path)) as incoming:
29 value = json.load(incoming)
30 except FileNotFoundError:
31 return None
32 if not name.startswith("ai-") or type(value.get("expiresAt")) is not int:
33 raise ValueError("The test VM's expiry record needs repair.")
34 if seconds is not None:
35 now = int(time.time())
36 if value["expiresAt"] <= now:
37 raise ValueError("This test VM expired. Create another VM.")
38 value["expiresAt"] = max(value["expiresAt"], now + seconds)
39 temporary = path.with_suffix(".new")
40 with open(temporary, "w", opener=lambda p, flags: os.open(p, flags | os.O_NOFOLLOW, 0o600)) as outgoing:
41 json.dump(value, outgoing)
42 os.replace(temporary, path)
43 return value
44
45
46def create(request):
47 if set(request) != {"action", "owner", "preset", "name", "vcpus", "memory"}:
48 raise ValueError("Use the fields listed for creating a test VM.")
49 if not isinstance(request["owner"], str) or not re.fullmatch(r"[a-zA-Z0-9_-]{1,128}", request["owner"]):
50 raise ValueError("Choose a signed-in account to own this VM.")
51 if not isinstance(request["name"], str) or not re.fullmatch(r"[a-z0-9][a-z0-9-]{0,35}", request["name"]):
52 raise ValueError("Use a name of 1–36 lowercase letters, numbers, or hyphens.")
53 if type(request["vcpus"]) is not int or not 1 <= request["vcpus"] <= LIMITS["vcpus"]:
54 raise ValueError("Choose between 1 and 4 vCPUs.")
55 if type(request["memory"]) is not int or not 1 <= request["memory"] <= LIMITS["memory_gib"]:
56 raise ValueError("Choose between 1 and 8 GiB of memory.")
57 preset = next((p for p in presets() if p["id"] == request["preset"]), None)
58 if preset is None:
59 raise ValueError("Choose a prepared preset from computer_presets.")
60 with open(vms.DISKS / "ai-create.lock", "w", opener=lambda p, flags: os.open(p, flags | os.O_NOFOLLOW, 0o600)) as lock:
61 fcntl.flock(lock, fcntl.LOCK_EX)
62 owned = total = 0
63 for name in vms.virsh("list", "--all", "--name").splitlines():
64 if not lease(name):
65 continue
66 total += 1
67 root = ET.fromstring(vms.virsh("dumpxml", name))
68 owned += root.findtext(f"metadata/{vms.NS}vm/{vms.NS}owner") == request["owner"]
69 if owned >= LIMITS["per_user"] or total >= LIMITS["host_total"]:
70 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.")
71 if shutil.disk_usage(vms.DISKS).free < preset["capacity"] + 10 * 2**30:
72 raise ValueError("There is not enough free space for another test VM. Delete an unused test VM.")
73 name = "ai-" + request["name"].removeprefix("ai-") + "-" + uuid.uuid4().hex[:8]
74 spec = {"name": name, "description": "", "image": preset["id"],
75 "vcpus": request["vcpus"], "memory": request["memory"] * 2**30,
76 "disk": preset["capacity"], "autostart": False, "start": True,
77 "mode": "preset", "firmware": preset["firmware"], "platform": preset["platform"],
78 "owner": request["owner"], "username": "sandbox"}
79 vms.validate("create", spec)
80 vms.create(spec, ai=True)
81 return {"id": name, "name": name, "platform": preset["platform"], "os": preset["os"],
82 "arch": preset["arch"], "aiOwned": True, **lease(name)}
83
84
85def manage(request):
86 if request.get("action") == "presets" and set(request) == {"action", "owner"}:
87 return {"presets": [{"id": p["id"], "os": p["os"], "platform": p["platform"],
88 "arch": p["arch"], "disk_bytes": p["capacity"]} for p in presets()], "limits": LIMITS}
89 if request.get("action") == "create":
90 return create(request)
91 if set(request) != {"action", "name", "owner", "seconds"} or request["action"] not in {"extend", "delete"}:
92 raise ValueError("Choose a supported test VM operation.")
93 name = request["name"]
94 vms.ensure_name(name)
95 if type(request["seconds"]) is not int or not 1 <= request["seconds"] <= LIMITS["extension_seconds"]:
96 raise ValueError("Extend a test VM by up to 24 hours from now.")
97 with vms.computer_lock(name):
98 root = ET.fromstring(vms.virsh("dumpxml", name))
99 if root.findtext(f"metadata/{vms.NS}vm/{vms.NS}owner") != request["owner"] or not lease(name):
100 raise ValueError("Choose a test VM owned by your account.")
101 if request["action"] == "extend":
102 return {"id": name, "aiOwned": True, **lease(name, request["seconds"])}
103 vms.remove({"name": name, "disks": True})
104 return {"id": name, "deleted": True}
105
106
107def reap():
108 for directory in vms.DISKS.glob("ai-*"):
109 if directory.is_symlink() or not directory.is_dir():
110 continue
111 try:
112 with vms.computer_lock(directory.name):
113 value = lease(directory.name)
114 if value and value["expiresAt"] <= time.time():
115 vms.remove({"name": directory.name, "disks": True}, missing_ok=True)
116 print("Expired test VM:", directory.name, flush=True)
117 except BlockingIOError:
118 continue
119 except FileNotFoundError:
120 if directory.exists():
121 raise
122
123
124if __name__ == "__main__":
125 try:
126 if sys.argv[1:] == ["reap"]:
127 reap()
128 else:
129 print(json.dumps(manage(json.load(sys.stdin))))
130 except (ValueError, OSError) as error:
131 print(str(error), file=sys.stderr)
132 sys.exit(2)
tools/vm-computer.py created+556
...@@ -0,0 +1,556 @@
1"""Bounded computer operations over the existing private VM transports."""
2import asyncio
3import base64
4import contextlib
5from contextvars import ContextVar
6import hashlib
7from importlib import import_module
8import json
9import os
10from pathlib import Path
11import shlex
12import subprocess
13import sys
14import threading
15import time
16import uuid
17import xml.etree.ElementTree as ET
18
19import vms
20
21rfb = import_module('vm-rfb')
22SCREEN_SLOT = threading.BoundedSemaphore(1)
23DEADLINE = ContextVar('computer_deadline', default=float('inf'))
24
25
26def remaining(limit):
27 seconds = min(limit, DEADLINE.get() - time.monotonic())
28 if seconds <= 0:
29 raise TimeoutError('The input batch timed out. Check the desktop before continuing.')
30 return seconds
31
32
33def qga(name, operation, **arguments):
34 payload = json.dumps({'execute': operation, 'arguments': arguments})
35 # Virsh reads stdin so guest scripts and file contents never enter host argv.
36 command = 'qemu-agent-command ' + shlex.quote(name) + ' --timeout 10 ' + shlex.quote(payload) + '\n'
37 result = subprocess.run(['virsh', '--quiet', '--connect', 'qemu:///system'], input=command,
38 capture_output=True, text=True, timeout=remaining(15))
39 for line in result.stdout.splitlines():
40 if line.startswith('{'):
41 value = json.loads(line)
42 if 'return' in value:
43 return value['return']
44 if operation == 'guest-exec-status' and result.stderr.rstrip().endswith((
45 f"PID {arguments['pid']} does not exist", 'PID ld does not exist', 'PID lld does not exist', "Invalid parameter 'pid'")):
46 raise ProcessLookupError('The guest process result is unavailable. Check the VM before retrying.')
47 raise ValueError('The guest agent did not answer. Wait for the VM to finish starting, then retry.')
48
49
50def guest_exec(name, path, args, data=None, timeout=20):
51 arguments = {'path': path, 'arg': args, 'capture-output': True}
52 if data is not None:
53 arguments['input-data'] = base64.b64encode(data).decode()
54 pid = qga(name, 'guest-exec', **arguments)['pid']
55 deadline = time.monotonic() + remaining(timeout)
56 finished = None
57 while time.monotonic() < deadline:
58 try:
59 result = qga(name, 'guest-exec-status', pid=pid)
60 except ProcessLookupError:
61 if finished is None:
62 raise
63 if finished.get('exitcode') != 0:
64 raise ValueError('The guest worker could not run. Check the desktop session and installed computer tools.')
65 if finished.get('out-truncated') or finished.get('err-truncated'):
66 raise ValueError('The guest response was truncated. Narrow the request.')
67 return base64.b64decode(finished.get('out-data', ''), validate=True)
68 # QGA retains completed records across PID reuse and returns the oldest first.
69 if result.get('exited'):
70 finished = result
71 continue
72 finished = None
73 time.sleep(.15)
74 raise ValueError('The guest operation is still running. Check its status before retrying.')
75
76
77def read_file(name, path, offset=0, length=65536):
78 handle = qga(name, 'guest-file-open', path=path, mode='rb')
79 try:
80 qga(name, 'guest-file-seek', handle=handle, offset=offset, whence=0)
81 value = qga(name, 'guest-file-read', handle=handle, count=length)
82 return base64.b64decode(value['buf-b64'], validate=True)
83 finally:
84 qga(name, 'guest-file-close', handle=handle)
85
86
87def write_file(name, path, data, offset=0):
88 handle = qga(name, 'guest-file-open', path=path, mode='r+b' if offset else 'wb')
89 try:
90 if offset:
91 end = qga(name, 'guest-file-seek', handle=handle, offset=0, whence=2)
92 if end['position'] != offset:
93 raise ValueError('The transfer offset changed. Start a new upload.')
94 for start in range(0, len(data), 49152):
95 chunk = data[start:start + 49152]
96 result = qga(name, 'guest-file-write', handle=handle, **{'buf-b64': base64.b64encode(chunk).decode()})
97 if result['count'] != len(chunk):
98 raise ValueError('The guest disk could not accept the file. Check free space.')
99 finally:
100 qga(name, 'guest-file-close', handle=handle)
101
102
103def powershell(name, script, timeout=20):
104 """Windows 7 PowerShell needs a hidden console when launched by QGA."""
105 stem = 'C:\\Windows\\Temp\\snowglobe-computer-' + uuid.uuid4().hex
106 command = 'cmd.exe /d /c powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File ' + stem + '.ps1'
107 command += ' > ' + stem + '.out 2> ' + stem + '.err'
108 launcher = '''Set shell = CreateObject("WScript.Shell")
109code = shell.Run("COMMAND", 0, True)
110Set stream = CreateObject("ADODB.Stream")
111stream.Type = 1
112stream.Open
113stream.LoadFromFile "STEM.out"
114Set xml = CreateObject("Msxml2.DOMDocument.6.0")
115Set node = xml.createElement("output")
116node.DataType = "bin.base64"
117node.nodeTypedValue = stream.Read
118WScript.StdOut.Write Replace(Replace(node.Text, vbCr, ""), vbLf, "")
119stream.Close
120Set files = CreateObject("Scripting.FileSystemObject")
121files.DeleteFile "STEM.out"
122files.DeleteFile "STEM.err"
123files.DeleteFile "STEM.ps1"
124files.DeleteFile WScript.ScriptFullName
125WScript.Quit code
126'''.replace('COMMAND', command).replace('STEM', stem)
127 write_file(name, stem + '.ps1', b'\xff\xfe' + script.encode('utf-16le'))
128 write_file(name, stem + '.vbs', launcher.encode('ascii'))
129 return base64.b64decode(guest_exec(name, 'cscript.exe', ['//nologo', stem + '.vbs'], timeout=timeout), validate=True)
130
131
132async def desktop(name, actions=None, platform=None, screenshot=True, native_text=True):
133 if not SCREEN_SLOT.acquire(blocking=False):
134 raise ValueError('Another screen capture is running. Retry in a moment.')
135 token = DEADLINE.set(min(DEADLINE.get(), time.monotonic() + 180))
136 try:
137 return await asyncio.wait_for(capture(name, actions, platform, screenshot, native_text), remaining(180))
138 finally:
139 DEADLINE.reset(token)
140 SCREEN_SLOT.release()
141
142
143async def capture(name, actions, platform, screenshot=True, native_text=True):
144 target = vms.console_target(name, timeout=remaining(10))
145 reader, writer = await asyncio.wait_for(asyncio.open_connection('127.0.0.1', target['port']), 10)
146 screen = rfb.Desktop(reader, writer, lambda _: None)
147 task = None
148 try:
149 await asyncio.wait_for(screen.start(), 10)
150 ready = asyncio.Event()
151 task = asyncio.create_task(screen.read(ready.set, ready.clear))
152 await asyncio.wait_for(ready.wait(), 10)
153 if actions:
154 for action in actions:
155 kind = action['type']
156 if kind in ('move', 'click', 'drag', 'scroll'):
157 x, y = action['x'], action['y']
158 if not 0 <= x < screen.width or not 0 <= y < screen.height:
159 raise ValueError('Choose coordinates inside the latest screenshot.')
160 screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0})
161 button = {'left': 1, 'middle': 2, 'right': 4}.get(action.get('button', 'left'))
162 if kind == 'click':
163 for _ in range(action.get('count', 1)):
164 screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': button})
165 await asyncio.sleep(.04)
166 screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0})
167 await asyncio.sleep(.06)
168 elif kind == 'drag':
169 end_x, end_y = action['to_x'], action['to_y']
170 if not 0 <= end_x < screen.width or not 0 <= end_y < screen.height:
171 raise ValueError('Choose drag coordinates inside the latest screenshot.')
172 screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': button})
173 for step in range(1, 21):
174 screen.input({'type': 'pointer', 'x': round(x+(end_x-x)*step/20),
175 'y': round(y+(end_y-y)*step/20), 'buttons': button})
176 await asyncio.sleep(action.get('duration_ms', 400)/20000)
177 screen.input({'type': 'release'})
178 elif kind == 'scroll':
179 wheel = {'up': 8, 'down': 16, 'left': 32, 'right': 64}[action['direction']]
180 for _ in range(action.get('amount', 3)):
181 screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': wheel})
182 screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0})
183 await asyncio.sleep(.02)
184 elif kind == 'key':
185 keys = [keycode(key) for key in action['keys']]
186 for key in keys:
187 screen.input({'type': 'key', 'key': key, 'down': True})
188 await asyncio.sleep(.04)
189 for key in reversed(keys):
190 screen.input({'type': 'key', 'key': key, 'down': False})
191 elif kind == 'text':
192 if native_text:
193 pending = asyncio.create_task(asyncio.to_thread(worker,name,platform,{'action':'input','actions':[action],'screenshot':False}))
194 try:
195 await asyncio.shield(pending)
196 finally:
197 # Cancellation cannot release the VM lock while its worker still runs.
198 with contextlib.suppress(Exception):
199 await pending
200 else:
201 text = action['text']
202 if any(not (32 <= ord(char) <= 126 or char in '\n\r\t') for char in text):
203 raise ValueError('This legacy guest supports ASCII text only. Use key actions for other keyboard input.')
204 shifted = dict(zip('~!@#$%^&*()_+{}|:"<>?', '`1234567890-=[]\\;\',./'))
205 for char in text.replace('\r\n', '\n'):
206 shift = char in shifted
207 base = shifted.get(char, char)
208 key = {'\n': 0xff0d, '\r': 0xff0d, '\t': 0xff09}.get(base, ord(base))
209 if shift:
210 screen.input({'type': 'key', 'key': 0xffe1, 'down': True})
211 screen.input({'type': 'key', 'key': key, 'down': True})
212 await writer.drain()
213 await asyncio.sleep(.01)
214 screen.input({'type': 'key', 'key': key, 'down': False})
215 if shift:
216 screen.input({'type': 'key', 'key': 0xffe1, 'down': False})
217 await writer.drain()
218 await asyncio.sleep(.01)
219 elif kind == 'wait':
220 await asyncio.sleep(action['duration_ms']/1000)
221 await writer.drain()
222 screen.input({'type': 'release'})
223 await asyncio.wait_for(writer.drain(), 5)
224 if not screenshot:
225 return {'completed': len(actions)}
226 await asyncio.sleep(.2)
227 # Force a full update even when the desktop has stopped painting.
228 ready.clear()
229 screen.request(False)
230 await writer.drain()
231 await asyncio.wait_for(ready.wait(), 10)
232 task.cancel()
233 with contextlib.suppress(asyncio.CancelledError):
234 await task
235 task = None
236 return {'width': screen.width, 'height': screen.height, 'bounds': [0,0,screen.width,screen.height],
237 'scale': [1,1], 'png': rfb.png(screen.width, screen.height, screen.pixels, bgra=True).split(',',1)[1]}
238 finally:
239 with contextlib.suppress(Exception):
240 screen.input({'type': 'release'})
241 await asyncio.wait_for(writer.drain(), 5)
242 if task:
243 task.cancel()
244 with contextlib.suppress(asyncio.CancelledError, Exception):
245 await task
246 writer.close()
247 await asyncio.wait_for(writer.wait_closed(), 5)
248
249
250def keycode(key):
251 names = {'Enter': 0xff0d, 'Tab': 0xff09, 'Escape': 0xff1b, 'Backspace': 0xff08,
252 'Delete': 0xffff, 'Insert': 0xff63, 'Home': 0xff50, 'End': 0xff57,
253 'PageUp': 0xff55, 'PageDown': 0xff56, 'Left': 0xff51, 'Up': 0xff52,
254 'Right': 0xff53, 'Down': 0xff54, 'Shift': 0xffe1, 'Control': 0xffe3,
255 'Alt': 0xffe9, 'Meta': 0xffeb, 'Space': 0x20}
256 names.update({'F'+str(i): 0xffbd+i for i in range(1,25)})
257 if key in names:
258 return names[key]
259 if len(key) == 1 and 32 <= ord(key) <= 126:
260 return ord(key)
261 raise ValueError('Use a named key or one ASCII character.')
262
263
264def validate(request):
265 if not isinstance(request, dict):
266 raise ValueError('Send a computer operation.')
267 action = request.get('action')
268 fields = {'status': set(), 'shot': set(), 'ui': {'pid'}, 'ui_action': {'target','operation','value','screenshot'},
269 'exec': {'script','runtime','timeout_ms','screenshot'}, 'input': {'actions','screenshot'},
270 'spawn': {'script','runtime'}, 'process': {'id','operation'},
271 'file': {'operation','path','offset','data','length','destination','overwrite'}}
272 if not isinstance(action,str) or action not in fields or set(request)-{'action'}-fields[action]:
273 raise ValueError('Use the fields listed for this computer operation.')
274 if action in ('exec','spawn'):
275 if not isinstance(request.get('script'),str) or not 1 <= len(request['script'].encode()) <= 65536:
276 raise ValueError('Provide a script of at most 64 KiB.')
277 if request.get('runtime') not in (None,'default','bash','zsh','powershell','cmd','ahk'):
278 raise ValueError('Choose a runtime reported by computer_status.')
279 if action == 'exec' and (type(request.get('timeout_ms')) is not int or not 1 <= request['timeout_ms'] <= 180000):
280 raise ValueError('Choose a timeout between 1 and 180000 milliseconds.')
281 if 'screenshot' in request and type(request['screenshot']) is not bool:
282 raise ValueError('Choose whether to return a screenshot.')
283 if action == 'ui' and request.get('pid') is not None and (type(request['pid']) is not int or request['pid']<=0):
284 raise ValueError('Choose a positive application PID.')
285 if action == 'ui_action':
286 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:
287 raise ValueError('Use a target and action returned by computer_ui.')
288 if 'value' in request and (not isinstance(request['value'],str) or len(request['value'].encode())>16384):
289 raise ValueError('Set a value of at most 16 KiB.')
290 if action == 'file':
291 path=request.get('path')
292 if not isinstance(path,str) or not 1<=len(path)<=4096 or '\x00' in path:
293 raise ValueError('Use an absolute guest file path.')
294 operation=request.get('operation')
295 if operation not in ('stat','list','mkdir','remove','read','write','commit'):
296 raise ValueError('Choose a supported file operation.')
297 if operation in ('read','write'):
298 offset=request.get('offset',0)
299 if type(offset) is not int or not 0<=offset<=64*2**20:
300 raise ValueError('Choose a transfer offset within 64 MiB.')
301 if operation=='read':
302 length=request.get('length',65536)
303 if type(length) is not int or not 1<=length<=65536: raise ValueError('Read at most 64 KiB at a time.')
304 else:
305 if not isinstance(request.get('data'),str) or len(request['data'])>90000 or len(base64.b64decode(request['data'],validate=True))>65536:
306 raise ValueError('Write at most 64 KiB at a time.')
307 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):
308 raise ValueError('Choose an absolute guest destination and whether to replace it.')
309 if action == 'process' and (not isinstance(request.get('id'),str) or len(request['id'])>128 or request.get('operation') not in ('status','stop')):
310 raise ValueError('Use the process handle returned by computer_spawn.')
311 if action == 'input':
312 actions = request.get('actions')
313 if not isinstance(actions,list) or not 1 <= len(actions) <= 100:
314 raise ValueError('Send between 1 and 100 desktop actions.')
315 for item in actions:
316 if not isinstance(item,dict):
317 raise ValueError('Send a desktop action object.')
318 kind = item.get('type')
319 required = {'move': {'x','y'}, 'click': {'x','y'}, 'drag': {'x','y','to_x','to_y'},
320 'scroll': {'x','y','direction'}, 'key': {'keys'}, 'text': {'text'}, 'wait': {'duration_ms'}}
321 optional = {'click': {'button','count'}, 'drag': {'button','duration_ms'}, 'scroll': {'amount'}}
322 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()):
323 raise ValueError('Use the fields listed for this desktop action.')
324 for key in ('x','y','to_x','to_y'):
325 if key in item and (type(item[key]) is not int or not 0 <= item[key] <= 16384):
326 raise ValueError('Use nonnegative screenshot pixel coordinates.')
327 if 'button' in item and item['button'] not in ('left','right','middle'):
328 raise ValueError('Choose the left, right, or middle mouse button.')
329 for key,maximum in [('count',3),('amount',100),('duration_ms',5000)]:
330 if key in item and (type(item[key]) is not int or not 1 <= item[key] <= maximum):
331 raise ValueError('The desktop action exceeds its limit.')
332 if kind == 'scroll' and item['direction'] not in ('up','down','left','right'):
333 raise ValueError('Choose a scroll direction.')
334 if kind == 'key':
335 if not isinstance(item['keys'],list) or not 1 <= len(item['keys']) <= 8 or any(not isinstance(k,str) for k in item['keys']):
336 raise ValueError('Send between 1 and 8 key names.')
337 for key in item['keys']: keycode(key)
338 if kind == 'text' and (not isinstance(item['text'],str) or len(item['text'].encode()) > 16384):
339 raise ValueError('Type at most 16 KiB of text at a time.')
340 if sum(a.get('duration_ms',0) for a in actions) > 15000:
341 raise ValueError('Keep total waits and drags under 15 seconds.')
342
343
344def record_operation(name, operation):
345 path = vms.DISKS / name / 'computer-operation.json'
346 temporary = path.with_suffix('.new')
347 with open(temporary, 'w', opener=lambda p, flags: os.open(p, flags | os.O_NOFOLLOW, 0o600)) as outgoing:
348 json.dump(operation, outgoing)
349 os.replace(temporary, path)
350
351
352def finish_operation(name, operation):
353 token = DEADLINE.set(time.monotonic() + 20)
354 try:
355 if operation['platform'] == 'linux':
356 guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [],
357 json.dumps({'_stop': operation['id']}).encode(), timeout=18)
358 else:
359 run = operation['id']
360 directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run
361 cleanup = "$ErrorActionPreference='Stop';$d='"+directory+"';"
362 cleanup += "if(Test-Path -LiteralPath ($d+'\\request.json')){Remove-Item -LiteralPath ($d+'\\request.json') -Force};"
363 cleanup += "$s=New-Object -ComObject Schedule.Service;$s.Connect();$f=$s.GetFolder('\\');"
364 cleanup += "$t=$f.GetTasks(1) | Where-Object {$_.Name -eq 'SnowglobeComputer-"+run+"'};if($t){"
365 if not operation.get('keep_spawn'):
366 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}});"
367 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;"
368 cleanup += "$stop=[Diagnostics.Process]::Start($info);if(!$stop.WaitForExit(12000) -or $stop.ExitCode -ne 0){throw 'Process cleanup failed'};$stop.Dispose();"
369 cleanup += "$t.Stop(0);while($t.State -eq 4){Start-Sleep -Milliseconds 100};"
370 cleanup += "foreach($p in $workers){if(!$p.WaitForExit(10000)){throw 'Process cleanup failed'};$p.Dispose()};"
371 cleanup += "$f.DeleteTask('SnowglobeComputer-"+run+"',0)};"
372 if not operation.get('keep_spawn'):
373 cleanup += "if(Test-Path -LiteralPath $d){Remove-Item -LiteralPath $d -Recurse -Force}"
374 powershell(name, cleanup, timeout=18)
375 (vms.DISKS / name / 'computer-operation.json').unlink()
376 except Exception as error:
377 raise BlockingIOError('The previous operation has not stopped. Check its screenshot, then retry when the guest agent reconnects.') from error
378 finally:
379 DEADLINE.reset(token)
380
381
382def worker(name, platform, request):
383 if platform == 'macos':
384 source = Path(__file__).resolve().parent.parent / 'guest/computer-legacy-mac.py'
385 bootstrap = "import json,sys; request=json.load(sys.stdin); exec(compile(request.pop('_worker'),'<computer>','exec'))"
386 environment = dict(os.environ, SSH_ORIGINAL_COMMAND='/usr/bin/python -c ' + shlex.quote(bootstrap))
387 result = subprocess.run([sys.executable, str(Path(__file__).with_name('vm-ssh.py')), name],
388 input=json.dumps({**request, '_worker':source.read_text()}).encode(),
389 capture_output=True, timeout=remaining(200), env=environment)
390 if result.returncode:
391 raise ValueError('The private guest connection failed. Wait for desktop setup to finish, then retry.')
392 reply = json.loads(result.stdout)
393 if 'error' in reply: raise ValueError(reply['error'])
394 return reply
395 if platform == 'linux':
396 source = Path(__file__).with_name('mac-computer.py').read_text()
397 unit = 'snowglobe-computer-request-' + uuid.uuid4().hex
398 guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [],
399 json.dumps({'_prepare': unit}).encode())
400 operation = {'platform': platform, 'id': unit}
401 record_operation(name, operation)
402 try:
403 raw = guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [],
404 json.dumps({**request, '_worker': source, '_unit': unit,
405 '_timeout': max(1, int(remaining(200)) - 5)}).encode(), timeout=220)
406 except BaseException:
407 finish_operation(name, operation)
408 raise
409 (vms.DISKS / name / 'computer-operation.json').unlink()
410 result = json.loads(raw)
411 if 'error' in result:
412 if result.get('status') == 409:
413 raise BlockingIOError(result['error'])
414 raise ValueError(result['error'])
415 return result
416 if platform == 'windows':
417 run = uuid.uuid4().hex
418 directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run
419 setup = "$d='" + directory + "'; New-Item -ItemType Directory -Force -Path $d | Out-Null; Test-Path C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe"
420 installed=powershell(name,setup).strip()==b'True'
421 ahk=os.environ.get('SNOWGLOBE_COMPUTER_AHK')
422 if not installed and ahk:
423 write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe',(Path(ahk)/'AutoHotkey32.exe').read_bytes())
424 write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey-license.txt',(Path(ahk)/'license.txt').read_bytes())
425 write_file(name,directory+'\\request.json',json.dumps(request).encode())
426 source = Path(__file__).resolve().parent.parent / 'guest/computer-windows.cs'
427 content = source.read_bytes()
428 digest = hashlib.sha256(content).hexdigest()[:16]
429 installed_worker = 'C:\\ProgramData\\SnowglobeComputer\\worker-' + digest + '.exe'
430 exists = powershell(name, "Test-Path -LiteralPath '" + installed_worker + "'").strip() == b'True'
431 if not exists:
432 source_path = directory + '\\worker.cs'
433 write_file(name, source_path, content)
434 compile_script = r"""
435$ErrorActionPreference='Stop'
436$lock=New-Object Threading.Mutex($false,'Global\SnowglobeComputerCompile-HASH')
437if(!$lock.WaitOne(0)){$lock.Dispose();Write-Output 'pending';return}
438try {
439if(Test-Path -LiteralPath 'WORKER'){return}
440$references=@('System.dll','System.Core.dll')
441foreach($assembly in @('System.Web.Extensions','UIAutomationClient','UIAutomationTypes','WindowsBase')) {
442 $references += [Reflection.Assembly]::LoadWithPartialName($assembly).Location
443}
444Remove-Item -LiteralPath 'WORKER.new.exe' -Force -ErrorAction SilentlyContinue
445Add-Type -TypeDefinition ([IO.File]::ReadAllText('SOURCE')) -ReferencedAssemblies $references -OutputAssembly 'WORKER.new.exe' -OutputType WindowsApplication
446Move-Item -LiteralPath 'WORKER.new.exe' -Destination 'WORKER'
447} finally {$lock.ReleaseMutex();$lock.Dispose()}
448""".replace('SOURCE', source_path).replace('WORKER', installed_worker).replace('HASH',digest)
449 if powershell(name, compile_script, timeout=60).strip() == b'pending':
450 raise ValueError('Computer tools are still installing. Retry in a moment.')
451 launch = """
452$ErrorActionPreference='Stop'
453$d='DIRECTORY'
454$user=(Get-WmiObject Win32_ComputerSystem).UserName
455if(!$user){throw 'No interactive user'}
456$service=New-Object -ComObject Schedule.Service
457$service.Connect()
458$task=$service.NewTask(0)
459$task.Principal.UserId=$user
460$task.Principal.LogonType=3
461$task.Principal.RunLevel=1
462$task.Settings.ExecutionTimeLimit='TASKLIMIT'
463$task.Settings.DisallowStartIfOnBatteries=$false
464$task.Settings.StopIfGoingOnBatteries=$false
465$action=$task.Actions.Create(0)
466$action.Path='WORKER'
467$action.Arguments='"'+$d+'\\request.json" "'+$d+'\\response.json"'
468$registered=$service.GetFolder('\\').RegisterTaskDefinition('SnowglobeComputer-RUN',$task,6,$user,$null,3)
469$registered.Run($null) | Out-Null
470""".replace('DIRECTORY',directory).replace('RUN',run).replace('WORKER',installed_worker).replace('TASKLIMIT','PT0S' if request['action'] == 'spawn' else 'PT210S')
471 operation = {'platform': platform, 'id': run}
472 record_operation(name, operation)
473 try:
474 powershell(name,launch)
475 deadline=time.monotonic()+remaining(200)
476 while time.monotonic()<deadline:
477 try:
478 raw=read_file(name,directory+'\\response.json',length=2*1024*1024)
479 result=json.loads(raw)
480 if 'error' in result: raise RuntimeError(result['error'])
481 if request['action'] == 'spawn':
482 record_operation(name, {**operation, 'keep_spawn': True})
483 operation['keep_spawn'] = True
484 return result
485 except ValueError:
486 time.sleep(.2)
487 raise ValueError('The desktop worker timed out. Check the VM before retrying.')
488 except RuntimeError as error:
489 raise ValueError(str(error)) from None
490 finally:
491 try:
492 finish_operation(name, operation)
493 except BlockingIOError:
494 if not operation.get('keep_spawn'):
495 raise
496 raise ValueError('This guest needs its platform worker installed.')
497
498
499
500def handle(name, request):
501 validate(request)
502 vms.ensure_name(name)
503 if vms.mac_vms.CONFIG and name == vms.mac_vms.CONFIG['name']:
504 return vms.mac_vms.handle('computer', {'name':name,'request':request})
505 token = DEADLINE.set(time.monotonic() + 210)
506 try:
507 with vms.computer_lock(name):
508 root = ET.fromstring(vms.virsh('dumpxml',name,timeout=remaining(15)))
509 if vms.virsh('domstate',name,timeout=remaining(15)).strip() != 'running':
510 raise ValueError('Start the VM before using its computer tools.')
511 platform = root.findtext(f'metadata/{vms.NS}vm/{vms.NS}platform','linux')
512 ai = import_module('vm-ai')
513 ai.lease(name, ai.LIMITS['idle_seconds'])
514 if request['action'] == 'shot':
515 return asyncio.run(desktop(name))
516 try:
517 with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer-operation.json')) as incoming:
518 unfinished = json.load(incoming)
519 except FileNotFoundError:
520 pass
521 else:
522 finish_operation(name, unfinished)
523 native_text = True
524 if platform == 'windows' and (request['action'] == 'status' or request['action'] == 'input' and any(a['type'] == 'text' for a in request['actions'])):
525 try:
526 info = qga(name, 'guest-info')
527 native_text = any(command['name'] == 'guest-exec' and command['enabled'] for command in info['supported_commands'])
528 except (ValueError, subprocess.TimeoutExpired):
529 native_text = False
530 if request['action'] == 'status':
531 result = {'platform':platform,'capabilities':['shot','input'],'coordinate_units':'screenshot_pixels'}
532 if not native_text:
533 return {**result, 'runtimes': [], 'text_encoding': 'ascii', 'worker_error': 'Desktop input is available. Commands, Unicode text and UI actions need a working guest agent with command support.'}
534 try:
535 result.update(worker(name,platform,request))
536 except ValueError as error:
537 result['worker_error'] = str(error)
538 return result
539 if request['action'] == 'input':
540 return asyncio.run(desktop(name,request['actions'],platform,request.get('screenshot',True),native_text))
541 if platform=='windows' and request['action']=='file' and request['operation'] in ('read','write'):
542 path=request['path']
543 if len(path)<3 or path[1:3] not in (':\\',':/') or not path[0].isalpha(): raise ValueError('Use an absolute guest file path.')
544 if request['operation']=='read':
545 data=read_file(name,path,request.get('offset',0),request.get('length',65536))
546 return {'data':base64.b64encode(data).decode(),'bytes':len(data)}
547 data=base64.b64decode(request['data'],validate=True)
548 write_file(name,path,data,offset=request.get('offset',0))
549 return {'bytes':len(data)}
550 result = worker(name,platform,request)
551 if request.get('screenshot'):
552 try: result.update(asyncio.run(desktop(name)))
553 except (ValueError,OSError,TimeoutError): result['capture_error']='Screen capture failed. Retry computer_shot.'
554 return result
555 finally:
556 DEADLINE.reset(token)
tools/vm-rfb.py created+160
...@@ -0,0 +1,160 @@
1import base64
2import struct
3import zlib
4
5MAX_PIXELS = 3840 * 2160
6
7
8def png(width, height, pixels, *, bgra=False):
9 def chunk(kind, data):
10 return struct.pack("!I", len(data)) + kind + data + struct.pack("!I", zlib.crc32(kind + data))
11 compressor = zlib.compressobj()
12 compressed = bytearray()
13 for row in range(height):
14 pixels_row = bytearray(pixels[row * width * 4:(row + 1) * width * 4])
15 if bgra:
16 pixels_row[0::4], pixels_row[2::4], pixels_row[3::4] = pixels_row[2::4], pixels_row[0::4], b'\xff' * width
17 compressed.extend(compressor.compress(b'\0' + pixels_row))
18 if len(compressed) > 8 * 1024 * 1024:
19 raise ValueError("The screenshot is too large. Lower the guest display resolution, then retry.")
20 compressed.extend(compressor.flush())
21 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"")
22 return "data:image/png;base64," + base64.b64encode(data).decode()
23
24
25
26class Desktop:
27 def __init__(self, reader, writer, notify):
28 self.reader, self.writer, self.notify = reader, writer, notify
29 self.keys = set()
30 self.pointer = (0, 0)
31 self.buttons = 0
32 self.pixels = bytearray()
33 self.cursor = None
34
35 def resize(self, width, height):
36 if not 0 < width <= 4096 or not 0 < height <= 4096 or width * height > MAX_PIXELS:
37 raise ValueError("The VM screen exceeds the supported 4K display size.")
38 self.width, self.height = width, height
39 self.pixels = bytearray(width * height * 4)
40
41 async def start(self):
42 if not (await self.reader.readexactly(12)).startswith(b"RFB 003."):
43 raise ValueError("The VM screen did not respond. Restart the VM and reconnect.")
44 self.writer.write(b"RFB 003.008\n")
45 count = (await self.reader.readexactly(1))[0]
46 security = await self.reader.readexactly(count)
47 if 1 not in security:
48 raise ValueError("Remove the VM's VNC password in its display settings, then reconnect.")
49 self.writer.write(b"\x01")
50 if await self.reader.readexactly(4) != b"\0\0\0\0":
51 raise ValueError("The VM rejected the screen connection. Restart it and reconnect.")
52 self.writer.write(b"\x01")
53 width, height, _, length = struct.unpack("!HH16sI", await self.reader.readexactly(24))
54 if length > 65536:
55 raise ValueError("The VM screen sent an unsupported desktop name.")
56 await self.reader.readexactly(length)
57 self.resize(width, height)
58 self.writer.write(struct.pack("!B3xBBBBHHHBBB3x", 0, 32, 24, 0, 1, 255, 255, 255, 16, 8, 0))
59 encodings = (0, 1, -223, -224, -239)
60 self.writer.write(struct.pack("!BBH" + "i" * len(encodings), 2, 0, len(encodings), *encodings))
61 self.request(False)
62
63 def request(self, incremental=True):
64 self.writer.write(struct.pack("!BBHHHH", 3, incremental, 0, 0, self.width, self.height))
65
66 def input(self, message):
67 kind = message.get("type")
68 if kind == "key":
69 key, down = message.get("key"), message.get("down")
70 if type(key) is not int or not 0 <= key <= 0xFFFFFFFF or type(down) is not bool:
71 raise ValueError("Unsupported key event.")
72 if down:
73 self.keys.add(key)
74 else:
75 self.keys.discard(key)
76 self.writer.write(struct.pack("!BB2xI", 4, down, key))
77 elif kind == "pointer":
78 x, y, buttons = message.get("x"), message.get("y"), message.get("buttons")
79 if any(type(value) is not int for value in (x, y, buttons)) or not 0 <= buttons <= 255:
80 raise ValueError("Unsupported pointer event.")
81 self.pointer = (max(0, min(x, self.width - 1)), max(0, min(y, self.height - 1)))
82 self.buttons = buttons
83 self.writer.write(struct.pack("!BBHH", 5, buttons, *self.pointer))
84 elif kind == "release":
85 for key in self.keys:
86 self.writer.write(struct.pack("!BB2xI", 4, 0, key))
87 self.keys.clear()
88 self.buttons = 0
89 self.writer.write(struct.pack("!BBHH", 5, 0, *self.pointer))
90 elif kind == "clipboard":
91 text = message.get("text")
92 if not isinstance(text, str) or len(text) > 65536:
93 raise ValueError("Shorten the clipboard text, then send it again.")
94 try:
95 data = text.encode("latin1")
96 except UnicodeEncodeError:
97 raise ValueError("Some clipboard characters aren't supported by this guest.") from None
98 self.writer.write(struct.pack("!B3xI", 6, len(data)) + data)
99 else:
100 raise ValueError("Unsupported input event.")
101
102 async def read(self, present, resized):
103 while True:
104 kind = (await self.reader.readexactly(1))[0]
105 if kind == 0:
106 _, count = struct.unpack("!BH", await self.reader.readexactly(3))
107 dirty = False
108 for _ in range(count):
109 x, y, width, height, encoding = struct.unpack("!HHHHi", await self.reader.readexactly(12))
110 if encoding == -224:
111 break
112 if encoding == -223:
113 self.resize(width, height)
114 resized()
115 continue
116 if encoding == -239:
117 if width * height > 256 * 256:
118 raise ValueError("The VM sent an unsupported cursor size.")
119 pixels = bytearray(await self.reader.readexactly(width * height * 4))
120 mask = await self.reader.readexactly(((width + 7) // 8) * height)
121 for row in range(height):
122 for col in range(width):
123 offset = (row * width + col) * 4
124 pixels[offset], pixels[offset + 2] = pixels[offset + 2], pixels[offset]
125 pixels[offset + 3] = 255 if mask[row * ((width + 7) // 8) + col // 8] & (128 >> (col % 8)) else 0
126 self.cursor = {"type": "cursor", "source": "qemu", "width": width, "height": height, "x": x, "y": y,
127 "frame": 0, "frames": 1, "duration": 100,
128 "image": png(width, height, pixels) if width and height else None}
129 self.notify(self.cursor)
130 continue
131 if x + width > self.width or y + height > self.height:
132 raise ValueError("The VM sent a screen update outside its display.")
133 if encoding == 0:
134 data = await self.reader.readexactly(width * height * 4)
135 elif encoding == 1:
136 source_x, source_y = struct.unpack("!HH", await self.reader.readexactly(4))
137 if source_x + width > self.width or source_y + height > self.height:
138 raise ValueError("The VM sent a screen copy outside its display.")
139 data = b"".join(self.pixels[((source_y + row) * self.width + source_x) * 4:
140 ((source_y + row) * self.width + source_x + width) * 4] for row in range(height))
141 else:
142 raise ValueError("The VM sent an unsupported screen encoding.")
143 for row in range(height):
144 start = ((y + row) * self.width + x) * 4
145 self.pixels[start:start + width * 4] = data[row * width * 4:(row + 1) * width * 4]
146 dirty = dirty or bool(width and height)
147 if dirty:
148 present()
149 self.request()
150 await self.writer.drain()
151 elif kind == 2:
152 pass
153 elif kind == 3:
154 _, length = struct.unpack("!3sI", await self.reader.readexactly(7))
155 if length > 65536:
156 raise ValueError("The VM sent too much clipboard text.")
157 self.notify({"type": "clipboard", "text": (await self.reader.readexactly(length)).decode("latin1")})
158 else:
159 raise ValueError("The VM sent an unsupported screen message.")
160
tools/vm-screen.py+4-145
...@@ -23,7 +23,10 @@ gi.require_version("GstRtp", "1.0")...@@ -23,7 +23,10 @@ gi.require_version("GstRtp", "1.0")
23from gi.repository import GLib, Gst, GstRtp, GstSdp, GstWebRTC23from gi.repository import GLib, Gst, GstRtp, GstSdp, GstWebRTC
2424
2525
26MAX_PIXELS = 3840 * 216026from importlib import import_module
27
28rfb = import_module("vm-rfb")
29Desktop, png = rfb.Desktop, rfb.png
27STUN = os.environ.get("SNOWGLOBE_VM_STUN_SERVER", "stun://stun.cloudflare.com:3478")30STUN = os.environ.get("SNOWGLOBE_VM_STUN_SERVER", "stun://stun.cloudflare.com:3478")
28SLOTS = asyncio.Semaphore(4)31SLOTS = asyncio.Semaphore(4)
29GUEST_PROTOCOL = json.loads(Path(os.environ.get("SNOWGLOBE_VM_GUEST_PROTOCOL", Path(__file__).resolve().parent.parent / "guest/protocol.json")).read_text())32GUEST_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:...@@ -93,14 +96,6 @@ class WindowDesktop:
93 self.copy(window, x, y, w, h, data)96 self.copy(window, x, y, w, h, data)
9497
9598
96def png(width, height, pixels):
97 def chunk(kind, data):
98 return struct.pack("!I", len(data)) + kind + data + struct.pack("!I", zlib.crc32(kind + data))
99 rows = b"".join(b"\0" + pixels[row * width * 4:(row + 1) * width * 4] for row in range(height))
100 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"")
101 return "data:image/png;base64," + base64.b64encode(data).decode()
102
103
104class GuestDesktop:99class GuestDesktop:
105 def __init__(self, name):100 def __init__(self, name):
106 self.name = name101 self.name = name
...@@ -342,142 +337,6 @@ class GuestDesktop:...@@ -342,142 +337,6 @@ class GuestDesktop:
342 await asyncio.sleep(2)337 await asyncio.sleep(2)
343338
344339
345class Desktop:
346 def __init__(self, reader, writer, notify):
347 self.reader, self.writer, self.notify = reader, writer, notify
348 self.keys = set()
349 self.pointer = (0, 0)
350 self.buttons = 0
351 self.pixels = bytearray()
352 self.cursor = None
353
354 def resize(self, width, height):
355 if not 0 < width <= 4096 or not 0 < height <= 4096 or width * height > MAX_PIXELS:
356 raise ValueError("The VM screen exceeds the supported 4K display size.")
357 self.width, self.height = width, height
358 self.pixels = bytearray(width * height * 4)
359
360 async def start(self):
361 if not (await self.reader.readexactly(12)).startswith(b"RFB 003."):
362 raise ValueError("The VM screen did not respond. Restart the VM and reconnect.")
363 self.writer.write(b"RFB 003.008\n")
364 count = (await self.reader.readexactly(1))[0]
365 security = await self.reader.readexactly(count)
366 if 1 not in security:
367 raise ValueError("Remove the VM's VNC password in its display settings, then reconnect.")
368 self.writer.write(b"\x01")
369 if await self.reader.readexactly(4) != b"\0\0\0\0":
370 raise ValueError("The VM rejected the screen connection. Restart it and reconnect.")
371 self.writer.write(b"\x01")
372 width, height, _, length = struct.unpack("!HH16sI", await self.reader.readexactly(24))
373 if length > 65536:
374 raise ValueError("The VM screen sent an unsupported desktop name.")
375 await self.reader.readexactly(length)
376 self.resize(width, height)
377 self.writer.write(struct.pack("!B3xBBBBHHHBBB3x", 0, 32, 24, 0, 1, 255, 255, 255, 16, 8, 0))
378 encodings = (0, 1, -223, -224, -239)
379 self.writer.write(struct.pack("!BBH" + "i" * len(encodings), 2, 0, len(encodings), *encodings))
380 self.request(False)
381
382 def request(self, incremental=True):
383 self.writer.write(struct.pack("!BBHHHH", 3, incremental, 0, 0, self.width, self.height))
384
385 def input(self, message):
386 kind = message.get("type")
387 if kind == "key":
388 key, down = message.get("key"), message.get("down")
389 if type(key) is not int or not 0 <= key <= 0xFFFFFFFF or type(down) is not bool:
390 raise ValueError("Unsupported key event.")
391 if down:
392 self.keys.add(key)
393 else:
394 self.keys.discard(key)
395 self.writer.write(struct.pack("!BB2xI", 4, down, key))
396 elif kind == "pointer":
397 x, y, buttons = message.get("x"), message.get("y"), message.get("buttons")
398 if any(type(value) is not int for value in (x, y, buttons)) or not 0 <= buttons <= 255:
399 raise ValueError("Unsupported pointer event.")
400 self.pointer = (max(0, min(x, self.width - 1)), max(0, min(y, self.height - 1)))
401 self.buttons = buttons
402 self.writer.write(struct.pack("!BBHH", 5, buttons, *self.pointer))
403 elif kind == "release":
404 for key in self.keys:
405 self.writer.write(struct.pack("!BB2xI", 4, 0, key))
406 self.keys.clear()
407 self.buttons = 0
408 self.writer.write(struct.pack("!BBHH", 5, 0, *self.pointer))
409 elif kind == "clipboard":
410 text = message.get("text")
411 if not isinstance(text, str) or len(text) > 65536:
412 raise ValueError("Shorten the clipboard text, then send it again.")
413 try:
414 data = text.encode("latin1")
415 except UnicodeEncodeError:
416 raise ValueError("Some clipboard characters aren't supported by this guest.") from None
417 self.writer.write(struct.pack("!B3xI", 6, len(data)) + data)
418 else:
419 raise ValueError("Unsupported input event.")
420
421 async def read(self, present, resized):
422 while True:
423 kind = (await self.reader.readexactly(1))[0]
424 if kind == 0:
425 _, count = struct.unpack("!BH", await self.reader.readexactly(3))
426 dirty = False
427 for _ in range(count):
428 x, y, width, height, encoding = struct.unpack("!HHHHi", await self.reader.readexactly(12))
429 if encoding == -224:
430 break
431 if encoding == -223:
432 self.resize(width, height)
433 resized()
434 continue
435 if encoding == -239:
436 if width * height > 256 * 256:
437 raise ValueError("The VM sent an unsupported cursor size.")
438 pixels = bytearray(await self.reader.readexactly(width * height * 4))
439 mask = await self.reader.readexactly(((width + 7) // 8) * height)
440 for row in range(height):
441 for col in range(width):
442 offset = (row * width + col) * 4
443 pixels[offset], pixels[offset + 2] = pixels[offset + 2], pixels[offset]
444 pixels[offset + 3] = 255 if mask[row * ((width + 7) // 8) + col // 8] & (128 >> (col % 8)) else 0
445 self.cursor = {"type": "cursor", "source": "qemu", "width": width, "height": height, "x": x, "y": y,
446 "frame": 0, "frames": 1, "duration": 100,
447 "image": png(width, height, pixels) if width and height else None}
448 self.notify(self.cursor)
449 continue
450 if x + width > self.width or y + height > self.height:
451 raise ValueError("The VM sent a screen update outside its display.")
452 if encoding == 0:
453 data = await self.reader.readexactly(width * height * 4)
454 elif encoding == 1:
455 source_x, source_y = struct.unpack("!HH", await self.reader.readexactly(4))
456 if source_x + width > self.width or source_y + height > self.height:
457 raise ValueError("The VM sent a screen copy outside its display.")
458 data = b"".join(self.pixels[((source_y + row) * self.width + source_x) * 4:
459 ((source_y + row) * self.width + source_x + width) * 4] for row in range(height))
460 else:
461 raise ValueError("The VM sent an unsupported screen encoding.")
462 for row in range(height):
463 start = ((y + row) * self.width + x) * 4
464 self.pixels[start:start + width * 4] = data[row * width * 4:(row + 1) * width * 4]
465 dirty = dirty or bool(width and height)
466 if dirty:
467 present()
468 self.request()
469 await self.writer.drain()
470 elif kind == 2:
471 pass
472 elif kind == 3:
473 _, length = struct.unpack("!3sI", await self.reader.readexactly(7))
474 if length > 65536:
475 raise ValueError("The VM sent too much clipboard text.")
476 self.notify({"type": "clipboard", "text": (await self.reader.readexactly(length)).decode("latin1")})
477 else:
478 raise ValueError("The VM sent an unsupported screen message.")
479
480
481class Screen:340class Screen:
482 def __init__(self, writer, desktop):341 def __init__(self, writer, desktop):
483 self.loop = asyncio.get_running_loop()342 self.loop = asyncio.get_running_loop()
tools/vms.py+95-42
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1#!/usr/bin/env python31#!/usr/bin/env python3
2import json2import json
3import contextlib
4import fcntl
3from importlib import import_module5from importlib import import_module
4import os6import os
5from pathlib import Path7from pathlib import Path
...@@ -70,7 +72,7 @@ PROFILES = {...@@ -70,7 +72,7 @@ PROFILES = {
70 },72 },
71}73}
72ACTION_FIELDS = {74ACTION_FIELDS = {
73 "node": None, "domains": None, "stats": None,75 "node": None, "domains": None, "computers": None, "stats": None,
74 "create": {"name", "description", "image", "vcpus", "memory", "disk", "autostart", "start", "mode", "firmware", "platform", "owner", "username"},76 "create": {"name", "description", "image", "vcpus", "memory", "disk", "autostart", "start", "mode", "firmware", "platform", "owner", "username"},
75 "act": {"name", "action"}, "update": {"name", "description", "autostart"},77 "act": {"name", "action"}, "update": {"name", "description", "autostart"},
76 "remove": {"name", "disks"},78 "remove": {"name", "disks"},
...@@ -205,12 +207,24 @@ def validate(action, payload):...@@ -205,12 +207,24 @@ def validate(action, payload):
205 raise ValueError("The VM disk directory is a symbolic link. Remove the link before continuing.")207 raise ValueError("The VM disk directory is a symbolic link. Remove the link before continuing.")
206208
207209
208def command(*args, pass_fds=()):210@contextlib.contextmanager
209 return subprocess.run(args, check=True, text=True, capture_output=True, pass_fds=pass_fds).stdout.strip()211def computer_lock(name):
212 ensure_name(name)
213 directory = DISKS / name
214 if directory.is_symlink():
215 raise ValueError("The VM disk directory is a symbolic link.")
216 descriptor = os.open(directory / "computer.lock", os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600)
217 with os.fdopen(descriptor, "w") as lock:
218 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
219 yield
220
221
222def command(*args, pass_fds=(), timeout=None):
223 return subprocess.run(args, check=True, text=True, capture_output=True, pass_fds=pass_fds, timeout=timeout).stdout.strip()
210224
211225
212def virsh(*args):226def virsh(*args, timeout=None):
213 return command("virsh", "-c", "qemu:///system", *args)227 return command("virsh", "-c", "qemu:///system", *args, timeout=timeout)
214228
215229
216def info(file):230def info(file):
...@@ -242,7 +256,7 @@ def image(file):...@@ -242,7 +256,7 @@ def image(file):
242 "disk": (64 if windows else 32) * 2**30}}256 "disk": (64 if windows else 32) * 2**30}}
243257
244258
245def domains():259def domains(*, computers=False):
246 result = []260 result = []
247 for name in virsh("list", "--all", "--name").splitlines():261 for name in virsh("list", "--all", "--name").splitlines():
248 if not name:262 if not name:
...@@ -251,6 +265,19 @@ def domains():...@@ -251,6 +265,19 @@ def domains():
251 state_line = virsh("domstate", name, "--reason").splitlines()[0].lower()265 state_line = virsh("domstate", name, "--reason").splitlines()[0].lower()
252 state = next((value for value in ("running", "blocked", "paused", "shutdown", "crashed", "pmsuspended") if state_line.startswith(value)), "shutoff")266 state = next((value for value in ("running", "blocked", "paused", "shutdown", "crashed", "pmsuspended") if state_line.startswith(value)), "shutoff")
253 reason = state_line.split("(", 1)[1].rstrip(")") if state in {"paused", "crashed"} and "(" in state_line else None267 reason = state_line.split("(", 1)[1].rstrip(")") if state in {"paused", "crashed"} and "(" in state_line else None
268 ai = import_module("vm-ai").lease(name)
269 entry = {
270 "name": name, "owner": root.findtext(f"metadata/{NS}vm/{NS}owner"),
271 "platform": root.findtext(f"metadata/{NS}vm/{NS}platform", "linux"),
272 "os": root.findtext(f"metadata/{NS}vm/{NS}os", root.findtext(f"metadata/{NS}os", "Other")),
273 "arch": root.find("./os/type").get("arch"), "state": state,
274 "aiOwned": ai is not None, "ownerOnly": ai is not None,
275 "expiresAt": ai["expiresAt"] if ai else None,
276 "capabilities": ["start", "shutdown", "reboot", "destroy", "resume", "suspend", "edit", "delete", "media", "preset", "computer"],
277 }
278 if computers:
279 result.append(entry)
280 continue
254 memory = int(root.findtext("memory", "0")) * 1024281 memory = int(root.findtext("memory", "0")) * 1024
255 balloon = int(root.findtext("currentMemory", str(memory // 1024))) * 1024282 balloon = int(root.findtext("currentMemory", str(memory // 1024))) * 1024
256 disks = []283 disks = []
...@@ -298,16 +325,11 @@ def domains():...@@ -298,16 +325,11 @@ def domains():
298 agent = root.find("./devices/channel/target[@name='org.qemu.guest_agent.0']")325 agent = root.find("./devices/channel/target[@name='org.qemu.guest_agent.0']")
299 setup = root.findtext(f"metadata/{NS}vm/{NS}setup")326 setup = root.findtext(f"metadata/{NS}vm/{NS}setup")
300 result.append({327 result.append({
301 "name": name,328 **entry,
302 "host": os.uname().nodename,329 "host": os.uname().nodename,
303 "arch": root.find("./os/type").get("arch"),
304 "pendingAction": None,330 "pendingAction": None,
305 "capabilities": ["start", "shutdown", "reboot", "destroy", "resume", "suspend", "edit", "delete", "media", "preset"],
306 "owner": root.findtext(f"metadata/{NS}vm/{NS}owner"),
307 "description": root.findtext("description", ""),331 "description": root.findtext("description", ""),
308 "state": state,
309 "reason": reason,332 "reason": reason,
310 "os": root.findtext(f"metadata/{NS}vm/{NS}os", root.findtext(f"metadata/{NS}os", "Other")),
311 "vcpus": int(root.findtext("vcpu", "1")),333 "vcpus": int(root.findtext("vcpu", "1")),
312 "pinned": None,334 "pinned": None,
313 "memory": memory,335 "memory": memory,
...@@ -319,7 +341,6 @@ def domains():...@@ -319,7 +341,6 @@ def domains():
319 "disks": disks,341 "disks": disks,
320 "media": media,342 "media": media,
321 "firmware": "uefi" if root.find("./os/loader") is not None or root.find("os").get("firmware") == "efi" else "bios",343 "firmware": "uefi" if root.find("./os/loader") is not None or root.find("os").get("firmware") == "efi" else "bios",
322 "platform": root.findtext(f"metadata/{NS}vm/{NS}platform", "linux"),
323 "console": root.find("./devices/graphics[@type='vnc']") is not None,344 "console": root.find("./devices/graphics[@type='vnc']") is not None,
324 "serial": root.find("./devices/console[@type='pty']/target[@type='serial']") is not None,345 "serial": root.find("./devices/console[@type='pty']/target[@type='serial']") is not None,
325 "interfaces": interfaces,346 "interfaces": interfaces,
...@@ -588,10 +609,10 @@ def save_preset(spec):...@@ -588,10 +609,10 @@ def save_preset(spec):
588 shutil.rmtree(temporary)609 shutil.rmtree(temporary)
589610
590611
591def console_target(name):612def console_target(name, *, timeout=None):
592 root = ET.fromstring(virsh("dumpxml", name))613 root = ET.fromstring(virsh("dumpxml", name, timeout=timeout))
593 graphics = root.find("./devices/graphics[@type='vnc']")614 graphics = root.find("./devices/graphics[@type='vnc']")
594 if virsh("domstate", name).strip() not in {"running", "paused", "blocked"} or graphics is None:615 if virsh("domstate", name, timeout=timeout).strip() not in {"running", "paused", "blocked"} or graphics is None:
595 raise ValueError("Start this VM before opening its screen.")616 raise ValueError("Start this VM before opening its screen.")
596 # Only libvirt's loopback VNC listener; callers cannot provide a host or port.617 # Only libvirt's loopback VNC listener; callers cannot provide a host or port.
597 if graphics.get("listen") != "127.0.0.1" or graphics.get("socket"):618 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)...@@ -642,7 +663,11 @@ old=$({account_tool}getent passwd 1000 | cut -d: -f1)
642test -n "$old"663test -n "$old"
643if [ "$old" != {username} ]; then664if [ "$old" != {username} ]; then
644 ! {account_tool}getent passwd {username}665 ! {account_tool}getent passwd {username}
645 {account_tool}usermod -l {username} -c {username} -d /home/{username} -m "$old"666 if [ -e /home/{username} ]; then
667 {account_tool}usermod -l {username} -c {username} "$old"
668 else
669 {account_tool}usermod -l {username} -c {username} -d /home/{username} -m "$old"
670 fi
646 if [ "$({account_tool}getent group 1000 | cut -d: -f1)" = "$old" ]; then {account_tool}groupmod -n {username} "$old"; fi671 if [ "$({account_tool}getent group 1000 | cut -d: -f1)" = "$old" ]; then {account_tool}groupmod -n {username} "$old"; fi
647fi672fi
648'''673'''
...@@ -654,13 +679,26 @@ fi...@@ -654,13 +679,26 @@ fi
654 files.insert(0, {"path": "/etc/ssh/sshd_config.d/00-snowglobe.conf", "permissions": "0600", "content": "PasswordAuthentication yes\nPermitRootLogin yes\n"})679 files.insert(0, {"path": "/etc/ssh/sshd_config.d/00-snowglobe.conf", "permissions": "0600", "content": "PasswordAuthentication yes\nPermitRootLogin yes\n"})
655 else:680 else:
656 files.append({"path": "/var/lib/snowglobe/hostname", "content": hostname + "\n"})681 files.append({"path": "/var/lib/snowglobe/hostname", "content": hostname + "\n"})
682 guest = Path(__file__).resolve().parent.parent / "guest"
683 files.extend([
684 {"path": "/usr/local/libexec/snowglobe-computer.py", "permissions": "0755", "content": (guest / "computer-linux.py").read_text()},
685 {"path": "/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer", "permissions": "0755", "content": (guest / "computer-linux-launch").read_text()},
686 ])
687 computer_setup = []
688 if fedora:
689 computer_setup = [["setsebool", "-P", "virt_qemu_ga_run_unconfined", "on"], ["restorecon", "-R", "/etc/qemu-ga/fsfreeze-hook.d"]]
690 if nixos:
691 files.append({"path": "/var/lib/snowglobe/computer.nix", "content": (guest / "computer-linux.nix").read_text()})
692 computer_setup = [["/run/current-system/sw/bin/bash", "-lc", "nix-env --profile /var/lib/snowglobe/computer -if /var/lib/snowglobe/computer.nix"]]
657 config = {693 config = {
658 "users": [], "disable_root": False, "ssh_pwauth": True, "ssh_deletekeys": True,694 "users": [], "disable_root": False, "ssh_pwauth": True, "ssh_deletekeys": True,
659 "bootcmd": ([["rm", "-f", gdm_config]] if nixos else []) + [["sh", "-c", rename]],695 "bootcmd": ([["rm", "-f", gdm_config]] if nixos else []) + [["sh", "-c", rename]],
660 "chpasswd": {"expire": False, "users": [{"name": user, "password": hashed, "type": "hash"} for user in [username, "root"]]},696 "chpasswd": {"expire": False, "users": [{"name": user, "password": hashed, "type": "hash"} for user in [username, "root"]]},
661 "write_files": files,697 "write_files": files,
662 "runcmd": [["systemctl", "enable", "--now", "sshd" if fedora or nixos else "ssh"], ["systemctl", "enable", "--now", f"serial-getty@{serial}.service"]],698 "runcmd": [["systemctl", "enable", "--now", "sshd" if fedora or nixos else "ssh"], ["systemctl", "enable", "--now", f"serial-getty@{serial}.service"]] + computer_setup,
663 }699 }
700 if not nixos:
701 config["packages"] = ["python3", "python3-gobject", "at-spi2-core"] if fedora else ["python3", "python3-gi", "gir1.2-atspi-2.0"]
664 with tempfile.TemporaryDirectory(prefix=".seed-", dir=directory) as temporary:702 with tempfile.TemporaryDirectory(prefix=".seed-", dir=directory) as temporary:
665 files = Path(temporary)703 files = Path(temporary)
666 (files / "user-data").write_text("#cloud-config\n" + json.dumps(config))704 (files / "user-data").write_text("#cloud-config\n" + json.dumps(config))
...@@ -696,7 +734,7 @@ def macos_seed(directory, username):...@@ -696,7 +734,7 @@ def macos_seed(directory, username):
696 (directory / "seed.iso").chmod(0o600)734 (directory / "seed.iso").chmod(0o600)
697735
698736
699def create(spec):737def create(spec, *, ai=False):
700 name = spec["name"]738 name = spec["name"]
701 ensure_name(name)739 ensure_name(name)
702 if name in virsh("list", "--all", "--name").splitlines():740 if name in virsh("list", "--all", "--name").splitlines():
...@@ -730,6 +768,8 @@ def create(spec):...@@ -730,6 +768,8 @@ def create(spec):
730 directory = DISKS / name768 directory = DISKS / name
731 directory.mkdir(mode=0o700, parents=True, exist_ok=False)769 directory.mkdir(mode=0o700, parents=True, exist_ok=False)
732 drive = directory / "disk.qcow2"770 drive = directory / "disk.qcow2"
771 if ai:
772 (directory / "ai.json").write_text(json.dumps({"expiresAt": int(time.time()) + import_module("vm-ai").LIMITS["idle_seconds"]}))
733773
734 def expired(_signum, _frame):774 def expired(_signum, _frame):
735 raise TimeoutError("VM image preparation timed out.")775 raise TimeoutError("VM image preparation timed out.")
...@@ -839,8 +879,11 @@ def create(spec):...@@ -839,8 +879,11 @@ def create(spec):
839 ET.SubElement(cpu, "feature", policy="require", name="hypervisor")879 ET.SubElement(cpu, "feature", policy="require", name="hypervisor")
840 elif root.get("type") == "qemu":880 elif root.get("type") == "qemu":
841 ET.SubElement(root, "cpu", mode="maximum")881 ET.SubElement(root, "cpu", mode="maximum")
842 if profile_name == "windows-q35-uefi":882 if spec["platform"] == "windows":
843 ET.SubElement(root.find("cpu"), "topology", sockets="1", cores=str(spec["vcpus"]), threads="1")883 cpu = root.find("cpu")
884 if cpu is None:
885 cpu = ET.SubElement(root, "cpu")
886 ET.SubElement(cpu, "topology", sockets="1", cores=str(spec["vcpus"]), threads="1")
844 clock = ET.SubElement(root, "clock", offset=details_profile["clock"])887 clock = ET.SubElement(root, "clock", offset=details_profile["clock"])
845 if profile_name == "windows-q35-uefi":888 if profile_name == "windows-q35-uefi":
846 ET.SubElement(clock, "timer", name="hypervclock", present="yes")889 ET.SubElement(clock, "timer", name="hypervclock", present="yes")
...@@ -942,14 +985,32 @@ def create(spec):...@@ -942,14 +985,32 @@ def create(spec):
942 signal.signal(signal.SIGALRM, previous)985 signal.signal(signal.SIGALRM, previous)
943986
944987
988def remove(spec, *, missing_ok=False):
989 name = spec["name"]
990 ensure_name(name)
991 exists = name in virsh("list", "--all", "--name").splitlines()
992 if not exists and not missing_ok:
993 raise ValueError("VM does not exist")
994 if exists:
995 if virsh("domstate", name).strip() != "shut off":
996 virsh("destroy", name)
997 root = ET.fromstring(virsh("dumpxml", name))
998 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 []))
999 directory = DISKS / name
1000 if spec["disks"] and directory.is_dir():
1001 for file in directory.iterdir():
1002 file.unlink()
1003 directory.rmdir()
1004
1005
945def main():1006def main():
946 action = sys.argv[1]1007 action = sys.argv[1]
947 payload = json.loads(sys.argv[2]) if len(sys.argv) > 2 else None1008 payload = json.loads(sys.argv[2]) if len(sys.argv) > 2 else None
948 validate(action, payload)1009 validate(action, payload)
949 if action == "node":1010 if action == "node":
950 return node()1011 return node()
951 if action == "domains":1012 if action in {"domains", "computers"}:
952 return domains() + mac_vms.domain()1013 return domains(computers=action == "computers") + mac_vms.domain()
953 if action == "stats":1014 if action == "stats":
954 result = {}1015 result = {}
955 for name in virsh("list", "--name").splitlines():1016 for name in virsh("list", "--name").splitlines():
...@@ -987,7 +1048,7 @@ def main():...@@ -987,7 +1048,7 @@ def main():
987 raise ValueError("This VM has no serial console. Add one in its hardware settings.")1048 raise ValueError("This VM has no serial console. Add one in its hardware settings.")
988 return {"path": path}1049 return {"path": path}
989 if action == "owner":1050 if action == "owner":
990 return {"owner": ET.fromstring(virsh("dumpxml", payload["name"])).findtext(f"metadata/{NS}vm/{NS}owner")}1051 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}
991 if action == "access":1052 if action == "access":
992 try:1053 try:
993 with os.fdopen(open_regular(DISKS / payload["name"] / "access.json")) as incoming:1054 with os.fdopen(open_regular(DISKS / payload["name"] / "access.json")) as incoming:
...@@ -1003,11 +1064,14 @@ def main():...@@ -1003,11 +1064,14 @@ def main():
1003 elif action == "act":1064 elif action == "act":
1004 name = payload["name"]1065 name = payload["name"]
1005 ensure_name(name)1066 ensure_name(name)
1006 if payload["action"] == "start":1067 with computer_lock(name):
1007 root = ET.fromstring(virsh("dumpxml", name, "--inactive"))1068 if payload["action"] == "start":
1008 if root.find("./devices/interface/source[@network='default']") is not None:1069 root = ET.fromstring(virsh("dumpxml", name, "--inactive"))
1009 ensure_network()1070 if root.find("./devices/interface/source[@network='default']") is not None:
1010 virsh({"start": "start", "shutdown": "shutdown", "reboot": "reboot", "destroy": "destroy", "resume": "resume", "suspend": "suspend"}[payload["action"]], name)1071 ensure_network()
1072 ai = import_module("vm-ai")
1073 ai.lease(name, ai.LIMITS["idle_seconds"])
1074 virsh({"start": "start", "shutdown": "shutdown", "reboot": "reboot", "destroy": "destroy", "resume": "resume", "suspend": "suspend"}[payload["action"]], name)
1011 elif action == "update":1075 elif action == "update":
1012 name = payload["name"]1076 name = payload["name"]
1013 ensure_name(name)1077 ensure_name(name)
...@@ -1019,19 +1083,8 @@ def main():...@@ -1019,19 +1083,8 @@ def main():
1019 flags.append("--live")1083 flags.append("--live")
1020 virsh("desc", name, *flags, "--", payload["description"])1084 virsh("desc", name, *flags, "--", payload["description"])
1021 elif action == "remove":1085 elif action == "remove":
1022 name = payload["name"]1086 with computer_lock(payload["name"]):
1023 ensure_name(name)1087 remove(payload)
1024 if name not in virsh("list", "--all", "--name").splitlines():
1025 raise ValueError("VM does not exist")
1026 if virsh("domstate", name).strip() != "shut off":
1027 virsh("destroy", name)
1028 root = ET.fromstring(virsh("dumpxml", name))
1029 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 []))
1030 directory = DISKS / name
1031 if payload["disks"] and directory.is_dir():
1032 for file in directory.iterdir():
1033 file.unlink()
1034 directory.rmdir()
1035 else:1088 else:
1036 raise ValueError("unsupported VM action")1089 raise ValueError("unsupported VM action")
1037 return None1090 return None