2026-11-17 03:13:59 +00:00
|
|
|
|
//! 一個 HTTP 用戶端: every HTTP client AnyBot makes starts here, so they all trust the same
|
|
|
|
|
|
//! certificates — the public ones every browser knows plus the ones this computer trusts itself
|
|
|
|
|
|
//! (tunnel::trusted_roots). Before, only the account server's client did: a connector, a model
|
|
|
|
|
|
//! service or a webhook on the local network with a certificate its company signs (trusted by
|
|
|
|
|
|
//! the computer, as in the browser) failed with "unknown issuer" everywhere else.
|
|
|
|
|
|
//!
|
|
|
|
|
|
//! Each caller still sets its own timeouts, redirects and user agent on top. `clippy.toml` refuses
|
|
|
|
|
|
//! `ureq::Agent::config_builder` anywhere but here (grok-build does the same for its HTTP clients).
|
|
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
use std::sync::{Mutex, OnceLock};
|
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
|
|
/// The certificates an AnyBot HTTP client accepts (made once: the system's list takes a moment).
|
|
|
|
|
|
fn tls() -> ureq::tls::TlsConfig {
|
|
|
|
|
|
static T: OnceLock<ureq::tls::TlsConfig> = OnceLock::new();
|
|
|
|
|
|
T.get_or_init(|| {
|
|
|
|
|
|
let roots: Vec<_> = crate::tunnel::trusted_roots().iter().map(|c| ureq::tls::Certificate::from_der(c.as_ref()).to_owned()).collect();
|
|
|
|
|
|
ureq::tls::TlsConfig::builder().root_certs(ureq::tls::RootCerts::new_with_certs(&roots)).build()
|
|
|
|
|
|
})
|
|
|
|
|
|
.clone()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// An HTTP client's settings, with AnyBot's certificates already in: add timeouts and the rest,
|
|
|
|
|
|
/// then `.build().into()`.
|
|
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
|
|
pub fn config() -> ureq::config::ConfigBuilder<ureq::typestate::AgentScope> {
|
|
|
|
|
|
ureq::Agent::config_builder().tls_config(tls())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// The common client: `secs` for the whole request, an HTTP error status is an answer (not an
|
|
|
|
|
|
/// Err). One per timeout, kept (its connections are reused).
|
2026-11-19 08:01:50 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// 連接本身 5 秒封顶:ureq 的默认连接超时是 30 秒——网络刚断、重开机、tailnet
|
|
|
|
|
|
/// 未就绪时,每个请求都会静静地挂满半分钟才发现连不上:App 重开后的「好久
|
|
|
|
|
|
/// 连不上」、按下停止或任何按钮后的长转圈,都是这 30 秒。快失败、快重试。
|
2026-11-17 03:13:59 +00:00
|
|
|
|
pub fn agent(secs: u64) -> ureq::Agent {
|
|
|
|
|
|
static AGENTS: OnceLock<Mutex<HashMap<u64, ureq::Agent>>> = OnceLock::new();
|
2026-11-19 09:28:14 +00:00
|
|
|
|
AGENTS.get_or_init(Mutex::default).lock().unwrap_or_else(|e| e.into_inner()).entry(secs).or_insert_with(|| config().timeout_global(Some(Duration::from_secs(secs))).timeout_connect(Some(Duration::from_secs(15))).http_status_as_error(false).build().into()).clone()
|
2026-11-17 03:13:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn every_client_trusts_the_same_certificates() {
|
|
|
|
|
|
// The public list at least, and the system's on top.
|
|
|
|
|
|
assert!(crate::tunnel::trusted_roots().len() >= webpki_root_certs::TLS_SERVER_ROOT_CERTS.len());
|
|
|
|
|
|
let a = super::agent(7);
|
|
|
|
|
|
let b = super::agent(7);
|
|
|
|
|
|
// (The same client for the same timeout: its connections are reused.)
|
|
|
|
|
|
assert_eq!(format!("{:?}", a.config().timeouts().global), format!("{:?}", b.config().timeouts().global));
|
|
|
|
|
|
assert!(!a.config().http_status_as_error());
|
|
|
|
|
|
}
|
2026-11-19 08:01:50 +00:00
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn connecting_dies_fast_everywhere() {
|
2026-11-19 09:28:14 +00:00
|
|
|
|
// 連接 15 秒封顶(不是 ureq 默认的 30):普通网络几秒见分晓;经 Tailscale 中继(DERP)
|
|
|
|
|
|
// 绕路的 TLS 握手实测可达 10 秒,5 秒会把中继用户锁在门外——15 秒两者都顾。
|
2026-11-19 08:01:50 +00:00
|
|
|
|
let a = super::agent(40);
|
2026-11-19 09:28:14 +00:00
|
|
|
|
assert_eq!(a.config().timeouts().connect, Some(std::time::Duration::from_secs(15)), "連接超時 15 秒");
|
2026-11-19 08:01:50 +00:00
|
|
|
|
}
|
2026-11-17 03:13:59 +00:00
|
|
|
|
}
|