use std::collections::HashMap;
use std::future::Future;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, Mutex as StdMutex, PoisonError};
use std::time::{Duration, Instant};
use crate::core::ProxySpec;
pub const EXIT_IP_ENV: &str = "CHROMELEON_EXIT_IP";
pub const TARGET_TZ_ENV: &str = "CHROMELEON_TARGET_TZ";
pub const DEFAULT_GEO_TTL: Duration = Duration::from_secs(600);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProxyRotation {
Sticky,
Rotating,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct GeoResolution {
pub exit_ip: String,
pub timezone: Option<String>,
}
impl GeoResolution {
pub fn new(exit_ip: impl Into<String>, timezone: Option<String>) -> Self {
GeoResolution {
exit_ip: exit_ip.into(),
timezone,
}
}
pub fn env(&self) -> Vec<(String, String)> {
let mut env = vec![(EXIT_IP_ENV.to_string(), self.exit_ip.clone())];
if let Some(tz) = &self.timezone {
env.push((TARGET_TZ_ENV.to_string(), tz.clone()));
}
env
}
}
pub fn is_valid_iana_tz(tz: &str) -> bool {
!tz.is_empty()
&& tz.contains('/')
&& tz.len() < 64
&& !tz.chars().any(|c| c.is_whitespace() || (c as u32) < 0x20)
}
struct CacheEntry {
resolution: GeoResolution,
resolved_at: Instant,
}
#[derive(Default)]
pub struct GeoCache {
entries: Mutex<HashMap<String, CacheEntry>>,
}
impl GeoCache {
pub fn new() -> Self {
GeoCache::default()
}
pub fn global() -> &'static GeoCache {
static GLOBAL: std::sync::OnceLock<GeoCache> = std::sync::OnceLock::new();
GLOBAL.get_or_init(GeoCache::new)
}
fn key(proxy: &ProxySpec) -> String {
format!("{}\0{}", proxy.server(), proxy.username().unwrap_or(""))
}
fn get(&self, key: &str, ttl: Duration) -> Option<GeoResolution> {
let entries = self.entries.lock().unwrap_or_else(PoisonError::into_inner);
entries
.get(key)
.filter(|e| e.resolved_at.elapsed() < ttl)
.map(|e| e.resolution.clone())
}
fn put(&self, key: String, resolution: GeoResolution) {
let mut entries = self.entries.lock().unwrap_or_else(PoisonError::into_inner);
entries.insert(
key,
CacheEntry {
resolution,
resolved_at: Instant::now(),
},
);
}
pub fn clear(&self) {
self.entries
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clear();
}
pub fn len(&self) -> usize {
self.entries
.lock()
.unwrap_or_else(PoisonError::into_inner)
.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn resolve_with<F, E>(
&self,
proxy: &ProxySpec,
rotation: ProxyRotation,
ttl: Duration,
resolve: F,
) -> Option<GeoResolution>
where
F: FnOnce(&ProxySpec) -> std::result::Result<GeoResolution, E>,
{
if rotation != ProxyRotation::Sticky {
return None;
}
let key = Self::key(proxy);
if let Some(hit) = self.get(&key, ttl) {
return Some(hit);
}
let resolved = sanitize(resolve(proxy).ok()?)?;
self.put(key, resolved.clone());
Some(resolved)
}
pub async fn resolve_with_async<F, Fut, E>(
&self,
proxy: &ProxySpec,
rotation: ProxyRotation,
ttl: Duration,
resolve: F,
) -> Option<GeoResolution>
where
F: FnOnce(ProxySpec) -> Fut,
Fut: Future<Output = std::result::Result<GeoResolution, E>>,
{
if rotation != ProxyRotation::Sticky {
return None;
}
let key = Self::key(proxy);
if let Some(hit) = self.get(&key, ttl) {
return Some(hit);
}
let resolved = sanitize(resolve(proxy.clone()).await.ok()?)?;
self.put(key, resolved.clone());
Some(resolved)
}
}
fn sanitize(mut resolution: GeoResolution) -> Option<GeoResolution> {
if resolution.exit_ip.trim().is_empty() {
return None; }
if let Some(tz) = &resolution.timezone {
if !is_valid_iana_tz(tz) {
resolution.timezone = None;
}
}
Some(resolution)
}
pub fn sticky_geo_env<F, E>(
proxy: &ProxySpec,
rotation: ProxyRotation,
resolve: F,
) -> Vec<(String, String)>
where
F: FnOnce(&ProxySpec) -> std::result::Result<GeoResolution, E>,
{
GeoCache::global()
.resolve_with(proxy, rotation, DEFAULT_GEO_TTL, resolve)
.map(|r| r.env())
.unwrap_or_default()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NoEndpoints;
impl std::fmt::Display for NoEndpoints {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("a BrowserPool needs at least one CDP endpoint")
}
}
impl std::error::Error for NoEndpoints {}
pub struct BrowserPool<C> {
endpoints: Vec<String>,
next: AtomicUsize,
slots: StdMutex<HashMap<String, Slot<C>>>,
}
type Slot<C> = Arc<async_lock::Mutex<Option<Arc<C>>>>;
impl<C> BrowserPool<C> {
pub fn new<I, S>(endpoints: I) -> std::result::Result<Self, NoEndpoints>
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
let endpoints: Vec<String> = endpoints.into_iter().map(Into::into).collect();
if endpoints.is_empty() {
return Err(NoEndpoints);
}
Ok(BrowserPool {
endpoints,
next: AtomicUsize::new(0),
slots: StdMutex::new(HashMap::new()),
})
}
pub fn endpoints(&self) -> &[String] {
&self.endpoints
}
pub fn next_endpoint(&self) -> &str {
let i = self.next.fetch_add(1, Ordering::Relaxed);
&self.endpoints[i % self.endpoints.len()]
}
pub fn warm_count(&self) -> usize {
self.slots
.lock()
.unwrap_or_else(PoisonError::into_inner)
.values()
.filter(|slot| slot.try_lock().is_some_and(|held| held.is_some()))
.count()
}
fn slot(&self, endpoint: &str) -> Slot<C> {
let mut slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(existing) = slots.get(endpoint) {
return Arc::clone(existing);
}
let created: Slot<C> = Arc::new(async_lock::Mutex::new(None));
slots.insert(endpoint.to_string(), Arc::clone(&created));
created
}
pub async fn acquire<F, Fut, E>(&self, connect: F) -> std::result::Result<Arc<C>, E>
where
F: FnOnce(String) -> Fut,
Fut: Future<Output = std::result::Result<C, E>>,
{
let endpoint = self.next_endpoint().to_string();
let slot = self.slot(&endpoint);
let mut held = slot.lock_arc().await;
if let Some(existing) = held.as_ref() {
return Ok(Arc::clone(existing));
}
let connection = Arc::new(connect(endpoint).await?);
*held = Some(Arc::clone(&connection));
Ok(connection)
}
pub async fn evict(&self, endpoint: &str) -> Option<Arc<C>> {
let slot = {
let slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner);
slots.get(endpoint).map(Arc::clone)
}?;
slot.lock_arc().await.take()
}
pub async fn drain(&self) -> Vec<Arc<C>> {
let slots: Vec<Slot<C>> = {
let mut map = self.slots.lock().unwrap_or_else(PoisonError::into_inner);
map.drain().map(|(_, slot)| slot).collect()
};
let mut drained = Vec::with_capacity(slots.len());
for slot in slots {
if let Some(mut held) = slot.try_lock() {
drained.extend(held.take());
}
}
drained
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_rotating_proxy_is_never_pinned() {
let proxy = ProxySpec::parse("http://u:p@gw:1").unwrap();
let env = sticky_geo_env(&proxy, ProxyRotation::Rotating, |_| {
Err::<GeoResolution, &str>("resolver must not run")
});
assert!(env.is_empty());
}
#[test]
fn two_credentials_on_one_gateway_are_two_exits() {
let cache = GeoCache::new();
let session_a = ProxySpec::parse("http://user-session-a:pw@gw:12321").unwrap();
let session_b = ProxySpec::parse("http://user-session-b:pw@gw:12321").unwrap();
let resolve_to = |ip: &'static str| {
move |_: &ProxySpec| {
Ok::<_, std::convert::Infallible>(GeoResolution::new(
ip,
Some("Europe/Warsaw".to_string()),
))
}
};
let a = cache
.resolve_with(
&session_a,
ProxyRotation::Sticky,
DEFAULT_GEO_TTL,
resolve_to("203.0.113.1"),
)
.unwrap();
let b = cache
.resolve_with(
&session_b,
ProxyRotation::Sticky,
DEFAULT_GEO_TTL,
resolve_to("198.51.100.2"),
)
.unwrap();
assert_eq!(a.exit_ip, "203.0.113.1");
assert_eq!(
b.exit_ip, "198.51.100.2",
"the sticky username selects the exit; a server-keyed cache would \
have served session A's IP to session B"
);
assert_eq!(cache.len(), 2);
}
#[test]
fn a_bad_timezone_keeps_the_ip_pin() {
let cache = GeoCache::new();
let proxy = ProxySpec::parse("http://u:p@gw:2").unwrap();
let resolved = cache
.resolve_with(&proxy, ProxyRotation::Sticky, DEFAULT_GEO_TTL, |_| {
Ok::<_, &str>(GeoResolution::new("203.0.113.7", Some("Not A Zone".into())))
})
.unwrap();
assert_eq!(resolved.exit_ip, "203.0.113.7");
assert_eq!(resolved.timezone, None);
assert_eq!(
resolved.env(),
[(EXIT_IP_ENV.to_string(), "203.0.113.7".to_string())]
);
}
#[test]
fn a_resolver_failure_is_not_a_launch_failure() {
let cache = GeoCache::new();
let proxy = ProxySpec::parse("http://u:p@gw:3").unwrap();
assert!(cache
.resolve_with(&proxy, ProxyRotation::Sticky, DEFAULT_GEO_TTL, |_| Err::<
GeoResolution,
&str,
>(
"timeout"
))
.is_none());
assert!(cache.is_empty()); }
#[test]
fn timezone_shape_gate() {
assert!(is_valid_iana_tz("Europe/Warsaw"));
assert!(is_valid_iana_tz("America/Argentina/Buenos_Aires"));
assert!(!is_valid_iana_tz("UTC")); assert!(!is_valid_iana_tz("Europe/ Warsaw"));
assert!(!is_valid_iana_tz(""));
assert!(!is_valid_iana_tz(&format!("Europe/{}", "x".repeat(64))));
}
}