1#!/usr/bin/env python3
2import argparse
3import contextlib
4import fcntl
5import hashlib
6import json
7import logging
8import multiprocessing
9import os
10from pathlib import Path
11import select
12import secrets
13import shlex
14import signal
15import socket
16import socketserver
17import struct
18import subprocess
19import sys
20import tempfile
21import threading
22import time
23
24
25ROOT = Path(os.environ.get("MAC_AGENT_VM_HOME", "/Volumes/Documents/VM/macOS Code Sandbox"))
26TART = ROOT / "runtime/tart.app/Contents/MacOS/tart"
27NAME = "gui-agent"
28PRIVATE = ROOT / "private"
29
30
31def tart(*args, **kwargs):
32 return subprocess.run([str(TART), *args], env={**os.environ, "TART_HOME": str(ROOT / "state")},
33 check=True, **kwargs)
34
35
36def ssh_args():
37 return ["ssh", "-F", "/dev/null", "-i", str(PRIVATE / "ssh_key"),
38 "-o", "IdentitiesOnly=yes", "-o", "BatchMode=yes", "-o", "ForwardAgent=no",
39 "-o", "StrictHostKeyChecking=yes", "-o", "HostKeyAlias=" + NAME,
40 "-o", f'UserKnownHostsFile="{PRIVATE / "known_hosts"}"',
41 "-o", "GlobalKnownHostsFile=/dev/null", "-o", "ConnectTimeout=10"]
42
43
44def exact(stream, size):
45 data = bytearray()
46 while len(data) < size:
47 chunk = stream.recv(size - len(data))
48 if not chunk:
49 raise ConnectionError("VNC connection closed")
50 data.extend(chunk)
51 return bytes(data)
52
53
54def wait_for_state(state):
55 for _ in range(90):
56 rows = json.loads(tart("list", "--format", "json", capture_output=True, text=True).stdout)
57 if next(row for row in rows if row["Name"] == NAME and row["Source"] == "local")["State"] == state:
58 return
59 time.sleep(1)
60 raise ValueError(f"The guest has not reached {state}; inspect status before another lifecycle action.")
61
62
63@contextlib.contextmanager
64def guest_screen():
65 ip = tart("ip", NAME, capture_output=True, text=True, timeout=5).stdout.strip()
66 with tempfile.TemporaryDirectory(prefix="vnc-", dir=PRIVATE) as directory:
67 path = Path(directory) / "console.sock"
68 tunnel = subprocess.Popen([*ssh_args(), "-N", "-o", "ExitOnForwardFailure=yes",
69 "-o", "ServerAliveInterval=5", "-o", "ServerAliveCountMax=2",
70 "-o", "StreamLocalBindMask=0177", "-L",
71 f"{path}:127.0.0.1:5900", "admin@" + ip],
72 stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
73 stderr=subprocess.DEVNULL)
74 try:
75 for _ in range(100):
76 if tunnel.poll() is not None:
77 raise ValueError("The guest console is unavailable. Wait for the VM to start, then reconnect.")
78 if path.exists():
79 break
80 time.sleep(0.1)
81 else:
82 raise ValueError("The guest console tunnel did not start. Reconnect to try again.")
83 with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as upstream:
84 upstream.settimeout(10)
85 upstream.connect(str(path))
86 authenticate_screen(upstream)
87 yield upstream
88 finally:
89 tunnel.terminate()
90 try:
91 tunnel.wait(timeout=5)
92 except subprocess.TimeoutExpired:
93 tunnel.kill()
94 tunnel.wait()
95
96
97def authenticate_screen(upstream):
98 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
99 password = (PRIVATE / "password").read_bytes()
100 if len(password) > 63:
101 raise ValueError("Apple Screen Sharing supports at most 63 UTF-8 password bytes.")
102 if not exact(upstream, 12).startswith(b"RFB 003."):
103 raise ValueError("Unsupported VNC server version")
104 upstream.sendall(b"RFB 003.008\n")
105 kinds = exact(upstream, exact(upstream, 1)[0])
106 if 30 not in kinds:
107 raise ValueError("The guest does not offer Apple Screen Sharing authentication.")
108 upstream.sendall(b"\x1e")
109 generator, size = struct.unpack(">HH", exact(upstream, 4))
110 if not 16 <= size <= 512:
111 raise ValueError("The guest sent an unsupported Screen Sharing key size.")
112 prime = int.from_bytes(exact(upstream, size), "big")
113 public = int.from_bytes(exact(upstream, size), "big")
114 if not 1 < generator < prime or not 1 < public < prime - 1:
115 raise ValueError("The guest sent invalid Screen Sharing key parameters.")
116 private = secrets.randbelow(prime - 3) + 2
117 client_public = pow(generator, private, prime).to_bytes(size, "big")
118 shared = pow(public, private, prime).to_bytes(size, "big")
119 credentials = bytearray(secrets.token_bytes(128))
120 credentials[:6] = b"admin\0"
121 credentials[64:65 + len(password)] = password + b"\0"
122 # MD5 and AES-ECB are Apple RFB type 30's wire protocol, carried inside pinned SSH.
123 encryptor = Cipher(algorithms.AES(hashlib.md5(shared).digest()), modes.ECB()).encryptor()
124 upstream.sendall(encryptor.update(bytes(credentials)) + encryptor.finalize() + client_public)
125 if exact(upstream, 4) != b"\0\0\0\0":
126 raise ValueError("VNC authentication failed")
127
128
129def relay_screen(upstream, incoming, outgoing):
130 def read(size):
131 data = bytearray()
132 while len(data) < size:
133 chunk = os.read(incoming, size - len(data))
134 if not chunk:
135 raise ConnectionError("The viewer disconnected.")
136 data.extend(chunk)
137 return bytes(data)
138
139 def write(data):
140 while data:
141 data = data[os.write(outgoing, data):]
142
143 write(b"RFB 003.008\n")
144 if not read(12).startswith(b"RFB 003."):
145 raise ValueError("Unsupported viewer protocol.")
146 write(b"\x01\x01")
147 if read(1) != b"\x01":
148 return
149 write(b"\0\0\0\0")
150 upstream.settimeout(None)
151 while True:
152 ready, _, _ = select.select([upstream, incoming], [], [], 300)
153 for source in ready:
154 data = os.read(source if isinstance(source, int) else source.fileno(), 65536)
155 if not data:
156 return
157 if source is upstream:
158 write(data)
159 else:
160 upstream.sendall(data)
161
162
163def console(host, port):
164 from websockify.auth_plugins import AuthenticationError
165 from websockify.websocketproxy import ProxyRequestHandler, WebSocketProxy
166
167 def terminate(signum, frame):
168 raise SystemExit(0)
169
170 signal.signal(signal.SIGTERM, terminate)
171 logging.basicConfig(level=logging.INFO, format="%(message)s")
172 multiprocessing.set_start_method("fork")
173
174 class SameOrigin:
175 def authenticate(self, headers, target_host, target_port):
176 if headers.get("Host") != f"{host}:{port}" or headers.get("Origin") != f"http://{host}:{port}":
177 raise AuthenticationError(response_msg="This console accepts only its configured origin.")
178
179 class ConsoleHandler(ProxyRequestHandler):
180 def do_GET(self):
181 if self.headers.get("Host") != f"{host}:{port}":
182 self.send_error(403)
183 return
184 super().do_GET()
185
186 class Relay(socketserver.BaseRequestHandler):
187 def handle(self):
188 with guest_screen() as upstream:
189 relay_screen(upstream, self.request.fileno(), self.request.fileno())
190
191 with socketserver.ThreadingTCPServer(("127.0.0.1", 0), Relay) as server:
192 server.daemon_threads = True
193 threading.Thread(target=server.serve_forever, daemon=True).start()
194 print(f"http://{host}:{port}/vnc_lite.html?autoconnect=true&resize=scale", flush=True)
195 try:
196 WebSocketProxy(RequestHandlerClass=ConsoleHandler, listen_host=host, listen_port=port,
197 web=str(ROOT / "runtime/novnc"), file_only=True, auth_plugin=SameOrigin(),
198 target_host="127.0.0.1", target_port=server.server_address[1]).start_server()
199 finally:
200 server.shutdown()
201
202
203def main():
204 parser = argparse.ArgumentParser(description="Manage this Mac's isolated GUI-agent VM.")
205 sub = parser.add_subparsers(dest="action", required=True)
206 sub.add_parser("start")
207 sub.add_parser("status")
208 sub.add_parser("stop")
209 sub.add_parser("suspend")
210 sub.add_parser("reboot")
211 for action in ["exec", "ssh"]:
212 command = sub.add_parser(action)
213 command.add_argument("command", nargs=argparse.REMAINDER)
214 forward = sub.add_parser("forward", help="Reach a guest loopback server from this Mac")
215 forward.add_argument("port", type=int, help="Guest server port")
216 forward.add_argument("--local-port", type=int, help="Host loopback port (default: same as guest)")
217 viewer = sub.add_parser("console")
218 viewer.add_argument("--listen", default="127.0.0.1")
219 viewer.add_argument("--port", type=int, default=17891)
220 args = parser.parse_args()
221 if args.action == "forward":
222 local_port = args.local_port if args.local_port is not None else args.port
223 if not 1 <= args.port <= 65535 or not 1 <= local_port <= 65535:
224 parser.error("Ports must be between 1 and 65535.")
225 ip = tart("ip", NAME, capture_output=True, text=True, timeout=5).stdout.strip()
226 print(f"Forwarding http://127.0.0.1:{local_port} to guest port {args.port}; Ctrl-C closes the tunnel.",
227 file=sys.stderr, flush=True)
228 os.execvp("ssh", [*ssh_args(), "-N", "-T", "-o", "ExitOnForwardFailure=yes",
229 "-o", "ServerAliveInterval=5", "-o", "ServerAliveCountMax=2",
230 "-L", f"127.0.0.1:{local_port}:127.0.0.1:{args.port}", "admin@" + ip])
231 if args.action in {"start", "stop", "suspend", "reboot"}:
232 lock = (PRIVATE / "lifecycle.lock").open("a")
233 os.chmod(PRIVATE / "lifecycle.lock", 0o600)
234 try:
235 fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
236 except BlockingIOError:
237 raise ValueError("Another VM power operation is running. Wait for it to finish.") from None
238 lock.seek(0)
239 lock.truncate()
240 lock.write(args.action)
241 lock.flush()
242 if args.action == "reboot":
243 tart("exec", NAME, "sudo", "-n", "shutdown", "-h", "now", timeout=15)
244 wait_for_state("stopped")
245 args.action = "start"
246 if args.action == "start":
247 rows = json.loads(tart("list", "--format", "json", capture_output=True, text=True).stdout)
248 if next(row for row in rows if row["Name"] == NAME and row["Source"] == "local")["Running"]:
249 print("GUI-agent VM is already running.")
250 return
251 # Closing an agent session kills every process it ever spawned, so sshd must own the VM.
252 run = shlex.join(["nohup", "/usr/bin/env", f"TART_HOME={ROOT / 'state'}", "/usr/bin/caffeinate", "-i",
253 str(TART), "run", NAME, "--no-graphics", "--no-audio", "--no-usb-accessories",
254 "--no-clipboard", "--suspendable", "--root-disk-opts=sync=full"])
255 (PRIVATE / "run.log").write_text("")
256 os.chmod(PRIVATE / "run.log", 0o600)
257 subprocess.run(["ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=10", "localhost",
258 f"{run} >{shlex.quote(str(PRIVATE / 'run.log'))} 2>&1 </dev/null &"], check=True)
259 deadline = time.monotonic() + 90
260 while time.monotonic() < deadline:
261 if subprocess.run(["pgrep", "-f", f"tart run {NAME}"], capture_output=True).returncode:
262 raise ValueError("VM startup failed; inspect the private run log locally.")
263 try:
264 tart("exec", NAME, "/usr/bin/true", capture_output=True, timeout=5)
265 except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
266 time.sleep(1)
267 else:
268 print("GUI-agent VM is ready in its logged-in guest session.")
269 return
270 raise ValueError("VM startup is still pending; inspect status before retrying.")
271 elif args.action == "status":
272 tart("get", NAME)
273 elif args.action == "exec":
274 if not args.command:
275 parser.error("exec requires a guest command")
276 tart("exec", "-i", NAME, *args.command)
277 elif args.action == "ssh":
278 ip = tart("ip", NAME, capture_output=True, text=True).stdout.strip()
279 command = ssh_args()
280 if not args.command:
281 command.append("-t")
282 os.execvp("ssh", [*command, "admin@" + ip, *args.command])
283 elif args.action == "stop":
284 tart("exec", NAME, "sudo", "-n", "shutdown", "-h", "now")
285 wait_for_state("stopped")
286 print("GUI-agent VM shut down.")
287 elif args.action == "suspend":
288 tart("suspend", NAME)
289 wait_for_state("suspended")
290 print("GUI-agent VM suspended.")
291 elif args.action == "console":
292 python = ROOT / "runtime/venv/bin/python"
293 if Path(sys.prefix) != python.parent.parent:
294 os.execv(str(python), [str(python), __file__, *sys.argv[1:]])
295 console(args.listen, args.port)
296
297
298if __name__ == "__main__":
299 try:
300 main()
301 except (ValueError, OSError, subprocess.CalledProcessError) as error:
302 print(str(error), file=sys.stderr)
303 sys.exit(1)