From eb1f605d222adef6cb1678ed9a51070b9bbe0406 Mon Sep 17 00:00:00 2001 From: Zach Kelling Date: Fri, 20 Nov 2026 19:38:16 -0800 Subject: [PATCH 1/2] feat(desktop): open a site, save a copy for the agent, and serve its redesign live (issue 15) local_site_capture opens a site from this computer (public addresses, or one on this computer the person typed; never a private network, redirects included), saves its page, words, stylesheets and, when a paired browser can, a picture under .hanzo/reference. local_preview_start serves /redesign on 127.0.0.1 with live reload; local_preview_open shows it in Chrome. Previews stop when the window closes. Claude-Session: https://claude.ai/code/session_015XYKLn69gF4x75RWePpDBV --- apps/hanzo-desktop/src-tauri/Cargo.toml | 2 +- .../src-tauri/capabilities/main.json | 5 + .../src/commands/local_site/fetch.rs | 448 ++++++++++++ .../src-tauri/src/commands/local_site/mod.rs | 333 +++++++++ .../src-tauri/src/commands/local_site/page.rs | 655 ++++++++++++++++++ .../src/commands/local_site/preview.rs | 620 +++++++++++++++++ .../src-tauri/src/commands/mod.rs | 1 + apps/hanzo-desktop/src-tauri/src/main.rs | 13 + 8 files changed, 2076 insertions(+), 1 deletion(-) create mode 100644 apps/hanzo-desktop/src-tauri/src/commands/local_site/fetch.rs create mode 100644 apps/hanzo-desktop/src-tauri/src/commands/local_site/mod.rs create mode 100644 apps/hanzo-desktop/src-tauri/src/commands/local_site/page.rs create mode 100644 apps/hanzo-desktop/src-tauri/src/commands/local_site/preview.rs diff --git a/apps/hanzo-desktop/src-tauri/Cargo.toml b/apps/hanzo-desktop/src-tauri/Cargo.toml index 39e73fa5..d02dfd1f 100644 --- a/apps/hanzo-desktop/src-tauri/Cargo.toml +++ b/apps/hanzo-desktop/src-tauri/Cargo.toml @@ -20,7 +20,7 @@ serde_json = "1.0" reqwest = { version = "0.11", features = ["json", "stream"] } mdns-sd = "0.20.1" lazy_static = "1.4.0" -tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "rt", "net", "sync", "io-util"] } +tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "rt", "net", "sync", "io-util", "time"] } chrono = "0.4.38" futures-util = "0.3" regex = "1.10.4" diff --git a/apps/hanzo-desktop/src-tauri/capabilities/main.json b/apps/hanzo-desktop/src-tauri/capabilities/main.json index c20d09a7..b846979a 100644 --- a/apps/hanzo-desktop/src-tauri/capabilities/main.json +++ b/apps/hanzo-desktop/src-tauri/capabilities/main.json @@ -173,6 +173,11 @@ "allow-local-workspace-delete", "allow-local-workspace-run-dev", "allow-local-workspace-discover", + "allow-local-site-folder", + "allow-local-site-capture", + "allow-local-preview-start", + "allow-local-preview-stop", + "allow-local-preview-open", "allow-browser", "allow-browsers", "allow-check-claude-installed", diff --git a/apps/hanzo-desktop/src-tauri/src/commands/local_site/fetch.rs b/apps/hanzo-desktop/src-tauri/src/commands/local_site/fetch.rs new file mode 100644 index 00000000..e3bf580b --- /dev/null +++ b/apps/hanzo-desktop/src-tauri/src/commands/local_site/fetch.rs @@ -0,0 +1,448 @@ +//! Opening a site the way a person's browser would, from this computer. +//! +//! The agent that redesigns a site works in a sandbox with no network, so the +//! window does the opening: it fetches the page and its stylesheets and saves +//! them for the agent to read (`page.rs`). That makes this the one place a typed +//! address becomes a request, so it is strict about where a request may go. +//! +//! The rule: a site is on the internet, or it is on this computer and the +//! person typed `localhost` or a loopback address themselves. Nothing on a +//! private network (10/8, 172.16/12, 192.168/16, link-local, carrier-grade NAT, +//! IPv6 unique-local) is ever opened, and a redirect cannot take a request +//! anywhere the typed address could not. A name is resolved here, each address +//! judged, and the request pinned to the one that passed, so a name that +//! answers a private address (DNS rebinding) is refused as well. + +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; +use std::time::Duration; + +use reqwest::{header, redirect, Client, Url}; + +/// The largest page saved, and the largest stylesheet. +pub const PAGE_LIMIT: usize = 5 * 1024 * 1024; +pub const SHEET_LIMIT: usize = 600 * 1024; + +/// The longest a page, or a stylesheet, may take to arrive. +const PAGE_TIME: Duration = Duration::from_secs(15); +const SHEET_TIME: Duration = Duration::from_secs(8); + +/// How many redirects are followed. +const HOPS: usize = 6; + +const AGENT: &str = concat!( + "Mozilla/5.0 (compatible; HanzoDesktop/", + env!("CARGO_PKG_VERSION"), + "; +https://hanzo.ai)" +); + +pub const NOT_AN_ADDRESS: &str = "That is not a web address. Paste one like https://example.com."; +const NOT_WEB: &str = "Hanzo can open web addresses that start with http:// or https://."; +const HAS_LOGIN: &str = "Leave the user name and password out of the address."; +const PRIVATE: &str = + "Hanzo opens sites on the internet, or one running on this computer. That address is on a private network."; +const NO_SITE: &str = "Hanzo could not find that site. Check the address and try again."; +const SLOW: &str = "The site took too long to answer."; +const TOO_BIG: &str = "That page is too large to open."; +const NOT_A_PAGE: &str = "That address is not a web page."; +const TOO_MANY: &str = "That address sends Hanzo around in circles."; + +/// What a request brought back. +pub struct Fetched { + /// Where it ended, after any redirects. + pub url: Url, + pub body: Vec, +} + +/// What a person typed, as an address Hanzo may open. A bare name gets `https://` +/// (`http://` for `localhost` and loopback addresses, which rarely serve TLS). +pub fn address(raw: &str) -> Result { + let text = raw.trim(); + if text.is_empty() || text.chars().any(char::is_whitespace) { + return Err(NOT_AN_ADDRESS.into()); + } + let full = if text.contains("://") { + text.to_string() + } else if local_text(text) { + format!("http://{text}") + } else { + format!("https://{text}") + }; + let url = Url::parse(&full).map_err(|_| NOT_AN_ADDRESS.to_string())?; + if !matches!(url.scheme(), "http" | "https") { + return Err(NOT_WEB.into()); + } + if !url.username().is_empty() || url.password().is_some() { + return Err(HAS_LOGIN.into()); + } + if url.host_str().map_or(true, str::is_empty) { + return Err(NOT_AN_ADDRESS.into()); + } + Ok(url) +} + +/// A scheme-less address that names this computer. +fn local_text(text: &str) -> bool { + let host = text.split(['/', '?', '#']).next().unwrap_or(""); + let host = host.rsplit_once('@').map_or(host, |(_, h)| h); + let name = if let Some(rest) = host.strip_prefix('[') { + rest.split(']').next().unwrap_or("") + } else { + host.rsplit_once(':').map_or(host, |(h, _)| h) + }; + name.eq_ignore_ascii_case("localhost") + || name.to_ascii_lowercase().ends_with(".localhost") + || name.parse::().is_ok_and(|ip| ip.is_loopback()) +} + +/// Whether the address names this computer: the one case a loopback request is allowed. +pub fn loopback(url: &Url) -> bool { + let Some(host) = url.host_str() else { return false }; + let name = host.trim_start_matches('[').trim_end_matches(']'); + name.eq_ignore_ascii_case("localhost") + || name.to_ascii_lowercase().ends_with(".localhost") + || name.parse::().is_ok_and(|ip| ip.is_loopback()) +} + +fn public4(ip: Ipv4Addr) -> bool { + let o = ip.octets(); + !(ip.is_private() + || ip.is_loopback() + || ip.is_link_local() + || ip.is_broadcast() + || ip.is_unspecified() + || ip.is_multicast() + || o[0] == 0 + || o[0] >= 240 + || (o[0] == 100 && (o[1] & 0xC0) == 64) + || (o[0] == 192 && o[1] == 0 && o[2] == 0) + || (o[0] == 198 && (o[1] == 18 || o[1] == 19))) +} + +fn public6(ip: Ipv6Addr) -> bool { + let s = ip.segments(); + !(ip.is_loopback() + || ip.is_unspecified() + || ip.is_multicast() + || (s[0] & 0xfe00) == 0xfc00 + || (s[0] & 0xffc0) == 0xfe80) +} + +/// Whether a request may go to `ip`: any public address, and a loopback one only +/// when the person typed a loopback address (`local`). +pub fn allowed(ip: IpAddr, local: bool) -> bool { + let ip = match ip { + IpAddr::V6(v6) => v6.to_ipv4_mapped().map_or(ip, IpAddr::V4), + v4 => v4, + }; + if ip.is_loopback() { + return local; + } + match ip { + IpAddr::V4(v4) => public4(v4), + IpAddr::V6(v6) => public6(v6), + } +} + +/// The address a request to `url` is pinned to: the first the name resolves to that +/// `allowed` lets through. An address typed as a number has nothing to pin. +async fn pin(url: &Url, local: bool) -> Result, String> { + let host = url.host_str().ok_or(NOT_AN_ADDRESS)?; + let bare = host.trim_start_matches('[').trim_end_matches(']'); + if let Ok(ip) = bare.parse::() { + return if allowed(ip, local) { Ok(None) } else { Err(PRIVATE.into()) }; + } + let port = url.port_or_known_default().ok_or(NOT_AN_ADDRESS)?; + let found: Vec = tokio::net::lookup_host((bare, port)) + .await + .map_err(|_| NO_SITE.to_string())? + .collect(); + if found.is_empty() { + return Err(NO_SITE.into()); + } + found + .into_iter() + .find(|a| allowed(a.ip(), local)) + .map(Some) + .ok_or_else(|| PRIVATE.to_string()) +} + +/// One request, redirects followed by hand so each hop is judged as the first was. +/// `local` is whether the person typed a loopback address; it never widens on a hop. +async fn get(first: &Url, local: bool, limit: usize, accept: &str, kinds: &[&str]) -> Result { + let mut url = first.clone(); + for _ in 0..=HOPS { + if !matches!(url.scheme(), "http" | "https") { + return Err(NOT_WEB.into()); + } + let pinned = pin(&url, local).await?; + let mut client = Client::builder() + .redirect(redirect::Policy::none()) + .user_agent(AGENT) + .connect_timeout(Duration::from_secs(8)); + if let (Some(addr), Some(name)) = (pinned, url.host_str()) { + client = client.resolve(name, addr); + } + let response = client + .build() + .map_err(|_| NO_SITE.to_string())? + .get(url.clone()) + .header(header::ACCEPT, accept) + .header(header::ACCEPT_LANGUAGE, "en;q=0.9,*;q=0.5") + .send() + .await + .map_err(|e| if e.is_timeout() { SLOW.to_string() } else { NO_SITE.to_string() })?; + let status = response.status(); + if status.is_redirection() { + let to = response + .headers() + .get(header::LOCATION) + .and_then(|v| v.to_str().ok()) + .and_then(|v| url.join(v).ok()) + .ok_or_else(|| TOO_MANY.to_string())?; + url = to; + continue; + } + if !status.is_success() { + return Err(format!("The site answered {}.", status.as_u16())); + } + let kind = response + .headers() + .get(header::CONTENT_TYPE) + .and_then(|v| v.to_str().ok()) + .unwrap_or("") + .split(';') + .next() + .unwrap_or("") + .trim() + .to_ascii_lowercase(); + if !kinds.iter().any(|k| kind == *k) { + return Err(NOT_A_PAGE.into()); + } + if response.content_length().is_some_and(|n| n as usize > limit) { + return Err(TOO_BIG.into()); + } + let mut body = Vec::new(); + let mut response = response; + while let Some(chunk) = response + .chunk() + .await + .map_err(|e| if e.is_timeout() { SLOW.to_string() } else { NO_SITE.to_string() })? + { + if body.len() + chunk.len() > limit { + return Err(TOO_BIG.into()); + } + body.extend_from_slice(&chunk); + } + return Ok(Fetched { url, body }); + } + Err(TOO_MANY.into()) +} + +/// A page: HTML only, 15 seconds, 5 MB. +pub async fn page(url: &Url) -> Result { + let local = loopback(url); + tokio::time::timeout( + PAGE_TIME, + get(url, local, PAGE_LIMIT, "text/html,application/xhtml+xml;q=0.9,*/*;q=0.5", &["text/html", "application/xhtml+xml"]), + ) + .await + .map_err(|_| SLOW.to_string())? +} + +/// A stylesheet of that page: CSS only, 8 seconds, 600 KB. `local` is the page's own. +pub async fn sheet(url: &Url, local: bool) -> Result { + tokio::time::timeout(SHEET_TIME, get(url, local, SHEET_LIMIT, "text/css,*/*;q=0.1", &["text/css"])) + .await + .map_err(|_| SLOW.to_string())? +} + +#[cfg(test)] +mod tests { + use std::net::SocketAddr; + + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + use tokio::net::TcpListener; + + use super::*; + + #[test] + fn a_bare_name_gets_a_scheme() { + assert_eq!(address("example.com").unwrap().as_str(), "https://example.com/"); + assert_eq!(address(" https://example.com/a?b=1 ").unwrap().as_str(), "https://example.com/a?b=1"); + assert_eq!(address("localhost:3000").unwrap().as_str(), "http://localhost:3000/"); + assert_eq!(address("127.0.0.1:8080/x").unwrap().as_str(), "http://127.0.0.1:8080/x"); + assert_eq!(address("[::1]:9000").unwrap().as_str(), "http://[::1]:9000/"); + } + + #[test] + fn only_web_addresses_are_opened() { + for bad in [ + "", + " ", + "file:///etc/passwd", + "ftp://example.com", + "javascript:alert(1)", + "data:text/html,hi", + "https://user:pw@example.com", + "https://user@example.com", + "two words", + "https://", + ] { + assert!(address(bad).is_err(), "{bad} must be refused"); + } + } + + #[test] + fn loopback_is_the_computers_own_and_only_when_typed() { + assert!(loopback(&address("localhost:3000").unwrap())); + assert!(loopback(&address("http://127.0.0.1:1").unwrap())); + assert!(loopback(&address("http://[::1]/").unwrap())); + assert!(loopback(&address("http://app.localhost/").unwrap())); + assert!(!loopback(&address("https://example.com").unwrap())); + assert!(!loopback(&address("http://10.0.0.1").unwrap())); + } + + #[test] + fn private_networks_are_never_opened() { + let no: [&str; 14] = [ + "10.0.0.1", + "10.255.255.255", + "172.16.0.1", + "172.31.255.255", + "192.168.1.1", + "169.254.169.254", + "100.64.0.1", + "0.0.0.0", + "224.0.0.1", + "255.255.255.255", + "fc00::1", + "fe80::1", + "::", + "::ffff:10.0.0.1", + ]; + for ip in no { + let ip: IpAddr = ip.parse().unwrap(); + assert!(!allowed(ip, false) && !allowed(ip, true), "{ip} must be refused even when local"); + } + for ip in ["93.184.216.34", "8.8.8.8", "172.32.0.1", "2606:2800:220:1:248:1893:25c8:1946", "::ffff:8.8.8.8"] { + let ip: IpAddr = ip.parse().unwrap(); + assert!(allowed(ip, false), "{ip} is public"); + } + for ip in ["127.0.0.1", "::1", "::ffff:127.0.0.1"] { + let ip: IpAddr = ip.parse().unwrap(); + assert!(!allowed(ip, false), "{ip} needs the person to have typed it"); + assert!(allowed(ip, true)); + } + } + + /// A server that answers each connection with the next canned response, and records the paths asked for. + async fn serve(answers: Vec) -> (SocketAddr, tokio::task::JoinHandle>) { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + let task = tokio::spawn(async move { + let mut asked = Vec::new(); + for answer in answers { + let (mut s, _) = listener.accept().await.unwrap(); + let mut buf = vec![0u8; 4096]; + let n = s.read(&mut buf).await.unwrap(); + let head = String::from_utf8_lossy(&buf[..n]).to_string(); + asked.push(head.lines().next().unwrap_or("").to_string()); + s.write_all(answer.as_bytes()).await.unwrap(); + let _ = s.shutdown().await; + } + asked + }); + (addr, task) + } + + fn ok(kind: &str, body: &str) -> String { + format!("HTTP/1.1 200 OK\r\nContent-Type: {kind}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()) + } + + #[tokio::test] + async fn a_page_on_this_computer_is_opened_when_its_address_was_typed() { + let (addr, task) = serve(vec![ok("text/html; charset=utf-8", "Hi")]).await; + let url = address(&format!("127.0.0.1:{}/a", addr.port())).unwrap(); + let got = page(&url).await.unwrap(); + assert_eq!(String::from_utf8(got.body).unwrap(), "Hi"); + assert_eq!(task.await.unwrap(), vec!["GET /a HTTP/1.1"]); + } + + #[tokio::test] + async fn a_redirect_is_followed_inside_the_same_rule() { + let (addr, task) = serve(vec![ + "HTTP/1.1 302 Found\r\nLocation: /next\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_string(), + ok("text/html", "

