//! Audio proxy: Range pass-through, servers without ranges, redirects, //! content types and limits, against the local test server. use std::time::Duration; use futures_util::StreamExt; use futures_util::stream::BoxStream; use super::net::{AddressClass, GuardError}; use super::proxy::{AudioProxy, ProxyError, sanitize_range, sniff_audio}; use std::sync::Arc; use super::test_server::{Route, ScriptedLookup, TestServer, noise, test_http}; // --------------------------------------------------------------------------- // Audio proxy // --------------------------------------------------------------------------- #[test] fn only_single_byte_ranges_are_forwarded() { assert_eq!(sanitize_range("bytes=0-").as_deref(), Some("bytes=0-")); assert_eq!( sanitize_range("bytes=100-199").as_deref(), Some("bytes=100-199") ); assert_eq!( sanitize_range(" bytes=-500 ").as_deref(), Some("bytes=-500") ); for invalid in [ "bytes=1-2,5-6", "items=0-10", "bytes=5-1", "bytes=-", "bytes=-0", "bytes=a-b", "bytes=1-2\r\nCookie: x", "bytes=99999999999999999999999-", "", ] { assert_eq!(sanitize_range(invalid), None, "{invalid:?}"); } } async fn collect( mut body: BoxStream<'static, Result>, ) -> (Vec, Option) { let mut out = Vec::new(); while let Some(chunk) = body.next().await { match chunk { Ok(chunk) => out.extend_from_slice(&chunk), Err(error) => return (out, Some(error.to_string())), } } (out, None) } fn proxy(lookup: Arc) -> AudioProxy { AudioProxy::with_http(test_http(lookup)) } #[tokio::test] async fn proxy_passes_ranges_through() { let server = TestServer::start(); let audio = noise(300_000, 7); server.set("/ep.mp3", Route::new(audio.clone(), "audio/mpeg")); let proxy = proxy(ScriptedLookup::with(&[])); let target = server.url("/ep.mp3"); let full = proxy.fetch(&target, None).await.unwrap(); assert_eq!(full.status, 200); assert_eq!(full.content_type.as_deref(), Some("audio/mpeg")); assert_eq!(full.content_length, Some(audio.len() as u64)); assert!(full.accept_ranges); assert_eq!(full.content_range, None); assert_eq!(collect(full.body).await, (audio.clone(), None)); for (range, start, end) in [ ("bytes=1000-1999", 1000, 2000), ("bytes=299000-", 299_000, 300_000), ("bytes=-500", 299_500, 300_000), ("bytes=0-", 0, 300_000), ] { let part = proxy.fetch(&target, Some(range)).await.unwrap(); assert_eq!(part.status, 206, "{range}"); assert_eq!( part.content_range, Some(format!("bytes {start}-{}/{}", end - 1, audio.len())), "{range}" ); assert_eq!(part.content_length, Some((end - start) as u64)); assert_eq!(collect(part.body).await, (audio[start..end].to_vec(), None)); } let unsatisfiable = proxy.fetch(&target, Some("bytes=400000-")).await.unwrap(); assert_eq!(unsatisfiable.status, 416); assert_eq!( unsatisfiable.content_range, Some(format!("bytes */{}", audio.len())) ); assert_eq!(collect(unsatisfiable.body).await, (Vec::new(), None)); // Malformed or multi-range requests fetch the whole file. let whole = proxy.fetch(&target, Some("bytes=5-6,9-10")).await.unwrap(); assert_eq!(whole.status, 200); let requests = server.requests("/ep.mp3"); let ranges: Vec> = requests .iter() .map(|request| request.get("range").map(String::as_str)) .collect(); assert_eq!( ranges, vec![ None, Some("bytes=1000-1999"), Some("bytes=299000-"), Some("bytes=-500"), Some("bytes=0-"), Some("bytes=400000-"), None, ] ); for request in &requests { assert_eq!(request["accept-encoding"], "identity"); assert!(!request.contains_key("cookie")); assert!(!request.contains_key("authorization")); } } #[tokio::test] async fn proxy_handles_servers_without_ranges_and_redirects() { let server = TestServer::start(); let audio = noise(50_000, 3); server.set( "/plain.mp3", Route::new(audio.clone(), "audio/mpeg").no_ranges(), ); server.set("/track/ep.mp3", Route::redirect(302, "/cdn/ep.mp3")); server.set("/cdn/ep.mp3", Route::new(audio.clone(), "audio/mpeg")); server.set( "/track/private.mp3", Route::redirect(302, "http://172.20.0.3/ep.mp3"), ); let proxy = proxy(ScriptedLookup::with(&[])); // No range support: the whole file with 200 and no Accept-Ranges, so the // browser knows it cannot seek by range. let plain = proxy .fetch(&server.url("/plain.mp3"), Some("bytes=100-")) .await .unwrap(); assert_eq!(plain.status, 200); assert!(!plain.accept_ranges); assert_eq!(plain.content_range, None); assert_eq!(collect(plain.body).await.0, audio); // Tracking redirects keep the range on every hop. let part = proxy .fetch(&server.url("/track/ep.mp3"), Some("bytes=10-19")) .await .unwrap(); assert_eq!(part.status, 206); assert_eq!(collect(part.body).await.0, audio[10..20].to_vec()); assert_eq!(server.requests("/track/ep.mp3")[0]["range"], "bytes=10-19"); assert_eq!(server.requests("/cdn/ep.mp3")[0]["range"], "bytes=10-19"); match proxy.fetch(&server.url("/track/private.mp3"), None).await { Err(ProxyError::Upstream(GuardError::Blocked(blocked))) => { assert_eq!(blocked.class, AddressClass::Private) } other => panic!("{other:?}"), } } /// `noise` that starts like a file of the given format. fn audio_file(magic: &[u8], len: usize) -> Vec { let mut file = magic.to_vec(); file.extend(noise(len - magic.len(), 3)); file } #[tokio::test] async fn proxy_only_serves_audio() { let server = TestServer::start(); let audio = noise(1000, 5); let mp3 = audio_file(b"ID3\x04\0\0", 1000); server.set( "/page.mp3", Route::new("", "text/html"), ); server.set("/video.mp4", Route::new(audio.clone(), "video/mp4")); // Generic types: the URL extension, or else the file's first bytes. server.set( "/binary", Route::new(audio.clone(), "application/octet-stream"), ); server.set( "/zip", Route::new(audio_file(b"PK\x03\x04", 1000), "application/octet-stream"), ); server.set( "/episode", Route::new(mp3.clone(), "application/octet-stream"), ); server.set("/untyped", Route::new(mp3.clone(), "x").no_content_type()); server.set( "/binary.m4a", Route::new(audio.clone(), "binary/octet-stream"), ); server.set( "/untyped.ogg", Route::new(audio.clone(), "x").no_content_type(), ); server.set("/ogg", Route::new(audio.clone(), "application/ogg")); server.set( "/typed", Route::new(audio.clone(), "audio/mp4; codecs=\"mp4a.40.2\""), ); server.set("/missing", Route::new("", "text/plain").status(404)); server.set("/error", Route::new("", "audio/mpeg").status(500)); let proxy = proxy(ScriptedLookup::with(&[])); let content_type = |path: &'static str, range: Option<&'static str>| { let proxy = proxy.clone(); let target = server.url(path); async move { proxy .fetch(&target, range) .await .map(|audio| audio.content_type.unwrap()) } }; for refused in ["/page.mp3", "/video.mp4", "/binary", "/zip"] { for range in [None, Some("bytes=10-")] { assert!( matches!( content_type(refused, range).await, Err(ProxyError::ContentType(_)) ), "{refused} {range:?}" ); } } assert_eq!(content_type("/episode", None).await.unwrap(), "audio/mpeg"); assert_eq!(content_type("/untyped", None).await.unwrap(), "audio/mpeg"); // The sniffed bytes are still sent to the browser. let whole = proxy.fetch(&server.url("/episode"), None).await.unwrap(); assert_eq!(collect(whole.body).await, (mp3.clone(), None)); // A range past the start: the file's head is read with its own request. let before = server.requests("/episode").len(); let part = proxy .fetch(&server.url("/episode"), Some("bytes=100-199")) .await .unwrap(); assert_eq!(part.content_type.as_deref(), Some("audio/mpeg")); assert_eq!(collect(part.body).await, (mp3[100..200].to_vec(), None)); let ranges: Vec = server.requests("/episode")[before..] .iter() .map(|request| request["range"].clone()) .collect(); assert_eq!(ranges, ["bytes=100-199", "bytes=0-15"]); assert_eq!( content_type("/binary.m4a", None).await.unwrap(), "audio/mp4" ); assert_eq!( content_type("/untyped.ogg", None).await.unwrap(), "audio/ogg" ); assert_eq!(content_type("/ogg", None).await.unwrap(), "application/ogg"); assert_eq!( content_type("/typed", None).await.unwrap(), "audio/mp4; codecs=\"mp4a.40.2\"" ); let missing = content_type("/missing", None).await.unwrap_err(); assert!(matches!(missing, ProxyError::Status(404))); assert_eq!(missing.http_status(), 502); assert!(matches!( content_type("/error", None).await, Err(ProxyError::Status(500)) )); assert!(matches!( proxy.fetch("not a url", None).await, Err(ProxyError::InvalidUrl(_)) )); } #[test] fn audio_formats_are_recognized_by_their_first_bytes() { for (head, format) in [ (&b"ID3\x04\0\0\0\0"[..], "audio/mpeg"), (&[0xFF, 0xFB, 0x90, 0x64][..], "audio/mpeg"), (&[0xFF, 0xF3, 0x48, 0xC4][..], "audio/mpeg"), (&[0xFF, 0xF1, 0x50, 0x80][..], "audio/aac"), (&b"OggS\0\x02"[..], "audio/ogg"), (&b"fLaC\0\0\0\x22"[..], "audio/flac"), (&b"RIFF\x24\x08\0\0WAVEfmt "[..], "audio/wav"), (&b"\0\0\0\x20ftypM4A \0\0\0\0"[..], "audio/mp4"), ] { assert_eq!(sniff_audio(head), Some(format), "{head:?}"); } for head in [ &b"\0\0\0\x20ftypisom\0\0\x02\0"[..], &b"\0\0\0\x18ftypmp42"[..], &b"PK\x03\x04"[..], &b""[..], &b"%PDF-1.7"[..], &b"RIFF\x24\x08\0\0AVI "[..], &[0xFF, 0xD8, 0xFF, 0xE0][..], &[0xFF][..], &[][..], ] { assert_eq!(sniff_audio(head), None, "{head:?}"); } } #[tokio::test] async fn proxy_limits_size_and_reports_dropped_streams() { let server = TestServer::start(); let audio = noise(100_000, 9); server.set("/sized.mp3", Route::new(audio.clone(), "audio/mpeg")); server.set( "/chunked.mp3", Route::new(audio.clone(), "audio/mpeg").no_length(), ); server.set( "/cut.mp3", Route::new(audio.clone(), "audio/mpeg").cut(40_000), ); let limited = proxy(ScriptedLookup::with(&[])).with_limits(60_000, Duration::from_secs(60)); assert!(matches!( limited.fetch(&server.url("/sized.mp3"), None).await, Err(ProxyError::TooLarge(100_000)) )); // A range below the limit is fine. let part = limited .fetch(&server.url("/sized.mp3"), Some("bytes=0-49999")) .await .unwrap(); assert_eq!(collect(part.body).await, (audio[..50_000].to_vec(), None)); // Without a declared length the stream stops at the limit. let chunked = limited .fetch(&server.url("/chunked.mp3"), None) .await .unwrap(); let (received, error) = collect(chunked.body).await; assert!(received.len() <= 60_000); assert!(error.unwrap().contains("size limit")); // A connection dropped mid-body ends the stream with an error instead // of a silently truncated success. let cut = proxy(ScriptedLookup::with(&[])) .fetch(&server.url("/cut.mp3"), None) .await .unwrap(); let (received, error) = collect(cut.body).await; assert!(received.len() <= 40_000); assert!(error.is_some()); } #[tokio::test] async fn proxy_stream_has_a_time_limit() { let server = TestServer::start(); server.set( "/slow.mp3", Route::new(noise(10, 1), "audio/mpeg").delay_body(Duration::from_secs(3)), ); let limited = proxy(ScriptedLookup::with(&[])).with_limits(1 << 20, Duration::from_millis(300)); let audio = limited.fetch(&server.url("/slow.mp3"), None).await.unwrap(); let (_, error) = collect(audio.body).await; assert!(error.unwrap().contains("time limit")); }