feat(client): additionally randomize headers

This commit is contained in:
Matthew Esposito 2024-11-19 14:54:06 -05:00
parent 18efb8c714
commit 6ecdedd2ed

View File

@ -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. // Build request to Reddit. When making a GET, request gzip compression.
// (Reddit doesn't do brotli yet.) // (Reddit doesn't do brotli yet.)
let builder = Request::builder() let mut headers = vec![
.method(method) ("User-Agent", user_agent),
.uri(&url) ("Client-Vendor-Id", vendor_id),
.header("User-Agent", user_agent) ("X-Reddit-Device-Id", device_id),
.header("Client-Vendor-Id", vendor_id) ("x-reddit-loid", loid),
.header("X-Reddit-Device-Id", device_id) ("Host", host.to_string()),
.header("x-reddit-loid", loid) ("Authorization", format!("Bearer {token}")),
.header("Host", host) ("Accept-Encoding", if method == Method::GET { "gzip".into() } else { "identity".into() }),
.header("Authorization", &format!("Bearer {token}")) (
.header("Accept-Encoding", if method == Method::GET { "gzip" } else { "identity" })
.header(key, value)
.header(
"Cookie", "Cookie",
if quarantine { 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 { } 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 { async move {
match builder { match builder {