Update oauth.rs to use Android client only (fixes #8)

This commit is contained in:
Matthew Esposito 2023-12-30 17:28:19 -05:00
parent d327ab2c95
commit cd836308db
No known key found for this signature in database
4 changed files with 26 additions and 53 deletions

View File

@ -24,7 +24,7 @@ echo "// Please do not edit manually" >> "$filename"
echo "// Filled in with real app versions" >> "$filename" echo "// Filled in with real app versions" >> "$filename"
# Open the array in the source file # Open the array in the source file
echo "pub static IOS_APP_VERSION_LIST: &[&str; $ios_app_count] = &[" >> "$filename" echo "pub static _IOS_APP_VERSION_LIST: &[&str; $ios_app_count] = &[" >> "$filename"
num=0 num=0
@ -89,7 +89,7 @@ ios_count=$(echo "$table" | wc -l)
echo -e "Fetching \e[34m$ios_count iOS versions...\e[0m" echo -e "Fetching \e[34m$ios_count iOS versions...\e[0m"
# Append to the source file # Append to the source file
echo "pub static IOS_OS_VERSION_LIST: &[&str; $ios_count] = &[" >> "$filename" echo "pub static _IOS_OS_VERSION_LIST: &[&str; $ios_count] = &[" >> "$filename"
num=0 num=0

View File

@ -2,7 +2,7 @@ use std::{collections::HashMap, time::Duration};
use crate::{ use crate::{
client::{CLIENT, OAUTH_CLIENT}, client::{CLIENT, OAUTH_CLIENT},
oauth_resources::{ANDROID_APP_VERSION_LIST, IOS_APP_VERSION_LIST, IOS_OS_VERSION_LIST}, oauth_resources::ANDROID_APP_VERSION_LIST,
}; };
use base64::{engine::general_purpose, Engine as _}; use base64::{engine::general_purpose, Engine as _};
use hyper::{client, Body, Method, Request}; use hyper::{client, Body, Method, Request};
@ -11,11 +11,10 @@ use log::info;
use serde_json::json; use serde_json::json;
static REDDIT_ANDROID_OAUTH_CLIENT_ID: &str = "ohXpoqrZYub1kg"; static REDDIT_ANDROID_OAUTH_CLIENT_ID: &str = "ohXpoqrZYub1kg";
static REDDIT_IOS_OAUTH_CLIENT_ID: &str = "LNDo9k1o8UAEUw";
static AUTH_ENDPOINT: &str = "https://accounts.reddit.com"; static AUTH_ENDPOINT: &str = "https://accounts.reddit.com";
// Spoofed client for Android and iOS devices // Spoofed client for Android devices
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct Oauth { pub struct Oauth {
pub(crate) initial_headers: HashMap<String, String>, pub(crate) initial_headers: HashMap<String, String>,
@ -32,8 +31,8 @@ impl Oauth {
oauth oauth
} }
pub(crate) fn default() -> Self { pub(crate) fn default() -> Self {
// Generate a random device to spoof // Generate a device to spoof
let device = Device::random(); let device = Device::new();
let headers_map = device.headers.clone(); let headers_map = device.headers.clone();
let initial_headers = device.initial_headers.clone(); let initial_headers = device.initial_headers.clone();
// For now, just insert headers - no token request // For now, just insert headers - no token request
@ -163,49 +162,9 @@ impl Device {
initial_headers: headers, initial_headers: headers,
} }
} }
fn ios() -> Self { fn new() -> Self {
// Generate uuid // See https://github.com/redlib-org/redlib/issues/8
let uuid = uuid::Uuid::new_v4().to_string(); Self::android()
// Generate random user-agent
let ios_app_version = choose(IOS_APP_VERSION_LIST).to_string();
let ios_os_version = choose(IOS_OS_VERSION_LIST).to_string();
let ios_user_agent = format!("Reddit/{ios_app_version}/iOS {ios_os_version}");
// Generate random device
let ios_device_num = fastrand::u8(8..=15).to_string();
let ios_device = format!("iPhone{ios_device_num},1").to_string();
let initial_headers = HashMap::from([
("X-Reddit-DPR".into(), "2".into()),
("User-Agent".into(), ios_user_agent.clone()),
("Device-Name".into(), ios_device.clone()),
]);
let headers = HashMap::from([
("X-Reddit-DPR".into(), "2".into()),
("Device-Name".into(), ios_device.clone()),
("User-Agent".into(), ios_user_agent),
("Client-Vendor-Id".into(), uuid.clone()),
("x-dev-ad-id".into(), "00000000-0000-0000-0000-000000000000".into()),
("Reddit-User_Id".into(), "anonymous_browsing_mode".into()),
("x-reddit-device-id".into(), uuid.clone()),
]);
info!("[🔄] Spoofing iOS client {ios_device} with headers: {headers:?}, uuid: \"{uuid}\", and OAuth ID \"{REDDIT_IOS_OAUTH_CLIENT_ID}\"");
Self {
oauth_id: REDDIT_IOS_OAUTH_CLIENT_ID.to_string(),
initial_headers,
headers,
}
}
// Randomly choose a device
fn random() -> Self {
if fastrand::bool() {
Self::android()
} else {
Self::ios()
}
} }
} }
@ -234,5 +193,5 @@ async fn test_oauth_headers_len() {
#[test] #[test]
fn test_creating_device() { fn test_creating_device() {
Device::random(); Device::new();
} }

View File

@ -2,7 +2,7 @@
// Rerun scripts/update_oauth_resources.sh to update this file // Rerun scripts/update_oauth_resources.sh to update this file
// Please do not edit manually // Please do not edit manually
// Filled in with real app versions // Filled in with real app versions
pub static IOS_APP_VERSION_LIST: &[&str; 67] = &[ pub static _IOS_APP_VERSION_LIST: &[&str; 67] = &[
"Version 2020.0.0/Build 306960", "Version 2020.0.0/Build 306960",
"Version 2020.10.0/Build 307041", "Version 2020.10.0/Build 307041",
"Version 2020.10.1/Build 307047", "Version 2020.10.1/Build 307047",
@ -223,7 +223,7 @@ pub static ANDROID_APP_VERSION_LIST: &[&str; 150] = &[
"Version 2021.43.0/Build 382019", "Version 2021.43.0/Build 382019",
"Version 2021.44.0/Build 385129", "Version 2021.44.0/Build 385129",
]; ];
pub static IOS_OS_VERSION_LIST: &[&str; 8] = &[ pub static _IOS_OS_VERSION_LIST: &[&str; 8] = &[
"Version 17.0.1 (Build 21A340)", "Version 17.0.1 (Build 21A340)",
"Version 17.0.2 (Build 21A350)", "Version 17.0.2 (Build 21A350)",
"Version 17.0.3 (Build 21A360)", "Version 17.0.3 (Build 21A360)",

View File

@ -1108,3 +1108,17 @@ mod tests {
assert_eq!(format_url("spoiler"), ""); assert_eq!(format_url("spoiler"), "");
} }
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fetching_subreddit_quarantined() {
let subreddit = Post::fetch("/r/drugs", true).await;
assert!(subreddit.is_ok());
assert!(!subreddit.unwrap().0.is_empty());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_fetching_nsfw_subreddit() {
let subreddit = Post::fetch("/r/randnsfw", false).await;
assert!(subreddit.is_ok());
assert!(!subreddit.unwrap().0.is_empty());
}