Files
javis_bot/app/tests/bot_browser.rs
javis-bot 24d0798bae feat(app): the bot's own Chrome in the data folder, reaching only public addresses
- install step browser: Chrome for Testing 157 (pinned SHA-256) into runtime/chrome
- services start it headless with its own profile when features.browser is on; CDP on a free
  loopback port passed to the worker as JARVIS_BROWSER_CDP; a failure leaves the services running
- every Chrome connection goes through the app's loopback egress proxy: one lookup, public
  addresses only (403 otherwise, no host logged), QUIC off, WebRTC only through the proxy
- stop: Browser.close, then kill after 5 s; dashboard shows the browser's state
- the worker's runtime environment follows the settings step, not every step being installed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:01:04 +09:00

88 lines
4.1 KiB
Rust

//! The bot browser with a real Chrome (admin.spec.md, "Bot browser"). Runs only with
//! `JARVIS_TEST_CHROME=<chrome executable>`; needs the internet for the public page. On Ubuntu also
//! set `JARVIS_CHROME_NO_SANDBOX=1`.
use std::sync::{Arc, Mutex};
use std::time::Duration;
use jarvis_app::browser::BotBrowser;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
async fn titles(client: &reqwest::Client, port: u16) -> Vec<(String, String)> {
let text = client.get(format!("http://127.0.0.1:{port}/json/list")).send().await.unwrap().text().await.unwrap();
let list: serde_json::Value = serde_json::from_str(&text).unwrap();
list.as_array().unwrap().iter()
.map(|t| (t["url"].as_str().unwrap_or("").to_owned(), t["title"].as_str().unwrap_or("").to_owned())).collect()
}
#[tokio::test]
async fn the_bot_browser_reaches_public_pages_never_this_pc_and_stops() {
let Ok(exe) = std::env::var("JARVIS_TEST_CHROME") else {
eprintln!("skipped: set JARVIS_TEST_CHROME");
return;
};
// A page on this PC the browser must never reach.
let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let local = l.local_addr().unwrap().port();
let hits = Arc::new(Mutex::new(0));
let h2 = hits.clone();
tokio::spawn(async move {
while let Ok((mut c, _)) = l.accept().await {
*h2.lock().unwrap() += 1;
let mut b = [0u8; 1024];
let _ = c.read(&mut b).await;
let _ = c.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 30\r\n\r\n<title>secret local</title>xx").await;
}
});
let profile = std::env::temp_dir().join(format!("jarvis-bot-browser-{}", std::process::id()));
let b = BotBrowser::start(std::path::Path::new(&exe), &profile).await.expect("chrome starts");
assert!(b.running());
let client = reqwest::Client::builder().no_proxy().build().unwrap();
for url in [format!("http://127.0.0.1:{local}/"), format!("http://localhost:{local}/"), "https://example.com/".into()] {
client.put(format!("http://127.0.0.1:{}/json/new?{url}", b.cdp_port)).send().await.unwrap();
}
let mut got = vec![];
for _ in 0..40 {
got = titles(&client, b.cdp_port).await;
if got.iter().any(|(_, t)| t == "Example Domain") {
break;
}
tokio::time::sleep(Duration::from_millis(250)).await;
}
eprintln!("tabs: {got:?}");
assert!(got.iter().any(|(_, t)| t == "Example Domain"), "the public page loads through the proxy");
assert!(!got.iter().any(|(_, t)| t.contains("secret local")));
assert_eq!(*hits.lock().unwrap(), 0, "nothing reached this PC");
let port = b.cdp_port;
let t0 = std::time::Instant::now();
b.stop().await;
eprintln!("stopped in {:?}", t0.elapsed());
assert!(!b.running());
assert!(client.get(format!("http://127.0.0.1:{port}/json/version")).send().await.is_err(), "CDP is closed");
let _ = std::fs::remove_dir_all(&profile);
}
/// The install step downloads the pinned Chrome (about 200 MB) and unpacks a runnable browser.
/// Runs only with `JARVIS_TEST_CHROME_INSTALL=1`.
#[tokio::test]
async fn the_browser_step_installs_a_chrome_that_starts() {
if std::env::var("JARVIS_TEST_CHROME_INSTALL").as_deref() != Ok("1") {
eprintln!("skipped: set JARVIS_TEST_CHROME_INSTALL=1");
return;
}
let data = std::env::temp_dir().join(format!("jarvis-chrome-install-{}", std::process::id()));
let layout = jarvis_app::install::Layout { data: data.clone(), src: std::path::PathBuf::new() };
let steps = jarvis_app::install::steps().into_iter().filter(|s| s.id() == "browser").collect();
let installer = jarvis_app::install::Installer::new(layout.clone(), steps);
assert!(installer.run_all().await, "{}", installer.status());
assert!(installer.done("browser"));
assert!(!data.join("cache").join("downloads").read_dir().map(|mut d| d.next().is_some()).unwrap_or(false),
"the archive is removed after unpacking");
let b = BotBrowser::start(&layout.chrome_exe(), &jarvis_app::browser::profile_dir(&data)).await.expect("starts");
assert!(b.running());
b.stop().await;
let _ = std::fs::remove_dir_all(&data);
}