there

"), + ]) + .await; + let url = address(&format!("http://127.0.0.1:{}/", addr.port())).unwrap(); + let got = page(&url).await.unwrap(); + assert_eq!(got.url.path(), "/next"); + assert_eq!(task.await.unwrap(), vec!["GET / HTTP/1.1", "GET /next HTTP/1.1"]); + } + + #[tokio::test] + async fn a_redirect_to_a_private_network_is_refused() { + let (addr, _task) = serve(vec![ + "HTTP/1.1 302 Found\r\nLocation: http://169.254.169.254/latest/meta-data/\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_string(), + ]) + .await; + let url = address(&format!("http://127.0.0.1:{}/", addr.port())).unwrap(); + assert_eq!(page(&url).await.err().unwrap(), PRIVATE); + } + + #[tokio::test] + async fn a_public_site_cannot_send_a_request_to_this_computer() { + // `local` is false as it is for a typed public address: a redirect to loopback is refused. + let (addr, _task) = serve(vec![ok("text/html", "

secret

")]).await; + let url = Url::parse(&format!("http://127.0.0.1:{}/", addr.port())).unwrap(); + let err = get(&url, false, PAGE_LIMIT, "*/*", &["text/html"]).await.err().unwrap(); + assert_eq!(err, PRIVATE); + } + + #[tokio::test] + async fn a_private_address_is_refused_before_anything_is_sent() { + for typed in ["http://192.168.1.10/", "http://10.1.2.3:8080/", "http://169.254.169.254/", "http://[fc00::1]/"] { + let url = address(typed).unwrap(); + assert_eq!(page(&url).await.err().unwrap(), PRIVATE, "{typed}"); + } + } + + #[tokio::test] + async fn what_is_not_a_page_is_refused() { + let (addr, _task) = serve(vec![ok("application/pdf", "%PDF-1.4")]).await; + let url = address(&format!("http://127.0.0.1:{}/", addr.port())).unwrap(); + assert_eq!(page(&url).await.err().unwrap(), NOT_A_PAGE); + } + + #[tokio::test] + async fn an_error_status_is_said_plainly() { + let (addr, _task) = serve(vec!["HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_string()]).await; + let url = address(&format!("http://127.0.0.1:{}/", addr.port())).unwrap(); + assert_eq!(page(&url).await.err().unwrap(), "The site answered 404."); + } + + #[tokio::test] + async fn a_page_over_the_limit_is_refused() { + let big = "x".repeat(PAGE_LIMIT + 1); + let (addr, _task) = serve(vec![ok("text/html", &big)]).await; + let url = address(&format!("http://127.0.0.1:{}/", addr.port())).unwrap(); + assert_eq!(page(&url).await.err().unwrap(), TOO_BIG); + } + + #[tokio::test] + async fn redirects_that_never_end_are_cut_off() { + let again = "HTTP/1.1 302 Found\r\nLocation: /\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_string(); + let (addr, _task) = serve(vec![again; HOPS + 1]).await; + let url = address(&format!("http://127.0.0.1:{}/", addr.port())).unwrap(); + assert_eq!(page(&url).await.err().unwrap(), TOO_MANY); + } + + #[tokio::test] + async fn a_stylesheet_is_css_only() { + let (addr, _task) = serve(vec![ok("text/css", "body{color:red}"), ok("text/html", "

no

