pub mod identity;
pub mod ua;
pub mod tz;
pub mod error;
pub mod cdp;
pub mod script;
pub mod preset;
use error::IdentityError;
use rustenium::browsers::chrome::browser::{ChromeBrowser, ChromeConfig};
use rustenium::browsers::cdp_browser::CdpBrowser;
pub use identity::*;
pub use error::IdentityError as Error;
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) = config.identity.proxy {
if !proxy.is_empty() {
let mut flags = chrome_config.browser_flags.unwrap_or_default();
flags.push(format!("--proxy-server={}", proxy));
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
}
}