From 6ecdedd2ede7fa72a6651bf51f1d535c9e64ec93 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Tue, 19 Nov 2024 14:54:06 -0500 Subject: [PATCH] feat(client): additionally randomize headers --- src/client.rs | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/client.rs b/src/client.rs index f13e864..98c42fb 100644 --- a/src/client.rs +++ b/src/client.rs @@ -229,34 +229,39 @@ fn request(method: &'static Method, path: String, redirect: bool, quarantine: bo ) }; - let (key, value) = match fastrand::u32(0..3) { - 0 => ("X-Reddit-Width", fastrand::u32(300..500).to_string()), - 1 => ("X-Reddit-DPR", "2".to_owned()), - _ => ("Device-Name", format!("Android {}", fastrand::u8(9..=14))), - }; - // Build request to Reddit. When making a GET, request gzip compression. // (Reddit doesn't do brotli yet.) - let builder = Request::builder() - .method(method) - .uri(&url) - .header("User-Agent", user_agent) - .header("Client-Vendor-Id", vendor_id) - .header("X-Reddit-Device-Id", device_id) - .header("x-reddit-loid", loid) - .header("Host", host) - .header("Authorization", &format!("Bearer {token}")) - .header("Accept-Encoding", if method == Method::GET { "gzip" } else { "identity" }) - .header(key, value) - .header( + let mut headers = vec![ + ("User-Agent", user_agent), + ("Client-Vendor-Id", vendor_id), + ("X-Reddit-Device-Id", device_id), + ("x-reddit-loid", loid), + ("Host", host.to_string()), + ("Authorization", format!("Bearer {token}")), + ("Accept-Encoding", if method == Method::GET { "gzip".into() } else { "identity".into() }), + ( "Cookie", if quarantine { - "_options=%7B%22pref_quarantine_optin%22%3A%20true%2C%20%22pref_gated_sr_optin%22%3A%20true%7D" + "_options=%7B%22pref_quarantine_optin%22%3A%20true%2C%20%22pref_gated_sr_optin%22%3A%20true%7D".into() } else { - "" + "".into() }, - ) - .body(Body::empty()); + ), + ("X-Reddit-Width", fastrand::u32(300..500).to_string()), + ("X-Reddit-DPR", "2".to_owned()), + ("Device-Name", format!("Android {}", fastrand::u8(9..=14))), + ]; + + // shuffle headers: https://github.com/redlib-org/redlib/issues/324 + fastrand::shuffle(&mut headers); + + let mut builder = Request::builder().method(method).uri(&url); + + for (key, value) in headers { + builder = builder.header(key, value); + } + + let builder = builder.body(Body::empty()); async move { match builder {