")]).await; + let css = Url::parse(&format!("http://127.0.0.1:{}/a.css", addr.port())).unwrap(); + assert_eq!(sheet(&css, true).await.unwrap().body, b"body{color:red}"); + assert_eq!(sheet(&css, true).await.err().unwrap(), NOT_A_PAGE); + } +} diff --git a/apps/hanzo-desktop/src-tauri/src/commands/local_site/mod.rs b/apps/hanzo-desktop/src-tauri/src/commands/local_site/mod.rs new file mode 100644 index 00000000..7fd4f4a4 --- /dev/null +++ b/apps/hanzo-desktop/src-tauri/src/commands/local_site/mod.rs @@ -0,0 +1,333 @@ +//! Redesigning a site, the window's half (hanzo-inc/app issue 312). +//! +//! A person pastes a site's address. The agent that rebuilds it works in a +//! folder with no network, so the window does what the agent cannot: +//! +//! `local_site_folder` a new folder under Documents/Hanzo for the redesign +//! `local_site_capture` open the site, save a copy of it for the agent +//! (`page.rs`), and a picture of it from the person's own +//! browser when one is paired +//! `local_preview_start` serve the folder's `redesign/` live (`preview.rs`) +//! `local_preview_open` show that address in Chrome, else the default browser +//! `local_preview_stop` stop serving it +//! +//! The agent itself runs through `local_workspace_run_dev`, as any run does. + +mod fetch; +mod page; +mod preview; + +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; +use std::time::Duration; + +use serde::Serialize; + +pub use preview::{stop_all, Preview}; + +/// What a redesign is built in: where the agent works, and where the preview is served from. +const BUILT: &str = "redesign"; + +/// The folder a redesign goes in. +#[derive(Serialize, Debug, PartialEq)] +pub struct SiteFolder { + pub name: String, + pub path: String, +} + +/// What `local_site_capture` saved. +#[derive(Serialize, Debug, PartialEq)] +pub struct SiteSaved { + /// What the site calls itself, or its address. + pub title: String, + /// Where the site ended, after any redirects. + pub url: String, + /// The files saved under `.hanzo/reference/`. + pub files: Vec, + /// Whether the saved copy includes a picture of the page. + pub screenshot: bool, +} + +/// A folder name from what a host sent: lowercase letters, digits and single dashes. +fn slug(name: &str) -> String { + let mut out = String::new(); + for c in name.trim().chars().flat_map(char::to_lowercase) { + if c.is_ascii_alphanumeric() { + out.push(c); + } else if !out.is_empty() && !out.ends_with('-') { + out.push('-'); + } + } + let out = out.trim_matches('-'); + if out.is_empty() { + "site-redesign".to_string() + } else { + out.chars().take(60).collect::().trim_matches('-').to_string() + } +} + +/// `/`, or `-2`, `-3`… when that is taken: a redesign never lands in a folder that has work in it. +fn free_folder(base: &Path, name: &str) -> PathBuf { + let name = slug(name); + (1u32..) + .map(|n| base.join(if n == 1 { name.clone() } else { format!("{name}-{n}") })) + .find(|at| !at.exists()) + .expect("a name that is free") +} + +/// The folder a redesign goes in: a new one under the person's Documents, in a folder called Hanzo. +/// It is named here and made when the site is saved (`local_site_capture`), so a site that will not open leaves nothing behind. +#[tauri::command] +pub async fn local_site_folder(name: String) -> Result { + let docs = dirs::document_dir().or_else(dirs::home_dir).ok_or("Hanzo could not find your Documents folder.")?; + let at = free_folder(&docs.join("Hanzo"), &name); + Ok(SiteFolder { + name: at.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(), + path: at.to_string_lossy().into_owned(), + }) +} + +/// The browser the person has paired with Hanzo, over ZAP (`commands::browser`); on a system with no such bridge, none. +#[cfg(unix)] +struct Paired; + +#[cfg(unix)] +impl page::Browse for Paired { + async fn call(&self, method: &str, params: std::collections::HashMap) -> Result { + super::browser::browser(method.to_string(), params).await + } +} + +#[cfg(unix)] +use Paired as Eye; +#[cfg(not(unix))] +use page::Nobody as Eye; + +/// Open `url`, save a copy of it under `/.hanzo/reference` for the agent (making `dir` if it is new), and say what was saved. +/// The page and its stylesheets come from this computer's own connection and are never sent anywhere. +#[tauri::command] +pub async fn local_site_capture(url: String, dir: String) -> Result { + capture(&url, &dir, &Eye).await +} + +/// `local_site_capture` with the browser it takes its picture from named. +async fn capture(url: &str, dir: &str, eye: &B) -> Result { + let dir = PathBuf::from(dir); + if dir.exists() && !dir.is_dir() { + return Err("That is not a folder.".into()); + } + let url = fetch::address(url)?; + let local = fetch::loopback(&url); + let fetched = fetch::page(&url).await?; + let html = String::from_utf8_lossy(&fetched.body).into_owned(); + let read = page::read(fetched.url, &html); + + let mut sheets = Vec::new(); + for at in &read.sheets { + // A stylesheet that will not come is one the agent reads without; the page is what matters. + if let Ok(got) = fetch::sheet(at, local).await { + sheets.push((at.clone(), got.body)); + } + } + + // How it looks, from the person's own browser when one is paired: best effort, never required. + let shot = tokio::time::timeout(Duration::from_secs(30), page::shoot(eye, read.url.as_str(), Duration::from_secs(3), Duration::from_secs(12))) + .await + .ok() + .flatten(); + + let title = if read.title.is_empty() { read.url.to_string() } else { read.title.clone() }; + let address = read.url.to_string(); + let saved = tokio::task::spawn_blocking(move || page::save(&dir, &read, &sheets, shot.as_ref().map(|(b, e)| (b.clone(), *e)))) + .await + .map_err(|e| e.to_string())? + .map_err(|e| format!("Hanzo could not save the site: {e}"))?; + Ok(SiteSaved { title, url: address, files: saved.files, screenshot: saved.screenshot }) +} + +/// Serve `/redesign` on this computer, live: the same address while it runs, and the page reloads as the files change. +#[tauri::command] +pub async fn local_preview_start(dir: String) -> Result { + preview::start(&PathBuf::from(dir).join(BUILT)).await +} + +/// Stop serving `/redesign`. +#[tauri::command] +pub async fn local_preview_stop(dir: String) -> Result<(), String> { + preview::stop(&PathBuf::from(dir).join(BUILT)); + Ok(()) +} + +/// Show a preview in the person's browser: Chrome when it is installed, else the browser they use. +/// It opens a preview this app is serving and nothing else, so it is no way to open an address. +#[tauri::command] +pub async fn local_preview_open(url: String) -> Result<(), String> { + url.strip_prefix("http://127.0.0.1:") + .and_then(|rest| rest.split(['/', '?', '#']).next()) + .and_then(|p| p.parse::().ok()) + .filter(|p| preview::serving(*p)) + .ok_or("That preview is not running any more.")?; + tokio::task::spawn_blocking(move || { + if !chrome(&url) { + opener::open_browser(&url).map_err(|e| format!("Hanzo could not open your browser: {e}"))?; + } + Ok(()) + }) + .await + .map_err(|e| e.to_string())? +} + +/// Open `url` in Chrome; whether Chrome took it. +fn chrome(url: &str) -> bool { + #[cfg(target_os = "macos")] + { + Command::new("open") + .args(["-a", "Google Chrome", url]) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .is_ok_and(|s| s.success()) + } + #[cfg(target_os = "windows")] + { + ["ProgramFiles", "ProgramFiles(x86)", "LOCALAPPDATA"] + .iter() + .filter_map(|root| std::env::var_os(root)) + .map(|base| PathBuf::from(base).join("Google").join("Chrome").join("Application").join("chrome.exe")) + .find(|exe| exe.is_file()) + .is_some_and(|exe| launch(Command::new(exe), url)) + } + #[cfg(all(unix, not(target_os = "macos")))] + { + ["google-chrome", "google-chrome-stable", "chromium", "chromium-browser"] + .iter() + .find(|name| on_path(name)) + .is_some_and(|name| launch(Command::new(name), url)) + } +} + +/// Start `command` on `url` and leave it running; whether it started. The child is reaped when it ends. +#[cfg(not(target_os = "macos"))] +fn launch(mut command: Command, url: &str) -> bool { + match command.arg(url).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).spawn() { + Ok(mut child) => { + std::thread::spawn(move || { + let _ = child.wait(); + }); + true + } + Err(_) => false, + } +} + +#[cfg(all(unix, not(target_os = "macos")))] +fn on_path(name: &str) -> bool { + use std::os::unix::fs::PermissionsExt; + std::env::var_os("PATH").is_some_and(|paths| { + std::env::split_paths(&paths).any(|dir| { + dir.join(name).metadata().is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0) + }) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_folder_name_is_made_of_letters_digits_and_dashes() { + assert_eq!(slug("Example-Redesign"), "example-redesign"); + assert_eq!(slug(" blog.example redesign "), "blog-example-redesign"); + assert_eq!(slug("../../etc/passwd"), "etc-passwd"); + assert_eq!(slug("日本語"), "site-redesign"); + assert_eq!(slug(""), "site-redesign"); + assert_eq!(slug(&"a".repeat(200)).len(), 60); + } + + #[test] + fn a_redesign_never_lands_in_a_folder_that_has_work_in_it() { + let dir = tempfile::tempdir().unwrap(); + let base = dir.path().join("Hanzo"); + let first = free_folder(&base, "Example redesign"); + assert_eq!(first, base.join("example-redesign")); + // Naming a folder makes nothing: a site that will not open leaves no folder behind. + assert!(!base.exists()); + std::fs::create_dir_all(&first).unwrap(); + std::fs::write(first.join("mine.txt"), "x").unwrap(); + let second = free_folder(&base, "example-redesign"); + assert_eq!(second, base.join("example-redesign-2")); + std::fs::create_dir_all(&second).unwrap(); + assert_eq!(free_folder(&base, "example-redesign"), base.join("example-redesign-3")); + assert_eq!(std::fs::read_to_string(first.join("mine.txt")).unwrap(), "x"); + } + + #[tokio::test] + async fn only_a_running_preview_is_opened() { + for url in ["https://example.com/", "http://127.0.0.1:1/", "http://127.0.0.1:notaport/", "file:///etc/passwd", "http://localhost:80/"] { + assert_eq!(local_preview_open(url.to_string()).await, Err("That preview is not running any more.".to_string()), "{url}"); + } + } + + /// The whole of it against a site on this computer: capture it into a folder, serve the redesign, read it back over HTTP. + /// No browser is paired, so the saved copy has no picture. + #[tokio::test] + async fn a_site_is_saved_and_a_redesign_of_it_is_served() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + use tokio::net::{TcpListener, TcpStream}; + + let _turn = preview::serial().lock().await; + let site = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = site.local_addr().unwrap().port(); + tokio::spawn(async move { + loop { + let (mut s, _) = site.accept().await.unwrap(); + let mut buf = vec![0u8; 4096]; + let n = s.read(&mut buf).await.unwrap(); + let path = String::from_utf8_lossy(&buf[..n]).lines().next().unwrap_or("").split(' ').nth(1).unwrap_or("/").to_string(); + let (kind, body) = match path.as_str() { + "/s.css" => ("text/css", "h1{font-size:3rem}".to_string()), + _ => ("text/html", "Old site

Welcome

We fix things.

".to_string()), + }; + let _ = s + .write_all(format!("HTTP/1.1 200 OK\r\nContent-Type: {kind}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).as_bytes()) + .await; + let _ = s.shutdown().await; + } + }); + + let dir = tempfile::tempdir().unwrap(); + let folder = dir.path().to_string_lossy().into_owned(); + let saved = capture(&format!("127.0.0.1:{port}"), &folder, &page::Nobody).await.unwrap(); + assert!(!saved.screenshot); + assert_eq!(saved.title, "Old site"); + assert_eq!(saved.url, format!("http://127.0.0.1:{port}/")); + assert!(saved.files.starts_with(&["page.html".to_string(), "text.md".to_string(), "style-1.css".to_string()]), "{:?}", saved.files); + let text = std::fs::read_to_string(dir.path().join(".hanzo/reference/text.md")).unwrap(); + assert!(text.contains("# Welcome") && text.contains("We fix things."), "{text}"); + + // Nothing is saved for a folder that is not there, or an address that is not allowed. + let file = dir.path().join("a-file"); + std::fs::write(&file, "x").unwrap(); + assert_eq!(capture("example.com", &file.to_string_lossy(), &page::Nobody).await, Err("That is not a folder.".to_string())); + assert!(capture("http://192.168.0.1/", &folder, &page::Nobody).await.is_err()); + + // A site that will not open leaves no folder behind; one that does makes its own. + let fresh = dir.path().join("Hanzo/new-site"); + assert!(capture("http://10.0.0.1/", &fresh.to_string_lossy(), &page::Nobody).await.is_err()); + assert!(!fresh.exists()); + capture(&format!("127.0.0.1:{port}"), &fresh.to_string_lossy(), &page::Nobody).await.unwrap(); + assert!(fresh.join(".hanzo/reference/text.md").is_file()); + + // The agent's files are served as they land. + let preview = local_preview_start(folder.clone()).await.unwrap(); + std::fs::write(dir.path().join("redesign/index.html"), "

Fresh

").unwrap(); + let mut s = TcpStream::connect(("127.0.0.1", preview.port)).await.unwrap(); + s.write_all(format!("GET / HTTP/1.1\r\nHost: 127.0.0.1:{}\r\n\r\n", preview.port).as_bytes()).await.unwrap(); + let mut out = String::new(); + s.read_to_string(&mut out).await.unwrap(); + assert!(out.contains("

Fresh

