use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
use dashmap::DashMap;
use crate::config::settings::SessionSettings;
use crate::engines::provider::{ProxyCategory, ProxyProvider};
use crate::utils::ua_pool::{UaPool, UaProfile};
const SWEEP_INTERVAL: Duration = Duration::from_secs(60);
pub struct SessionIdentity {
pub id: u64,
#[cfg_attr(not(test), allow(dead_code))]
affinity_key: u64,
pub proxy_url: Option<String>,
ua_profile_idx: usize,
mobile: bool,
cookie_jar: parking_lot::Mutex<HashMap<String, String>>,
created_at: Instant,
uses: AtomicU64,
error_score: parking_lot::Mutex<f64>,
}
impl SessionIdentity {
pub fn error_score(&self) -> f64 {
*self.error_score.lock()
}
pub fn uses(&self) -> u64 {
self.uses.load(Ordering::Relaxed)
}
pub fn age(&self) -> Duration {
self.created_at.elapsed()
}
pub fn ua_profile<'a>(&self, pool: &'a UaPool) -> &'a UaProfile {
let group = if self.mobile {
&pool.mobile
} else {
&pool.desktop
};
&group[self.ua_profile_idx % group.len()]
}
}
pub struct SessionManager {
enabled: bool,
sessions: DashMap<u64, std::sync::Arc<SessionIdentity>>,
ua_pool: std::sync::Arc<UaPool>,
proxy_provider: Option<std::sync::Arc<dyn ProxyProvider>>,
max_sessions: usize,
max_error_score: f64,
max_uses: u64,
max_age: Duration,
cookie_cache_max_bytes: usize,
next_id: AtomicU64,
last_sweep: parking_lot::Mutex<Instant>,
}
impl SessionManager {
pub fn from_settings(
settings: &SessionSettings,
ua_pool: std::sync::Arc<UaPool>,
proxy_provider: Option<std::sync::Arc<dyn ProxyProvider>>,
) -> Self {
Self {
enabled: settings.enabled,
sessions: DashMap::new(),
ua_pool,
proxy_provider,
max_sessions: settings.max_sessions.max(1),
max_error_score: settings.max_error_score,
max_uses: settings.max_uses.max(1),
max_age: Duration::from_secs(settings.max_age_secs),
cookie_cache_max_bytes: settings.cookie_cache_max_bytes.max(64),
next_id: AtomicU64::new(0),
last_sweep: parking_lot::Mutex::new(Instant::now()),
}
}
pub fn disabled() -> Self {
Self::from_settings(
&SessionSettings::default(),
std::sync::Arc::new(UaPool::new()),
None,
)
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn ua_pool(&self) -> &std::sync::Arc<UaPool> {
&self.ua_pool
}
pub fn acquire(
&self,
host: &str,
session_id: Option<&str>,
mobile: bool,
) -> std::sync::Arc<SessionIdentity> {
use dashmap::mapref::entry::Entry;
self.maybe_sweep();
let key = Self::affinity_key(host, session_id);
let at_capacity = self.sessions.len() >= self.max_sessions;
let identity = match self.sessions.entry(key) {
Entry::Occupied(mut occ) => {
let existing = occ.get().clone();
if self.should_retire(&existing) {
let fresh = self.build_identity(key, mobile);
log::info!(
"{}",
crate::i18n::tr_log_args(
"session-identity-retired",
&[
("id", (existing.id as i64).to_string(),),
("score", existing.error_score().to_string(),),
("uses", (existing.uses() as i64).to_string()),
("age", format!("{:?}", existing.age()),),
("fresh_id", (fresh.id as i64).to_string()),
],
)
);
occ.insert(fresh.clone());
fresh
} else {
existing
}
}
Entry::Vacant(vac) => {
let fresh = self.build_identity(key, mobile);
if at_capacity {
log::debug!(
"session pool at capacity ({}), serving ephemeral identity #{}",
self.max_sessions,
fresh.id
);
} else {
vac.insert(fresh.clone());
}
fresh
}
};
identity.uses.fetch_add(1, Ordering::Relaxed);
identity
}
pub fn mark_bad(&self, identity: &SessionIdentity) {
*identity.error_score.lock() += 1.0;
}
pub fn mark_good(&self, identity: &SessionIdentity) {
let mut score = identity.error_score.lock();
*score = (*score - 0.5).max(0.0);
}
pub fn retire(&self, host: &str, session_id: Option<&str>) {
let key = Self::affinity_key(host, session_id);
if let Some((_, identity)) = self.sessions.remove(&key) {
log::info!(
"{}",
crate::i18n::tr_log_args(
"session-identity-force-retired",
&[
("id", (identity.id as i64).to_string()),
("host", host.to_string()),
("score", identity.error_score().to_string(),),
("uses", (identity.uses() as i64).to_string()),
],
)
);
}
}
pub fn capture_cookies(
&self,
identity: &SessionIdentity,
host: &str,
headers: &HashMap<String, String>,
) {
let mut jar = identity.cookie_jar.lock();
let blob = jar.entry(host.to_string()).or_default();
for (name, value) in headers {
if !name.eq_ignore_ascii_case("set-cookie") {
continue;
}
self.append_set_cookie_blob(blob, value, identity.id, self.cookie_cache_max_bytes);
}
}
fn append_set_cookie_blob(&self, blob: &mut String, value: &str, identity_id: u64, cap: usize) {
for cookie_str in value.split('\n') {
let pair = cookie_str.split(';').next().unwrap_or("").trim();
if pair.is_empty() {
continue;
}
let pair_len = pair.len() + if blob.is_empty() { 0 } else { 2 };
if pair_len > cap {
log::warn!(
"{}",
crate::i18n::tr_log_args(
"session-cookie-cap-dropped",
&[
("bytes", (pair.len() as i64).to_string()),
("cap", (cap as i64).to_string()),
("id", (identity_id as i64).to_string(),),
],
)
);
continue;
}
if !blob.is_empty() {
blob.push_str("; ");
}
blob.push_str(pair);
while blob.len() > cap {
match blob.find("; ") {
Some(pos) => {
blob.drain(..pos + 2);
}
None => {
blob.clear();
break;
}
}
}
}
}
pub fn cookie_header(&self, identity: &SessionIdentity, host: &str) -> Option<String> {
let jar = identity.cookie_jar.lock();
jar.get(host).filter(|blob| !blob.is_empty()).cloned()
}
fn affinity_key(host: &str, session_id: Option<&str>) -> u64 {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
match session_id {
Some(sid) => sid.hash(&mut hasher),
None => host.hash(&mut hasher),
}
hasher.finish()
}
fn should_retire(&self, identity: &SessionIdentity) -> bool {
identity.error_score() >= self.max_error_score
|| identity.uses() >= self.max_uses
|| identity.age() >= self.max_age
}
fn build_identity(&self, key: u64, mobile: bool) -> std::sync::Arc<SessionIdentity> {
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
let proxy_url = self.proxy_provider.as_ref().and_then(|provider| {
let sticky_key = format!("session-identity-{id}");
provider
.sticky(&sticky_key)
.or_else(|| provider.next(ProxyCategory::Html))
});
let pool_len = if mobile {
self.ua_pool.mobile.len()
} else {
self.ua_pool.desktop.len()
};
let ua_profile_idx = (id as usize) % pool_len.max(1);
std::sync::Arc::new(SessionIdentity {
id,
affinity_key: key,
proxy_url,
ua_profile_idx,
mobile,
cookie_jar: parking_lot::Mutex::new(HashMap::new()),
created_at: Instant::now(),
uses: AtomicU64::new(0),
error_score: parking_lot::Mutex::new(0.0),
})
}
fn maybe_sweep(&self) {
let mut last = self.last_sweep.lock();
if last.elapsed() < SWEEP_INTERVAL {
return;
}
*last = Instant::now();
drop(last);
self.sessions
.retain(|_, identity| !self.should_retire(identity));
}
pub fn len(&self) -> usize {
self.sessions.len()
}
pub fn is_empty(&self) -> bool {
self.sessions.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::engines::proxy_pool::ProxyPool;
use std::sync::Arc;
fn make_provider() -> Arc<dyn ProxyProvider> {
Arc::new(ProxyPool::from_urls(
vec![
"http://proxy-a:8080".to_string(),
"http://proxy-b:8080".to_string(),
"http://proxy-c:8080".to_string(),
],
Duration::from_secs(60),
Duration::from_secs(30),
))
}
fn make_manager(
max_error_score: f64,
max_uses: u64,
max_age_secs: u64,
cookie_cap: usize,
) -> SessionManager {
SessionManager::from_settings(
&SessionSettings {
enabled: true,
max_sessions: 16,
max_error_score,
max_uses,
max_age_secs,
cookie_cache_max_bytes: cookie_cap,
},
Arc::new(UaPool::new()),
Some(make_provider()),
)
}
#[test]
fn acquire_same_host_session_returns_stable_identity() {
let mgr = make_manager(3.0, 100, 1800, 4096);
let a = mgr.acquire("shop.example.com", Some("sess-1"), false);
let b = mgr.acquire("shop.example.com", Some("sess-1"), false);
let c = mgr.acquire("shop.example.com", Some("sess-1"), false);
assert_eq!(a.id, b.id, "第二次 acquire 必须命中同一身份");
assert_eq!(b.id, c.id, "第三次 acquire 仍命中同一身份");
assert_eq!(
a.proxy_url, b.proxy_url,
"同身份代理 URL 必须稳定(sticky 绑定)"
);
assert!(a.proxy_url.is_some(), "配置了 provider 时身份必须持有代理");
let pool = mgr.ua_pool();
assert_eq!(
a.ua_profile(pool).ua,
b.ua_profile(pool).ua,
"同身份 UA 必须稳定"
);
assert_eq!(
a.ua_profile(pool).tls_emulation,
b.ua_profile(pool).tls_emulation,
"同身份 TLS 档必须稳定"
);
}
#[test]
fn different_sessions_get_different_identities() {
let mgr = make_manager(3.0, 100, 1800, 4096);
let s1 = mgr.acquire("h.example.com", Some("s1"), false);
let s2 = mgr.acquire("h.example.com", Some("s2"), false);
assert_ne!(s1.id, s2.id, "不同 session_id 必须是不同身份");
assert_ne!(s1.proxy_url, s2.proxy_url, "不同身份从 RR 池取得不同代理");
}
#[test]
fn error_score_retirement_on_threshold() {
let mgr = make_manager(3.0, 100, 1800, 4096);
let id = mgr.acquire("api.example.com", Some("s"), false);
mgr.mark_bad(&id);
mgr.mark_bad(&id);
assert!(
(id.error_score() - 2.0).abs() < f64::EPSILON,
"两次 mark_bad 后 score=2.0"
);
assert_eq!(
mgr.acquire("api.example.com", Some("s"), false).id,
id.id,
"score 2.0 < 3.0 不得退休"
);
mgr.mark_bad(&id);
assert!((id.error_score() - 3.0).abs() < f64::EPSILON);
let fresh = mgr.acquire("api.example.com", Some("s"), false);
assert_ne!(fresh.id, id.id, "score 达阈值后 acquire 必须换新身份");
assert!(
(fresh.error_score() - 0.0).abs() < f64::EPSILON,
"新身份 score 归零"
);
assert_ne!(
mgr.sessions.get(&id.affinity_key).map(|i| i.id),
Some(id.id),
"旧身份不得继续留在原亲和键上"
);
assert_eq!(
fresh.affinity_key, id.affinity_key,
"亲和键不变(同 session)"
);
}
#[test]
fn mark_good_floors_at_zero() {
let mgr = make_manager(3.0, 100, 1800, 4096);
let id = mgr.acquire("ok.example.com", Some("s"), false);
mgr.mark_bad(&id);
assert!((id.error_score() - 1.0).abs() < f64::EPSILON);
mgr.mark_good(&id);
mgr.mark_good(&id);
assert_eq!(id.error_score(), 0.0, "mark_good 抵扣到 0 为下限,不得为负");
}
#[test]
fn max_uses_retires_identity() {
let mgr = make_manager(100.0, 2, 1800, 4096); let a = mgr.acquire("h.example.com", Some("s"), false);
let b = mgr.acquire("h.example.com", Some("s"), false);
assert_eq!(a.id, b.id, "uses=1、2 时仍同一身份");
assert_eq!(b.uses(), 2);
let c = mgr.acquire("h.example.com", Some("s"), false);
assert_ne!(c.id, a.id, "uses=2 达 max_uses 后必须换新身份");
assert_eq!(c.uses(), 1, "新身份 uses 从本次 acquire 起计");
}
#[test]
fn max_age_zero_rotates_identity_every_acquire() {
let mgr = make_manager(100.0, 100, 0, 4096);
let a = mgr.acquire("h.example.com", Some("s"), false);
let b = mgr.acquire("h.example.com", Some("s"), false);
assert_ne!(a.id, b.id, "max_age=0 时每次 acquire 都必须换新身份");
}
#[test]
fn retire_forces_new_identity_on_next_acquire() {
let mgr = make_manager(100.0, 100, 1800, 4096);
let host = "stall.example.com";
let a = mgr.acquire(host, None, false);
mgr.retire(host, None);
let b = mgr.acquire(host, None, false);
assert_ne!(a.id, b.id, "retire 后同 host acquire 必须换新身份");
mgr.retire(host, None);
mgr.retire("never-seen.example.com", Some("nope"));
}
#[test]
fn capture_and_render_cookie_header() {
let mgr = make_manager(3.0, 100, 1800, 4096);
let id = mgr.acquire("h.example.com", Some("s"), false);
assert!(
mgr.cookie_header(&id, "h.example.com").is_none(),
"未捕获前无 Cookie 头"
);
let mut first = HashMap::new();
first.insert("Content-Type".to_string(), "text/html".to_string());
first.insert(
"set-cookie".to_string(),
"sid=abc123; Path=/; HttpOnly".to_string(),
);
mgr.capture_cookies(&id, "h.example.com", &first);
assert_eq!(
mgr.cookie_header(&id, "h.example.com").as_deref(),
Some("sid=abc123"),
"只取 name=value 对,属性丢弃"
);
let mut second = HashMap::new();
second.insert("Set-Cookie".to_string(), "theme=dark".to_string());
mgr.capture_cookies(&id, "h.example.com", &second);
assert_eq!(
mgr.cookie_header(&id, "h.example.com").as_deref(),
Some("sid=abc123; theme=dark"),
"多次捕获累进,旧条目在前"
);
}
#[test]
fn cookies_do_not_leak_across_hosts_under_session_affinity() {
let mgr = make_manager(3.0, 100, 1800, 4096);
let id = mgr.acquire("a.example.com", Some("shared-session"), false);
let mut h = HashMap::new();
h.insert(
"set-cookie".to_string(),
"secret=a-site-credential".to_string(),
);
mgr.capture_cookies(&id, "a.example.com", &h);
assert_eq!(
mgr.cookie_header(&id, "a.example.com").as_deref(),
Some("secret=a-site-credential"),
"目标 host 桶正常渲染"
);
assert!(
mgr.cookie_header(&id, "b.example.com").is_none(),
"非捕获 host 必须无 Cookie 头"
);
assert!(
!mgr.cookie_header(&id, "b.example.com")
.unwrap_or_default()
.contains("a-site-credential"),
"A 站凭据不得出现在其他 host 的请求中"
);
}
#[test]
fn cookie_cache_truncates_oldest_without_panic() {
let mgr = make_manager(3.0, 100, 1800, 64); let id = mgr.acquire("h.example.com", Some("s"), false);
for i in 0..10 {
let mut h = HashMap::new();
h.insert("set-cookie".to_string(), format!("k{i}=value{i}"));
mgr.capture_cookies(&id, "h.example.com", &h);
}
let blob = mgr
.cookie_header(&id, "h.example.com")
.expect("截断后仍有存活条目");
assert!(blob.len() <= 64, "缓存 {blob:?} 超过 64 字节上限");
assert!(!blob.contains("k0="), "最旧条目必须被丢弃,得到 {blob:?}");
assert!(blob.contains("k9="), "最新条目必须保留,得到 {blob:?}");
let before = mgr.cookie_header(&id, "h.example.com").unwrap();
let mut big = HashMap::new();
big.insert("set-cookie".to_string(), "x".repeat(64));
mgr.capture_cookies(&id, "h.example.com", &big);
assert_eq!(
mgr.cookie_header(&id, "h.example.com").as_deref(),
Some(before.as_str()),
"单条超限必须整条丢弃且不污染既有缓存"
);
}
#[test]
fn capacity_limit_degrades_to_ephemeral_identity() {
let settings = SessionSettings {
enabled: true,
max_sessions: 1,
max_error_score: 3.0,
max_uses: 100,
max_age_secs: 1800,
cookie_cache_max_bytes: 4096,
};
let mgr = SessionManager::from_settings(
&settings,
Arc::new(UaPool::new()),
Some(make_provider()),
);
let a = mgr.acquire("a.example.com", None, false);
let b = mgr.acquire("b.example.com", None, false);
assert_ne!(a.id, b.id);
assert_eq!(mgr.len(), 1, "超限临时身份不得入池");
assert!(b.proxy_url.is_some(), "临时身份仍持有代理(完整三件套)");
assert_eq!(
mgr.acquire("a.example.com", None, false).id,
a.id,
"已有绑定不受容量降级影响"
);
}
#[tokio::test]
async fn concurrent_acquire_and_mark_no_deadlock() {
let mgr = Arc::new(make_manager(3.0, 100_000, 1800, 4096));
let mut handles = Vec::new();
for t in 0..8 {
let m = Arc::clone(&mgr);
handles.push(tokio::spawn(async move {
for i in 0..50 {
let id = m.acquire("hot.example.com", Some("shared"), false);
if i % 3 == 0 {
m.mark_bad(&id);
} else {
m.mark_good(&id);
}
let mut h = HashMap::new();
h.insert("set-cookie".to_string(), format!("t{t}={i}"));
m.capture_cookies(&id, "hot.example.com", &h);
let _ = m.cookie_header(&id, "hot.example.com");
}
}));
}
tokio::time::timeout(Duration::from_secs(10), futures::future::join_all(handles))
.await
.expect("并发 session 操作不得死锁");
}
}