Files
javis_bot/app/src/browser.rs
javis-bot cbe898d3d0 feat(app): log screen saves (shown lines, conversation, voice recordings), stop-or-finish on a new request, video pages that play
- 로그 screen: "보이는 글 txt로 저장" (logs_save_text), a 대화 view that saves the days'
  conversation with every trace in full (history_export), and a 목소리 view with the
  voice.record switch and the days' recordings as a zip with an index (voice_export).
- Voice worker: with JARVIS_RECORDINGS_DIR (voice.record, off by default) each final and each
  spoken answer is kept as a WAV with an index line; day folders older than 7 days removed.
- response.interrupt (default on): off, latest/important finish the running answer first.
- Bot browser: Chrome lets pages start video and sound; botBrowser reports each page's media
  state, waits for a player to start after open/click, and presses play on a video page a click
  led to when its player stayed paused (live: a clicked YouTube video stayed paused).
- webSearch reads `query` when `search_query` is missing (live: both searches of an answer failed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:27:57 +09:00

647 lines
30 KiB
Rust

//! The bot browser (`app/src/admin.spec.md`, "Bot browser"): the bot's own Chrome in the data
//! folder, driven over CDP, and the egress proxy every Chrome connection goes through so it can
//! only reach public addresses.
use std::net::{IpAddr, SocketAddr};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
const RESOLVE_TIMEOUT: Duration = Duration::from_secs(10);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const HEAD_TIMEOUT: Duration = Duration::from_secs(30);
const MAX_HEAD: usize = 64 * 1024;
const READY_TIMEOUT: Duration = Duration::from_secs(30);
const CLOSE_WAIT: Duration = Duration::from_secs(5);
/// How often the windows the bot opens on the virtual monitor are looked for.
const WATCH_EVERY: Duration = Duration::from_millis(200);
/// Whether an address is on the public internet: not this PC, the local network or a special range.
pub fn is_public(ip: &IpAddr) -> bool {
match ip {
IpAddr::V4(v4) => {
let o = v4.octets();
!(v4.is_private() || v4.is_loopback() || v4.is_link_local() || v4.is_broadcast()
|| v4.is_documentation() || v4.is_unspecified() || v4.is_multicast()
|| o[0] == 0 // "this network"
|| (o[0] == 100 && (64..128).contains(&o[1])) // CGNAT 100.64/10
|| (o[0] == 192 && o[1] == 0 && o[2] == 0) // IETF 192.0.0/24
|| (o[0] == 198 && (18..20).contains(&o[1])) // benchmarking 198.18/15
|| o[0] >= 240) // reserved
}
IpAddr::V6(v6) => {
if let Some(v4) = v6.to_ipv4_mapped() {
return is_public(&IpAddr::V4(v4));
}
// Only global unicast 2000::/3 is routed on the internet (this also leaves out site
// local fec0::/10, unique and link local, IPv4-compatible ::a.b.c.d and NAT64).
let s = v6.segments();
(s[0] & 0xe000) == 0x2000
&& !(s[0] == 0x2001 && s[1] < 0x0200) // IETF 2001::/23 (Teredo, benchmarking, ...)
&& !(s[0] == 0x2001 && s[1] == 0x0db8) // documentation
&& s[0] != 0x2002 // 6to4 (embeds any IPv4)
}
}
}
// ---- egress proxy ---------------------------------------------------------------------------------
/// A loopback HTTP proxy that connects only to addresses `allow` accepts (`is_public` for Chrome).
pub struct EgressProxy {
pub port: u16,
shutdown: tokio::sync::watch::Sender<bool>,
task: Mutex<Option<tokio::task::JoinHandle<()>>>,
}
impl EgressProxy {
pub async fn start(allow: fn(&IpAddr) -> bool) -> std::io::Result<Self> {
let listener = TcpListener::bind("127.0.0.1:0").await?;
let port = listener.local_addr()?.port();
let (shutdown, rx) = tokio::sync::watch::channel(false);
let mut stopping = rx.clone();
let task = tokio::spawn(async move {
// Every connection is a task of this set, so a stop ends the open tunnels too.
let mut conns = tokio::task::JoinSet::new();
loop {
tokio::select! {
r = listener.accept() => if let Ok((client, _)) = r {
let mut stop = rx.clone();
conns.spawn(async move {
tokio::select! {
_ = serve(client, allow) => {}
_ = stop.wait_for(|s| *s) => {}
}
});
},
_ = stopping.wait_for(|s| *s) => break,
Some(_) = conns.join_next(), if !conns.is_empty() => {}
}
}
drop(listener);
conns.shutdown().await;
});
Ok(Self { port, shutdown, task: Mutex::new(Some(task)) })
}
/// Stop taking connections, close every open one and wait until they are gone.
pub async fn stop(&self) {
let _ = self.shutdown.send(true);
let task = self.task.lock().unwrap().take();
if let Some(t) = task {
let _ = t.await;
}
}
}
impl Drop for EgressProxy {
fn drop(&mut self) {
let _ = self.shutdown.send(true);
}
}
/// The request head (up to the blank line) and whatever came after it.
async fn read_head(s: &mut TcpStream) -> std::io::Result<Option<(String, Vec<u8>)>> {
let mut buf = Vec::new();
let mut chunk = [0u8; 8192];
loop {
if let Some(end) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
let rest = buf.split_off(end + 4);
return Ok(String::from_utf8(buf).ok().map(|h| (h, rest)));
}
if buf.len() > MAX_HEAD {
return Ok(None);
}
let n = s.read(&mut chunk).await?;
if n == 0 {
return Ok(None);
}
buf.extend_from_slice(&chunk[..n]);
}
}
/// `host:port` with an optional `[..]` around an IPv6 address.
fn split_host_port(s: &str) -> Option<(String, u16)> {
let (host, port) = s.rsplit_once(':')?;
let host = host.strip_prefix('[').and_then(|h| h.strip_suffix(']')).unwrap_or(host);
if host.is_empty() {
return None;
}
Some((host.to_owned(), port.parse().ok()?))
}
/// Resolve once, require every address to be allowed, and connect only to those.
async fn connect_allowed(host: &str, port: u16, allow: fn(&IpAddr) -> bool) -> Result<TcpStream, &'static str> {
let addrs: Vec<SocketAddr> = match host.parse::<IpAddr>() {
Ok(ip) => vec![SocketAddr::new(ip, port)],
Err(_) => match tokio::time::timeout(RESOLVE_TIMEOUT, tokio::net::lookup_host((host, port))).await {
Ok(Ok(a)) => a.collect(),
_ => return Err("name not resolved"),
},
};
if addrs.is_empty() || !addrs.iter().all(|a| allow(&a.ip())) {
return Err("not a public address");
}
for a in &addrs {
if let Ok(Ok(s)) = tokio::time::timeout(CONNECT_TIMEOUT, TcpStream::connect(a)).await {
return Ok(s);
}
}
Err("could not connect")
}
async fn reply(client: &mut TcpStream, status: &str) -> std::io::Result<()> {
client.write_all(format!("HTTP/1.1 {status}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").as_bytes()).await
}
async fn serve(mut client: TcpStream, allow: fn(&IpAddr) -> bool) -> std::io::Result<()> {
let Ok(Ok(Some((head, rest)))) = tokio::time::timeout(HEAD_TIMEOUT, read_head(&mut client)).await else {
return reply(&mut client, "400 Bad Request").await;
};
let mut lines = head.split("\r\n");
let mut first = lines.next().unwrap_or("").split(' ');
let (method, target, version) = (first.next().unwrap_or(""), first.next().unwrap_or(""), first.next().unwrap_or(""));
if method.eq_ignore_ascii_case("CONNECT") {
let Some((host, port)) = split_host_port(target) else { return reply(&mut client, "400 Bad Request").await };
let mut up = match connect_allowed(&host, port, allow).await {
Ok(s) => s,
Err(why) => return refused(&mut client, why).await,
};
client.write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n").await?;
up.write_all(&rest).await?;
tokio::io::copy_bidirectional(&mut client, &mut up).await?;
return Ok(());
}
let Ok(url) = reqwest::Url::parse(target) else { return reply(&mut client, "400 Bad Request").await };
let (Some(host), Some(port)) = (url.host_str(), url.port_or_known_default()) else {
return reply(&mut client, "400 Bad Request").await;
};
if url.scheme() != "http" || !version.starts_with("HTTP/") {
return reply(&mut client, "400 Bad Request").await;
}
let host = host.trim_start_matches('[').trim_end_matches(']').to_owned();
let mut up = match connect_allowed(&host, port, allow).await {
Ok(s) => s,
Err(why) => return refused(&mut client, why).await,
};
// Origin form, no proxy headers, one request per connection (the next may be for another host).
let path = match url.query() {
Some(q) => format!("{}?{q}", url.path()),
None => url.path().to_owned(),
};
let mut out = format!("{method} {path} {version}\r\n");
for l in lines.filter(|l| !l.is_empty()) {
let name = l.split(':').next().unwrap_or("").trim().to_ascii_lowercase();
if !matches!(name.as_str(), "proxy-connection" | "proxy-authorization" | "connection" | "keep-alive") {
out.push_str(l);
out.push_str("\r\n");
}
}
out.push_str("Connection: close\r\n\r\n");
up.write_all(out.as_bytes()).await?;
up.write_all(&rest).await?;
tokio::io::copy_bidirectional(&mut client, &mut up).await?;
Ok(())
}
async fn refused(client: &mut TcpStream, why: &str) -> std::io::Result<()> {
// Never the host or the address: they are what the bot was asked to open.
crate::debug_log!("browser", "proxy refused a connection: {why}");
reply(client, "403 Forbidden").await
}
// ---- Chrome ---------------------------------------------------------------------------------------
/// The arguments the bot's Chrome runs with (its profile, CDP on a free port, every connection
/// through the proxy on `proxy_port`): headless, or with `screen` a real window the app opens later
/// over CDP (admin.spec.md, "Virtual monitor").
pub fn chrome_args(profile: &Path, proxy_port: u16, no_sandbox: bool, screen: bool) -> Vec<String> {
let mut a = vec![
format!("--user-data-dir={}", profile.display()),
"--remote-debugging-port=0".into(),
format!("--proxy-server=http://127.0.0.1:{proxy_port}"),
"--proxy-bypass-list=<-loopback>".into(),
"--disable-quic".into(),
"--force-webrtc-ip-handling-policy=disable_non_proxied_udp".into(),
"--no-first-run".into(),
"--no-default-browser-check".into(),
"--disable-sync".into(),
"--disable-background-networking".into(),
"--disable-component-update".into(),
"--disable-default-apps".into(),
"--disable-features=Translate,MediaRouter,OptimizationHints".into(),
"--window-size=1280,800".into(),
// Video and sound start when a page starts them: the tool clicks with page scripts, which
// Chrome does not count as a user's gesture, so a video page opened for "play X" stayed paused.
"--autoplay-policy=no-user-gesture-required".into(),
];
if no_sandbox {
a.push("--no-sandbox".into());
}
if cfg!(windows) {
// Started by an app running as administrator, Chrome would start again as the user and end
// the process the app holds, which could then neither see nor stop it.
a.push("--do-not-de-elevate".into());
}
if screen {
// A window opened at start would take the focus from the game the user is playing.
a.extend(["--no-startup-window", "--disable-backgrounding-occluded-windows", "--disable-renderer-backgrounding",
"--disable-background-timer-throttling"].map(String::from));
} else {
a.insert(0, "--headless=new".into());
a.push("about:blank".into());
}
a
}
pub struct BotBrowser {
child: Mutex<Option<tokio::process::Child>>,
proxy: EgressProxy,
pub cdp_port: u16,
/// While set (with a screen, on Windows), a thread settles the windows the bot opens.
watching: Arc<AtomicBool>,
}
impl Drop for BotBrowser {
fn drop(&mut self) {
self.watching.store(false, Ordering::Relaxed);
}
}
impl BotBrowser {
/// Start Chrome `exe` with profile folder `profile` and wait until CDP answers; with `screen`,
/// its one window sits there (the virtual monitor), opened without taking the focus and kept out
/// of Alt+Tab and the taskbar.
pub async fn start(exe: &Path, profile: &Path, screen: Option<crate::vdisplay::Rect>) -> Result<Self, String> {
let proxy = EgressProxy::start(is_public).await.map_err(|e| format!("proxy: {e}"))?;
std::fs::create_dir_all(profile).map_err(|e| e.to_string())?;
turn_off_translation(profile);
let port_file = profile.join("DevToolsActivePort");
let _ = std::fs::remove_file(&port_file);
let no_sandbox = cfg!(not(windows)) && std::env::var("JARVIS_CHROME_NO_SANDBOX").is_ok_and(|v| v == "1");
let mut cmd = tokio::process::Command::new(exe);
cmd.args(chrome_args(profile, proxy.port, no_sandbox, screen.is_some())).stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).kill_on_drop(true);
#[cfg(unix)]
cmd.process_group(0);
#[cfg(windows)]
cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW
let mut child = cmd.spawn().map_err(|e| format!("Chrome을 실행하지 못했습니다: {e}"))?;
let until = tokio::time::Instant::now() + READY_TIMEOUT;
let cdp_port = loop {
if let Some(p) = std::fs::read_to_string(&port_file).ok()
.and_then(|t| t.lines().next().and_then(|l| l.trim().parse::<u16>().ok())) {
if version(p).await.is_some() {
break p;
}
}
if let Ok(Some(status)) = child.try_wait() {
return Err(format!("Chrome이 바로 끝났습니다 ({status})"));
}
if tokio::time::Instant::now() > until {
kill_tree(&mut child).await;
return Err("Chrome이 30초 안에 준비되지 않았습니다".into());
}
tokio::time::sleep(Duration::from_millis(200)).await;
};
let b = Self { child: Mutex::new(Some(child)), proxy, cdp_port, watching: Arc::new(AtomicBool::new(false)) };
if let Some(r) = screen {
if let Err(e) = b.open_window(r).await {
b.stop().await;
return Err(e);
}
if cfg!(windows) {
// The bot opens a window per person (bot_browser.spec.md), each settled like the first.
b.watching.store(true, Ordering::Relaxed);
let (on, pid) = (b.watching.clone(), b.pid());
std::thread::spawn(move || {
while on.load(Ordering::Relaxed) {
if crate::vdisplay::settle_windows(pid) > 0 {
crate::debug_log!("browser", "a new bot browser window settled on the virtual monitor");
}
std::thread::sleep(WATCH_EVERY);
}
});
}
}
crate::info_log!("browser", "bot browser ready (CDP on 127.0.0.1:{cdp_port}{})",
if screen.is_some() { ", on the virtual monitor" } else { "" });
Ok(b)
}
/// The window on `r`: a background page in a new window (CDP), then out of the window switcher
/// and fitted to the monitor's work area.
/// It holds the first page, which the bot never closes, so the window stays; the bot's own
/// windows open where it is.
async fn open_window(&self, r: crate::vdisplay::Rect) -> Result<(), String> {
let ws = version(self.cdp_port).await.and_then(|v| v["webSocketDebuggerUrl"].as_str().map(str::to_owned))
.ok_or("Chrome의 CDP 주소를 읽지 못했습니다")?;
cdp_call(&ws, serde_json::json!({"id": 1, "method": "Target.createTarget", "params": {"url": "about:blank",
"newWindow": true, "background": true, "left": r.x, "top": r.y, "width": r.w, "height": r.h}})).await
.map_err(|e| format!("Chrome 창을 열지 못했습니다: {e}"))?;
let pid = self.pid();
for _ in 0..25 {
if cfg!(not(windows)) || crate::vdisplay::settle_windows(pid) > 0 {
return Ok(());
}
tokio::time::sleep(Duration::from_millis(200)).await;
}
Err("Chrome 창을 찾지 못했습니다".into())
}
/// Chrome's main process (0 once it ended), the one owning its windows.
pub fn pid(&self) -> u32 {
self.child.lock().unwrap().as_ref().and_then(|c| c.id()).unwrap_or(0)
}
pub fn cdp_url(&self) -> String {
format!("http://127.0.0.1:{}", self.cdp_port)
}
/// Whether Chrome still runs.
pub fn running(&self) -> bool {
self.child.lock().unwrap().as_mut().is_some_and(|c| matches!(c.try_wait(), Ok(None)))
}
/// `Browser.close`, then up to 5 s for Chrome to end, else it is killed; the proxy ends too.
pub async fn stop(&self) {
self.watching.store(false, Ordering::Relaxed);
let child = self.child.lock().unwrap().take();
if let Some(mut child) = child {
if let Some(ws) = version(self.cdp_port).await.and_then(|v| v["webSocketDebuggerUrl"].as_str().map(str::to_owned)) {
let _ = close_over_cdp(&ws).await;
}
if !matches!(tokio::time::timeout(CLOSE_WAIT, child.wait()).await, Ok(Ok(_))) {
crate::warn_log!("browser", "Chrome did not close in {} s; killing it", CLOSE_WAIT.as_secs());
kill_tree(&mut child).await;
}
crate::info_log!("browser", "bot browser stopped");
}
self.proxy.stop().await;
}
}
/// CDP's `/json/version`, if it answers.
async fn version(port: u16) -> Option<serde_json::Value> {
let client = reqwest::Client::builder().no_proxy().timeout(Duration::from_secs(2)).build().ok()?;
let text = client.get(format!("http://127.0.0.1:{port}/json/version")).send().await.ok()?.text().await.ok()?;
serde_json::from_str(&text).ok()
}
async fn close_over_cdp(ws: &str) -> Result<(), String> {
use futures_util::SinkExt;
let (mut s, _) = tokio::time::timeout(Duration::from_secs(2), tokio_tungstenite::connect_async(ws)).await
.map_err(|_| "timeout".to_string())?.map_err(|e| e.to_string())?;
s.send(tokio_tungstenite::tungstenite::Message::text(r#"{"id":1,"method":"Browser.close"}"#)).await
.map_err(|e| e.to_string())
}
/// One CDP call on the browser connection `ws`; its result, or the error Chrome gave (5 s at most).
async fn cdp_call(ws: &str, msg: serde_json::Value) -> Result<serde_json::Value, String> {
use futures_util::{SinkExt, StreamExt};
let id = msg["id"].clone();
tokio::time::timeout(Duration::from_secs(5), async {
let (mut s, _) = tokio_tungstenite::connect_async(ws).await.map_err(|e| e.to_string())?;
s.send(tokio_tungstenite::tungstenite::Message::text(msg.to_string())).await.map_err(|e| e.to_string())?;
while let Some(m) = s.next().await {
let m = m.map_err(|e| e.to_string())?;
let Ok(v) = serde_json::from_str::<serde_json::Value>(m.to_text().unwrap_or("")) else { continue };
if v["id"] == id {
return match v.get("error") {
Some(e) => Err(e["message"].as_str().unwrap_or("error").to_owned()),
None => Ok(v["result"].clone()),
};
}
}
Err("connection closed".to_owned())
}).await.map_err(|_| "timeout".to_owned())?
}
async fn kill_tree(child: &mut tokio::process::Child) {
if let Some(pid) = child.id() {
#[cfg(unix)]
// SAFETY: a plain signal to Chrome's own process group (made with process_group(0)).
unsafe {
libc::kill(-(pid as i32), libc::SIGKILL);
}
#[cfg(windows)]
{
let mut tk = tokio::process::Command::new("taskkill");
tk.args(["/T", "/F", "/PID", &pid.to_string()]).stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null()).stderr(std::process::Stdio::null()).creation_flags(0x0800_0000);
let _ = tk.status().await;
}
}
let _ = child.kill().await;
}
/// Chrome no longer reads `--disable-features=Translate`: without this preference it offers to
/// translate every page in another language, a bubble that covers the page on the shared screen.
/// Written before Chrome starts; the profile's other preferences stay.
fn turn_off_translation(profile: &Path) {
let file = profile.join("Default").join("Preferences");
let mut prefs = std::fs::read_to_string(&file).ok()
.and_then(|t| serde_json::from_str::<serde_json::Value>(&t).ok())
.filter(serde_json::Value::is_object).unwrap_or_else(|| serde_json::json!({}));
if !prefs["translate"].is_object() {
prefs["translate"] = serde_json::json!({});
}
prefs["translate"]["enabled"] = false.into();
let written = std::fs::create_dir_all(file.parent().unwrap_or(profile))
.and_then(|_| std::fs::write(&file, prefs.to_string()));
if let Err(e) = written {
crate::warn_log!("browser", "could not turn off translation in the profile: {e}");
}
}
/// Where the bot browser's profile lives in the data folder.
pub fn profile_dir(data: &Path) -> PathBuf {
data.join("browser").join("profile")
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
#[test]
fn only_public_addresses_are_public() {
let public = ["8.8.8.8", "1.1.1.1", "142.250.1.1", "2606:4700:4700::1111", "2a00:1450:4001::1",
"2001:4860:4860::8888", "2400:cb00::1"];
let not = ["127.0.0.1", "10.0.0.1", "172.16.5.4", "192.168.10.9", "169.254.169.254", "100.64.0.1",
"0.0.0.0", "0.1.2.3", "192.0.0.8", "192.0.2.1", "198.18.0.1", "203.0.113.5", "224.0.0.1", "240.0.0.1",
"255.255.255.255", "::1", "::", "fc00::1", "fd12::1", "fe80::1", "ff02::1", "2001:db8::1",
"::ffff:127.0.0.1", "::ffff:192.168.0.1", "64:ff9b::a00:1", "2002:c0a8:1::1", "2001:0:4136:e378::1",
"fec0::1", "fef0::1", "::1.2.3.4", "100::1", "2001:2::1", "2001:10::1", "4000::1", "e000::1"];
for a in public {
assert!(is_public(&a.parse().unwrap()), "{a} should be public");
}
for a in not {
assert!(!is_public(&a.parse().unwrap()), "{a} should not be public");
}
}
/// A local server recording each connection's first bytes and answering `answer`.
async fn server(answer: &'static [u8]) -> (u16, Arc<Mutex<Vec<String>>>) {
let l = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = l.local_addr().unwrap().port();
let seen = Arc::new(Mutex::new(Vec::new()));
let s2 = seen.clone();
tokio::spawn(async move {
while let Ok((mut c, _)) = l.accept().await {
let seen = s2.clone();
tokio::spawn(async move {
let mut buf = vec![0u8; 4096];
let n = c.read(&mut buf).await.unwrap_or(0);
seen.lock().unwrap().push(String::from_utf8_lossy(&buf[..n]).into_owned());
let _ = c.write_all(answer).await;
});
}
});
(port, seen)
}
async fn ask(proxy: u16, req: String) -> String {
let mut c = TcpStream::connect(("127.0.0.1", proxy)).await.unwrap();
c.write_all(req.as_bytes()).await.unwrap();
let mut out = Vec::new();
let _ = tokio::time::timeout(Duration::from_secs(5), c.read_to_end(&mut out)).await;
String::from_utf8_lossy(&out).into_owned()
}
fn loopback(ip: &IpAddr) -> bool {
ip.is_loopback()
}
#[tokio::test]
async fn the_proxy_refuses_this_pc_and_the_local_network() {
let (port, seen) = server(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi").await;
let p = EgressProxy::start(is_public).await.unwrap();
for req in [format!("CONNECT 127.0.0.1:{port} HTTP/1.1\r\nHost: x\r\n\r\n"),
format!("CONNECT localhost:{port} HTTP/1.1\r\n\r\n"),
format!("CONNECT [::1]:{port} HTTP/1.1\r\n\r\n"),
format!("GET http://127.0.0.1:{port}/a HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"),
format!("GET http://localhost:{port}/a HTTP/1.1\r\n\r\n"),
"CONNECT 192.168.10.1:443 HTTP/1.1\r\n\r\n".to_string(),
"GET http://10.0.0.1/ HTTP/1.1\r\n\r\n".to_string()] {
let r = ask(p.port, req.clone()).await;
assert!(r.starts_with("HTTP/1.1 403"), "{req:?} -> {r:?}");
}
assert!(seen.lock().unwrap().is_empty());
for bad in ["nonsense\r\n\r\n", "GET /relative HTTP/1.1\r\n\r\n", "GET ftp://a.example/ HTTP/1.1\r\n\r\n",
"CONNECT nohost HTTP/1.1\r\n\r\n"] {
assert!(ask(p.port, bad.into()).await.starts_with("HTTP/1.1 400"), "{bad:?}");
}
}
#[tokio::test]
async fn the_proxy_tunnels_and_forwards_to_allowed_addresses() {
let (port, seen) = server(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi").await;
let p = EgressProxy::start(loopback).await.unwrap();
let r = ask(p.port, format!("CONNECT 127.0.0.1:{port} HTTP/1.1\r\n\r\nping")).await;
assert!(r.starts_with("HTTP/1.1 200 Connection Established\r\n\r\nHTTP/1.1 200 OK"), "{r:?}");
let r = ask(p.port, format!("GET http://127.0.0.1:{port}/a/b?c=1 HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\n\
Proxy-Connection: keep-alive\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")).await;
assert!(r.ends_with("hi"), "{r:?}");
let seen = seen.lock().unwrap().clone();
assert_eq!(seen[0], "ping");
assert_eq!(seen[1], format!("GET /a/b?c=1 HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nAccept: */*\r\nConnection: close\r\n\r\n"));
}
#[test]
fn the_profile_never_offers_to_translate_and_keeps_its_other_preferences() {
let dir = std::env::temp_dir().join(format!("jarvis-translate-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let prefs = |d: &Path| -> serde_json::Value {
serde_json::from_str(&std::fs::read_to_string(d.join("Default").join("Preferences")).unwrap()).unwrap()
};
turn_off_translation(&dir);
assert_eq!(prefs(&dir)["translate"]["enabled"], false, "a new profile");
std::fs::write(dir.join("Default").join("Preferences"),
r#"{"homepage":"x","translate":{"enabled":true,"blocked_languages":["ko"]}}"#).unwrap();
turn_off_translation(&dir);
let p = prefs(&dir);
assert_eq!(p["translate"]["enabled"], false, "a profile Chrome already used");
assert_eq!(p["homepage"], "x");
assert_eq!(p["translate"]["blocked_languages"][0], "ko");
std::fs::write(dir.join("Default").join("Preferences"), "not json").unwrap();
turn_off_translation(&dir);
assert_eq!(prefs(&dir)["translate"]["enabled"], false, "a damaged file");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn chrome_goes_through_the_proxy_with_its_own_profile() {
let a = chrome_args(Path::new("/d/browser/profile"), 4321, false, false);
for want in ["--headless=new", "--user-data-dir=/d/browser/profile", "--remote-debugging-port=0",
"--proxy-server=http://127.0.0.1:4321", "--proxy-bypass-list=<-loopback>", "--disable-quic",
"--autoplay-policy=no-user-gesture-required"] {
assert!(a.iter().any(|x| x == want), "{want}");
}
assert!(!a.iter().any(|x| x == "--no-sandbox"));
assert!(chrome_args(Path::new("/p"), 1, true, false).iter().any(|x| x == "--no-sandbox"));
assert_eq!(a.iter().any(|x| x == "--do-not-de-elevate"), cfg!(windows), "the app keeps hold of Chrome on Windows");
}
#[test]
fn on_a_screen_chrome_has_a_window_it_opens_later_and_keeps_drawing_when_covered() {
let a = chrome_args(Path::new("/p"), 4321, false, true);
assert!(!a.iter().any(|x| x.starts_with("--headless")));
// No window at start: the app opens it over CDP in the background, so it never takes focus.
assert!(a.iter().any(|x| x == "--no-startup-window"));
assert!(!a.iter().any(|x| x == "about:blank"));
for want in ["--disable-backgrounding-occluded-windows", "--disable-renderer-backgrounding",
"--proxy-server=http://127.0.0.1:4321", "--disable-quic", "--autoplay-policy=no-user-gesture-required"] {
assert!(a.iter().any(|x| x == want), "{want}");
}
}
}
#[cfg(test)]
mod stop_tests {
use super::*;
#[tokio::test]
async fn stopping_the_proxy_closes_the_tunnels_it_holds() {
// An echo server the tunnel reaches.
let l = TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = l.local_addr().unwrap().port();
let (closed_tx, closed_rx) = tokio::sync::oneshot::channel();
tokio::spawn(async move {
let (mut c, _) = l.accept().await.unwrap();
let mut b = [0u8; 64];
loop {
match c.read(&mut b).await {
Ok(0) | Err(_) => break,
Ok(n) => { let _ = c.write_all(&b[..n]).await; }
}
}
let _ = closed_tx.send(());
});
let p = EgressProxy::start(|ip| ip.is_loopback()).await.unwrap();
let mut c = TcpStream::connect(("127.0.0.1", p.port)).await.unwrap();
c.write_all(format!("CONNECT 127.0.0.1:{port} HTTP/1.1\r\n\r\n").as_bytes()).await.unwrap();
let mut b = vec![0u8; 39];
c.read_exact(&mut b).await.unwrap();
assert!(b.starts_with(b"HTTP/1.1 200 Connection Established"));
c.write_all(b"ping").await.unwrap();
let mut e = [0u8; 4];
c.read_exact(&mut e).await.unwrap();
assert_eq!(&e, b"ping");
tokio::time::timeout(Duration::from_secs(5), p.stop()).await.expect("stop waits only for the cleanup");
// The open tunnel is gone on both sides, and no new connection is taken.
let _ = c.write_all(b"after").await;
let mut rest = Vec::new();
let n = tokio::time::timeout(Duration::from_secs(2), c.read_to_end(&mut rest)).await.expect("closed").unwrap_or(0);
assert_eq!(n, 0, "nothing passes after the stop");
tokio::time::timeout(Duration::from_secs(2), closed_rx).await.expect("the upstream side closed").unwrap();
assert!(TcpStream::connect(("127.0.0.1", p.port)).await.is_err());
}
}