") && out.contains("/__hanzo/reload.js"), "{out}"); + local_preview_stop(folder).await.unwrap(); + assert!(!preview::serving(preview.port)); + } +} diff --git a/apps/hanzo-desktop/src-tauri/src/commands/local_site/page.rs b/apps/hanzo-desktop/src-tauri/src/commands/local_site/page.rs new file mode 100644 index 00000000..0dbb55b2 --- /dev/null +++ b/apps/hanzo-desktop/src-tauri/src/commands/local_site/page.rs @@ -0,0 +1,655 @@ +//! A saved copy of a site, for the agent to read. +//! +//! The agent works in a sandbox without a network, so the window saves what it +//! needs into the folder it will work in, under `.hanzo/reference/`: +//! +//! page.html the page as the site sent it +//! text.md its words in reading order, with its address, description, +//! the pictures it shows and the stylesheets saved beside it +//! style-N.css up to six of its stylesheets +//! screenshot.* how it looks, when the person's own browser could take one +//! +//! Each capture replaces the last, so nothing stale is left to read. + +use std::collections::HashMap; +use std::io; +use std::path::Path; +use std::time::Duration; + +use regex::Regex; +use reqwest::Url; + +/// Where the copy goes, under the folder. +pub const REFERENCE: &str = ".hanzo/reference"; + +/// How many stylesheets are saved, and how many pictures are listed. +pub const SHEETS: usize = 6; +const IMAGES: usize = 40; + +/// The words of a page are cut off here. +const TEXT_LIMIT: usize = 200_000; + +/// A page, read. +pub struct Page { + /// Where it was, after any redirects. + pub url: Url, + pub title: String, + pub description: String, + pub html: String, + /// Its words, in reading order. + pub text: String, + /// The pictures it shows: their address and what they say they are of. + pub images: Vec<(String, String)>, + /// The stylesheets it links, in order. + pub sheets: Vec, +} + +fn regex(cell: &'static std::sync::OnceLock, pattern: &str) -> &'static Regex { + cell.get_or_init(|| Regex::new(pattern).expect("a fixed pattern")) +} + +/// Read a page's HTML. +pub fn read(url: Url, html: &str) -> Page { + static TITLE: std::sync::OnceLock = std::sync::OnceLock::new(); + static META: std::sync::OnceLock = std::sync::OnceLock::new(); + static IMG: std::sync::OnceLock = std::sync::OnceLock::new(); + static LINK: std::sync::OnceLock = std::sync::OnceLock::new(); + + let title = regex(&TITLE, r"(?is)]*>(.*?)") + .captures(html) + .map(|c| squeeze(&decode(&c[1]))) + .unwrap_or_default(); + + let mut description = String::new(); + for m in regex(&META, r"(?is)]*>").find_iter(html) { + let tag = m.as_str(); + let name = attribute(tag, "name").or_else(|| attribute(tag, "property")); + if matches!(name.as_deref(), Some("description" | "og:description")) { + if let Some(content) = attribute(tag, "content") { + description = squeeze(&decode(&content)); + break; + } + } + } + + let mut images = Vec::new(); + for m in regex(&IMG, r"(?is)]*>").find_iter(html) { + if images.len() >= IMAGES { + break; + } + let tag = m.as_str(); + let Some(src) = attribute(tag, "src").filter(|s| !s.starts_with("data:")) else { continue }; + let Ok(at) = url.join(&decode(&src)) else { continue }; + if !matches!(at.scheme(), "http" | "https") || images.iter().any(|(a, _): &(String, String)| *a == at.as_str()) { + continue; + } + images.push((at.to_string(), squeeze(&decode(&attribute(tag, "alt").unwrap_or_default())))); + } + + let mut sheets: Vec = Vec::new(); + for m in regex(&LINK, r"(?is)]*>").find_iter(html) { + let tag = m.as_str(); + let rel = attribute(tag, "rel").unwrap_or_default().to_ascii_lowercase(); + if !rel.split_whitespace().any(|r| r == "stylesheet") { + continue; + } + let Some(href) = attribute(tag, "href") else { continue }; + let Ok(at) = url.join(&decode(&href)) else { continue }; + if matches!(at.scheme(), "http" | "https") && !sheets.contains(&at) { + sheets.push(at); + } + } + sheets.truncate(SHEETS); + + Page { text: readable(html), url, title, description, html: html.to_string(), images, sheets } +} + +/// The value of an attribute in one tag, quoted either way or bare. +fn attribute(tag: &str, name: &str) -> Option { + static ATTR: std::sync::OnceLock = std::sync::OnceLock::new(); + let re = regex(&ATTR, r#"(?is)([a-zA-Z_:][-a-zA-Z0-9_:.]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))"#); + re.captures_iter(tag).find_map(|c| { + c[1].eq_ignore_ascii_case(name) + .then(|| c.get(2).or_else(|| c.get(3)).or_else(|| c.get(4)).map_or(String::new(), |v| v.as_str().to_string())) + }) +} + +/// Collapse every run of whitespace to one space. +fn squeeze(s: &str) -> String { + s.split_whitespace().collect::>().join(" ") +} + +/// HTML character references, the common named ones and every numeric one. +fn decode(s: &str) -> String { + static ENTITY: std::sync::OnceLock = std::sync::OnceLock::new(); + let re = regex(&ENTITY, r"&(#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6}|[a-zA-Z][a-zA-Z0-9]{1,8});"); + re.replace_all(s, |c: ®ex::Captures| { + let name = &c[1]; + let named = match name { + "amp" => Some('&'), + "lt" => Some('<'), + "gt" => Some('>'), + "quot" => Some('"'), + "apos" => Some('\''), + "nbsp" => Some(' '), + "ndash" => Some('–'), + "mdash" => Some('—'), + "hellip" => Some('…'), + "copy" => Some('©'), + "reg" => Some('®'), + "trade" => Some('™'), + "lsquo" => Some('‘'), + "rsquo" => Some('’'), + "ldquo" => Some('“'), + "rdquo" => Some('”'), + "middot" => Some('·'), + "bull" => Some('•'), + "laquo" => Some('«'), + "raquo" => Some('»'), + "euro" => Some('€'), + "pound" => Some('£'), + _ => None, + }; + let number = name.strip_prefix('#').and_then(|n| match n.strip_prefix(['x', 'X']) { + Some(hex) => u32::from_str_radix(hex, 16).ok(), + None => n.parse().ok(), + }); + named + .or_else(|| number.and_then(char::from_u32)) + .map_or_else(|| c[0].to_string(), |ch| ch.to_string()) + }) + .into_owned() +} + +/// A page's words in reading order, as markdown the agent can read: headings as +/// `#`, list items as `-`, links as `[text](address)`, one block per line. +/// Scripts, styles, the head and anything hidden from a reader are dropped. +pub fn readable(html: &str) -> String { + static NOISE: std::sync::OnceLock = std::sync::OnceLock::new(); + static TOKEN: std::sync::OnceLock = std::sync::OnceLock::new(); + let noise = regex( + &NOISE, + r"(?is)|]*>|<\?.*?\?>|<(script|style|noscript|svg|template|head|iframe|object|select|textarea)\b[^>]*>.*?", + ); + let token = regex( + &TOKEN, + r#"(?s)<(/?)([A-Za-z][A-Za-z0-9-]*)((?:[^>"']|"[^"]*"|'[^']*')*)>|([^<]+|<)"#, + ); + let body = noise.replace_all(html, " "); + let mut out = String::new(); + let mut href: Vec> = Vec::new(); + let mut pre = 0usize; + for m in token.captures_iter(&body) { + if let Some(text) = m.get(4) { + if pre > 0 { + out.push_str(&decode(text.as_str())); + } else { + let words = decode(text.as_str()); + let squeezed = squeeze(&words); + if squeezed.is_empty() { + if !out.is_empty() && !out.ends_with([' ', '\n']) && words.chars().any(char::is_whitespace) { + out.push(' '); + } + continue; + } + if words.starts_with(char::is_whitespace) && !out.is_empty() && !out.ends_with([' ', '\n']) { + out.push(' '); + } + out.push_str(&squeezed); + if words.ends_with(char::is_whitespace) { + out.push(' '); + } + } + continue; + } + let closing = &m[1] == "/"; + let name = m[2].to_ascii_lowercase(); + let attrs = m.get(3).map_or("", |a| a.as_str()); + match name.as_str() { + "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => { + out.push_str("\n\n"); + if !closing { + out.push_str(&"#".repeat(name[1..].parse::().unwrap_or(1))); + out.push(' '); + } + } + "li" | "dt" | "dd" => { + if !closing { + out.push_str("\n- "); + } + } + "br" | "hr" => out.push('\n'), + "pre" => { + if closing { + pre = pre.saturating_sub(1); + if pre == 0 { + out.push_str("\u{2}\n"); + } + } else { + if pre == 0 { + out.push_str("\n\u{1}"); + } + pre += 1; + } + } + "a" => { + if closing { + if let Some(Some(to)) = href.pop() { + if out.ends_with('[') { + out.pop(); + } else { + out.push_str(&format!("]({to})")); + } + } + } else { + let to = attribute(attrs, "href") + .map(|h| decode(&h)) + .filter(|h| !h.is_empty() && !h.starts_with('#') && !h.to_ascii_lowercase().starts_with("javascript:")); + if to.is_some() { + out.push('['); + } + href.push(to); + } + } + "img" => { + if let Some(alt) = attribute(attrs, "alt").map(|a| squeeze(&decode(&a))).filter(|a| !a.is_empty()) { + out.push_str(&format!("[image: {alt}]")); + } + } + "td" | "th" => out.push(' '), + "p" | "div" | "section" | "article" | "header" | "footer" | "nav" | "main" | "aside" | "ul" | "ol" | "dl" + | "table" | "tr" | "blockquote" | "form" | "figure" | "figcaption" | "details" | "summary" | "fieldset" + | "address" | "body" | "html" => out.push('\n'), + _ => {} + } + } + // A line inside
 keeps its indent; every other line is trimmed, and blank lines never repeat.
+    let mut lines: Vec = Vec::new();
+    let mut inside = false;
+    for raw in out.split('\n') {
+        let keep = inside || raw.starts_with('\u{1}');
+        if raw.contains('\u{1}') {
+            inside = true;
+        }
+        if raw.contains('\u{2}') {
+            inside = false;
+        }
+        let bare = raw.replace(['\u{1}', '\u{2}'], "");
+        let line = if keep { bare.trim_end().to_string() } else { bare.trim().to_string() };
+        if line.is_empty() && lines.last().map_or(true, String::is_empty) {
+            continue;
+        }
+        lines.push(line);
+    }
+    let mut text = lines.join("\n").trim().to_string();
+    if text.len() > TEXT_LIMIT {
+        let mut cut = TEXT_LIMIT;
+        while !text.is_char_boundary(cut) {
+            cut -= 1;
+        }
+        text.truncate(cut);
+        text.push_str("\n\n[The rest of the page is cut off.]");
+    }
+    text
+}
+
+/// The text file that goes with a copy: what the page is, then its words, then what was saved beside it.
+fn digest(page: &Page, sheets: &[(Url, usize)], screenshot: Option<&str>) -> String {
+    let mut s = String::new();
+    s.push_str(&format!("# {}\n\n", if page.title.is_empty() { page.url.as_str() } else { &page.title }));
+    s.push_str(&format!("Address: {}\n", page.url));
+    if !page.description.is_empty() {
+        s.push_str(&format!("Description: {}\n", page.description));
+    }
+    s.push_str("\n---\n\n");
+    s.push_str(&page.text);
+    s.push_str("\n\n---\n\n## Saved beside this file\n\n- page.html: the page as the site sent it\n");
+    for (n, (url, _)) in sheets.iter().enumerate() {
+        s.push_str(&format!("- style-{}.css: {url}\n", n + 1));
+    }
+    if let Some(name) = screenshot {
+        s.push_str(&format!("- {name}: how the page looks\n"));
+    }
+    if !page.images.is_empty() {
+        s.push_str("\n## Pictures on the page\n\nThese can be linked from their own addresses.\n\n");
+        for (at, alt) in &page.images {
+            if alt.is_empty() {
+                s.push_str(&format!("- {at}\n"));
+            } else {
+                s.push_str(&format!("- {alt}: {at}\n"));
+            }
+        }
+    }
+    s
+}
+
+/// What was saved.
+pub struct Saved {
+    /// The names of the files written, under the reference folder.
+    pub files: Vec,
+    pub screenshot: bool,
+}
+
+/// Save a copy of `page` under `/.hanzo/reference`, replacing any copy there.
+/// `sheets` are the stylesheets fetched, in order; `shot` is a picture and its extension.
+pub fn save(dir: &Path, page: &Page, sheets: &[(Url, Vec)], shot: Option<(Vec, &str)>) -> io::Result {
+    let at = dir.join(REFERENCE);
+    match std::fs::remove_dir_all(&at) {
+        Err(e) if e.kind() != io::ErrorKind::NotFound => return Err(e),
+        _ => {}
+    }
+    std::fs::create_dir_all(&at)?;
+    let mut files = vec!["page.html".to_string(), "text.md".to_string()];
+    std::fs::write(at.join("page.html"), &page.html)?;
+    let mut kept = Vec::new();
+    for (n, (url, css)) in sheets.iter().enumerate() {
+        let name = format!("style-{}.css", n + 1);
+        let mut body = format!("/* {url} */\n").into_bytes();
+        body.extend_from_slice(css);
+        std::fs::write(at.join(&name), body)?;
+        files.push(name);
+        kept.push((url.clone(), css.len()));
+    }
+    let picture = match &shot {
+        Some((bytes, ext)) => {
+            let name = format!("screenshot.{ext}");
+            std::fs::write(at.join(&name), bytes)?;
+            files.push(name.clone());
+            Some(name)
+        }
+        None => None,
+    };
+    std::fs::write(at.join("text.md"), digest(page, &kept, picture.as_deref()))?;
+    Ok(Saved { files, screenshot: picture.is_some() })
+}
+
+/// The extension a picture's bytes call for, or None when they are not a picture a browser makes.
+pub fn picture(bytes: &[u8]) -> Option<&'static str> {
+    if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
+        Some("png")
+    } else if bytes.starts_with(&[0xFF, 0xD8, 0xFF]) {
+        Some("jpg")
+    } else if bytes.len() > 12 && &bytes[..4] == b"RIFF" && &bytes[8..12] == b"WEBP" {
+        Some("webp")
+    } else {
+        None
+    }
+}
+
+/// What the person's own browser can do: a method and string params in, JSON out.
+/// It is the extension's command surface (`commands::browser`), named here so the
+/// capture can be driven by anything that answers it.
+pub trait Browse {
+    fn call(
+        &self,
+        method: &str,
+        params: HashMap,
+    ) -> impl std::future::Future> + Send;
+}
+
+/// No browser at all: where nothing is paired with Hanzo (Windows has no bridge to one), and what a test uses to keep a capture off the real one.
+#[cfg(any(not(unix), test))]
+pub struct Nobody;
+
+#[cfg(any(not(unix), test))]
+impl Browse for Nobody {
+    async fn call(&self, _: &str, _: HashMap) -> Result {
+        Err("There is no browser paired with Hanzo.".into())
+    }
+}
+
+/// The first value under `key`, at any depth: the extension wraps its answers and the wrapper is not ours to depend on.
+fn find<'a>(v: &'a serde_json::Value, key: &str) -> Option<&'a serde_json::Value> {
+    match v {
+        serde_json::Value::Object(map) => map.get(key).or_else(|| map.values().find_map(|x| find(x, key))),
+        serde_json::Value::Array(items) => items.iter().find_map(|x| find(x, key)),
+        _ => None,
+    }
+}
+
+/// A picture of the page as the person's own browser shows it: open it in a new tab, wait for it to
+/// finish loading, take the whole page, close the tab. None when there is no browser, or it declined.
+/// The tab is closed whatever happens after it opened.
+pub async fn shoot(browser: &B, url: &str, settle: Duration, patience: Duration) -> Option<(Vec, &'static str)> {
+    use base64::Engine;
+
+    let made = browser.call("Target.createTarget", HashMap::from([("url".into(), url.to_string())])).await.ok()?;
+    let id = match (find(&made, "targetId"), find(&made, "tabId")) {
+        (Some(serde_json::Value::String(s)), _) => s.clone(),
+        (_, Some(n)) => n.to_string().trim_matches('"').to_string(),
+        _ => return None,
+    };
+    let result = async {
+        // Ready when the page says so; a browser that will not answer that is given the settle time instead.
+        let mut waited = Duration::ZERO;
+        let mut answered = false;
+        while waited < patience {
+            let state = browser
+                .call(
+                    "Runtime.evaluate",
+                    HashMap::from([
+                        ("expression".into(), "document.readyState".into()),
+                        ("returnByValue".into(), "true".into()),
+                        ("targetId".into(), id.clone()),
+                    ]),
+                )
+                .await;
+            match state {
+                Ok(v) if v.to_string().contains("complete") => {
+                    answered = true;
+                    break;
+                }
+                Ok(_) => {}
+                Err(_) => break,
+            }
+            tokio::time::sleep(Duration::from_millis(400)).await;
+            waited += Duration::from_millis(400);
+        }
+        if !answered {
+            tokio::time::sleep(settle).await;
+        }
+        let shot = browser
+            .call("hanzo.screenshot", HashMap::from([("tabId".into(), id.clone()), ("fullPage".into(), "true".into())]))
+            .await
+            .ok()?;
+        let data = find(&shot, "data")?.as_str()?;
+        let data = data.split_once("base64,").map_or(data, |(_, b)| b);
+        let bytes = base64::engine::general_purpose::STANDARD.decode(data.trim()).ok()?;
+        let ext = picture(&bytes)?;
+        Some((bytes, ext))
+    }
+    .await;
+    let _ = browser.call("Target.closeTarget", HashMap::from([("targetId".into(), id)])).await;
+    result
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    fn at(s: &str) -> Url {
+        Url::parse(s).unwrap()
+    }
+
+    const PAGE: &str = r##"
+      Acme & Sons — Plumbing
+      
+      
+      
+      
+      
+      
+

Fast fixes

Call us any time — we answer.

+ + + Our  vaninline +
  keep
+   this
+

One bold word.

"##; + + #[test] + fn reads_what_a_page_is() { + let p = read(at("https://acme.test/start/"), PAGE); + assert_eq!(p.title, "Acme & Sons — Plumbing"); + assert_eq!(p.description, "Plumbers you can trust, since 1982."); + assert_eq!( + p.sheets, + vec![at("https://acme.test/css/site.css"), at("https://cdn.example.net/ui.css")] + ); + assert_eq!( + p.images, + vec![ + ("https://acme.test/logo.png".to_string(), String::new()), + ("https://acme.test/van.jpg".to_string(), "Our van".to_string()), + ] + ); + } + + #[test] + fn the_words_come_in_reading_order_without_the_machinery() { + let text = readable(PAGE); + assert!(!text.contains("secret") && !text.contains("color:red") && !text.contains("hidden") && !text.contains("enable js"), "{text}"); + assert!(text.contains("[Home](/) [About us](/about)"), "{text}"); + assert!(!text.contains("Top]"), "an anchor link is not a destination: {text}"); + assert!(text.contains("# Fast fixes"), "{text}"); + assert!(text.contains("Call us any time — we answer."), "{text}"); + assert!(text.contains("- Leaks\n- [Pipes](https://x.test/p?a=1&b=2)"), "{text}"); + assert!(text.contains("[image: Our van]"), "{text}"); + assert!(text.contains(" keep\n this"), "preformatted text keeps its shape: {text}"); + assert!(text.contains("One bold word."), "{text}"); + assert!(!text.contains("\n\n\n"), "{text}"); + } + + #[test] + fn a_doctype_is_not_a_word() { + assert_eq!(readable("

Hi

"), "Hi"); + } + + #[test] + fn an_empty_link_leaves_no_brackets() { + assert_eq!(readable(r#"

a

b

"#), "a\n\nb"); + } + + #[test] + fn character_references_are_decoded() { + assert_eq!(decode("a&b <c> AB &unknown; "q""), "a&b AB &unknown; \"q\""); + } + + #[test] + fn long_pages_are_cut_off_on_a_boundary() { + let long = format!("

