From c706a030831169fa9563d49cb8992ec2aa4508c4 Mon Sep 17 00:00:00 2001 From: Perci <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@buzz.block.builderlab.xyz> Date: Sat, 22 Aug 2026 18:55:21 -0400 Subject: [PATCH] perf(desktop): compress relay HTTP responses Co-authored-by: Perci <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@buzz.block.builderlab.xyz> Signed-off-by: Perci <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@buzz.block.builderlab.xyz> --- desktop/src-tauri/Cargo.lock | 34 ++++++++ desktop/src-tauri/Cargo.toml | 2 +- desktop/src-tauri/src/app_state.rs | 18 +--- .../src-tauri/src/commands/media_download.rs | 4 +- desktop/src-tauri/src/http_client.rs | 87 +++++++++++++++++++ desktop/src-tauri/src/lib.rs | 8 +- desktop/src-tauri/src/media_proxy.rs | 2 +- 7 files changed, 131 insertions(+), 24 deletions(-) create mode 100644 desktop/src-tauri/src/http_client.rs diff --git a/desktop/src-tauri/Cargo.lock b/desktop/src-tauri/Cargo.lock index fb60a35189..a3deed4f3e 100644 --- a/desktop/src-tauri/Cargo.lock +++ b/desktop/src-tauri/Cargo.lock @@ -326,6 +326,18 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "async-compression" +version = "0.4.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3976abdc8fe7d1133d43d304afd42abdf5bc3e1319d263d223bde07b5efc4be8" +dependencies = [ + "compression-codecs", + "compression-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "async-executor" version = "1.14.0" @@ -1680,6 +1692,23 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "compression-codecs" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce2548391e9c1929c21bf6aa2680af86fe4c1b33e6cea9ac1cfeec0bd11218cf" +dependencies = [ + "compression-core", + "flate2", + "memchr", +] + +[[package]] +name = "compression-core" +version = "0.4.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc14f565cf027a105f7a44ccf9e5b424348421a1d8952a8fc9d499d313107789" + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -11481,12 +11510,17 @@ version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" dependencies = [ + "async-compression", "bitflags 2.13.0", "bytes", + "futures-core", "futures-util", "http", "http-body", + "http-body-util", "pin-project-lite", + "tokio", + "tokio-util", "tower", "tower-layer", "tower-service", diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml index 3f7189deea..e648e55156 100644 --- a/desktop/src-tauri/Cargo.toml +++ b/desktop/src-tauri/Cargo.toml @@ -99,7 +99,7 @@ nostr = { version = "0.44", features = ["nip44", "nip49"] } getrandom = "0.2" zeroize = "1" percent-encoding = "2" -reqwest = { version = "0.13", features = ["json", "query", "stream", "blocking"] } +reqwest = { version = "0.13", features = ["json", "query", "stream", "blocking", "gzip"] } rustls = { version = "0.23", default-features = false, features = ["aws_lc_rs", "std"] } url = "2" buzz_core_pkg = { package = "buzz-core", path = "../../crates/buzz-core" } diff --git a/desktop/src-tauri/src/app_state.rs b/desktop/src-tauri/src/app_state.rs index 7c41f6bfe2..22786b96c3 100644 --- a/desktop/src-tauri/src/app_state.rs +++ b/desktop/src-tauri/src/app_state.rs @@ -166,15 +166,6 @@ fn identity_from_env() -> Option { /// Returned as a `Result` so the fail-closed invariant is testable — callers /// must never substitute a redirect-following client on build failure. Shares /// the localhost `resolve`/pool config with the app-wide `http_client`. -pub fn build_media_fetch_client() -> reqwest::Result { - reqwest::Client::builder() - .resolve("localhost", std::net::SocketAddr::from(([127, 0, 0, 1], 0))) - .pool_idle_timeout(std::time::Duration::from_secs(10)) - .pool_max_idle_per_host(1) - .redirect(reqwest::redirect::Policy::none()) - .build() -} - pub fn build_app_state() -> AppState { // Env var takes precedence (dev/CI). If absent, resolve_persisted_identity() // in setup() will replace the ephemeral placeholder with a persisted key. @@ -192,13 +183,8 @@ pub fn build_app_state() -> AppState { AppState { keys: Mutex::new(keys), identity_storage: AtomicU8::new(identity_storage as u8), - http_client: reqwest::Client::builder() - .resolve("localhost", std::net::SocketAddr::from(([127, 0, 0, 1], 0))) - .pool_idle_timeout(std::time::Duration::from_secs(10)) - .pool_max_idle_per_host(1) - .build() - .unwrap_or_else(|_| reqwest::Client::new()), - media_fetch_client: build_media_fetch_client().expect( + http_client: crate::http_client::build().unwrap_or_else(|_| reqwest::Client::new()), + media_fetch_client: crate::http_client::build_media().expect( "media_fetch_client must build with redirect::Policy::none(); a \ redirect-following fallback would forward the minted media auth \ header across origins (redirect-hop SSRF)", diff --git a/desktop/src-tauri/src/commands/media_download.rs b/desktop/src-tauri/src/commands/media_download.rs index 7bc94da25d..29f2925f54 100644 --- a/desktop/src-tauri/src/commands/media_download.rs +++ b/desktop/src-tauri/src/commands/media_download.rs @@ -937,7 +937,7 @@ mod tests { }); // Drive the exact client the command path uses, not an ad-hoc one. - let client = crate::app_state::build_media_fetch_client() + let client = crate::http_client::build_media() .expect("media fetch client must build with no-redirect policy"); let resp = client .get(format!("http://{addr}/media/clip.mp4")) @@ -975,7 +975,7 @@ mod tests { // panics loudly (see `build_app_state`) rather than substituting an // insecure client. assert!( - crate::app_state::build_media_fetch_client().is_ok(), + crate::http_client::build_media().is_ok(), "media fetch client must build; a redirect-following fallback is forbidden", ); } diff --git a/desktop/src-tauri/src/http_client.rs b/desktop/src-tauri/src/http_client.rs new file mode 100644 index 0000000000..b22f9d60c0 --- /dev/null +++ b/desktop/src-tauri/src/http_client.rs @@ -0,0 +1,87 @@ +//! HTTP clients with separate decompression contracts for relay JSON and media. + +pub fn build() -> reqwest::Result { + base().build() +} + +/// Build the media client without redirects or transparent decoding. +/// +/// Media callers forward upstream Content-Length/Content-Range. Decoding would +/// change the body while stripping or invalidating those video-seeking headers. +pub fn build_media() -> reqwest::Result { + base() + .no_gzip() + .redirect(reqwest::redirect::Policy::none()) + .build() +} + +fn base() -> reqwest::ClientBuilder { + reqwest::Client::builder() + .resolve("localhost", std::net::SocketAddr::from(([127, 0, 0, 1], 0))) + .pool_idle_timeout(std::time::Duration::from_secs(10)) + .pool_max_idle_per_host(1) +} + +#[cfg(test)] +mod tests { + use axum::{http::HeaderMap, response::Response, routing::get, Router}; + + const GZIP_BODY: &[u8] = &[ + 31, 139, 8, 0, 0, 0, 0, 0, 2, 255, 43, 72, 76, 207, 204, 75, 44, 201, 204, 207, 83, 40, 72, + 172, 204, 201, 79, 76, 1, 0, 132, 249, 73, 160, 18, 0, 0, 0, + ]; + + async fn serve(app: Router) -> (std::net::SocketAddr, tokio::task::JoinHandle<()>) { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + (address, server) + } + + #[tokio::test] + async fn app_client_transparently_decodes_gzip() { + let app = Router::new().route( + "/", + get(|headers: HeaderMap| async move { + assert!(headers + .get("accept-encoding") + .and_then(|value| value.to_str().ok()) + .is_some_and(|value| value.split(',').any(|coding| coding.trim() == "gzip"))); + Response::builder() + .header("content-encoding", "gzip") + .header("content-length", GZIP_BODY.len()) + .body(axum::body::Body::from(GZIP_BODY)) + .unwrap() + }), + ); + let (address, server) = serve(app).await; + let response = super::build() + .unwrap() + .get(format!("http://{address}/")) + .send() + .await + .unwrap(); + assert_eq!(response.text().await.unwrap(), "pagination payload"); + server.abort(); + } + + #[tokio::test] + async fn media_client_does_not_advertise_gzip() { + let app = Router::new().route( + "/", + get(|headers: HeaderMap| async move { + assert!(headers.get("accept-encoding").is_none()); + "range bytes" + }), + ); + let (address, server) = serve(app).await; + let response = super::build_media() + .unwrap() + .get(format!("http://{address}/")) + .send() + .await + .unwrap(); + assert_eq!(response.text().await.unwrap(), "range bytes"); + server.abort(); + } +} diff --git a/desktop/src-tauri/src/lib.rs b/desktop/src-tauri/src/lib.rs index 71a5eb3806..f8041ec38b 100644 --- a/desktop/src-tauri/src/lib.rs +++ b/desktop/src-tauri/src/lib.rs @@ -8,6 +8,7 @@ mod deep_link; mod egress_guard; mod event_sync; mod events; +mod http_client; mod huddle; mod identity_storage; mod initial_window; @@ -354,10 +355,9 @@ pub fn run() { tauri::async_runtime::spawn(crate::mesh_llm::start_coordinator(app_handle.clone())); } - // Start the localhost media streaming proxy. Uses the shared HTTP - // client so VPN tunnelling applies. The port is stored in AppState - // and exposed to the frontend via the `get_media_proxy_port` command. - let proxy_client = state.http_client.clone(); + // Use the no-redirect, no-transparent-decompression media client: + // the proxy forwards range metadata verbatim for video seeking. + let proxy_client = state.media_fetch_client.clone(); let proxy_handle = app_handle.clone(); tauri::async_runtime::spawn(async move { let port = media_proxy::spawn_media_proxy(proxy_client, proxy_handle.clone()).await; diff --git a/desktop/src-tauri/src/media_proxy.rs b/desktop/src-tauri/src/media_proxy.rs index 21692ce523..58357533fb 100644 --- a/desktop/src-tauri/src/media_proxy.rs +++ b/desktop/src-tauri/src/media_proxy.rs @@ -181,7 +181,7 @@ pub async fn handle_buzz_media( // Forward Range header if present — enables video seeking through the proxy. let mut upstream = state - .http_client + .media_fetch_client .get(&upstream_url) .timeout(std::time::Duration::from_secs(60));