diff --git a/crates/tw-control/src/lib.rs b/crates/tw-control/src/lib.rs index 77433b26..3e1b321a 100644 --- a/crates/tw-control/src/lib.rs +++ b/crates/tw-control/src/lib.rs @@ -103,11 +103,6 @@ impl ControlState { pub fn bus(&self) -> &EventBus { &self.gateway.bus } - /// 探测复用数据面的 HTTP 客户端 —— 同一套超时、同一套代理设置。 - /// 另起一个会让「探测通了但实际请求不通」变成可能。 - pub fn http(&self) -> &reqwest::Client { - &self.gateway.http - } pub fn health(&self) -> &Arc { &self.gateway.health } diff --git a/crates/tw-control/src/replay.rs b/crates/tw-control/src/replay.rs index 4c6d9f89..103875ab 100644 --- a/crates/tw-control/src/replay.rs +++ b/crates/tw-control/src/replay.rs @@ -171,22 +171,20 @@ pub async fn run( ), ) })?; - // OAuth 要联网换 token。重放不经过数据面,但**凭据这一层 - // 必须走同一条路** —— 否则一个 OAuth 上游在重放里永远是「密钥取不到」 - let pk_http = s.gateway.client_for(&provider.name); - let headers = s - .gateway - .headers_for(provider, &pk_http) - .await - .map_err(|e| { - fail( - StatusCode::BAD_REQUEST, - msg!( - "control.credentials_failed", upstream = provider.name.clone(), detail = e => - "The credential for upstream `{upstream}` could not be obtained: {detail}" - ), - ) - })?; + // **这一家自己的 client,和数据面转发用的是同一个**:它带着这家该走的 + // 代理(`direct` 就是不走任何代理,连系统代理也不读)。换 token 和发请求 + // 都用它 —— 用别的 client,重放就会走一条和原请求不同的出站路径:本机 + // 上游被送进系统代理、指定的代理被绕过,比出来的结果是另一条路的 + let http = s.gateway.client_for(&provider.name); + let headers = s.gateway.headers_for(provider, &http).await.map_err(|e| { + fail( + StatusCode::BAD_REQUEST, + msg!( + "control.credentials_failed", upstream = provider.name.clone(), detail = e => + "The credential for upstream `{upstream}` could not be obtained: {detail}" + ), + ) + })?; // **脱敏照做。**重放不经过数据面的管线,少了这一行,一条本来会被 // 脱敏的请求会因为「重放」这个动作把密钥原样发出去 @@ -198,7 +196,6 @@ pub async fn run( ); let url = tw_gateway::forward::upstream_url(&provider.base_url, &row.path, None); - let http = s.http().clone(); let started = Instant::now(); let mut r = http.post(&url).header("content-type", "application/json"); r = tw_gateway::forward::apply_headers(r, &headers); diff --git a/crates/tw-control/tests/replay.rs b/crates/tw-control/tests/replay.rs new file mode 100644 index 00000000..67267a5a --- /dev/null +++ b/crates/tw-control/tests/replay.rs @@ -0,0 +1,207 @@ +//! 重放走的出站路径。 +//! +//! **重放必须和数据面走同一条出站路径**:同一家上游的同一个 client,带着它该走的 +//! 代理。以前重放用的是一个读系统代理的公用 client —— 本机的上游(`127.0.0.1`) +//! 被送进了系统代理,拿回来的是代理的 503 页面;指定了代理的上游反过来绕过了 +//! 它的代理。两种都是在比另一条路。 + +use std::net::SocketAddr; +use std::sync::Arc; + +use axum::body::Body; +use axum::http::{Request, StatusCode}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tower::ServiceExt; +use tw_control::{ConfigManager, ControlState}; + +/// 一个只会回同一句话的 HTTP 服务。当上游用,也当 HTTP 代理用 —— 对 `http://` +/// 目标,代理收到的就是一个请求行里带完整 URL 的普通请求。 +async fn answering(status: u16, body: &'static str) -> SocketAddr { + let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = l.local_addr().unwrap(); + tokio::spawn(async move { + loop { + let Ok((mut sock, _)) = l.accept().await else { + return; + }; + tokio::spawn(async move { + // 读到头结束再回;请求体很小,和头一起到 + let mut buf = vec![0u8; 64 * 1024]; + let mut seen = 0; + while !buf[..seen].windows(4).any(|w| w == b"\r\n\r\n") { + match sock.read(&mut buf[seen..]).await { + Ok(0) | Err(_) => return, + Ok(n) => seen += n, + } + } + let resp = format!( + "HTTP/1.1 {status} X\r\ncontent-type: text/plain\r\ncontent-length: {}\r\n\ + connection: close\r\n\r\n{body}", + body.len() + ); + let _ = sock.write_all(resp.as_bytes()).await; + }); + } + }); + addr +} + +/// 一个假的系统代理:谁的请求进来都回 503,和真机上撞到的那一页一样。 +/// +/// **同一个测试进程里只设一次**:reqwest 建 client 时读环境变量,这个文件里的 +/// 测试都在它之后建 client。代理跑在自己的线程和运行时上 —— 每个 +/// `#[tokio::test]` 有自己的运行时,挂在头一个测试上的话,那个测试一结束它就没了 +fn system_proxy() { + static ONCE: std::sync::OnceLock<()> = std::sync::OnceLock::new(); + ONCE.get_or_init(|| { + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + rt.block_on(async { + tx.send(answering(503, "via the system proxy").await) + .unwrap(); + std::future::pending::<()>().await + }); + }); + let url = format!("http://{}", rx.recv().unwrap()); + // SAFETY: 这个测试文件里只有这里写环境变量,而且写在任何 client 建起来之前 + unsafe { + for k in [ + "HTTP_PROXY", + "http_proxy", + "HTTPS_PROXY", + "https_proxy", + "ALL_PROXY", + ] { + std::env::set_var(k, &url); + } + for k in ["NO_PROXY", "no_proxy"] { + std::env::remove_var(k); + } + } + }); +} + +fn row(id: i64, provider: &str) -> tw_store::db::RequestRow { + tw_store::db::RequestRow { + key_masked: None, + peer: None, + id, + at_ms: 1000 + id, + client: "我".into(), + client_hint: None, + session: None, + provider: provider.into(), + model: "claude-sonnet-4-5".into(), + path: "/v1/messages".into(), + status: Some(200), + ttfb_ms: Some(100), + duration_ms: Some(200), + bytes: Some(10), + input_tokens: Some(5), + output_tokens: Some(2), + cache_read_tokens: None, + cache_write_tokens: None, + cost_micros: Some(0), + cost_estimated: false, + error: None, + local: false, + cancelled: false, + routing: None, + billing: "free".into(), + cache_saved_micros: None, + price_source: None, + translated: None, + } +} + +/// 一个记着一条请求(连同请求体)的控制面。 +fn app(config: &str) -> (tempfile::TempDir, axum::Router) { + let d = tempfile::tempdir().unwrap(); + let p = d.path().join("config.yaml"); + std::fs::write(&p, config).unwrap(); + let db = tw_store::Db::open(&d.path().join("data.db")).unwrap(); + let r = row(1, "本机"); + db.insert(&r).unwrap(); + let blobs = tw_store::Blobs::new(d.path().join("blobs")); + let body = br#"{"model":"claude-sonnet-4-5","max_tokens":8,"messages":[{"role":"user","content":"hi"}]}"#; + assert!(blobs.put(r.at_ms, r.id, tw_store::Which::Request, body)); + let rec = tw_store::Recorder::new( + db, + blobs, + tw_pricing::shared(tw_pricing::PriceBook::builtin().unwrap()), + ); + let cfg: tw_config::Config = serde_yaml_ng::from_str(config).unwrap(); + let gw = tw_gateway::AppState::new(cfg).unwrap(); + let bus = gw.bus.clone(); + let state = ControlState { + shutdown: Default::default(), + cfg: Arc::new(ConfigManager::new(p, gw.clone(), bus)), + gateway: gw, + store: Some(Arc::new(tokio::sync::Mutex::new(rec))), + started: std::time::Instant::now(), + price_updater: Default::default(), + chatgpt: Default::default(), + zai: Default::default(), + home: d.path().join("home"), + }; + (d, tw_control::router(state)) +} + +async fn replay(app: &axum::Router, provider: &str) -> serde_json::Value { + let req = tw_api::ReplayRequest { + id: 1, + provider: provider.into(), + }; + let r = app + .clone() + .oneshot( + Request::post("/replay/run") + .header("content-type", "application/json") + .body(Body::from(serde_json::to_vec(&req).unwrap())) + .unwrap(), + ) + .await + .unwrap(); + let status = r.status(); + let b = axum::body::to_bytes(r.into_body(), 1 << 20).await.unwrap(); + let v: serde_json::Value = serde_json::from_slice(&b).unwrap(); + assert_eq!(status, StatusCode::OK, "{v}"); + v +} + +/// 直连的本机上游:系统里开着代理也不走它。数据面转发这一家时就是这样。 +#[tokio::test] +async fn a_direct_upstream_is_replayed_directly_even_with_a_system_proxy() { + system_proxy(); + let upstream = answering(200, "from the upstream").await; + let (_d, app) = app(&format!( + "version: 1\nclients:\n - name: 我\n key: tw-一把钥匙就够\nproviders:\n \ + - name: 本机\n base_url: http://{upstream}\n key: sk-x\n billing: free\n" + )); + + let v = replay(&app, "本机").await; + assert_eq!(v["status"], 200, "{v}"); + assert_eq!(v["body"], "from the upstream", "{v}"); +} + +/// 指定了代理的上游:重放走它的代理,而不是直连或系统代理。 +#[tokio::test] +async fn an_upstream_with_a_proxy_is_replayed_through_that_proxy() { + system_proxy(); + let proxy = answering(200, "via its own proxy").await; + // 没有人听的地址:直连的话只会连不上 + let (_d, app) = app(&format!( + "version: 1\nclients:\n - name: 我\n key: tw-一把钥匙就够\nproxies:\n \ + - {{ name: 自己的, type: http, addr: \"{proxy}\" }}\nproviders:\n \ + - name: 本机\n base_url: http://127.0.0.1:9\n key: sk-x\n billing: free\n \ + proxy: 自己的\n" + )); + + let v = replay(&app, "本机").await; + assert_eq!(v["status"], 200, "{v}"); + assert_eq!(v["body"], "via its own proxy", "{v}"); +} diff --git a/crates/tw-gateway/src/state.rs b/crates/tw-gateway/src/state.rs index d70e76bf..1415e2af 100644 --- a/crates/tw-gateway/src/state.rs +++ b/crates/tw-gateway/src/state.rs @@ -123,7 +123,7 @@ pub struct AppState { /// 换的话,每改一次配置,排着的请求就会失去位置,而已经在跑的那些的 /// 通行证会变成孤儿。上限改了由它自己在原地加减(见 `limits`)。 pub(crate) gate: Arc, - /// 探测和别的杂事用的默认 Client(不走代理) + /// 一家上游不在当前运行时里时顶上的 Client(直连,不读系统代理) pub http: reqwest::Client, /// 观测事件往这里丢。没有订阅者时是零成本的 —— 数据面不该知道有 /// 没有人在看。**跨重载存活**:界面上的实时列表不该因为改了配置断一次。 @@ -202,7 +202,9 @@ pub struct AppState { impl AppState { pub fn new(config: tw_config::Config) -> Result { - let http = base_client_builder().build().map_err(|e| { + // **不走任何代理,连系统代理也不读** —— 和上游默认的 `direct` 一样。 + // 它只在一家上游不在当前运行时里时顶上,那时没有别的出站设置可依 + let http = base_client_builder().no_proxy().build().map_err(|e| { GatewayError::config(msg!( "gw.config.http_client", detail = e => "The HTTP client could not be created: {detail}" ))