{}

", "é".repeat(TEXT_LIMIT)); + let text = readable(&long); + assert!(text.ends_with("[The rest of the page is cut off.]")); + assert!(text.len() < TEXT_LIMIT + 64); + } + + #[test] + fn a_copy_replaces_the_last_and_leaves_the_rest_of_the_folder_alone() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("keep.txt"), "mine").unwrap(); + let old = dir.path().join(REFERENCE); + std::fs::create_dir_all(&old).unwrap(); + std::fs::write(old.join("stale.css"), "old").unwrap(); + + let p = read(at("https://acme.test/"), PAGE); + let sheets = vec![(at("https://acme.test/css/site.css"), b"h1{margin:0}".to_vec())]; + let saved = save(dir.path(), &p, &sheets, Some((b"\x89PNG\r\n\x1a\nxx".to_vec(), "png"))).unwrap(); + assert_eq!(saved.files, vec!["page.html", "text.md", "style-1.css", "screenshot.png"]); + assert!(saved.screenshot); + assert!(!old.join("stale.css").exists()); + assert_eq!(std::fs::read_to_string(dir.path().join("keep.txt")).unwrap(), "mine"); + assert_eq!(std::fs::read_to_string(old.join("page.html")).unwrap(), PAGE); + assert_eq!(std::fs::read_to_string(old.join("style-1.css")).unwrap(), "/* https://acme.test/css/site.css */\nh1{margin:0}"); + let digest = std::fs::read_to_string(old.join("text.md")).unwrap(); + assert!(digest.starts_with("# Acme & Sons — Plumbing\n\nAddress: https://acme.test/\nDescription: Plumbers you can trust"), "{digest}"); + assert!(digest.contains("- style-1.css: https://acme.test/css/site.css"), "{digest}"); + assert!(digest.contains("- screenshot.png: how the page looks"), "{digest}"); + assert!(digest.contains("- Our van: https://acme.test/van.jpg"), "{digest}"); + assert!(digest.contains("# Fast fixes"), "{digest}"); + } + + #[test] + fn no_screenshot_is_said_by_saying_nothing() { + let dir = tempfile::tempdir().unwrap(); + let p = read(at("https://acme.test/"), "x

hi

