pub mod cdp;
pub mod error;
pub mod identity;
pub mod local_proxy_server;
pub mod preset;
pub mod script;
pub mod tz;
pub mod ua;
use error::IdentityError;
use local_proxy_server::start_overlay;
use rustenium::browsers::{
BidiBrowser,
chrome::browser::{ChromeBrowser, ChromeConfig},
};
pub use error::IdentityError as Error;
pub use identity::*;
pub struct IdentityConfig {
pub identity: Identity,
pub chrome: ChromeConfig,
}
impl From<Identity> for IdentityConfig {
fn from(identity: Identity) -> Self {
Self {
identity,
chrome: ChromeConfig {
enable_bidi: false,
enable_cdp: true,
..Default::default()
},
}
}
}
impl IdentityConfig {
pub fn new(identity: Identity, chrome: ChromeConfig) -> Self {
Self { identity, chrome }
}
}
pub struct IdentitySession {
identity: Identity,
browser: ChromeBrowser,
}
impl IdentitySession {
pub async fn launch(config: impl Into<IdentityConfig>) -> Result<Self, IdentityError> {
let config = config.into();
let timezone = tz::resolve_timezone(
config.identity.timezone.as_deref(),
config.identity.proxy.as_deref(),
)
.await?;
let mut chrome_config = config.chrome;
chrome_config.enable_bidi = false;
chrome_config.enable_cdp = true;
if let Some(ref proxy_url) = config.identity.proxy {
if !proxy_url.is_empty() {
let local_addr = start_overlay(proxy_url)
.await
.map_err(|e| IdentityError::ProxyError(e.to_string()))?;
let mut flags = chrome_config.browser_flags.unwrap_or_default();
flags.push(format!(
"--proxy-server=http://127.0.0.1:{}",
local_addr.port()
));
chrome_config.browser_flags = Some(flags);
}
}
let mut browser = ChromeBrowser::new(chrome_config).await;
cdp::apply_identity(&mut browser, &config.identity, &timezone).await?;
Ok(Self {
identity: config.identity,
browser,
})
}
pub fn browser(&self) -> &ChromeBrowser {
&self.browser
}
pub fn browser_mut(&mut self) -> &mut ChromeBrowser {
&mut self.browser
}
pub fn identity(&self) -> &Identity {
&self.identity
}
pub async fn close(self) -> bool {
self.browser.close().await.map_err(|_| false).is_ok()
}
}