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.
// (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 {