Skip to content

Commit

Permalink
feat(oauth): better oauth client matching
Browse files Browse the repository at this point in the history
  • Loading branch information
sigaloid committed Nov 21, 2024
1 parent 95ab6c5 commit 6be6f89
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ serde_json_path = "0.7.1"
async-recursion = "1.1.1"
common-words-all = { version = "0.0.2", default-features = false, features = ["english", "one"] }
hyper-rustls = { version = "0.24.2", features = [ "http2" ] }
tegen = "0.1.4"


[dev-dependencies]
Expand Down
36 changes: 13 additions & 23 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,40 +218,30 @@ fn request(method: &'static Method, path: String, redirect: bool, quarantine: bo
// Construct the hyper client from the HTTPS connector.
let client: &Lazy<Client<_, Body>> = &CLIENT;

let (token, vendor_id, device_id, user_agent, loid) = {
let client = OAUTH_CLIENT.load_full();
(
client.token.clone(),
client.headers_map.get("Client-Vendor-Id").cloned().unwrap_or_default(),
client.headers_map.get("X-Reddit-Device-Id").cloned().unwrap_or_default(),
client.headers_map.get("User-Agent").cloned().unwrap_or_default(),
client.headers_map.get("x-reddit-loid").cloned().unwrap_or_default(),
)
};

// Build request to Reddit. When making a GET, request gzip compression.
// (Reddit doesn't do brotli yet.)
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() }),
let mut headers: Vec<(String, String)> = vec![
("Host".into(), host.into()),
("Accept-Encoding".into(), if method == Method::GET { "gzip".into() } else { "identity".into() }),
(
"Cookie",
"Cookie".into(),
if quarantine {
"_options=%7B%22pref_quarantine_optin%22%3A%20true%2C%20%22pref_gated_sr_optin%22%3A%20true%7D".into()
} else {
"".into()
},
),
("X-Reddit-Width", fastrand::u32(300..500).to_string()),
("X-Reddit-DPR", "2".to_owned()),
("Device-Name", format!("Android {}", fastrand::u8(9..=14))),
];

{
let client = OAUTH_CLIENT.load_full();
for (key, value) in client.initial_headers.clone() {
headers.push((key, value));
}
}

trace!("Headers: {:#?}", headers);

// shuffle headers: https://github.com/redlib-org/redlib/issues/324
fastrand::shuffle(&mut headers);

Expand Down
20 changes: 15 additions & 5 deletions src/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::{
use base64::{engine::general_purpose, Engine as _};
use hyper::{client, Body, Method, Request};
use log::{error, info, trace};

use serde_json::json;
use tegen::tegen::TextGenerator;
use tokio::time::{error::Elapsed, timeout};

static REDDIT_ANDROID_OAUTH_CLIENT_ID: &str = "ohXpoqrZYub1kg";
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Oauth {

// Set JSON body. I couldn't tell you what this means. But that's what the client sends
let json = json!({
"scopes": ["*","email"]
"scopes": ["*","email", "pii"]
});
let body = Body::from(json.to_string());

Expand Down Expand Up @@ -185,11 +185,21 @@ impl Device {

let android_user_agent = format!("Reddit/{android_app_version}/Android {android_version}");

let qos = fastrand::u32(1000..=100_000);
let qos: f32 = qos as f32 / 1000.0;
let qos = format!("{:.3}", qos);

let codecs = TextGenerator::new().generate("available-codecs=video/avc, video/hevc{, video/x-vnd.on2.vp9|}");

// Android device headers
let headers = HashMap::from([
("Client-Vendor-Id".into(), uuid.clone()),
("X-Reddit-Device-Id".into(), uuid.clone()),
let headers: HashMap<String, String> = HashMap::from([
("User-Agent".into(), android_user_agent),
("x-reddit-retry".into(), "algo=no-retries".into()),
("x-reddit-compression".into(), "1".into()),
("x-reddit-qos".into(), qos),
("x-reddit-media-codecs".into(), codecs),
("Content-Type".into(), "application/json; charset=UTF-8".into()),
("client-vendor-id".into(), uuid.clone()),
]);

info!("[🔄] Spoofing Android client with headers: {headers:?}, uuid: \"{uuid}\", and OAuth ID \"{REDDIT_ANDROID_OAUTH_CLIENT_ID}\"");
Expand Down
8 changes: 2 additions & 6 deletions src/oauth_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Rerun scripts/update_oauth_resources.sh to update this file
// Please do not edit manually
// Filled in with real app versions
pub static _IOS_APP_VERSION_LIST: &[&str; 1] = &[
"",
];
pub static _IOS_APP_VERSION_LIST: &[&str; 1] = &[""];
pub static ANDROID_APP_VERSION_LIST: &[&str; 150] = &[
"Version 2024.22.1/Build 1652272",
"Version 2024.23.1/Build 1665606",
Expand Down Expand Up @@ -157,6 +155,4 @@ pub static ANDROID_APP_VERSION_LIST: &[&str; 150] = &[
"Version 2022.41.0/Build 630468",
"Version 2022.41.1/Build 634168",
];
pub static _IOS_OS_VERSION_LIST: &[&str; 1] = &[
"",
];
pub static _IOS_OS_VERSION_LIST: &[&str; 1] = &[""];

0 comments on commit 6be6f89

Please sign in to comment.