Skip to main content

agent_first_http/host/takeover/
mod.rs

1//! In-container human-takeover supervision.
2//!
3//! Takeover providers are process-level adapters behind `/takeover/panel`, each
4//! a concrete screen-share method selected by `--takeover-provider` (parallel
5//! to how `--browser` selects an engine). Add one by giving
6//! [`TakeoverProviderKind`] a variant and a `match` arm in [`launch_provider`]
7//! and [`TakeoverProxyState::proxy`]. KasmVNC is the only provider today and is
8//! kept as an external GPL process: afhttp only starts `Xvnc`, waits for its X
9//! display + localhost web listener, and reverse-proxies the web client from
10//! the authenticated host listener.
11
12use std::net::SocketAddr;
13use std::path::{Path, PathBuf};
14use std::process::Stdio;
15use std::sync::Arc;
16use std::time::Duration;
17
18use axum::body::{Body, to_bytes};
19use axum::extract::ws::rejection::WebSocketUpgradeRejection;
20use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade};
21use axum::http::{HeaderMap, StatusCode, Uri, header};
22use axum::response::{IntoResponse, Response};
23use futures::{SinkExt, StreamExt};
24// KasmVNC takeover is Unix-only (Xvnc + a unix-socket control channel); these
25// imports are used solely by the `#[cfg(unix)]` launch path below. On Windows
26// `launch_kasmvnc_provider()` returns BackendUnsupported, so the rest of
27// afhttp — fetch, host, takeover-panel screencast — still builds and runs.
28#[cfg(unix)]
29use tokio::io::AsyncBufReadExt;
30#[cfg(unix)]
31use tokio::net::UnixStream;
32use tokio::process::Child;
33#[cfg(unix)]
34use tokio::process::Command;
35use tokio::sync::Mutex;
36use tokio::task::JoinHandle;
37use tokio_tungstenite::tungstenite;
38
39use crate::host::bootstrap::TakeoverProviderKind;
40use crate::host::browser::{pick_ephemeral_port, resolve_named_bin, wait_for_tcp_ready};
41use crate::shared::error::{Error, ErrorCode};
42
43/// Virtual framebuffer geometry for the takeover display. No window manager
44/// runs on the X display, so the headful browser window is pinned to this size
45/// (see `AppState::launch`) to fill the framebuffer.
46pub const DISPLAY_WIDTH: u16 = 1280;
47pub const DISPLAY_HEIGHT: u16 = 720;
48
49/// Running KasmVNC display. Cloning the surrounding `Arc` keeps the process
50/// alive until the listener state is dropped.
51pub struct ProviderHandle {
52    pub display: String,
53    pub web_port: u16,
54    /// Whether a window manager is running on the display. With one, the
55    /// browser is kept maximized so the client can use `resize=remote` (the
56    /// framebuffer tracks the browser window exactly); without one, the client
57    /// must fall back to `resize=scale` (letterboxed).
58    pub window_manager: bool,
59    _rfb_port: u16,
60    child: Mutex<Child>,
61    wm_child: Option<Mutex<Child>>,
62    stderr_task: Option<JoinHandle<()>>,
63}
64
65impl Drop for ProviderHandle {
66    fn drop(&mut self) {
67        if let Some(task) = self.stderr_task.take() {
68            task.abort();
69        }
70        if let Some(wm) = &self.wm_child
71            && let Ok(mut child) = wm.try_lock()
72        {
73            let _ = child.start_kill();
74        }
75        if let Ok(mut child) = self.child.try_lock() {
76            let _ = child.start_kill();
77        }
78    }
79}
80
81/// Launch a display provider and return the runtime state used by `/takeover/panel`.
82pub async fn launch_provider(
83    provider: TakeoverProviderKind,
84    quality: u8,
85) -> Result<TakeoverProxyState, Error> {
86    match provider {
87        TakeoverProviderKind::KasmVnc => Ok(TakeoverProxyState::new(
88            provider,
89            launch_kasmvnc_provider().await?,
90            quality,
91        )),
92    }
93}
94
95/// Launch KasmVNC's `Xvnc` and wait until both the X display socket and the
96/// embedded web client are reachable on localhost.
97pub async fn launch_kasmvnc_provider() -> Result<Arc<ProviderHandle>, Error> {
98    #[cfg(not(unix))]
99    {
100        return Err(Error::new(
101            ErrorCode::BackendUnsupported,
102            "KasmVNC display provider is only supported on Unix-like container hosts",
103        ));
104    }
105
106    #[cfg(unix)]
107    {
108        launch_kasmvnc_unix().await
109    }
110}
111
112#[cfg(unix)]
113async fn launch_kasmvnc_unix() -> Result<Arc<ProviderHandle>, Error> {
114    let bin = resolve_kasmvnc_bin()?;
115    let web_root = resolve_kasmvnc_web_root()?;
116    let display_num = pick_display_number()?;
117    let display = format!(":{display_num}");
118    let web_port = pick_ephemeral_port().map_err(|e| {
119        Error::new(
120            ErrorCode::BrowserLaunchFailed,
121            format!("could not reserve KasmVNC web port: {e}"),
122        )
123    })?;
124    let rfb_port = pick_ephemeral_port().map_err(|e| {
125        Error::new(
126            ErrorCode::BrowserLaunchFailed,
127            format!("could not reserve KasmVNC VNC port: {e}"),
128        )
129    })?;
130
131    let mut cmd = Command::new(&bin);
132    cmd.arg(&display)
133        .arg("-geometry")
134        .arg(format!("{DISPLAY_WIDTH}x{DISPLAY_HEIGHT}"))
135        .arg("-depth")
136        .arg("24")
137        .arg("-interface")
138        .arg("127.0.0.1")
139        .arg("-rfbport")
140        .arg(rfb_port.to_string())
141        .arg("-websocketPort")
142        .arg(web_port.to_string())
143        .arg("-httpd")
144        .arg(web_root)
145        .arg("-sslOnly=0")
146        .arg("-SecurityTypes")
147        .arg("None")
148        .arg("-disableBasicAuth")
149        .arg("-PublicIP")
150        .arg("127.0.0.1")
151        .arg("-Log")
152        .arg("*:stderr:30")
153        .kill_on_drop(true)
154        .stdout(Stdio::null())
155        .stderr(Stdio::piped());
156
157    let mut child = cmd.spawn().map_err(|e| {
158        Error::new(
159            ErrorCode::BrowserLaunchFailed,
160            format!("spawn KasmVNC Xvnc {}: {e}", bin.display()),
161        )
162    })?;
163    let stderr_task = child.stderr.take().map(|stderr| {
164        tokio::spawn(async move {
165            let mut lines = tokio::io::BufReader::new(stderr).lines();
166            while matches!(lines.next_line().await, Ok(Some(_))) {}
167        })
168    });
169
170    if let Err(e) = wait_for_x_display(display_num, Duration::from_secs(10)).await {
171        let _ = child.start_kill();
172        return Err(Error::new(
173            ErrorCode::BrowserLaunchFailed,
174            format!("KasmVNC display {display} did not become ready: {e}"),
175        ));
176    }
177    if let Err(e) = wait_for_tcp_ready(("127.0.0.1", web_port), Duration::from_secs(10)).await {
178        let _ = child.start_kill();
179        return Err(Error::new(
180            ErrorCode::BrowserLaunchFailed,
181            format!("KasmVNC web client did not accept connections on port {web_port}: {e}"),
182        ));
183    }
184
185    // Start a minimal window manager so the headful browser is auto-maximized
186    // and tracks framebuffer-size changes (enables `resize=remote` dynamic
187    // fit). Optional: if no WM binary is on PATH the display still works, the
188    // client just falls back to scaled rendering.
189    let wm_child = spawn_window_manager(&display);
190
191    Ok(Arc::new(ProviderHandle {
192        display,
193        web_port,
194        window_manager: wm_child.is_some(),
195        _rfb_port: rfb_port,
196        child: Mutex::new(child),
197        wm_child: wm_child.map(Mutex::new),
198        stderr_task,
199    }))
200}
201
202/// Spawn a lightweight window manager (matchbox or openbox) on `display` to
203/// keep the single browser window maximized. Returns `None` if none is on
204/// PATH — the takeover still works, just without dynamic resize.
205#[cfg(unix)]
206fn spawn_window_manager(display: &str) -> Option<Child> {
207    for (bin, args) in [
208        ("matchbox-window-manager", &["-use_titlebar", "no"][..]),
209        ("openbox", &[][..]),
210    ] {
211        if resolve_named_bin(bin, &None).is_err() {
212            continue;
213        }
214        match Command::new(bin)
215            .args(args)
216            .env("DISPLAY", display)
217            .stdout(Stdio::null())
218            .stderr(Stdio::null())
219            .kill_on_drop(true)
220            .spawn()
221        {
222            Ok(child) => return Some(child),
223            Err(_) => continue,
224        }
225    }
226    None
227}
228
229fn resolve_kasmvnc_bin() -> Result<PathBuf, Error> {
230    if let Ok(raw) = std::env::var("AFHTTP_KASMVNC_BIN") {
231        let path = PathBuf::from(raw);
232        if path.exists() {
233            return Ok(path);
234        }
235    }
236    resolve_named_bin("Xvnc", &None).or_else(|_| resolve_named_bin("kasmvncserver", &None))
237}
238
239fn resolve_kasmvnc_web_root() -> Result<PathBuf, Error> {
240    if let Ok(raw) = std::env::var("AFHTTP_KASMVNC_WEB_ROOT") {
241        let path = PathBuf::from(raw);
242        if path.exists() {
243            return Ok(path);
244        }
245    }
246    for candidate in [
247        "/usr/share/kasmvnc/www",
248        "/usr/local/share/kasmvnc/www",
249        "/opt/kasmvnc/share/kasmvnc/www",
250    ] {
251        let path = PathBuf::from(candidate);
252        if path.exists() {
253            return Ok(path);
254        }
255    }
256    Err(Error::new(
257        ErrorCode::BrowserLaunchFailed,
258        "could not find KasmVNC web root; set AFHTTP_KASMVNC_WEB_ROOT",
259    ))
260}
261
262#[cfg(unix)]
263fn pick_display_number() -> Result<u16, Error> {
264    for display in 90..200 {
265        let socket = x_socket_path(display);
266        if !socket.exists() {
267            return Ok(display);
268        }
269    }
270    Err(Error::new(
271        ErrorCode::BrowserLaunchFailed,
272        "could not find a free X display number for KasmVNC",
273    ))
274}
275
276#[cfg(unix)]
277async fn wait_for_x_display(display: u16, timeout: Duration) -> Result<(), String> {
278    let deadline = tokio::time::Instant::now() + timeout;
279    let socket = x_socket_path(display);
280    loop {
281        if tokio::time::Instant::now() >= deadline {
282            return Err(format!("timed out after {timeout:?}"));
283        }
284        if UnixStream::connect(&socket).await.is_ok() {
285            return Ok(());
286        }
287        tokio::time::sleep(Duration::from_millis(50)).await;
288    }
289}
290
291#[cfg(unix)]
292fn x_socket_path(display: u16) -> PathBuf {
293    Path::new("/tmp/.X11-unix").join(format!("X{display}"))
294}
295
296/// Minimal state used by the listener reverse proxy. Tests can construct this
297/// without spawning KasmVNC; production keeps the process alive via `_handle`.
298#[derive(Clone)]
299pub struct TakeoverProxyState {
300    pub provider: TakeoverProviderKind,
301    pub display: String,
302    pub web_addr: SocketAddr,
303    /// A window manager is running, so the client can use `resize=remote`
304    /// (dynamic framebuffer fit) rather than the letterboxed `resize=scale`.
305    pub window_manager: bool,
306    /// Image quality 0-100 seeded onto the client (see `host::bootstrap`).
307    pub quality: u8,
308    _handle: Option<Arc<ProviderHandle>>,
309}
310
311impl TakeoverProxyState {
312    pub fn new(provider: TakeoverProviderKind, handle: Arc<ProviderHandle>, quality: u8) -> Self {
313        Self {
314            provider,
315            display: handle.display.clone(),
316            web_addr: SocketAddr::from(([127, 0, 0, 1], handle.web_port)),
317            window_manager: handle.window_manager,
318            quality,
319            _handle: Some(handle),
320        }
321    }
322
323    #[cfg(any(test, feature = "host"))]
324    pub fn for_tests(web_port: u16) -> Self {
325        Self {
326            provider: TakeoverProviderKind::KasmVnc,
327            display: ":99".to_string(),
328            web_addr: SocketAddr::from(([127, 0, 0, 1], web_port)),
329            window_manager: false,
330            quality: 100,
331            _handle: None,
332        }
333    }
334
335    /// Proxy an authenticated `/takeover/panel` request to the active provider.
336    pub async fn proxy(
337        self,
338        ws: Result<WebSocketUpgrade, WebSocketUpgradeRejection>,
339        uri: Uri,
340        request: axum::extract::Request,
341    ) -> Response {
342        match self.provider {
343            TakeoverProviderKind::KasmVnc => proxy_kasmvnc(self, ws, uri, request).await,
344        }
345    }
346}
347
348async fn proxy_kasmvnc(
349    display: TakeoverProxyState,
350    ws: Result<WebSocketUpgrade, WebSocketUpgradeRejection>,
351    uri: Uri,
352    request: axum::extract::Request,
353) -> Response {
354    // noVNC builds its WebSocket URL from the host root (default
355    // `/websockify`), ignoring this `/takeover/panel/` mount prefix — so without
356    // help the client connects to `/websockify` and 404s. Seed noVNC's `path`
357    // setting via query param so it targets the proxied
358    // `/takeover/panel/websockify`. Applied by redirecting the landing page when
359    // the param is absent; the redirect also normalizes the missing trailing
360    // slash (axum's `{*path}` wildcard never matches the bare `/takeover/panel/`).
361    // `resize=remote` makes the framebuffer (and the WM-maximized browser)
362    // track the client window for an exact fit; without a window manager fall
363    // back to `resize=scale` (letterboxed) since the browser window can't
364    // follow a framebuffer resize on its own.
365    let resize = if display.window_manager {
366        "remote"
367    } else {
368        "scale"
369    };
370    let is_landing = matches!(uri.path(), "/takeover/panel" | "/takeover/panel/");
371    let q = uri.query().unwrap_or("");
372    let has_path = q.split('&').any(|p| p.starts_with("path="));
373    // Match the exact preferred value, not just presence, so a client that
374    // cached `resize=scale` from before a window manager was available gets
375    // upgraded to `resize=remote` on its next landing hit.
376    let want_resize = format!("resize={resize}");
377    let resize_ok = q.split('&').any(|p| p == want_resize);
378    if is_landing && ws.is_err() && !(has_path && resize_ok) {
379        // Rebuild canonically, dropping stale `path`/`resize` and the one-time
380        // handoff query. Redirecting whenever *either* is
381        // missing also fixes clients that cached a `?path=...` URL from before
382        // `resize` existed. Once both are present the condition is false.
383        let quality = kasmvnc_quality_params(display.quality);
384        let mut location =
385            format!("/takeover/panel/?path=takeover/panel/websockify&resize={resize}{quality}");
386        for pair in q.split('&') {
387            if pair.is_empty()
388                || pair.starts_with("path=")
389                || pair.starts_with("resize=")
390                || pair.starts_with("handoff_secret=")
391                || pair.starts_with("handoff=")
392            {
393                continue;
394            }
395            location.push('&');
396            location.push_str(pair);
397        }
398        return (
399            StatusCode::TEMPORARY_REDIRECT,
400            [(header::LOCATION, location)],
401        )
402            .into_response();
403    }
404
405    let upstream_path = display_upstream_path_and_query(&uri);
406    if let Ok(ws) = ws {
407        let upstream = format!("ws://{}{}", display.web_addr, upstream_path);
408        // KasmVNC's websockify speaks the `binary` WebSocket subprotocol and
409        // closes the stream if it isn't negotiated. Echo it back to the
410        // browser (it always offers `binary`) so noVNC accepts the socket;
411        // the upstream leg requests it in `forward_display_ws`.
412        return ws
413            .protocols(["binary"])
414            .on_upgrade(move |socket| forward_kasmvnc_ws(socket, upstream));
415    }
416
417    forward_display_http(display.web_addr, upstream_path, request).await
418}
419
420/// KasmVNC client quality settings seeded on the display panel (the client's
421/// values override the server's). The `pct` (0-100, from
422/// `--takeover-quality-percent`) maps to KasmVNC's 0-9 JPEG quality tiers.
423/// Static/idle content can always climb to tier 9 (`dynamic_quality_max`);
424/// `pct` sets the floor for moving content. Regardless of `pct` we stop the
425/// client's default 960x540 "video mode" downscale (`max_video_resolution`),
426/// which is what blurs detailed images like captcha challenges while they
427/// load/animate.
428fn kasmvnc_quality_params(pct: u8) -> String {
429    let level = (u32::from(pct.min(100)) * 9 + 50) / 100; // 0-9, rounded
430    format!(
431        "&quality={level}&dynamic_quality_min={level}&dynamic_quality_max=9\
432         &jpeg_video_quality={level}&webp_video_quality={level}\
433         &max_video_resolution_x=3840&max_video_resolution_y=2160"
434    )
435}
436
437async fn forward_display_http(
438    upstream: std::net::SocketAddr,
439    path_and_query: String,
440    request: axum::extract::Request,
441) -> Response {
442    let (parts, body) = request.into_parts();
443    let url = format!("http://{upstream}{path_and_query}");
444    let method = reqwest::Method::from_bytes(parts.method.as_str().as_bytes())
445        .unwrap_or(reqwest::Method::GET);
446    let body = match to_bytes(body, 64 * 1024 * 1024).await {
447        Ok(bytes) => bytes,
448        Err(e) => {
449            return (
450                StatusCode::BAD_REQUEST,
451                format!("display proxy: could not read request body: {e}"),
452            )
453                .into_response();
454        }
455    };
456
457    let client = reqwest::Client::new();
458    let mut upstream_req = client.request(method, url).body(body);
459    for (name, value) in &parts.headers {
460        if should_forward_header(name) {
461            upstream_req = upstream_req.header(name, value);
462        }
463    }
464    upstream_req = upstream_req.header(header::HOST.as_str(), upstream.to_string());
465
466    let resp = match upstream_req.send().await {
467        Ok(resp) => resp,
468        Err(e) => {
469            return (
470                StatusCode::BAD_GATEWAY,
471                format!("display proxy: upstream request failed: {e}"),
472            )
473                .into_response();
474        }
475    };
476
477    let status = StatusCode::from_u16(resp.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
478    let mut headers = HeaderMap::new();
479    for (name, value) in resp.headers() {
480        if should_forward_response_header(name) {
481            headers.insert(name.clone(), value.clone());
482        }
483    }
484    let bytes = match resp.bytes().await {
485        Ok(bytes) => bytes,
486        Err(e) => {
487            return (
488                StatusCode::BAD_GATEWAY,
489                format!("display proxy: upstream body failed: {e}"),
490            )
491                .into_response();
492        }
493    };
494
495    (status, headers, Body::from(bytes)).into_response()
496}
497
498async fn forward_kasmvnc_ws(client: WebSocket, upstream_url: String) {
499    use tungstenite::client::IntoClientRequest;
500    let (mut client_tx, mut client_rx) = client.split();
501    // Request KasmVNC's `binary` subprotocol on the upstream leg; without it
502    // websockify refuses to bridge the RFB stream (101 then immediate close).
503    let upstream_req = match upstream_url.as_str().into_client_request() {
504        Ok(mut req) => {
505            req.headers_mut().insert(
506                tungstenite::http::header::SEC_WEBSOCKET_PROTOCOL,
507                tungstenite::http::HeaderValue::from_static("binary"),
508            );
509            // KasmVNC rejects WS upgrades that lack an `Origin` header (404),
510            // so synthesize one matching the upstream authority. The browser's
511            // own Origin points at afhttp's listener, not KasmVNC, so it can't
512            // be forwarded verbatim.
513            if let Some(authority) = req.uri().authority().map(|a| a.to_string())
514                && let Ok(origin) =
515                    tungstenite::http::HeaderValue::from_str(&format!("http://{authority}"))
516            {
517                req.headers_mut()
518                    .insert(tungstenite::http::header::ORIGIN, origin);
519            }
520            req
521        }
522        Err(_) => {
523            let _ = client_tx.close().await;
524            return;
525        }
526    };
527    let upstream_stream = match tokio_tungstenite::connect_async(upstream_req).await {
528        Ok((stream, _resp)) => stream,
529        Err(_) => {
530            let _ = client_tx.close().await;
531            return;
532        }
533    };
534    let (mut upstream_tx, mut upstream_rx) = upstream_stream.split();
535
536    let c2u = async {
537        while let Some(Ok(msg)) = client_rx.next().await {
538            let outbound = match msg {
539                Message::Text(t) => tungstenite::Message::Text(t.as_str().into()),
540                Message::Binary(b) => tungstenite::Message::Binary(b.to_vec().into()),
541                Message::Ping(p) => tungstenite::Message::Ping(p.to_vec().into()),
542                Message::Pong(p) => tungstenite::Message::Pong(p.to_vec().into()),
543                Message::Close(_) => break,
544            };
545            if upstream_tx.send(outbound).await.is_err() {
546                break;
547            }
548        }
549        let _ = upstream_tx.send(tungstenite::Message::Close(None)).await;
550    };
551    let u2c = async {
552        while let Some(Ok(msg)) = upstream_rx.next().await {
553            let inbound = match msg {
554                tungstenite::Message::Text(t) => Message::Text(t.as_str().into()),
555                tungstenite::Message::Binary(b) => Message::Binary(b.to_vec().into()),
556                tungstenite::Message::Ping(p) => Message::Ping(p.to_vec().into()),
557                tungstenite::Message::Pong(p) => Message::Pong(p.to_vec().into()),
558                tungstenite::Message::Close(_) => break,
559                tungstenite::Message::Frame(_) => continue,
560            };
561            if client_tx.send(inbound).await.is_err() {
562                break;
563            }
564        }
565        let _ = client_tx.close().await;
566    };
567    tokio::pin!(c2u);
568    tokio::pin!(u2c);
569    tokio::select! {
570        _ = &mut c2u => {},
571        _ = &mut u2c => {},
572    }
573}
574
575fn display_upstream_path_and_query(uri: &Uri) -> String {
576    let suffix = uri
577        .path()
578        .strip_prefix("/takeover/panel")
579        .filter(|s| !s.is_empty())
580        .unwrap_or("/");
581    let path = if suffix.starts_with('/') {
582        suffix.to_string()
583    } else {
584        format!("/{suffix}")
585    };
586    let Some(query) = uri.query() else {
587        return path;
588    };
589    let filtered: Vec<&str> = query
590        .split('&')
591        .filter(|pair| {
592            !pair.starts_with("handoff_secret=")
593                && !pair.starts_with("handoff=")
594                && !pair.starts_with("token_secret=")
595        })
596        .collect();
597    if filtered.is_empty() {
598        path
599    } else {
600        format!("{path}?{}", filtered.join("&"))
601    }
602}
603
604fn should_forward_header(name: &header::HeaderName) -> bool {
605    !matches!(
606        name.as_str().to_ascii_lowercase().as_str(),
607        "host"
608            | "connection"
609            | "upgrade"
610            | "content-length"
611            | "sec-websocket-key"
612            | "sec-websocket-version"
613            | "sec-websocket-protocol"
614            | "sec-websocket-extensions"
615            | "authorization"
616            | "cookie"
617    )
618}
619
620fn should_forward_response_header(name: &header::HeaderName) -> bool {
621    !matches!(
622        name.as_str().to_ascii_lowercase().as_str(),
623        "connection" | "transfer-encoding" | "upgrade" | "content-length"
624    )
625}
626
627#[cfg(test)]
628mod tests {
629    use super::kasmvnc_quality_params;
630
631    #[test]
632    fn quality_pct_maps_to_kasmvnc_tiers() {
633        assert!(kasmvnc_quality_params(100).contains("&quality=9&"));
634        assert!(kasmvnc_quality_params(0).contains("&quality=0&"));
635        assert!(kasmvnc_quality_params(50).contains("&quality=5&"));
636        // Clamps above 100.
637        assert!(kasmvnc_quality_params(200).contains("&quality=9&"));
638        // Never downscales, regardless of quality.
639        assert!(kasmvnc_quality_params(10).contains("max_video_resolution_x=3840"));
640    }
641}