| 1 | """Bounded computer operations over the existing private VM transports.""" |
| 2 | import asyncio |
| 3 | import base64 |
| 4 | import contextlib |
| 5 | from contextvars import ContextVar |
| 6 | import hashlib |
| 7 | from importlib import import_module |
| 8 | import json |
| 9 | import os |
| 10 | from pathlib import Path |
| 11 | import shlex |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import threading |
| 15 | import time |
| 16 | import uuid |
| 17 | import xml.etree.ElementTree as ET |
| 18 | |
| 19 | import vms |
| 20 | |
| 21 | rfb = import_module('vm-rfb') |
| 22 | SCREEN_SLOT = threading.BoundedSemaphore(1) |
| 23 | DEADLINE = ContextVar('computer_deadline', default=float('inf')) |
| 24 | OPERATION = ContextVar('computer_operation', default=None) |
| 25 | FILE_CHUNK = json.loads((Path(__file__).resolve().parent.parent / 'dashboard/src/host-protocol.json').read_text())['file_chunk'] |
| 26 | DRAG_DURATION_MS = 400 |
| 27 | |
| 28 | |
| 29 | def remaining(limit): |
| 30 | operation = OPERATION.get() |
| 31 | if operation is not None: |
| 32 | name, identity = operation |
| 33 | with vms.computer_control(name) as control: |
| 34 | if control.read() == identity: |
| 35 | raise InterruptedError('The computer operation was cancelled. Inspect its result before retrying.') |
| 36 | seconds = min(limit, DEADLINE.get() - time.monotonic()) |
| 37 | if seconds <= 0: |
| 38 | raise TimeoutError('The computer operation timed out. Inspect its status before retrying.') |
| 39 | return seconds |
| 40 | |
| 41 | |
| 42 | def qga(name, operation, **arguments): |
| 43 | payload = json.dumps({'execute': operation, 'arguments': arguments}) |
| 44 | # Virsh reads stdin so guest scripts and file contents never enter host argv. |
| 45 | command = 'qemu-agent-command ' + shlex.quote(name) + ' --timeout 10 ' + shlex.quote(payload) + '\n' |
| 46 | try: |
| 47 | result = subprocess.run(['virsh', '--quiet', '--connect', 'qemu:///system'], input=command, |
| 48 | capture_output=True, text=True, timeout=remaining(15)) |
| 49 | except subprocess.TimeoutExpired: |
| 50 | raise TimeoutError('The guest agent timed out. Inspect the operation status before retrying.') from None |
| 51 | details = result.stderr.casefold() |
| 52 | rejected = False |
| 53 | for line in result.stdout.splitlines(): |
| 54 | if line.startswith('{'): |
| 55 | value = json.loads(line) |
| 56 | if 'return' in value: |
| 57 | return value['return'] |
| 58 | if 'error' in value: |
| 59 | details += json.dumps(value['error']).casefold() |
| 60 | rejected = True |
| 61 | if operation == 'guest-exec-status' and any(line.endswith(( |
| 62 | f"PID {arguments['pid']} does not exist", 'PID ld does not exist', 'PID lld does not exist', "Invalid parameter 'pid'")) |
| 63 | for line in result.stderr.splitlines()): |
| 64 | raise ProcessLookupError('The guest process result is unavailable. Check the VM before retrying.') |
| 65 | if any(message in details for message in ('guest agent command timed out', 'timed out waiting', 'timed out during operation')): |
| 66 | raise TimeoutError('The guest agent timed out. Inspect the operation status before retrying.') |
| 67 | if rejected or 'guest agent command failed' in details or 'unable to execute qemu agent command' in details: |
| 68 | if operation == 'guest-exec' and 'failed to execute child process' in details: |
| 69 | raise ValueError('The program could not start. Check its path, arguments, permissions, and architecture.') |
| 70 | for fragments, message in [ |
| 71 | (('no such file or directory', 'cannot find the file specified', 'cannot find the path specified'), 'The guest file was not found. Check its path.'), |
| 72 | (('permission denied', 'access is denied'), 'The guest denied access. Check the file permissions and security policy.'), |
| 73 | (('no space left on device', 'not enough space on the disk', 'disk is full'), 'The guest disk is full. Free space before retrying.'), |
| 74 | (('commandnotfound', 'has not been found', 'not supported'), 'The guest agent does not support this operation. Update the agent.')]: |
| 75 | if any(fragment in details for fragment in fragments): |
| 76 | raise ValueError(message) |
| 77 | raise ValueError('The guest agent rejected the operation. Check its arguments, permissions, and installed tools.') |
| 78 | raise ValueError('The guest agent did not answer. Wait for the VM to finish starting, then retry.') |
| 79 | |
| 80 | |
| 81 | def guest_exec(name, path, args, data=None, timeout=20): |
| 82 | arguments = {'path': path, 'arg': args, 'capture-output': True} |
| 83 | if data is not None: |
| 84 | arguments['input-data'] = base64.b64encode(data).decode() |
| 85 | pid = qga(name, 'guest-exec', **arguments)['pid'] |
| 86 | deadline = time.monotonic() + remaining(timeout) |
| 87 | finished = None |
| 88 | while time.monotonic() < deadline: |
| 89 | try: |
| 90 | result = qga(name, 'guest-exec-status', pid=pid) |
| 91 | except ProcessLookupError: |
| 92 | if finished is None: |
| 93 | raise |
| 94 | if finished.get('exitcode') != 0: |
| 95 | raise ValueError('The guest worker could not run. Check the desktop session and installed computer tools.') |
| 96 | if finished.get('out-truncated') or finished.get('err-truncated'): |
| 97 | raise ValueError('The guest response was truncated. Narrow the request.') |
| 98 | return base64.b64decode(finished.get('out-data', ''), validate=True) |
| 99 | # QGA retains completed records across PID reuse and returns the oldest first. |
| 100 | if result.get('exited'): |
| 101 | finished = result |
| 102 | continue |
| 103 | finished = None |
| 104 | time.sleep(.15) |
| 105 | raise ValueError('The guest operation is still running. Check its status before retrying.') |
| 106 | |
| 107 | |
| 108 | def close_file(name, handle): |
| 109 | """Guest file handles outlive the request unless explicitly closed.""" |
| 110 | deadline = DEADLINE.set(time.monotonic() + 15) |
| 111 | operation = OPERATION.set(None) |
| 112 | try: |
| 113 | qga(name, 'guest-file-close', handle=handle) |
| 114 | finally: |
| 115 | OPERATION.reset(operation) |
| 116 | DEADLINE.reset(deadline) |
| 117 | |
| 118 | |
| 119 | def read_file(name, path, offset=0, length=65536): |
| 120 | handle = qga(name, 'guest-file-open', path=path, mode='rb') |
| 121 | try: |
| 122 | qga(name, 'guest-file-seek', handle=handle, offset=offset, whence=0) |
| 123 | value = qga(name, 'guest-file-read', handle=handle, count=length) |
| 124 | return base64.b64decode(value['buf-b64'], validate=True) |
| 125 | finally: |
| 126 | close_file(name, handle) |
| 127 | |
| 128 | |
| 129 | def write_file(name, path, data, offset=0): |
| 130 | handle = qga(name, 'guest-file-open', path=path, mode='r+b' if offset else 'wb') |
| 131 | try: |
| 132 | if offset: |
| 133 | end = qga(name, 'guest-file-seek', handle=handle, offset=0, whence=2) |
| 134 | if end['position'] != offset: |
| 135 | raise ValueError('The transfer offset changed. Start a new upload.') |
| 136 | for start in range(0, len(data), 49152): |
| 137 | chunk = data[start:start + 49152] |
| 138 | result = qga(name, 'guest-file-write', handle=handle, **{'buf-b64': base64.b64encode(chunk).decode()}) |
| 139 | if result['count'] != len(chunk): |
| 140 | raise ValueError('The guest disk could not accept the file. Check free space.') |
| 141 | finally: |
| 142 | close_file(name, handle) |
| 143 | |
| 144 | |
| 145 | def powershell(name, script, timeout=60): |
| 146 | """Win7 needs a hidden console; ARM64 QGA cannot capture WSH's console output.""" |
| 147 | stem = 'C:\\Windows\\Temp\\snowglobe-computer-' + uuid.uuid4().hex |
| 148 | command = 'cmd.exe /d /c powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File ' + stem + '.ps1' |
| 149 | command += ' > ' + stem + '.out 2> ' + stem + '.err' |
| 150 | launcher = '''Set shell = CreateObject("WScript.Shell") |
| 151 | code = shell.Run("COMMAND", 0, True) |
| 152 | Set files = CreateObject("Scripting.FileSystemObject") |
| 153 | If code <> 0 Then files.DeleteFile "STEM.out" |
| 154 | files.DeleteFile "STEM.err" |
| 155 | files.DeleteFile "STEM.ps1" |
| 156 | files.DeleteFile WScript.ScriptFullName |
| 157 | WScript.Quit code |
| 158 | '''.replace('COMMAND', command).replace('STEM', stem) |
| 159 | write_file(name, stem + '.ps1', b'\xff\xfe' + script.encode('utf-16le')) |
| 160 | write_file(name, stem + '.vbs', launcher.encode('ascii')) |
| 161 | guest_exec(name, 'cscript.exe', ['//nologo', stem + '.vbs'], timeout=timeout) |
| 162 | try: |
| 163 | return read_file(name, stem + '.out', length=2*1024*1024) |
| 164 | finally: |
| 165 | guest_exec(name, 'cmd.exe', ['/d', '/c', 'del ' + stem + '.out']) |
| 166 | |
| 167 | |
| 168 | async def desktop(name, actions=None, platform=None, screenshot=True, native_text=True): |
| 169 | if not SCREEN_SLOT.acquire(blocking=False): |
| 170 | raise ValueError('Another screen capture is running. Retry in a moment.') |
| 171 | token = DEADLINE.set(min(DEADLINE.get(), time.monotonic() + 180)) |
| 172 | try: |
| 173 | return await asyncio.wait_for(capture(name, actions, platform, screenshot, native_text), remaining(180)) |
| 174 | finally: |
| 175 | DEADLINE.reset(token) |
| 176 | SCREEN_SLOT.release() |
| 177 | |
| 178 | |
| 179 | async def capture(name, actions, platform, screenshot=True, native_text=True): |
| 180 | if not native_text: |
| 181 | for action in actions or (): |
| 182 | if action['type'] == 'text' and any(not (32 <= ord(char) <= 126 or char in '\n\r\t') for char in action['text']): |
| 183 | raise ValueError('This connection supports ASCII text only. Use key actions for other keyboard input.') |
| 184 | target = vms.console_target(name, timeout=remaining(10)) |
| 185 | reader, writer = await asyncio.wait_for(asyncio.open_connection('127.0.0.1', target['port']), 10) |
| 186 | screen = rfb.Desktop(reader, writer, lambda _: None) |
| 187 | task = None |
| 188 | try: |
| 189 | await asyncio.wait_for(screen.start(), 10) |
| 190 | ready = asyncio.Event() |
| 191 | task = asyncio.create_task(screen.read(ready.set, ready.clear)) |
| 192 | await asyncio.wait_for(ready.wait(), 10) |
| 193 | if actions: |
| 194 | for action in actions: |
| 195 | remaining(180) |
| 196 | kind = action['type'] |
| 197 | if kind in ('move', 'click', 'drag', 'scroll'): |
| 198 | x, y = action['x'], action['y'] |
| 199 | if not 0 <= x < screen.width or not 0 <= y < screen.height: |
| 200 | raise ValueError('Choose coordinates inside the latest screenshot.') |
| 201 | screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0}) |
| 202 | button = {'left': 1, 'middle': 2, 'right': 4}.get(action.get('button', 'left')) |
| 203 | if kind == 'click': |
| 204 | for _ in range(action.get('count', 1)): |
| 205 | screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': button}) |
| 206 | await asyncio.sleep(.04) |
| 207 | screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0}) |
| 208 | await asyncio.sleep(.06) |
| 209 | elif kind == 'drag': |
| 210 | end_x, end_y = action['to_x'], action['to_y'] |
| 211 | if not 0 <= end_x < screen.width or not 0 <= end_y < screen.height: |
| 212 | raise ValueError('Choose drag coordinates inside the latest screenshot.') |
| 213 | screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': button}) |
| 214 | for step in range(1, 21): |
| 215 | screen.input({'type': 'pointer', 'x': round(x+(end_x-x)*step/20), |
| 216 | 'y': round(y+(end_y-y)*step/20), 'buttons': button}) |
| 217 | await asyncio.sleep(action.get('duration_ms', DRAG_DURATION_MS)/20000) |
| 218 | screen.input({'type': 'release'}) |
| 219 | elif kind == 'scroll': |
| 220 | wheel = {'up': 8, 'down': 16, 'left': 32, 'right': 64}[action['direction']] |
| 221 | for _ in range(action.get('amount', 3)): |
| 222 | screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': wheel}) |
| 223 | screen.input({'type': 'pointer', 'x': x, 'y': y, 'buttons': 0}) |
| 224 | await asyncio.sleep(.02) |
| 225 | elif kind == 'key': |
| 226 | keys = [keycode(key) for key in action['keys']] |
| 227 | for key in keys: |
| 228 | screen.input({'type': 'key', 'key': key, 'down': True}) |
| 229 | await asyncio.sleep(.04) |
| 230 | for key in reversed(keys): |
| 231 | screen.input({'type': 'key', 'key': key, 'down': False}) |
| 232 | elif kind == 'text': |
| 233 | if native_text: |
| 234 | pending = asyncio.create_task(asyncio.to_thread(worker,name,platform,{'action':'input','actions':[action],'screenshot':False})) |
| 235 | try: |
| 236 | await asyncio.shield(pending) |
| 237 | finally: |
| 238 | # Cancellation cannot release the VM lock while its worker still runs. |
| 239 | with contextlib.suppress(Exception): |
| 240 | await pending |
| 241 | else: |
| 242 | shifted = dict(zip('~!@#$%^&*()_+{}|:"<>?', '`1234567890-=[]\\;\',./')) |
| 243 | for char in action['text'].replace('\r\n', '\n'): |
| 244 | shift = char in shifted |
| 245 | base = shifted.get(char, char) |
| 246 | key = {'\n': 0xff0d, '\r': 0xff0d, '\t': 0xff09}.get(base, ord(base)) |
| 247 | if shift: |
| 248 | screen.input({'type': 'key', 'key': 0xffe1, 'down': True}) |
| 249 | screen.input({'type': 'key', 'key': key, 'down': True}) |
| 250 | await writer.drain() |
| 251 | await asyncio.sleep(.01) |
| 252 | screen.input({'type': 'key', 'key': key, 'down': False}) |
| 253 | if shift: |
| 254 | screen.input({'type': 'key', 'key': 0xffe1, 'down': False}) |
| 255 | await writer.drain() |
| 256 | await asyncio.sleep(.01) |
| 257 | elif kind == 'wait': |
| 258 | await asyncio.sleep(action['duration_ms']/1000) |
| 259 | await writer.drain() |
| 260 | screen.input({'type': 'release'}) |
| 261 | await asyncio.wait_for(writer.drain(), 5) |
| 262 | if not screenshot: |
| 263 | return {'completed': len(actions)} |
| 264 | await asyncio.sleep(.2) |
| 265 | # Force a full update even when the desktop has stopped painting. |
| 266 | ready.clear() |
| 267 | screen.request(False) |
| 268 | await writer.drain() |
| 269 | await asyncio.wait_for(ready.wait(), 10) |
| 270 | task.cancel() |
| 271 | with contextlib.suppress(asyncio.CancelledError): |
| 272 | await task |
| 273 | task = None |
| 274 | return {'width': screen.width, 'height': screen.height, 'bounds': [0,0,screen.width,screen.height], |
| 275 | 'scale': [1,1], 'png': rfb.png(screen.width, screen.height, screen.pixels, bgra=True).split(',',1)[1]} |
| 276 | finally: |
| 277 | with contextlib.suppress(Exception): |
| 278 | screen.input({'type': 'release'}) |
| 279 | await asyncio.wait_for(writer.drain(), 5) |
| 280 | if task: |
| 281 | task.cancel() |
| 282 | with contextlib.suppress(asyncio.CancelledError, Exception): |
| 283 | await task |
| 284 | writer.close() |
| 285 | await asyncio.wait_for(writer.wait_closed(), 5) |
| 286 | |
| 287 | |
| 288 | def keycode(key): |
| 289 | names = {'Enter': 0xff0d, 'Tab': 0xff09, 'Escape': 0xff1b, 'Backspace': 0xff08, |
| 290 | 'Delete': 0xffff, 'Insert': 0xff63, 'Home': 0xff50, 'End': 0xff57, |
| 291 | 'PageUp': 0xff55, 'PageDown': 0xff56, 'Left': 0xff51, 'Up': 0xff52, |
| 292 | 'Right': 0xff53, 'Down': 0xff54, 'Shift': 0xffe1, 'Control': 0xffe3, |
| 293 | 'Alt': 0xffe9, 'Meta': 0xffeb, 'Space': 0x20} |
| 294 | names.update({'F'+str(i): 0xffbd+i for i in range(1,25)}) |
| 295 | if key in names: |
| 296 | return names[key] |
| 297 | if len(key) == 1 and 32 <= ord(key) <= 126: |
| 298 | return ord(key) |
| 299 | raise ValueError('Use a named key or one ASCII character.') |
| 300 | |
| 301 | |
| 302 | def validate(request): |
| 303 | if not isinstance(request, dict): |
| 304 | raise ValueError('Send a computer operation.') |
| 305 | action = request.get('action') |
| 306 | fields = {'status': set(), 'shot': set(), 'ui': {'pid'}, 'ui_action': {'target','operation','value','screenshot'}, |
| 307 | 'exec': {'script','runtime','timeout_ms','screenshot'}, 'input': {'actions','screenshot'}, |
| 308 | 'spawn': {'script','runtime'}, 'process': {'id','operation'}, |
| 309 | 'file': {'operation','path','offset','data','length','destination','overwrite'}} |
| 310 | if not isinstance(action,str) or action not in fields or set(request)-{'action'}-fields[action]: |
| 311 | raise ValueError('Use the fields listed for this computer operation.') |
| 312 | if action in ('exec','spawn'): |
| 313 | if not isinstance(request.get('script'),str) or not 1 <= len(request['script'].encode()) <= 65536: |
| 314 | raise ValueError('Provide a script of at most 64 KiB.') |
| 315 | if request.get('runtime') not in (None,'default','bash','zsh','powershell','cmd','ahk'): |
| 316 | raise ValueError('Choose a runtime reported by computer_status.') |
| 317 | if action == 'exec' and (type(request.get('timeout_ms')) is not int or not 1 <= request['timeout_ms'] <= 180000): |
| 318 | raise ValueError('Choose a timeout between 1 and 180000 milliseconds.') |
| 319 | if 'screenshot' in request and type(request['screenshot']) is not bool: |
| 320 | raise ValueError('Choose whether to return a screenshot.') |
| 321 | if action == 'ui' and request.get('pid') is not None and (type(request['pid']) is not int or request['pid']<=0): |
| 322 | raise ValueError('Choose a positive application PID.') |
| 323 | if action == 'ui_action': |
| 324 | 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: |
| 325 | raise ValueError('Use a target and action returned by computer_ui.') |
| 326 | if 'value' in request and (not isinstance(request['value'],str) or len(request['value'].encode())>16384): |
| 327 | raise ValueError('Set a value of at most 16 KiB.') |
| 328 | if action == 'file': |
| 329 | path=request.get('path') |
| 330 | if not isinstance(path,str) or not 1<=len(path)<=4096 or '\x00' in path: |
| 331 | raise ValueError('Use an absolute guest file path.') |
| 332 | operation=request.get('operation') |
| 333 | if operation not in ('stat','list','mkdir','remove','read','write','commit'): |
| 334 | raise ValueError('Choose a supported file operation.') |
| 335 | if operation in ('read','write'): |
| 336 | offset=request.get('offset',0) |
| 337 | if type(offset) is not int or not 0<=offset<=64*2**20: |
| 338 | raise ValueError('Choose a transfer offset within 64 MiB.') |
| 339 | if operation=='read': |
| 340 | length=request.get('length',65536) |
| 341 | if type(length) is not int or not 1<=length<=FILE_CHUNK: raise ValueError('Read at most 1 MiB at a time.') |
| 342 | else: |
| 343 | if not isinstance(request.get('data'),str) or len(request['data'])>((FILE_CHUNK+2)//3)*4 or len(base64.b64decode(request['data'],validate=True))>FILE_CHUNK: |
| 344 | raise ValueError('Write at most 1 MiB at a time.') |
| 345 | 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): |
| 346 | raise ValueError('Choose an absolute guest destination and whether to replace it.') |
| 347 | if action == 'process' and (not isinstance(request.get('id'),str) or len(request['id'])>128 or request.get('operation') not in ('status','stop')): |
| 348 | raise ValueError('Use the process handle returned by computer_spawn.') |
| 349 | if action == 'input': |
| 350 | actions = request.get('actions') |
| 351 | if not isinstance(actions,list) or not 1 <= len(actions) <= 100: |
| 352 | raise ValueError('Send between 1 and 100 desktop actions.') |
| 353 | for index, item in enumerate(actions, 1): |
| 354 | if not isinstance(item,dict): |
| 355 | raise ValueError('Send a desktop action object.') |
| 356 | kind = item.get('type') |
| 357 | required = {'move': {'x','y'}, 'click': {'x','y'}, 'drag': {'x','y','to_x','to_y'}, |
| 358 | 'scroll': {'x','y','direction'}, 'key': {'keys'}, 'text': {'text'}, 'wait': {'duration_ms'}} |
| 359 | optional = {'click': {'button','count'}, 'drag': {'button','duration_ms'}, 'scroll': {'amount'}} |
| 360 | 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()): |
| 361 | raise ValueError('Use the fields listed for this desktop action.') |
| 362 | for key in ('x','y','to_x','to_y'): |
| 363 | if key in item and (type(item[key]) is not int or not 0 <= item[key] <= 16384): |
| 364 | raise ValueError('Use nonnegative screenshot pixel coordinates.') |
| 365 | if 'button' in item and item['button'] not in ('left','right','middle'): |
| 366 | raise ValueError('Choose the left, right, or middle mouse button.') |
| 367 | for key,maximum in [('count',3),('amount',100),('duration_ms',5000)]: |
| 368 | if key in item and (type(item[key]) is not int or not 1 <= item[key] <= maximum): |
| 369 | raise ValueError(f'Action {index}: {key} must be an integer from 1 to {maximum}.') |
| 370 | if kind == 'scroll' and item['direction'] not in ('up','down','left','right'): |
| 371 | raise ValueError('Choose a scroll direction.') |
| 372 | if kind == 'key': |
| 373 | if not isinstance(item['keys'],list) or not 1 <= len(item['keys']) <= 8 or any(not isinstance(k,str) for k in item['keys']): |
| 374 | raise ValueError('Send between 1 and 8 key names.') |
| 375 | for key in item['keys']: keycode(key) |
| 376 | if kind == 'text' and (not isinstance(item['text'],str) or len(item['text'].encode()) > 16384): |
| 377 | raise ValueError('Type at most 16 KiB of text at a time.') |
| 378 | if sum(a.get('duration_ms', DRAG_DURATION_MS if a['type'] == 'drag' else 0) for a in actions) > 15000: |
| 379 | raise ValueError('Waits and drags must total at most 15 seconds.') |
| 380 | |
| 381 | |
| 382 | def record_operation(name, operation): |
| 383 | path = vms.DISKS / name / 'computer-operation.json' |
| 384 | temporary = path.with_suffix('.new') |
| 385 | with open(temporary, 'w', opener=lambda p, flags: os.open(p, flags | os.O_NOFOLLOW, 0o600)) as outgoing: |
| 386 | json.dump(operation, outgoing) |
| 387 | os.replace(temporary, path) |
| 388 | |
| 389 | |
| 390 | def finish_operation(name, operation): |
| 391 | token = DEADLINE.set(time.monotonic() + 60) |
| 392 | active = OPERATION.set(None) |
| 393 | try: |
| 394 | if operation['platform'] == 'linux': |
| 395 | guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [], |
| 396 | json.dumps({'_stop': operation['id']}).encode(), timeout=18) |
| 397 | elif operation.get('worker'): |
| 398 | directory = 'C:\\ProgramData\\SnowglobeComputer\\' + operation['id'] |
| 399 | try: |
| 400 | guest_exec(name, operation['worker'], ['--release' if operation.get('keep_spawn') else '--cleanup', directory], timeout=58) |
| 401 | except ValueError: |
| 402 | pass |
| 403 | else: |
| 404 | (vms.DISKS / name / 'computer-operation.json').unlink() |
| 405 | return |
| 406 | if operation['platform'] == 'windows': |
| 407 | run = operation['id'] |
| 408 | directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run |
| 409 | cleanup = "$ErrorActionPreference='Stop';$d='"+directory+"';" |
| 410 | cleanup += "if(Test-Path -LiteralPath ($d+'\\request.json')){Remove-Item -LiteralPath ($d+'\\request.json') -Force};" |
| 411 | cleanup += "$s=New-Object -ComObject Schedule.Service;$s.Connect();$f=$s.GetFolder('\\');" |
| 412 | cleanup += "$t=$f.GetTasks(1) | Where-Object {$_.Name -eq 'SnowglobeComputer-"+run+"'};" |
| 413 | if not operation.get('keep_spawn'): |
| 414 | if operation.get('worker'): |
| 415 | cleanup += "[Reflection.Assembly]::LoadFrom('"+operation['worker']+"')|Out-Null;[ComputerProcess]::Stop($d+'\\request.json');" |
| 416 | 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}});" |
| 417 | if not operation.get('worker'): |
| 418 | cleanup += "if($t){[Reflection.Assembly]::LoadFrom($t.Definition.Actions.Item(1).Path)|Out-Null;[ComputerProcess]::Stop($d+'\\request.json')};" |
| 419 | cleanup += "if($t){$t.Stop(0);while($t.State -eq 4){Start-Sleep -Milliseconds 100}};" |
| 420 | cleanup += "foreach($p in $workers){if(!$p.WaitForExit(10000)){throw 'Process cleanup failed'};$p.Dispose()};" |
| 421 | cleanup += "if($t){$f.DeleteTask('SnowglobeComputer-"+run+"',0)};" |
| 422 | if not operation.get('keep_spawn'): |
| 423 | cleanup += "if(Test-Path -LiteralPath $d){Remove-Item -LiteralPath $d -Recurse -Force}" |
| 424 | powershell(name, cleanup, timeout=58) |
| 425 | (vms.DISKS / name / 'computer-operation.json').unlink() |
| 426 | except Exception as error: |
| 427 | raise BlockingIOError('The previous operation has not stopped. Check its screenshot, then retry when the guest agent reconnects.') from error |
| 428 | finally: |
| 429 | OPERATION.reset(active) |
| 430 | DEADLINE.reset(token) |
| 431 | |
| 432 | |
| 433 | def worker(name, platform, request): |
| 434 | if platform == 'macos': |
| 435 | source = Path(__file__).resolve().parent.parent / 'guest/computer-legacy-mac.py' |
| 436 | bootstrap = "import json,sys; request=json.load(sys.stdin); exec(compile(request.pop('_worker'),'<computer>','exec'))" |
| 437 | environment = dict(os.environ, SSH_ORIGINAL_COMMAND='/usr/bin/python -c ' + shlex.quote(bootstrap)) |
| 438 | result = subprocess.run([sys.executable, str(Path(__file__).with_name('vm-ssh.py')), name], |
| 439 | input=json.dumps({**request, '_worker':source.read_text()}).encode(), |
| 440 | capture_output=True, timeout=remaining(200), env=environment) |
| 441 | if result.returncode: |
| 442 | raise ValueError('The private guest connection failed. Wait for desktop setup to finish, then retry.') |
| 443 | reply = json.loads(result.stdout) |
| 444 | if 'error' in reply: raise ValueError(reply['error']) |
| 445 | return reply |
| 446 | if platform == 'linux': |
| 447 | source = Path(__file__).with_name('mac-computer.py').read_text() |
| 448 | identity = OPERATION.get() |
| 449 | unit = 'snowglobe-computer-request-' + (identity[1].removeprefix('operation:') if identity else uuid.uuid4().hex) |
| 450 | guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [], |
| 451 | json.dumps({'_prepare': unit}).encode()) |
| 452 | operation = {'platform': platform, 'id': unit} |
| 453 | record_operation(name, operation) |
| 454 | try: |
| 455 | raw = guest_exec(name, '/etc/qemu-ga/fsfreeze-hook.d/snowglobe-computer', [], |
| 456 | json.dumps({**request, '_worker': source, '_unit': unit, |
| 457 | '_timeout': max(1, int(remaining(200)) - 5)}).encode(), timeout=220) |
| 458 | except BaseException: |
| 459 | finish_operation(name, operation) |
| 460 | raise |
| 461 | (vms.DISKS / name / 'computer-operation.json').unlink() |
| 462 | result = json.loads(raw) |
| 463 | if 'error' in result: |
| 464 | if result.get('status') == 409: |
| 465 | raise BlockingIOError(result['error']) |
| 466 | raise ValueError(result['error']) |
| 467 | return result |
| 468 | if platform == 'windows': |
| 469 | identity = OPERATION.get() |
| 470 | run = identity[1].removeprefix('operation:') if identity else uuid.uuid4().hex |
| 471 | directory = 'C:\\ProgramData\\SnowglobeComputer\\' + run |
| 472 | artifact = os.environ.get('SNOWGLOBE_COMPUTER_WINDOWS_WORKER') |
| 473 | if not artifact: |
| 474 | raise ValueError('The Windows computer worker is unavailable on the host.') |
| 475 | content = Path(artifact).read_bytes() |
| 476 | digest = hashlib.sha256(content).hexdigest()[:16] |
| 477 | installed_worker = 'C:\\ProgramData\\SnowglobeComputer\\worker-' + digest + '.exe' |
| 478 | try: |
| 479 | read_file(name, installed_worker, length=1) |
| 480 | except ValueError: |
| 481 | staging = 'C:\\Windows\\Temp\\snowglobe-worker-' + uuid.uuid4().hex |
| 482 | try: |
| 483 | guest_exec(name, 'cmd.exe', ['/d', '/c', 'mkdir', staging]) |
| 484 | write_file(name, staging + '\\worker.exe', content) |
| 485 | guest_exec(name, staging + '\\worker.exe', ['--install', installed_worker]) |
| 486 | finally: |
| 487 | cleanup_deadline = DEADLINE.set(time.monotonic() + 15) |
| 488 | cleanup_operation = OPERATION.set(None) |
| 489 | try: |
| 490 | guest_exec(name, 'cmd.exe', ['/d', '/c', 'rmdir', '/s', '/q', staging, '&', 'exit', '/b', '0']) |
| 491 | finally: |
| 492 | OPERATION.reset(cleanup_operation) |
| 493 | DEADLINE.reset(cleanup_deadline) |
| 494 | guest_exec(name, installed_worker, ['--prepare', directory]) |
| 495 | try: |
| 496 | read_file(name, 'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe', length=1) |
| 497 | installed = True |
| 498 | except ValueError: |
| 499 | installed = False |
| 500 | ahk=os.environ.get('SNOWGLOBE_COMPUTER_AHK') |
| 501 | if not installed and ahk: |
| 502 | write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey.exe',(Path(ahk)/'AutoHotkey32.exe').read_bytes()) |
| 503 | write_file(name,'C:\\ProgramData\\SnowglobeComputer\\AutoHotkey-license.txt',(Path(ahk)/'license.txt').read_bytes()) |
| 504 | write_file(name,directory+'\\request.json',json.dumps(request).encode()) |
| 505 | operation = {'platform': platform, 'id': run, 'worker': installed_worker} |
| 506 | record_operation(name, operation) |
| 507 | try: |
| 508 | guest_exec(name, installed_worker, ['--launch', directory+'\\request.json', directory+'\\response.json']) |
| 509 | deadline=time.monotonic()+remaining(200) |
| 510 | while time.monotonic()<deadline: |
| 511 | try: |
| 512 | raw=read_file(name,directory+'\\response.json',length=2*1024*1024) |
| 513 | result=json.loads(raw) |
| 514 | if 'error' in result: raise RuntimeError(result['error']) |
| 515 | if request['action'] == 'spawn': |
| 516 | record_operation(name, {**operation, 'keep_spawn': True}) |
| 517 | operation['keep_spawn'] = True |
| 518 | return result |
| 519 | except ValueError: |
| 520 | time.sleep(.2) |
| 521 | raise ValueError('The desktop worker timed out. Check the VM before retrying.') |
| 522 | except RuntimeError as error: |
| 523 | raise ValueError(str(error)) from None |
| 524 | finally: |
| 525 | try: |
| 526 | finish_operation(name, operation) |
| 527 | except BlockingIOError: |
| 528 | if not operation.get('keep_spawn'): |
| 529 | raise |
| 530 | raise ValueError('This guest needs its platform worker installed.') |
| 531 | |
| 532 | |
| 533 | |
| 534 | def handle(name, request, *, active_snapshot=None): |
| 535 | """Admission snapshots cannot start guest work on reserved control workers.""" |
| 536 | validate(request) |
| 537 | vms.ensure_name(name) |
| 538 | if request['action'] == 'process' and request['id'].startswith('operation:'): |
| 539 | if vms.mac_vms.owns(name): |
| 540 | raise ValueError('This VM does not provide active operation handles.') |
| 541 | identity = request['id'] |
| 542 | with vms.computer_control(name) as control: |
| 543 | active = vms.computer_activity(name) |
| 544 | if active is not None: |
| 545 | if active.get('id') != identity: |
| 546 | return {'running': False} |
| 547 | if request['operation'] == 'stop': |
| 548 | vms.computer_cancel(control, identity) |
| 549 | else: |
| 550 | active['stopping'] = control.read() == identity |
| 551 | return {'running': True, **active, 'stopping': request['operation'] == 'stop' or active.get('stopping', False)} |
| 552 | try: |
| 553 | with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer-operation.json')) as incoming: |
| 554 | unfinished = json.load(incoming) |
| 555 | except FileNotFoundError: |
| 556 | return {'running': False} |
| 557 | if 'operation:' + unfinished['id'].removeprefix('snowglobe-computer-request-') != identity: |
| 558 | return {'running': False} |
| 559 | if request['operation'] == 'status' or active_snapshot is not None: |
| 560 | return {'running': None, 'recovery_required': True, 'id': identity} |
| 561 | with vms.computer_lock(name, 'cancel', identity): |
| 562 | try: |
| 563 | with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer-operation.json')) as incoming: |
| 564 | unfinished = json.load(incoming) |
| 565 | except FileNotFoundError: |
| 566 | return {'running': False} |
| 567 | if 'operation:' + unfinished['id'].removeprefix('snowglobe-computer-request-') != identity: |
| 568 | return {'running': False} |
| 569 | finish_operation(name, unfinished) |
| 570 | return {'running': False, 'cancelled': True, 'id': identity} |
| 571 | if vms.mac_vms.owns(name): |
| 572 | return vms.mac_vms.handle('computer', {'name':name,'request':request}) |
| 573 | token = DEADLINE.set(time.monotonic() + 210) |
| 574 | operation_token = None |
| 575 | try: |
| 576 | with contextlib.ExitStack() as scope: |
| 577 | busy = active_snapshot is not None |
| 578 | if not busy: |
| 579 | try: |
| 580 | active = scope.enter_context(vms.computer_lock(name, request['action'])) |
| 581 | operation_token = OPERATION.set((name, active['id'])) |
| 582 | except BlockingIOError: |
| 583 | if request['action'] not in ('status', 'shot'): |
| 584 | raise BlockingIOError('Another computer operation is running. Read computer_status before retrying.') from None |
| 585 | busy = True |
| 586 | root = ET.fromstring(vms.virsh('dumpxml',name,timeout=remaining(15))) |
| 587 | power = vms.virsh('domstate',name,timeout=remaining(15)).strip() |
| 588 | platform = root.findtext(f'metadata/{vms.NS}vm/{vms.NS}platform','linux') |
| 589 | status = {'platform': platform, 'power_state': power, 'readiness': 'starting', |
| 590 | 'cpu_acceleration': {'kvm': 'hardware', 'qemu': 'emulated'}.get(root.get('type')), |
| 591 | 'capabilities': ['shot', 'input'] if power == 'running' else [], |
| 592 | 'runtimes': [], 'coordinate_units': 'screenshot_pixels'} |
| 593 | if power != 'running': |
| 594 | if request['action'] == 'status': |
| 595 | return {**status, 'readiness': 'stopped' if power == 'shut off' else power} |
| 596 | raise ValueError('Start the VM before using its computer tools.') |
| 597 | ai = import_module('vm-ai') |
| 598 | if not busy: |
| 599 | ai.lease(name, ai.LIMITS['idle_seconds']) |
| 600 | if request['action'] == 'shot': |
| 601 | return asyncio.run(desktop(name)) |
| 602 | if busy: |
| 603 | active = active_snapshot or vms.computer_activity(name) |
| 604 | return {**status, 'readiness': 'busy', 'capabilities': ['shot'], 'active_operation': active} |
| 605 | try: |
| 606 | with os.fdopen(vms.open_regular(vms.DISKS / name / 'computer-operation.json')) as incoming: |
| 607 | unfinished = json.load(incoming) |
| 608 | except FileNotFoundError: |
| 609 | pass |
| 610 | else: |
| 611 | if request['action'] == 'status': |
| 612 | active = {**unfinished, 'id': 'operation:' + unfinished['id'].removeprefix('snowglobe-computer-request-')} |
| 613 | return {**status, 'readiness': 'recovery_required', 'capabilities': ['shot'], 'active_operation': active, |
| 614 | 'worker_error': 'The previous operation needs cleanup. Inspect its screenshot or restart the VM before retrying.'} |
| 615 | finish_operation(name, unfinished) |
| 616 | native_text = True |
| 617 | if platform in ('windows', 'linux') and (request['action'] == 'status' or request['action'] == 'input' and any(a['type'] == 'text' for a in request['actions'])): |
| 618 | try: |
| 619 | info = qga(name, 'guest-info') |
| 620 | native_text = any(command['name'] == 'guest-exec' and command['enabled'] for command in info['supported_commands']) |
| 621 | except (ValueError, TimeoutError): |
| 622 | native_text = False |
| 623 | if request['action'] == 'status': |
| 624 | return {**status, 'readiness': 'agent_unavailable', 'text_encoding': 'ascii', |
| 625 | 'worker_error': 'The guest agent is unavailable. Wait for startup or ask an administrator to check its installation.'} |
| 626 | if request['action'] == 'status': |
| 627 | if not native_text: |
| 628 | return {**status, 'readiness': 'agent_unsupported', 'text_encoding': 'ascii', |
| 629 | 'worker_error': 'The guest agent does not support commands. Use console input or ask an administrator to update the agent.'} |
| 630 | if platform == 'windows' and root.findtext(f'metadata/{vms.NS}vm/{vms.NS}setup') in {'windows-v3', 'windows-v4'}: |
| 631 | setup = vms.windows_setup(name) |
| 632 | if setup != 'ready': |
| 633 | return {**status, 'readiness': 'setup_failed' if setup == 'failed' else 'personalizing', 'setup': setup, |
| 634 | 'worker_error': 'Windows account setup failed. Inspect the console and C:\\Snowglobe\\setup.log.' if setup == 'failed' else 'Windows is applying the VM account. Wait for setup to finish.'} |
| 635 | status['readiness'] = 'worker_starting' |
| 636 | try: |
| 637 | status.update(worker(name,platform,request)) |
| 638 | status['readiness'] = 'ready' |
| 639 | except (ValueError, TimeoutError) as error: |
| 640 | status['worker_error'] = str(error) |
| 641 | return status |
| 642 | if request['action'] == 'input': |
| 643 | return asyncio.run(desktop(name,request['actions'],platform,request.get('screenshot',True),native_text)) |
| 644 | if platform=='windows' and request['action']=='file' and request['operation'] in ('read','write'): |
| 645 | path=request['path'] |
| 646 | if len(path)<3 or path[1:3] not in (':\\',':/') or not path[0].isalpha(): raise ValueError('Use an absolute guest file path.') |
| 647 | if request['operation']=='read': |
| 648 | data=read_file(name,path,request.get('offset',0),request.get('length',65536)) |
| 649 | return {'data':base64.b64encode(data).decode(),'bytes':len(data),'chunk_bytes':FILE_CHUNK} |
| 650 | data=base64.b64decode(request['data'],validate=True) |
| 651 | write_file(name,path,data,offset=request.get('offset',0)) |
| 652 | return {'bytes':len(data),'chunk_bytes':FILE_CHUNK} |
| 653 | result = worker(name,platform,request) |
| 654 | if request.get('screenshot'): |
| 655 | try: result.update(asyncio.run(desktop(name))) |
| 656 | except (ValueError,OSError,TimeoutError): result['capture_error']='Screen capture failed. Retry computer_shot.' |
| 657 | return result |
| 658 | finally: |
| 659 | if operation_token is not None: |
| 660 | OPERATION.reset(operation_token) |
| 661 | DEADLINE.reset(token) |