"); + let saved = save(dir.path(), &p, &[], None).unwrap(); + assert_eq!(saved.files, vec!["page.html", "text.md"]); + assert!(!saved.screenshot); + assert!(!std::fs::read_to_string(dir.path().join(REFERENCE).join("text.md")).unwrap().contains("screenshot")); + } + + #[test] + fn pictures_are_known_by_their_bytes() { + assert_eq!(picture(b"\x89PNG\r\n\x1a\nrest"), Some("png")); + assert_eq!(picture(&[0xFF, 0xD8, 0xFF, 0xE0, 0]), Some("jpg")); + assert_eq!(picture(b"RIFF\0\0\0\0WEBPVP8 "), Some("webp")); + assert_eq!(picture(b""), None); + assert_eq!(picture(b""), None); + } + + mod browser { + use std::sync::Mutex; + + use base64::Engine; + use serde_json::{json, Value}; + + use super::super::*; + + /// A browser that answers like the extension: a tab id, a ready state, a picture, a closed tab. + struct Fake { + calls: Mutex)>>, + ready_after: usize, + picture: Vec, + } + + impl Browse for Fake { + async fn call(&self, method: &str, params: HashMap) -> Result { + let mut calls = self.calls.lock().unwrap(); + calls.push((method.to_string(), params)); + match method { + "Target.createTarget" => Ok(json!({ "result": { "targetId": "tab-7", "tabId": 7 } })), + "Runtime.evaluate" => { + let asked = calls.iter().filter(|(m, _)| m == "Runtime.evaluate").count(); + let state = if asked > self.ready_after { "complete" } else { "loading" }; + Ok(json!({ "result": { "result": { "type": "string", "value": state } } })) + } + "hanzo.screenshot" => Ok(json!({ "data": base64::engine::general_purpose::STANDARD.encode(&self.picture) })), + "Target.closeTarget" => Ok(json!({ "success": true })), + other => Err(format!("unknown method {other}")), + } + } + } + + fn names(f: &Fake) -> Vec { + f.calls.lock().unwrap().iter().map(|(m, _)| m.clone()).collect() + } + + #[tokio::test] + async fn takes_a_picture_once_the_page_is_ready_and_closes_the_tab() { + let f = Fake { calls: Mutex::default(), ready_after: 2, picture: b"\x89PNG\r\n\x1a\nbytes".to_vec() }; + let got = shoot(&f, "https://acme.test/", Duration::from_millis(1), Duration::from_secs(5)).await.unwrap(); + assert_eq!(got, (b"\x89PNG\r\n\x1a\nbytes".to_vec(), "png")); + assert_eq!( + names(&f), + vec!["Target.createTarget", "Runtime.evaluate", "Runtime.evaluate", "Runtime.evaluate", "hanzo.screenshot", "Target.closeTarget"] + ); + let calls = f.calls.lock().unwrap(); + assert_eq!(calls[0].1["url"], "https://acme.test/"); + let shot = calls.iter().find(|(m, _)| m == "hanzo.screenshot").unwrap(); + assert_eq!(shot.1["tabId"], "tab-7"); + assert_eq!(calls.last().unwrap().1["targetId"], "tab-7"); + } + + #[tokio::test] + async fn a_picture_that_is_not_one_is_no_picture_and_the_tab_is_still_closed() { + let f = Fake { calls: Mutex::default(), ready_after: 0, picture: b"not an image".to_vec() }; + assert!(shoot(&f, "https://acme.test/", Duration::from_millis(1), Duration::from_secs(5)).await.is_none()); + assert_eq!(names(&f).last().unwrap(), "Target.closeTarget"); + } + + #[tokio::test] + async fn no_browser_is_no_picture() { + assert!(shoot(&Nobody, "https://acme.test/", Duration::from_millis(1), Duration::from_secs(1)).await.is_none()); + } + } +} diff --git a/apps/hanzo-desktop/src-tauri/src/commands/local_site/preview.rs b/apps/hanzo-desktop/src-tauri/src/commands/local_site/preview.rs new file mode 100644 index 00000000..60d0cf6b --- /dev/null +++ b/apps/hanzo-desktop/src-tauri/src/commands/local_site/preview.rs @@ -0,0 +1,620 @@ +//! A live preview of a folder on this computer. +//! +//! The agent writes a site as static files; this serves them on `127.0.0.1`, on a +//! port the system picks, so the person watches the design appear in their own +//! browser. Every page it serves carries a small script that listens on a +//! server-sent event stream (`/__hanzo/events`) and reloads when a file in the +//! folder is added, changed or removed, so the browser follows each run's work +//! without being asked. +//! +//! It serves GET and HEAD only, files under the folder only (a path that climbs, +//! carries a NUL or a backslash, or reaches outside through a link is refused), +//! and only to a request that names this server's own address in `Host`, so a +//! web page cannot reach it through a name it controls. Until the folder has an +//! `index.html` it answers with a page that says the design is being built, which +//! the same script swaps for the real one the moment it lands. + +use std::collections::hash_map::DefaultHasher; +use std::collections::HashMap; +use std::hash::{Hash, Hasher}; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex, OnceLock}; +use std::time::Duration; + +use serde::Serialize; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::net::{TcpListener, TcpStream}; +use tokio::sync::watch; + +/// What serves a folder. +#[derive(Serialize, Clone, Debug, PartialEq)] +pub struct Preview { + /// The address to open: `http://127.0.0.1:/`. + pub url: String, + pub port: u16, +} + +struct Running { + port: u16, + stop: watch::Sender, +} + +fn running() -> &'static Mutex> { + static RUNNING: OnceLock>> = OnceLock::new(); + RUNNING.get_or_init(Mutex::default) +} + +/// The most files whose change is watched, and how deep. +const WATCHED: usize = 5000; +const DEPTH: usize = 16; + +/// The longest a request's head may be, and how long it may take to arrive. +const HEAD: usize = 16 * 1024; +const HEAD_TIME: Duration = Duration::from_secs(10); + +const SCRIPT: &str = "(function(){var s=new EventSource('/__hanzo/events');s.addEventListener('reload',function(){location.reload()})})();"; +const TAG: &str = ""; + +const BUILDING: &str = "Building your new design

Hanzo is building your new design

This page changes by itself when it is ready.

"; + +const MISSING: &str = "Not found

There is nothing here yet

This page is not part of the design.

