use std::path::PathBuf;
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
static BROWSER_COUNTER: AtomicU64 = AtomicU64::new(0);
use crate::cdp::transport::launch_chrome;
use crate::cdp::types::{EmulationSetUserAgentOverride, UserAgentBrandVersion, UserAgentMetadata};
use crate::cdp::{Connection, Transport};
use crate::error::{Error, Result};
use crate::page::Page;
use crate::stealth::fingerprint::Fingerprint;
use crate::stealth::{build_evasion_script_for, find_chrome, ChromePatcher};
use crate::StealthConfig;
fn stealth_args(config: &StealthConfig, fingerprint: &Fingerprint) -> Vec<String> {
let mut args = vec![
"--disable-blink-features=AutomationControlled".into(),
"--disable-features=IsolateOrigins,site-per-process,AutomationControlled,EnableAutomation"
.into(),
"--enable-features=NetworkService,NetworkServiceInProcess".into(),
"--disable-infobars".into(),
"--disable-dev-shm-usage".into(),
"--disable-ipc-flooding-protection".into(),
"--disable-renderer-backgrounding".into(),
"--disable-background-timer-throttling".into(),
"--disable-backgrounding-occluded-windows".into(),
"--no-first-run".into(),
"--no-default-browser-check".into(),
"--no-sandbox".into(),
"--disable-default-apps".into(),
"--disable-component-extensions-with-background-pages".into(),
"--disable-hang-monitor".into(),
"--disable-popup-blocking".into(),
"--disable-prompt-on-repost".into(),
"--disable-sync".into(),
"--disable-translate".into(),
"--metrics-recording-only".into(),
"--safebrowsing-disable-auto-update".into(),
"--disable-client-side-phishing-detection".into(),
"--password-store=basic".into(),
"--use-mock-keychain".into(),
"--lang=en-US".into(),
format!(
"--window-size={},{}",
config.viewport_width, config.viewport_height
),
format!(
"--ozone-override-screen-size={},{}",
config.viewport_width, config.viewport_height
),
];
args.push(format!("--user-agent={}", fingerprint.user_agent));
if config.headless {
args.push("--headless=new".into());
}
if let Some(ref proxy) = config.proxy {
args.push(format!("--proxy-server={}", proxy));
}
for arg in &config.extra_args {
args.push(arg.clone());
}
args
}
#[derive(Debug, Clone)]
pub struct TabInfo {
pub id: String,
pub title: String,
pub url: String,
}
fn ua_override_for(fp: &Fingerprint) -> EmulationSetUserAgentOverride {
let to_brands = |v: Vec<(String, String)>| -> Vec<UserAgentBrandVersion> {
v.into_iter()
.map(|(brand, version)| UserAgentBrandVersion { brand, version })
.collect()
};
let architecture = if fp.webgl_renderer.contains("Apple M") {
"arm"
} else {
"x86"
};
EmulationSetUserAgentOverride {
user_agent: fp.user_agent.clone(),
accept_language: Some("en-US,en;q=0.9".to_string()),
platform: Some(fp.nav_platform().to_string()),
user_agent_metadata: Some(UserAgentMetadata {
brands: to_brands(fp.ch_brands()),
full_version_list: to_brands(fp.ch_full_version_brands()),
platform: fp.ch_platform().to_string(),
platform_version: fp.platform_version.clone(),
architecture: architecture.to_string(),
model: String::new(),
mobile: false,
bitness: "64".to_string(),
wow64: false,
}),
}
}
pub struct Browser {
connection: Connection,
config: Arc<StealthConfig>,
user_data_dir: Option<PathBuf>,
fingerprint: Option<Fingerprint>,
evasion_script: String,
}
impl Browser {
pub async fn launch() -> Result<Self> {
Self::launch_with_config(StealthConfig::default()).await
}
pub async fn launch_visible() -> Result<Self> {
Self::launch_with_config(StealthConfig::visible()).await
}
pub async fn launch_debug() -> Result<Self> {
Self::launch_with_config(StealthConfig::debug()).await
}
pub async fn launch_with(configure: impl FnOnce(&mut StealthConfig)) -> Result<Self> {
let mut config = StealthConfig::default();
configure(&mut config);
Self::launch_with_config(config).await
}
pub async fn launch_with_config(config: StealthConfig) -> Result<Self> {
let config = Arc::new(config);
let (user_data_dir, owns_user_data_dir) = match config.user_data_dir.as_deref() {
Some(path) => {
let path = PathBuf::from(path);
std::fs::create_dir_all(&path)?;
(path, false)
}
None => {
let instance_id = BROWSER_COUNTER.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!(
"eoka-browser-{}-{}",
std::process::id(),
instance_id
));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path)?;
(path, true)
}
};
let launch = async {
let chrome_path = match &config.chrome_path {
Some(p) => PathBuf::from(p),
None => find_chrome()?,
};
let chrome_path = if config.patch_binary && owns_user_data_dir {
ChromePatcher::new(&chrome_path)?.get_patched_path()?
} else {
chrome_path
};
let detected_user_agent = if config.user_agent.is_none() {
installed_chrome_user_agent(&chrome_path)
} else {
None
};
let fingerprint = Fingerprint::resolve_for_profile(
config
.user_agent
.as_deref()
.or(detected_user_agent.as_deref()),
config.timezone.as_deref(),
(!owns_user_data_dir).then_some(user_data_dir.as_path()),
)?;
let mut args = stealth_args(&config, &fingerprint);
args.push(format!("--user-data-dir={}", user_data_dir.display()));
tracing::info!("Launching Chrome from {:?}", chrome_path);
let (child, ws_url) = launch_chrome(&chrome_path, &args)?;
let proxy_auth = match (&config.proxy_username, &config.proxy_password) {
(Some(u), Some(p)) => Some((u.clone(), p.clone())),
_ => None,
};
let transport =
Transport::new_with_options(child, &ws_url, proxy_auth, config.cdp_timeout).await?;
let connection = Connection::new(transport);
let version = connection.version().await?;
tracing::info!("Connected to Chrome: {}", version.product);
Ok::<(Connection, Fingerprint), Error>((connection, fingerprint))
}
.await;
let (connection, fingerprint) = match launch {
Ok(result) => result,
Err(e) => {
if owns_user_data_dir {
let _ = std::fs::remove_dir_all(&user_data_dir);
}
return Err(e);
}
};
let evasion_script = build_evasion_script_for(&config, &fingerprint);
Ok(Self {
connection,
config,
user_data_dir: owns_user_data_dir.then_some(user_data_dir),
fingerprint: Some(fingerprint),
evasion_script,
})
}
pub async fn connect(ws_url: &str) -> Result<Self> {
Self::connect_with_config(ws_url, StealthConfig::live()).await
}
pub async fn connect_with_config(ws_url: &str, config: StealthConfig) -> Result<Self> {
let config = Arc::new(config);
let transport = crate::cdp::transport::Transport::connect_with_options(
ws_url,
config.cdp_timeout,
config.filter_cdp,
)
.await?;
let connection = Connection::new(transport);
let version = connection.version().await?;
tracing::info!("Connected to Chrome: {}", version.product);
let (fingerprint, evasion_script) = if config.live_session {
(None, String::new())
} else {
let fp = Fingerprint::resolve(config.user_agent.as_deref(), config.timezone.as_deref());
let script = build_evasion_script_for(&config, &fp);
(Some(fp), script)
};
Ok(Self {
connection,
config,
user_data_dir: None,
fingerprint,
evasion_script,
})
}
pub async fn connect_port(port: u16) -> Result<Self> {
let ws_url = crate::cdp::discover::discover_browser_ws("127.0.0.1", port)?;
Self::connect(&ws_url).await
}
pub async fn connect_port_with_config(port: u16, config: StealthConfig) -> Result<Self> {
let ws_url = crate::cdp::discover::discover_browser_ws("127.0.0.1", port)?;
Self::connect_with_config(&ws_url, config).await
}
async fn setup_session(&self, session: &crate::cdp::Session) -> Result<()> {
session.page_enable().await?;
if self.config.proxy_username.is_some() && self.config.proxy_password.is_some() {
session.fetch_enable(true).await?;
}
if !self.config.live_session {
if let Some(ref fp) = self.fingerprint {
session.set_user_agent_full(ua_override_for(fp)).await?;
}
session
.add_script_to_evaluate_on_new_document(&self.evasion_script)
.await?;
}
Ok(())
}
pub async fn new_page(&self, url: &str) -> Result<Page> {
let target_id = self
.connection
.create_target("about:blank", None, None)
.await?;
let session = self.connection.attach_to_target(&target_id).await?;
self.setup_session(&session).await?;
let nav_result = session.navigate(url, None).await?;
if let Some(error) = nav_result.error_text {
return Err(Error::Navigation(error));
}
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
Ok(Page::new(session, Arc::clone(&self.config)))
}
pub async fn new_blank_page(&self) -> Result<Page> {
let target_id = self
.connection
.create_target("about:blank", None, None)
.await?;
let session = self.connection.attach_to_target(&target_id).await?;
self.setup_session(&session).await?;
Ok(Page::new(session, Arc::clone(&self.config)))
}
pub async fn version(&self) -> Result<String> {
let v = self.connection.version().await?;
Ok(v.product)
}
pub async fn tabs(&self) -> Result<Vec<TabInfo>> {
let targets = self.connection.get_targets().await?;
Ok(targets
.into_iter()
.filter(|t| t.r#type == "page")
.map(|t| TabInfo {
id: t.target_id,
title: t.title,
url: t.url,
})
.collect())
}
pub async fn attach_page(&self, target_id: &str) -> Result<Page> {
let session = self.connection.attach_to_target(target_id).await?;
self.setup_session(&session).await?;
Ok(Page::new(session, Arc::clone(&self.config)))
}
pub async fn activate_tab(&self, target_id: &str) -> Result<()> {
self.connection.activate_target(target_id).await
}
pub async fn close_tab(&self, target_id: &str) -> Result<()> {
self.connection.close_target(target_id).await?;
Ok(())
}
pub async fn close(self) -> Result<()> {
if self.config.live_session {
self.connection.transport().close().await?;
} else {
self.connection.close().await?;
}
if let Some(ref dir) = self.user_data_dir {
let _ = std::fs::remove_dir_all(dir);
}
Ok(())
}
pub async fn disconnect(self) -> Result<()> {
self.connection.transport().close().await
}
}
fn installed_chrome_user_agent(chrome_path: &std::path::Path) -> Option<String> {
let output = Command::new(chrome_path).arg("--version").output().ok()?;
let version = String::from_utf8_lossy(&output.stdout);
let full_version = version.split_whitespace().find(|part| {
let pieces: Vec<_> = part.split('.').collect();
pieces.len() == 4
&& pieces
.iter()
.all(|piece| !piece.is_empty() && piece.bytes().all(|byte| byte.is_ascii_digit()))
})?;
Fingerprint::native_chrome_user_agent(full_version)
}
impl Drop for Browser {
fn drop(&mut self) {
if let Some(ref dir) = self.user_data_dir {
let _ = std::fs::remove_dir_all(dir);
}
}
}