Skip to content

Commit

Permalink
feat(client): additionally randomize headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sigaloid committed Nov 19, 2024
1 parent 18efb8c commit 6ecdedd
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6ecdedd

Please sign in to comment.