"; + +/// Start serving ``, or answer the server already serving it. The folder is made if it is not there. +pub async fn start(root: &Path) -> Result { + std::fs::create_dir_all(root).map_err(|e| format!("Hanzo could not make the preview folder: {e}"))?; + let root = std::fs::canonicalize(root).map_err(|e| format!("Hanzo could not open the preview folder: {e}"))?; + if let Some(up) = running().lock().map_err(|_| "The preview is busy.".to_string())?.get(&root) { + return Ok(address(up.port)); + } + let listener = TcpListener::bind(("127.0.0.1", 0)).await.map_err(|e| format!("Hanzo could not start the preview: {e}"))?; + let port = listener.local_addr().map_err(|e| e.to_string())?.port(); + let (stop, stopped) = watch::channel(false); + { + let mut all = running().lock().map_err(|_| "The preview is busy.".to_string())?; + // Two starts racing: the first one in stays, and the other's listener is dropped unused. + if let Some(up) = all.get(&root) { + return Ok(address(up.port)); + } + all.insert(root.clone(), Running { port, stop }); + } + tokio::spawn(serve(listener, root, port, stopped)); + Ok(address(port)) +} + +fn address(port: u16) -> Preview { + Preview { url: format!("http://127.0.0.1:{port}/"), port } +} + +/// Stop serving ``; whether it was being served. +pub fn stop(root: &Path) -> bool { + let root = std::fs::canonicalize(root).unwrap_or_else(|_| root.to_path_buf()); + let Ok(mut all) = running().lock() else { return false }; + match all.remove(&root) { + Some(up) => { + let _ = up.stop.send(true); + true + } + None => false, + } +} + +/// Stop every preview: the window closed, or the app is leaving. +pub fn stop_all() { + if let Ok(mut all) = running().lock() { + for (_, up) in all.drain() { + let _ = up.stop.send(true); + } + } +} + +/// Whether `port` is a preview this app is serving: the only address it opens in a browser. +pub fn serving(port: u16) -> bool { + running().lock().map_or(false, |all| all.values().any(|up| up.port == port)) +} + +struct Site { + root: PathBuf, + port: u16, +} + +async fn serve(listener: TcpListener, root: PathBuf, port: u16, mut stopped: watch::Receiver) { + let site = Arc::new(Site { root: root.clone(), port }); + let (changes, changed) = watch::channel(stamp(&root)); + let mut watching = stopped.clone(); + tokio::spawn(async move { + loop { + tokio::select! { + _ = watching.changed() => break, + _ = tokio::time::sleep(Duration::from_millis(400)) => { + let at = root.clone(); + if let Ok(now) = tokio::task::spawn_blocking(move || stamp(&at)).await { + changes.send_if_modified(|was| if *was == now { false } else { *was = now; true }); + } + } + } + } + }); + loop { + tokio::select! { + _ = stopped.changed() => break, + accepted = listener.accept() => { + if let Ok((socket, _)) = accepted { + tokio::spawn(answer(socket, site.clone(), changed.clone(), stopped.clone())); + } + } + } + } +} + +/// A fingerprint of what is in the folder: every file's place, size and modified time. +/// It changes when a file is added, changed or removed, and not otherwise. +pub fn stamp(root: &Path) -> u64 { + let mut hasher = DefaultHasher::new(); + let mut seen = 0usize; + let mut todo = vec![(root.to_path_buf(), 0usize)]; + while let Some((dir, depth)) = todo.pop() { + let Ok(entries) = std::fs::read_dir(&dir) else { continue }; + let mut names: Vec<_> = entries.filter_map(Result::ok).collect(); + names.sort_by_key(|e| e.file_name()); + for entry in names { + seen += 1; + if seen > WATCHED { + return hasher.finish(); + } + let Ok(meta) = entry.path().symlink_metadata() else { continue }; + entry.path().strip_prefix(root).unwrap_or(&entry.path()).hash(&mut hasher); + meta.len().hash(&mut hasher); + meta.modified() + .ok() + .and_then(|m| m.duration_since(std::time::UNIX_EPOCH).ok()) + .map(|d| d.as_nanos()) + .hash(&mut hasher); + if meta.is_dir() && depth < DEPTH { + todo.push((entry.path(), depth + 1)); + } + } + } + hasher.finish() +} + +struct Request { + method: String, + target: String, + host: String, +} + +fn parse(head: &str) -> Option { + let mut lines = head.split("\r\n"); + let mut first = lines.next()?.split(' '); + let method = first.next()?.to_string(); + let target = first.next()?.to_string(); + if !first.next()?.starts_with("HTTP/1.") || first.next().is_some() { + return None; + } + let host = lines + .filter_map(|l| l.split_once(':')) + .find(|(k, _)| k.eq_ignore_ascii_case("host")) + .map(|(_, v)| v.trim().to_ascii_lowercase()) + .unwrap_or_default(); + Some(Request { method, target, host }) +} + +async fn read_head(socket: &mut TcpStream) -> Option { + let mut buf = Vec::with_capacity(1024); + let mut chunk = [0u8; 1024]; + loop { + let n = socket.read(&mut chunk).await.ok()?; + if n == 0 { + return None; + } + buf.extend_from_slice(&chunk[..n]); + if let Some(end) = buf.windows(4).position(|w| w == b"\r\n\r\n") { + return String::from_utf8(buf[..end].to_vec()).ok(); + } + if buf.len() > HEAD { + return None; + } + } +} + +/// What a request resolves to. +#[derive(Debug, PartialEq)] +pub enum Route { + Events, + Script, + File(PathBuf), + /// A folder asked for without its closing slash: its own address, so its relative links resolve inside it. + Slash(String), + /// The folder has no `index.html` yet. + Building, + Missing, + /// A path that climbs, hides a NUL or a backslash, or leaves the folder. + Refused, +} + +/// A request target's path, percent-decoded; None when the encoding is not valid UTF-8. +fn decode(path: &str) -> Option { + let bytes = path.as_bytes(); + let mut out = Vec::with_capacity(bytes.len()); + let mut i = 0; + while i < bytes.len() { + if bytes[i] == b'%' { + let hex = path.get(i + 1..i + 3)?; + out.push(u8::from_str_radix(hex, 16).ok()?); + i += 3; + } else { + out.push(bytes[i]); + i += 1; + } + } + String::from_utf8(out).ok() +} + +/// Where a request for `target` goes, under `root` (already canonical). +pub fn route(root: &Path, target: &str) -> Route { + let path = target.split(['?', '#']).next().unwrap_or(""); + if !path.starts_with('/') { + return Route::Refused; + } + match path { + "/__hanzo/events" => return Route::Events, + "/__hanzo/reload.js" => return Route::Script, + _ => {} + } + let Some(path) = decode(path) else { return Route::Refused }; + if path.contains(['\0', '\\']) { + return Route::Refused; + } + let mut at = root.to_path_buf(); + for part in path.split('/').filter(|p| !p.is_empty() && *p != ".") { + if part == ".." { + return Route::Refused; + } + at.push(part); + } + if at != root && !path.ends_with('/') && at.is_dir() && std::fs::canonicalize(&at).is_ok_and(|real| real.starts_with(root)) { + return Route::Slash(format!("{}/", target.split(['?', '#']).next().unwrap_or(""))); + } + let candidate = if path.ends_with('/') || at == root { at.join("index.html") } else { at }; + match std::fs::canonicalize(&candidate) { + Ok(real) if real.starts_with(root) && real.is_file() => Route::File(real), + Ok(real) if !real.starts_with(root) => Route::Refused, + _ if candidate == root.join("index.html") => Route::Building, + _ => Route::Missing, + } +} + +/// The type a file is served as. +fn kind(path: &Path) -> &'static str { + match path.extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase).as_deref() { + Some("html" | "htm") => "text/html; charset=utf-8", + Some("css") => "text/css; charset=utf-8", + Some("js" | "mjs") => "text/javascript; charset=utf-8", + Some("json" | "map") => "application/json; charset=utf-8", + Some("svg") => "image/svg+xml", + Some("png") => "image/png", + Some("jpg" | "jpeg") => "image/jpeg", + Some("gif") => "image/gif", + Some("webp") => "image/webp", + Some("avif") => "image/avif", + Some("ico") => "image/x-icon", + Some("woff") => "font/woff", + Some("woff2") => "font/woff2", + Some("ttf") => "font/ttf", + Some("otf") => "font/otf", + Some("txt" | "md") => "text/plain; charset=utf-8", + Some("xml") => "application/xml; charset=utf-8", + Some("mp4") => "video/mp4", + Some("webm") => "video/webm", + Some("mp3") => "audio/mpeg", + Some("pdf") => "application/pdf", + Some("wasm") => "application/wasm", + _ => "application/octet-stream", + } +} + +/// `html` with the reload script before its last ``, or at its end. +pub fn inject(html: &[u8]) -> Vec { + let lower: Vec = html.iter().map(u8::to_ascii_lowercase).collect(); + let at = lower.windows(7).rposition(|w| w == b"").unwrap_or(html.len()); + let mut out = Vec::with_capacity(html.len() + TAG.len()); + out.extend_from_slice(&html[..at]); + out.extend_from_slice(TAG.as_bytes()); + out.extend_from_slice(&html[at..]); + out +} + +async fn say(socket: &mut TcpStream, status: u16, reason: &str, kind: &str, body: &[u8], head_only: bool) { + let head = format!( + "HTTP/1.1 {status} {reason}\r\nContent-Type: {kind}\r\nContent-Length: {}\r\nCache-Control: no-store\r\nX-Content-Type-Options: nosniff\r\nConnection: close\r\n\r\n", + body.len() + ); + let _ = socket.write_all(head.as_bytes()).await; + if !head_only { + let _ = socket.write_all(body).await; + } + let _ = socket.shutdown().await; +} + +async fn answer(mut socket: TcpStream, site: Arc, mut changed: watch::Receiver, mut stopped: watch::Receiver) { + let Ok(Some(head)) = tokio::time::timeout(HEAD_TIME, read_head(&mut socket)).await else { return }; + let Some(request) = parse(&head) else { + return say(&mut socket, 400, "Bad Request", "text/plain; charset=utf-8", b"Bad request", false).await; + }; + let own = [format!("127.0.0.1:{}", site.port), format!("localhost:{}", site.port)]; + if !own.contains(&request.host) { + return say(&mut socket, 421, "Misdirected Request", "text/plain; charset=utf-8", b"This preview answers only at its own address", false).await; + } + let head_only = request.method == "HEAD"; + if request.method != "GET" && !head_only { + return say(&mut socket, 405, "Method Not Allowed", "text/plain; charset=utf-8", b"Method not allowed", false).await; + } + match route(&site.root, &request.target) { + Route::Refused => say(&mut socket, 403, "Forbidden", "text/plain; charset=utf-8", b"Forbidden", head_only).await, + Route::Script => say(&mut socket, 200, "OK", "text/javascript; charset=utf-8", SCRIPT.as_bytes(), head_only).await, + Route::Slash(to) => { + let head = format!("HTTP/1.1 301 Moved Permanently\r\nLocation: {to}\r\nContent-Length: 0\r\nCache-Control: no-store\r\nConnection: close\r\n\r\n"); + let _ = socket.write_all(head.as_bytes()).await; + let _ = socket.shutdown().await; + } + Route::Building => say(&mut socket, 200, "OK", "text/html; charset=utf-8", &inject(BUILDING.as_bytes()), head_only).await, + Route::Missing => say(&mut socket, 404, "Not Found", "text/html; charset=utf-8", &inject(MISSING.as_bytes()), head_only).await, + Route::File(path) => match tokio::fs::read(&path).await { + Ok(bytes) => { + let kind = kind(&path); + let body = if kind.starts_with("text/html") { inject(&bytes) } else { bytes }; + say(&mut socket, 200, "OK", kind, &body, head_only).await + } + Err(_) => say(&mut socket, 404, "Not Found", "text/html; charset=utf-8", &inject(MISSING.as_bytes()), head_only).await, + }, + Route::Events => { + // What changes from here on is what this connection reports, so it is marked before the client hears it opened. + changed.mark_unchanged(); + let opened = "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nCache-Control: no-store\r\nConnection: keep-alive\r\n\r\nretry: 1000\n\n"; + if socket.write_all(opened.as_bytes()).await.is_err() { + return; + } + loop { + let line: &[u8] = tokio::select! { + _ = stopped.changed() => return, + c = changed.changed() => { + if c.is_err() { + return; + } + b"event: reload\ndata: changed\n\n" + } + _ = tokio::time::sleep(Duration::from_secs(15)) => b": still here\n\n", + }; + if socket.write_all(line).await.is_err() { + return; + } + } + } + } +} + +/// The registry is the process's, so the tests that start servers (and the one that stops them all) take turns. +#[cfg(test)] +pub(super) fn serial() -> &'static tokio::sync::Mutex<()> { + static LOCK: OnceLock> = OnceLock::new(); + LOCK.get_or_init(Default::default) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// One raw request, answered in full (the server closes each connection). + async fn ask(port: u16, request: &str) -> String { + let mut s = TcpStream::connect(("127.0.0.1", port)).await.unwrap(); + s.write_all(request.as_bytes()).await.unwrap(); + let mut out = Vec::new(); + tokio::time::timeout(Duration::from_secs(5), s.read_to_end(&mut out)).await.unwrap().unwrap(); + String::from_utf8_lossy(&out).into_owned() + } + + fn get(port: u16, path: &str) -> String { + format!("GET {path} HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\n\r\n") + } + + fn body(response: &str) -> &str { + response.split_once("\r\n\r\n").map_or("", |(_, b)| b) + } + + fn status(response: &str) -> &str { + response.lines().next().unwrap_or("") + } + + #[test] + fn paths_stay_inside_the_folder() { + let dir = tempfile::tempdir().unwrap(); + let root = std::fs::canonicalize(dir.path()).unwrap(); + std::fs::write(root.join("index.html"), "

hi

").unwrap(); + std::fs::create_dir_all(root.join("css")).unwrap(); + std::fs::write(root.join("css/a.css"), "a{}").unwrap(); + std::fs::create_dir_all(root.join("blog")).unwrap(); + std::fs::write(root.join("blog/index.html"), "

blog

").unwrap(); + std::fs::write(root.join("a b.txt"), "x").unwrap(); + let outside = tempfile::tempdir().unwrap(); + std::fs::write(outside.path().join("secret.txt"), "secret").unwrap(); + + assert_eq!(route(&root, "/"), Route::File(root.join("index.html"))); + assert_eq!(route(&root, "/index.html?x=1#y"), Route::File(root.join("index.html"))); + assert_eq!(route(&root, "/css/a.css"), Route::File(root.join("css/a.css"))); + assert_eq!(route(&root, "/blog"), Route::Slash("/blog/".into())); + assert_eq!(route(&root, "/blog?a=1"), Route::Slash("/blog/".into())); + assert_eq!(route(&root, "/blog/"), Route::File(root.join("blog/index.html"))); + assert_eq!(route(&root, "/a%20b.txt"), Route::File(root.join("a b.txt"))); + assert_eq!(route(&root, "/nope.css"), Route::Missing); + assert_eq!(route(&root, "/__hanzo/events"), Route::Events); + assert_eq!(route(&root, "/__hanzo/reload.js"), Route::Script); + + for bad in [ + "/../x", + "/css/../../x", + "/%2e%2e/x", + "/%2E%2E%2Fsecret", + "/css/..%2f..%2fx", + "/a\\b", + "/a%5Cb", + "/a%00b", + "/%ff", + "/%zz", + "/%2", + "*", + "http://evil/x", + ] { + assert_eq!(route(&root, bad), Route::Refused, "{bad}"); + } + + #[cfg(unix)] + { + std::os::unix::fs::symlink(outside.path().join("secret.txt"), root.join("link.txt")).unwrap(); + std::os::unix::fs::symlink(outside.path(), root.join("out")).unwrap(); + assert_eq!(route(&root, "/link.txt"), Route::Refused); + assert_eq!(route(&root, "/out/secret.txt"), Route::Refused); + } + } + + #[test] + fn an_empty_folder_is_building_and_other_paths_are_missing() { + let dir = tempfile::tempdir().unwrap(); + let root = std::fs::canonicalize(dir.path()).unwrap(); + assert_eq!(route(&root, "/"), Route::Building); + assert_eq!(route(&root, "/index.html"), Route::Building); + assert_eq!(route(&root, "/about.html"), Route::Missing); + } + + #[test] + fn the_script_goes_before_the_last_body_end() { + let out = String::from_utf8(inject(b"

a

")).unwrap(); + assert_eq!(out, format!("

a

{TAG}")); + let bare = String::from_utf8(inject(b"

a

")).unwrap(); + assert_eq!(bare, format!("

a

