From a9ba348d178c4f7c662b5740eb701ae4676f8148 Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Tue, 10 Nov 2026 00:15:14 +0100 Subject: [PATCH 1/2] feat: make HTTP/2 a cargo feature --http2 has to be asked for, yet h2 was compiled into every build. It is now the http2 feature, part of the default set, so the release tarballs and cargo install do not change. A build without it hides --http2 from --help and refuses it, instead of quietly running the test over HTTP/1.1. In the shape OpenWrt builds, the binary is 12.5 % smaller that way, 877 KB against 1003 KB with gzip on aarch64, and links 133 crates instead of 140. The OpenWrt package builds with --no-default-features, so it leaves HTTP/2 out. CI tests rustls-tls and native-tls each with and without http2, and the MSRV job checks all four. --- .github/workflows/ci.yml | 8 ++++++++ Cargo.toml | 11 +++++++---- README.md | 12 +++++++++--- src/cli.rs | 4 +++- src/http/mod.rs | 6 ++++++ src/http/tls.rs | 21 +++++++++++++-------- src/speedtest.rs | 4 ++++ tests/integration.rs | 18 ++++++++++++++++-- 8 files changed, 66 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c96e8f..2208714 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,6 +37,9 @@ jobs: - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 - run: cargo build --verbose - run: cargo test --verbose + # The default backend without HTTP/2, which no other job tests. + - if: matrix.os == 'ubuntu-latest' + run: cargo test --no-default-features --features rustls-tls --verbose native-tls: name: Test with the OpenSSL backend @@ -50,6 +53,9 @@ jobs: toolchain: stable - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 - run: cargo test --no-default-features --features native-tls --verbose + # HTTP/2 over this backend is negotiated through OpenSSL's ALPN, and no + # other job builds the two together. + - run: cargo test --no-default-features --features native-tls,http2 --verbose msrv: name: Minimum supported Rust version @@ -72,7 +78,9 @@ jobs: toolchain: ${{ steps.msrv.outputs.version }} - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 - run: cargo check --all-targets + - run: cargo check --all-targets --no-default-features --features rustls-tls - run: cargo check --all-targets --no-default-features --features native-tls + - run: cargo check --all-targets --no-default-features --features native-tls,http2 fuzz: name: Fuzz targets build diff --git a/Cargo.toml b/Cargo.toml index 905ace9..9c46fb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ name = "librespeed-cli" path = "src/main.rs" [features] -default = ["rustls-tls"] +default = ["rustls-tls", "http2"] # rustls with the ring crypto provider. Statically linked, no system TLS # library needed. ring has assembly only for x86, x86_64, aarch64 and arm and # runs portable C elsewhere, which is slow on a core without crypto instructions. @@ -57,6 +57,9 @@ rustls-tls = [ # packaging uses: it depends on the libopenssl package instead of carrying a # TLS library of its own. native-tls = ["dep:hyper-tls", "dep:native-tls", "dep:tokio-native-tls"] +# HTTP/2 behind --http2. Without it the option is refused and h2 is not +# compiled in, which is what a size-constrained build such as OpenWrt's wants. +http2 = ["hyper/http2", "hyper-util/http2", "hyper-rustls?/http2"] [dependencies] anyhow = "1" @@ -68,13 +71,13 @@ futures-util = { version = "0.3", default-features = false, features = ["std"] } http = "1" http-body = "1" http-body-util = "0.1" -hyper = { version = "1", features = ["client", "http1", "http2"] } -hyper-rustls = { version = "0.27", default-features = false, features = ["http1", "http2", "tls12", "ring", "rustls-native-certs"], optional = true } +hyper = { version = "1", features = ["client", "http1"] } +hyper-rustls = { version = "0.27", default-features = false, features = ["http1", "tls12", "ring", "rustls-native-certs"], optional = true } # alpn: without it hyper-tls compiles out its report of the negotiated # protocol, so hyper would write an HTTP/1.1 head onto a connection both ends # agreed to speak h2 on. It forwards to native-tls/alpn, already enabled. hyper-tls = { version = "0.6", features = ["alpn"], optional = true } -hyper-util = { version = "0.1", features = ["client", "client-legacy", "http1", "http2", "tokio"] } +hyper-util = { version = "0.1", features = ["client", "client-legacy", "http1", "tokio"] } libc = "0.2" native-tls = { version = "0.2", features = ["alpn"], optional = true } rand = "0.10" diff --git a/README.md b/README.md index 2f7541d..1c49e0d 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,13 @@ librespeed-cli --fwmark 42 # Linux only | `native-tls` | system OpenSSL | libopenssl | anything OpenSSL builds for | ```sh -cargo build --release --no-default-features --features native-tls +cargo build --release --no-default-features --features native-tls,http2 ``` +HTTP/2 is the `http2` feature, on by default. `--no-default-features` drops it +as well, so name it again if `--http2` is wanted. A build without it is smaller +and refuses `--http2`. + > [!IMPORTANT] > `ring` has assembly only for x86, x86_64, aarch64 and arm. Elsewhere it runs > portable C. On a slow core without crypto instructions that can cost enough @@ -82,8 +86,9 @@ The Go client cannot run on 32-bit PowerPC, because Go targets only `ppc64` and `ppc64le`. Rust reaches that hardware through the Tier 3 target `powerpc-unknown-linux-muslspe`, so this port also covers Turris 1.x. -The OpenWrt package `librespeed-cli-rust` builds the `native-tls` backend and -depends on `libopenssl`. +The OpenWrt package `librespeed-cli-rust` builds the `native-tls` backend with +`--no-default-features` and depends on `libopenssl`. That leaves HTTP/2 out, +as in the Turris build below, while a plain `cargo build` has it. To build for Turris 1.x by hand: @@ -153,6 +158,7 @@ deliberately different. over one TCP connection, so `--concurrent` would stop meaning connections. Go negotiates h2 whenever a server offers it. With `--http2` the flow-control windows are raised to Go's values, 4 MiB per stream and 1 GiB per connection. + A build without the `http2` feature refuses the option. - **Scheme-less server URLs with a port work.** `example.com:8080/backend` becomes an `http://` URL. Go takes `example.com` for the scheme, and `127.0.0.1:8080/` makes it reject the whole list. diff --git a/src/cli.rs b/src/cli.rs index 3c2abfe..336fc3d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -225,7 +225,9 @@ pub struct Cli { /// Allow HTTP/2 when the server offers it. Off by default: HTTP/2 carries /// every stream over one TCP connection, so --concurrent would no longer /// mean concurrent connections, which is what a speed test measures - #[arg(long = "http2")] + // Hidden from --help in a build without the `http2` feature, where it is + // refused with an error that says why. + #[arg(long = "http2", hide = cfg!(not(feature = "http2")))] pub http2: bool, } diff --git a/src/http/mod.rs b/src/http/mod.rs index 0b9bb15..e13cf76 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -66,7 +66,9 @@ pub const MAX_TELEMETRY_RESPONSE: usize = 64 * 1024; /// The protocol default is 64 KiB, which caps a stream at window/RTT: about /// 105 Mbps at 5 ms and 26 Mbps at 20 ms. Leaving it there would silently /// understate any link faster than that. +#[cfg(feature = "http2")] const H2_STREAM_WINDOW: u32 = 4 * 1024 * 1024; +#[cfg(feature = "http2")] const H2_CONNECTION_WINDOW: u32 = 1024 * 1024 * 1024; pub type ReqBody = BoxBody; @@ -190,11 +192,15 @@ pub(crate) fn client_builder( let mut builder = Client::builder(TokioExecutor::new()); builder.pool_max_idle_per_host(concurrent + 2); + #[cfg(feature = "http2")] if http2 { builder .http2_initial_stream_window_size(H2_STREAM_WINDOW) .http2_initial_connection_window_size(H2_CONNECTION_WINDOW); } + // A build without the `http2` feature refuses --http2 before it gets here. + #[cfg(not(feature = "http2"))] + let _ = http2; builder } diff --git a/src/http/tls.rs b/src/http/tls.rs index 0fef333..97123f6 100644 --- a/src/http/tls.rs +++ b/src/http/tls.rs @@ -322,15 +322,20 @@ mod imp { .https_or_http(); // ALPN decides the protocol, so h2 is only reachable when offered here. - Ok(ReportingConnector(if tls.http2 { - builder - .enable_all_versions() - .wrap_connector(BoundConnector::new(bind)) - } else { + #[cfg(feature = "http2")] + if tls.http2 { + return Ok(ReportingConnector( + builder + .enable_all_versions() + .wrap_connector(BoundConnector::new(bind)), + )); + } + + Ok(ReportingConnector( builder .enable_http1() - .wrap_connector(BoundConnector::new(bind)) - })) + .wrap_connector(BoundConnector::new(bind)), + )) } } @@ -370,7 +375,7 @@ mod imp { builder.disable_built_in_roots(true); } - if tls.http2 { + if cfg!(feature = "http2") && tls.http2 { builder.request_alpns(&["h2", "http/1.1"]); } diff --git a/src/speedtest.rs b/src/speedtest.rs index b008141..ac502fd 100644 --- a/src/speedtest.rs +++ b/src/speedtest.rs @@ -59,6 +59,10 @@ pub async fn run(cli: &Cli) -> anyhow::Result<()> { anyhow::bail!("incompatible options 'source' and 'interface'"); } + if cli.http2 && !cfg!(feature = "http2") { + anyhow::bail!("this build has no HTTP/2 support, so --http2 cannot be used"); + } + let delimiter = cli.csv_delimiter_byte()?; // If --csv-header is given, print the header and exit (same behavior as speedtest-cli). diff --git a/tests/integration.rs b/tests/integration.rs index 1bb53b8..67e2e53 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -909,8 +909,22 @@ fn help_carries_the_go_clients_option_texts() { ] { assert!(flat.contains(text), "missing from --help: {text}\n{flat}"); } - // The one option this port adds is documented too. - assert!(flat.contains("--http2"), "{flat}"); + // The one option this port adds is documented too, where it is built in. + assert_eq!(flat.contains("--http2"), cfg!(feature = "http2"), "{flat}"); +} + +// A build without the `http2` feature has no h2 to offer, so it says so +// instead of quietly running the test over HTTP/1.1. +#[cfg(not(feature = "http2"))] +#[test] +fn http2_is_refused_where_it_is_not_built_in() { + let out = run(&["--http2", "--list"]); + assert_eq!(out.status.code(), Some(1)); + let stderr = String::from_utf8_lossy(&out.stderr); + assert!( + stderr.contains("this build has no HTTP/2 support, so --http2 cannot be used"), + "{stderr}" + ); } // The debug log carries the Go client's lines and no others, and names the From 22c6d489494aad8112560450961f65d1fe88325b Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Tue, 10 Nov 2026 11:01:29 +0100 Subject: [PATCH 2/2] test: check the protocol the client offers and speaks over TLS The backend of the integration tests is plain HTTP/1.1, so nothing checked ALPN or h2 against a real handshake. tests/alpn.rs runs the binary against a rustls server in the test process and records what the client offers, what is agreed and the first bytes it sends. Without --http2 neither backend sends ALPN and the client speaks HTTP/1.1. With it the client offers h2 and http/1.1, sends the HTTP/2 preface when h2 is agreed, and falls back to HTTP/1.1 when the server picks http/1.1 or does not use ALPN. rustls is a dev-dependency, so the test also runs with native-tls. The comment in tls.rs said hyper-rustls sets ALPN from enable_http1(). It leaves ALPN empty then. --- .github/workflows/ci.yml | 4 +- Cargo.toml | 2 + src/http/tls.rs | 3 +- tests/alpn.rs | 215 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 221 insertions(+), 3 deletions(-) create mode 100644 tests/alpn.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2208714..7c11b5e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -53,8 +53,8 @@ jobs: toolchain: stable - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 - run: cargo test --no-default-features --features native-tls --verbose - # HTTP/2 over this backend is negotiated through OpenSSL's ALPN, and no - # other job builds the two together. + # With HTTP/2 this backend offers h2 through OpenSSL's ALPN, which + # tests/alpn.rs checks against a TLS server. - run: cargo test --no-default-features --features native-tls,http2 --verbose msrv: diff --git a/Cargo.toml b/Cargo.toml index 9c46fb8..bcbd56e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,5 +108,7 @@ chrono = { version = "0.4", default-features = false, features = ["std", "clock" # dependency tree through url, so this adds no crate, only a direct edge. icu_properties = "2.2" serde_json = "1" +# The TLS server in tests/alpn.rs, so ALPN is checked with either backend. +rustls = { version = "0.23", default-features = false, features = ["ring", "std"] } # Paused clock for the connection racing tests. tokio = { version = "1", features = ["test-util"] } diff --git a/src/http/tls.rs b/src/http/tls.rs index 97123f6..49b39d1 100644 --- a/src/http/tls.rs +++ b/src/http/tls.rs @@ -269,7 +269,8 @@ mod imp { fn client_config(tls: &TlsSettings<'_>) -> anyhow::Result { let provider = Arc::new(rustls::crypto::ring::default_provider()); - // ALPN is left untouched: hyper-rustls sets it from `enable_http1()`. + // ALPN is left to hyper-rustls: `enable_all_versions()` offers h2 and + // http/1.1, `enable_http1()` alone offers nothing. if tls.skip_verify { // Say so rather than ignoring the bundle in silence: a run that // named a CA file and verified nothing looks like a run that diff --git a/tests/alpn.rs b/tests/alpn.rs new file mode 100644 index 0000000..b7a2240 --- /dev/null +++ b/tests/alpn.rs @@ -0,0 +1,215 @@ +//! What the client offers in ALPN and which protocol it then speaks, against a +//! TLS server in this process. The backend in integration.rs is plain HTTP/1.1, +//! so this is where `--http2` and both TLS backends meet a real handshake. + +use std::io::Read; +use std::net::TcpListener; +use std::path::PathBuf; +use std::process::{Command, Stdio}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use rustls::pki_types::pem::PemObject as _; +use rustls::pki_types::{CertificateDer, PrivateKeyDer}; + +const BIN: &str = env!("CARGO_BIN_EXE_librespeed-cli"); + +/// What an HTTP/2 client sends first, before any frame. +const H2_PREFACE: &[u8] = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"; + +/// The self-signed certificate and key the unit tests in src/http/mod.rs use. +/// The client runs with `--skip-cert-verify`. +const CERT: &str = "-----BEGIN CERTIFICATE----- +MIIBmzCCAUGgAwIBAgIUYO6HUq+LfqLyuDs6KRY13lQEHOEwCgYIKoZIzj0EAwIw +FDESMBAGA1UEAwwJbG9jYWxob3N0MCAXDTI2MDkxNTA5MjYxOVoYDzIxMjYwODIy +MDkyNjE5WjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwWTATBgcqhkjOPQIBBggqhkjO +PQMBBwNCAASKZCFHvCS6BEkimY9mqjPZhDcULO3EZD/4gnUj9TooNmtCjXdP4GAK +xDoCb0FzQoHhDoi8BXY4DFLQYpbnX4Nuo28wbTAdBgNVHQ4EFgQUZgrzyhvx3Uvo +bB/Fanc9qOkc2q0wHwYDVR0jBBgwFoAUZgrzyhvx3UvobB/Fanc9qOkc2q0wDwYD +VR0TAQH/BAUwAwEB/zAaBgNVHREEEzARgglsb2NhbGhvc3SHBH8AAAEwCgYIKoZI +zj0EAwIDSAAwRQIhANiLitk3Rlnv12z50HG/whfDtlrRKDWvxniZExYKrm8HAiAy +4RRR3gvO9obcQwMncVNQLZsC6C6J00O0EcR4TET5zA== +-----END CERTIFICATE----- +"; + +const KEY: &str = "-----BEGIN PRIVATE KEY----- +MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgSiYX/YODAAF5OWIE +qQuLN6hmz3baz471Yn8bjYeNL0yhRANCAASKZCFHvCS6BEkimY9mqjPZhDcULO3E +ZD/4gnUj9TooNmtCjXdP4GAKxDoCb0FzQoHhDoi8BXY4DFLQYpbnX4Nu +-----END PRIVATE KEY----- +"; + +/// One connection as the server saw it. +#[derive(Debug)] +struct Seen { + /// The ALPN protocols in the client's hello, in its order. + offered: Vec>, + /// The protocol the server agreed to, if any. + chosen: Option>, + /// The first bytes the client sent once the handshake was done. + first: Vec, +} + +type Log = Arc>>; + +/// Accepts TLS connections, agreeing to the first of `alpn` the client also +/// offers (no ALPN at all when `alpn` is empty), and records each connection +/// that sends something. Nothing is answered: the first bytes are all a test +/// needs. Returns the server's URL and its log. +fn serve(alpn: &[&[u8]]) -> (String, Log) { + let provider = Arc::new(rustls::crypto::ring::default_provider()); + let mut config = rustls::ServerConfig::builder_with_provider(provider) + .with_safe_default_protocol_versions() + .unwrap() + .with_no_client_auth() + .with_single_cert( + vec![CertificateDer::from_pem_slice(CERT.as_bytes()).unwrap()], + PrivateKeyDer::from_pem_slice(KEY.as_bytes()).unwrap(), + ) + .unwrap(); + config.alpn_protocols = alpn.iter().map(|p| p.to_vec()).collect(); + let config = Arc::new(config); + + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("https://{}/", listener.local_addr().unwrap()); + let log = Log::default(); + let seen = log.clone(); + std::thread::spawn(move || { + for tcp in listener.incoming() { + let Ok(mut tcp) = tcp else { continue }; + let _ = tcp.set_read_timeout(Some(Duration::from_secs(10))); + + // The Acceptor shows the client's hello before the handshake goes on. + let mut acceptor = rustls::server::Acceptor::default(); + let accepted = loop { + if !matches!(acceptor.read_tls(&mut tcp), Ok(n) if n > 0) { + break None; + } + match acceptor.accept() { + Ok(Some(accepted)) => break Some(accepted), + Ok(None) => {} + Err(_) => break None, + } + }; + let Some(accepted) = accepted else { continue }; + let offered = accepted + .client_hello() + .alpn() + .map(|protocols| protocols.map(<[u8]>::to_vec).collect()) + .unwrap_or_default(); + let Ok(conn) = accepted.into_connection(config.clone()) else { + continue; + }; + + let mut tls = rustls::StreamOwned::new(conn, tcp); + let mut first = vec![0; H2_PREFACE.len()]; + let mut filled = 0; + while filled < first.len() { + match tls.read(&mut first[filled..]) { + Ok(n) if n > 0 => filled += n, + _ => break, + } + } + if filled == 0 { + continue; + } + first.truncate(filled); + let chosen = tls.conn.alpn_protocol().map(<[u8]>::to_vec); + seen.lock().unwrap().push(Seen { + offered, + chosen, + first, + }); + } + }); + (url, log) +} + +/// Runs the client against `url` until the server has recorded a connection, +/// stops it, and returns what the server saw on that first connection. Later +/// ones may have been cut short by stopping the client. +fn first_connection(url: &str, log: &Log, extra: &[&str]) -> Seen { + let port = url.rsplit(':').next().unwrap().trim_end_matches('/'); + let list: PathBuf = std::env::temp_dir().join(format!("librespeed-cli-alpn-{port}.json")); + let json = format!( + r#"[{{"name":"ALPN","server":"{url}","id":1,"dlURL":"garbage.php","ulURL":"empty.php","pingURL":"empty.php","getIpURL":"getIP.php"}}]"# + ); + std::fs::write(&list, json).unwrap(); + + let mut child = Command::new(BIN) + .arg("--local-json") + .arg(&list) + .args(["--server", "1", "--no-icmp", "--skip-cert-verify"]) + .args(extra) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .expect("run librespeed-cli"); + let deadline = Instant::now() + Duration::from_secs(30); + while log.lock().unwrap().is_empty() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(20)); + } + let _ = child.kill(); + let _ = child.wait(); + let _ = std::fs::remove_file(&list); + + let mut seen = std::mem::take(&mut *log.lock().unwrap()); + assert!(!seen.is_empty(), "the client never sent anything over TLS"); + seen.remove(0) +} + +/// The connection carries an HTTP/1.1 request line, such as `GET /getIP.php`. +fn assert_http1(conn: &Seen) { + assert!( + [&b"GET /"[..], b"POST /"] + .iter() + .any(|method| conn.first.starts_with(method)), + "not an HTTP/1.1 request: {conn:?}" + ); +} + +#[test] +fn without_http2_the_client_offers_and_speaks_only_http1() { + let (url, log) = serve(&[b"h2", b"http/1.1"]); + let conn = first_connection(&url, &log, &[]); + + // Neither backend sends ALPN then, so the server cannot pick h2. + assert!(conn.offered.is_empty(), "{conn:?}"); + assert_eq!(conn.chosen, None, "{conn:?}"); + assert_http1(&conn); +} + +#[cfg(feature = "http2")] +#[test] +fn with_http2_the_client_offers_h2_first_and_speaks_it() { + let (url, log) = serve(&[b"h2", b"http/1.1"]); + let conn = first_connection(&url, &log, &["--http2"]); + + assert_eq!( + conn.offered, + [b"h2".to_vec(), b"http/1.1".to_vec()], + "{conn:?}" + ); + assert_eq!(conn.chosen.as_deref(), Some(&b"h2"[..]), "{conn:?}"); + assert_eq!(conn.first, H2_PREFACE, "{conn:?}"); +} + +#[cfg(feature = "http2")] +#[test] +fn with_http2_a_server_that_picks_http1_gets_http1() { + let (url, log) = serve(&[b"http/1.1"]); + let conn = first_connection(&url, &log, &["--http2"]); + + assert_eq!(conn.chosen.as_deref(), Some(&b"http/1.1"[..]), "{conn:?}"); + assert_http1(&conn); +} + +#[cfg(feature = "http2")] +#[test] +fn with_http2_a_server_without_alpn_gets_http1() { + let (url, log) = serve(&[]); + let conn = first_connection(&url, &log, &["--http2"]); + + assert_eq!(conn.chosen, None, "{conn:?}"); + assert_http1(&conn); +}