{TAG}")); + let two = String::from_utf8(inject(b"x")).unwrap(); + assert_eq!(two, format!("x")); + } + + #[test] + fn the_stamp_follows_the_files() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + let empty = stamp(root); + std::fs::write(root.join("index.html"), "a").unwrap(); + let one = stamp(root); + assert_ne!(empty, one); + assert_eq!(one, stamp(root)); + std::fs::write(root.join("index.html"), "ab").unwrap(); + assert_ne!(one, stamp(root)); + std::fs::create_dir_all(root.join("img")).unwrap(); + std::fs::write(root.join("img/a.png"), "x").unwrap(); + let three = stamp(root); + std::fs::remove_file(root.join("img/a.png")).unwrap(); + assert_ne!(three, stamp(root)); + } + + #[tokio::test] + async fn serves_the_folder_and_follows_it() { + let _turn = serial().lock().await; + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().join("redesign"); + let preview = start(&root).await.unwrap(); + assert!(preview.url.starts_with("http://127.0.0.1:") && preview.url.ends_with('/')); + assert!(serving(preview.port)); + // The same folder is the same server. + assert_eq!(start(&root).await.unwrap(), preview); + let port = preview.port; + + // Nothing built yet: the page says so, and carries the script that will swap it. + let first = ask(port, &get(port, "/")).await; + assert!(status(&first).contains("200"), "{first}"); + assert!(body(&first).contains("Hanzo is building your new design")); + assert!(body(&first).contains(TAG)); + + // A live reload connection, opened before the design lands. + let mut events = TcpStream::connect(("127.0.0.1", port)).await.unwrap(); + events.write_all(get(port, "/__hanzo/events").as_bytes()).await.unwrap(); + let mut seen = Vec::new(); + let mut buf = [0u8; 512]; + while !String::from_utf8_lossy(&seen).contains("retry:") { + let n = tokio::time::timeout(Duration::from_secs(5), events.read(&mut buf)).await.unwrap().unwrap(); + assert!(n > 0); + seen.extend_from_slice(&buf[..n]); + } + assert!(String::from_utf8_lossy(&seen).contains("text/event-stream")); + + // The agent writes its files. + std::fs::write(root.join("index.html"), "

New

").unwrap(); + std::fs::write(root.join("site.css"), "h1{color:teal}").unwrap(); + let mut got = String::new(); + let started = std::time::Instant::now(); + while !got.contains("event: reload") { + assert!(started.elapsed() < Duration::from_secs(5), "no reload arrived: {got:?}"); + let n = tokio::time::timeout(Duration::from_secs(5), events.read(&mut buf)).await.unwrap().unwrap(); + assert!(n > 0); + got.push_str(&String::from_utf8_lossy(&buf[..n])); + } + + let page = ask(port, &get(port, "/")).await; + assert!(body(&page).contains("

New

") && body(&page).contains(TAG), "{page}"); + assert!(page.contains("Cache-Control: no-store")); + let css = ask(port, &get(port, "/site.css")).await; + assert!(css.contains("Content-Type: text/css") && body(&css) == "h1{color:teal}", "{css}"); + let script = ask(port, &get(port, "/__hanzo/reload.js")).await; + assert!(body(&script).contains("EventSource('/__hanzo/events')")); + let head = ask(port, &format!("HEAD / HTTP/1.1\r\nHost: localhost:{port}\r\n\r\n")).await; + assert!(status(&head).contains("200") && body(&head).is_empty(), "{head}"); + let missing = ask(port, &get(port, "/nope")).await; + assert!(status(&missing).contains("404")); + + // Stopped, it stops answering, and its port is no longer one the app opens. + assert!(stop(&root)); + assert!(!serving(port)); + tokio::time::sleep(Duration::from_millis(200)).await; + assert!(TcpStream::connect(("127.0.0.1", port)).await.is_err()); + assert!(!stop(&root)); + } + + #[tokio::test] + async fn refuses_what_it_should() { + let _turn = serial().lock().await; + let dir = tempfile::tempdir().unwrap(); + let root = dir.path().join("redesign"); + std::fs::create_dir_all(&root).unwrap(); + std::fs::write(dir.path().join("secret.txt"), "secret").unwrap(); + let preview = start(&root).await.unwrap(); + let port = preview.port; + + // A climbing path, sent as the browser would never send it. + let up = ask(port, &get(port, "/../secret.txt")).await; + assert!(status(&up).contains("403") && !up.contains("secret\r\n") && body(&up) == "Forbidden", "{up}"); + let encoded = ask(port, &get(port, "/%2e%2e/secret.txt")).await; + assert!(status(&encoded).contains("403"), "{encoded}"); + + // A name the page controls pointing at this port is not this server's own address. + let rebound = ask(port, &format!("GET / HTTP/1.1\r\nHost: evil.example:{port}\r\n\r\n")).await; + assert!(status(&rebound).contains("421"), "{rebound}"); + let none = ask(port, "GET / HTTP/1.1\r\n\r\n").await; + assert!(status(&none).contains("421"), "{none}"); + + let post = ask(port, &format!("POST / HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nContent-Length: 0\r\n\r\n")).await; + assert!(status(&post).contains("405"), "{post}"); + let junk = ask(port, "nonsense\r\n\r\n").await; + assert!(status(&junk).contains("400"), "{junk}"); + + stop(&root); + } + + #[tokio::test] + async fn stop_all_stops_every_preview() { + let _turn = serial().lock().await; + let a = tempfile::tempdir().unwrap(); + let b = tempfile::tempdir().unwrap(); + let pa = start(&a.path().join("redesign")).await.unwrap(); + let pb = start(&b.path().join("redesign")).await.unwrap(); + assert_ne!(pa.port, pb.port); + assert!(serving(pa.port) && serving(pb.port)); + stop_all(); + assert!(!serving(pa.port) && !serving(pb.port)); + } +} diff --git a/apps/hanzo-desktop/src-tauri/src/commands/mod.rs b/apps/hanzo-desktop/src-tauri/src/commands/mod.rs index fdda9c82..7c96e344 100644 --- a/apps/hanzo-desktop/src-tauri/src/commands/mod.rs +++ b/apps/hanzo-desktop/src-tauri/src/commands/mod.rs @@ -13,4 +13,5 @@ pub mod mining; pub mod secure_storage; pub mod spotlight_commands; pub mod studio; +pub mod local_site; pub mod local_workspace; diff --git a/apps/hanzo-desktop/src-tauri/src/main.rs b/apps/hanzo-desktop/src-tauri/src/main.rs index 94486ca4..f78603a6 100644 --- a/apps/hanzo-desktop/src-tauri/src/main.rs +++ b/apps/hanzo-desktop/src-tauri/src/main.rs @@ -55,6 +55,9 @@ use crate::commands::local_workspace::{ local_workspace_write_file, local_workspace_create_file, local_workspace_create_dir, local_workspace_delete, local_workspace_discover, local_workspace_run_dev, }; +use crate::commands::local_site::{ + local_preview_open, local_preview_start, local_preview_stop, local_site_capture, local_site_folder, +}; use crate::mining::MiningManager; use crate::model_tools::{ model_tools_bitdelta_apply, model_tools_bitdelta_encode, model_tools_grpo_run, @@ -285,6 +288,11 @@ fn main() { local_workspace_delete, local_workspace_run_dev, local_workspace_discover, + local_site_folder, + local_site_capture, + local_preview_start, + local_preview_stop, + local_preview_open, #[cfg(feature = "machine")] machine::commands::machine_health, #[cfg(feature = "machine")] @@ -469,6 +477,7 @@ fn main() { api.prevent_exit(); } RunEvent::Exit { .. } => { + commands::local_site::stop_all(); tauri::async_runtime::spawn(async { log::debug!("killing hanzo-node before exit"); @@ -485,6 +494,10 @@ fn main() { RunEvent::Reopen { .. } => { let _ = recreate_window(app_handle.clone(), Window::Main, true); } + // A preview serves for the window that asked for it: the window closing ends it. + RunEvent::WindowEvent { label, event: WindowEvent::Destroyed, .. } if label == Window::Main.as_str() => { + commands::local_site::stop_all(); + } RunEvent::Ready => {} RunEvent::Resumed => {} RunEvent::MainEventsCleared => {} From 636479b2b1be03f326ae48f996cbc570d93a873d Mon Sep 17 00:00:00 2001 From: Zach Kelling Date: Fri, 20 Nov 2026 20:01:52 -0800 Subject: [PATCH 2/2] docs: redesign a site, the window's half Claude-Session: https://claude.ai/code/session_015XYKLn69gF4x75RWePpDBV --- LLM.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/LLM.md b/LLM.md index af0bc0e3..7848aa63 100644 --- a/LLM.md +++ b/LLM.md @@ -653,6 +653,42 @@ Auto (the Folder pill) is `@hanzo/build` `route(folders, ask)`: the folder whose folders it sits in, else the folder used last; same words, same folder, a tie goes to the more recent. The pill reads `Auto · ` as the ask is typed, and a folder the person chose is never moved by the words. +## Redesign a site: open it, critique it, rebuild it, watch it live (hanzo-inc/app#312) + +A person pastes a site's address in Dev's New on This computer (or picks **Redesign a site…**). The agent that +rebuilds it has no network, so the window does what the agent cannot +(`src-tauri/src/commands/local_site/`, each command its own `allow-` entry in `capabilities/main.json`; +`@hanzo/app` `lib/local-site.ts` `desktopSite()` is `@hanzo/build`'s `Host.site` over them, `site.ts` there is the +contract and the prompt the agent is given): + +- `local_site_folder(name)` names a new folder `/Hanzo/` (`-2`, `-3`… when taken) and makes + nothing: a site that will not open leaves no folder behind. A folder the person chose is built in instead. +- `local_site_capture(url, dir)` opens the site from this computer's connection (`fetch.rs`) and saves + `/.hanzo/reference/` (`page.rs`): `page.html`, `text.md` (address, description, words in reading order, + pictures), up to six `style-N.css`, and `screenshot.*` when a paired browser can take one. Each capture replaces + the last. **What it will open:** http and https only, no login in the address; any public address, and an + address on this computer only when the person typed `localhost` or a loopback address themselves. A private + network (10/8, 172.16/12, 192.168/16, link-local, carrier-grade NAT, IPv6 unique-local) is never opened, and a + redirect cannot reach anywhere the typed address could not: a name is resolved here, every address judged, and + the request pinned to the one that passed (so a name that answers a private address is refused too). + 15 seconds, 5 MB a page, 600 KB a stylesheet, six redirects, HTML only. The picture is best effort over ZAP + (`commands/browser.rs`: open the page in a tab, wait for `document.readyState`, `hanzo.screenshot`, close the + tab); no browser paired, or Windows (no ZAP bridge), means no picture and no failure. +- `local_preview_start(dir)` serves `/redesign` (`preview.rs`) on `127.0.0.1` on a free port: GET and HEAD, + files under the folder only (a climbing path, NUL, backslash or a link out is refused), only for a `Host` that + is its own address (a name a web page controls pointing at it is refused), and a server-sent event stream + (`/__hanzo/events`) that every HTML page's injected script listens on: a file added, changed or removed reloads + the page. Until `index.html` exists it answers "Hanzo is building your new design" and the same script swaps it + for the real one. The same folder is the same address while it runs. Previews stop when the main window is + destroyed, and on exit; `local_preview_stop(dir)` stops one. +- `local_preview_open(url)` shows a preview in Chrome (macOS `open -a "Google Chrome"`, Windows and Linux Chrome + or Chromium by path), else the default browser. It opens only a preview this app is serving. + +The agent is the ordinary `local_workspace_run_dev`: it reads `.hanzo/reference`, answers with a critique and writes +static files into `redesign/`; the preview follows each file it saves, and later runs in that folder (Auto picks it +next) show live in the same tab. Verify the Rust side with `cargo test --bin hanzo-desktop --no-default-features +local_site` (the `machine` feature's own tests need an import the tree does not have yet). + ## Machines (Docker Desktop replacement) Desktop UI for Linux VMs (vfkit) and the docker / k3s stack served by the