2023-06-07 07:05:20 +12:00
|
|
|
use std::{collections::HashMap, time::Duration};
|
2023-06-06 12:31:25 +12:00
|
|
|
|
2023-12-29 09:37:02 +13:00
|
|
|
use crate::{
|
|
|
|
client::{CLIENT, OAUTH_CLIENT},
|
2023-12-31 11:28:19 +13:00
|
|
|
oauth_resources::ANDROID_APP_VERSION_LIST,
|
2023-12-29 09:37:02 +13:00
|
|
|
};
|
2023-06-06 12:31:25 +12:00
|
|
|
use base64::{engine::general_purpose, Engine as _};
|
|
|
|
use hyper::{client, Body, Method, Request};
|
2023-06-07 07:05:20 +12:00
|
|
|
use log::info;
|
2023-06-09 06:33:54 +12:00
|
|
|
|
2023-06-06 12:31:25 +12:00
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
static REDDIT_ANDROID_OAUTH_CLIENT_ID: &str = "ohXpoqrZYub1kg";
|
|
|
|
|
|
|
|
static AUTH_ENDPOINT: &str = "https://accounts.reddit.com";
|
2023-06-07 07:05:20 +12:00
|
|
|
|
2023-12-31 11:28:19 +13:00
|
|
|
// Spoofed client for Android devices
|
2023-06-09 06:33:54 +12:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2023-12-29 06:42:06 +13:00
|
|
|
pub struct Oauth {
|
2023-12-29 09:37:02 +13:00
|
|
|
pub(crate) initial_headers: HashMap<String, String>,
|
2023-06-06 12:31:25 +12:00
|
|
|
pub(crate) headers_map: HashMap<String, String>,
|
|
|
|
pub(crate) token: String,
|
2023-06-07 07:05:20 +12:00
|
|
|
expires_in: u64,
|
|
|
|
device: Device,
|
2023-06-06 12:31:25 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Oauth {
|
2023-06-09 06:33:54 +12:00
|
|
|
pub(crate) async fn new() -> Self {
|
2023-12-29 06:42:06 +13:00
|
|
|
let mut oauth = Self::default();
|
2023-06-09 06:33:54 +12:00
|
|
|
oauth.login().await;
|
|
|
|
oauth
|
|
|
|
}
|
|
|
|
pub(crate) fn default() -> Self {
|
2023-12-31 11:28:19 +13:00
|
|
|
// Generate a device to spoof
|
|
|
|
let device = Device::new();
|
2023-12-29 09:37:02 +13:00
|
|
|
let headers_map = device.headers.clone();
|
|
|
|
let initial_headers = device.initial_headers.clone();
|
2023-06-07 07:05:20 +12:00
|
|
|
// For now, just insert headers - no token request
|
2023-12-29 06:42:06 +13:00
|
|
|
Self {
|
2023-12-29 09:37:02 +13:00
|
|
|
headers_map,
|
|
|
|
initial_headers,
|
2023-06-06 12:31:25 +12:00
|
|
|
token: String::new(),
|
2023-06-07 07:05:20 +12:00
|
|
|
expires_in: 0,
|
|
|
|
device,
|
2023-06-06 12:31:25 +12:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 07:05:20 +12:00
|
|
|
async fn login(&mut self) -> Option<()> {
|
|
|
|
// Construct URL for OAuth token
|
2024-01-20 14:16:17 +13:00
|
|
|
let url = format!("{AUTH_ENDPOINT}/api/access_token");
|
2023-06-06 12:31:25 +12:00
|
|
|
let mut builder = Request::builder().method(Method::POST).uri(&url);
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
// Add headers from spoofed client
|
2024-01-20 14:16:17 +13:00
|
|
|
for (key, value) in &self.initial_headers {
|
2023-12-29 09:37:02 +13:00
|
|
|
builder = builder.header(key, value);
|
2023-06-06 12:31:25 +12:00
|
|
|
}
|
2023-06-07 07:05:20 +12:00
|
|
|
// Set up HTTP Basic Auth - basically just the const OAuth ID's with no password,
|
|
|
|
// Base64-encoded. https://en.wikipedia.org/wiki/Basic_access_authentication
|
|
|
|
// This could be constant, but I don't think it's worth it. OAuth ID's can change
|
|
|
|
// over time and we want to be flexible.
|
|
|
|
let auth = general_purpose::STANDARD.encode(format!("{}:", self.device.oauth_id));
|
2023-06-06 12:31:25 +12:00
|
|
|
builder = builder.header("Authorization", format!("Basic {auth}"));
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
// Set JSON body. I couldn't tell you what this means. But that's what the client sends
|
2023-06-06 12:31:25 +12:00
|
|
|
let json = json!({
|
2023-12-29 09:37:02 +13:00
|
|
|
"scopes": ["*","email"]
|
2023-06-06 12:31:25 +12:00
|
|
|
});
|
|
|
|
let body = Body::from(json.to_string());
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
// Build request
|
2023-06-06 12:31:25 +12:00
|
|
|
let request = builder.body(body).unwrap();
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
// Send request
|
2024-01-20 14:16:17 +13:00
|
|
|
let client: client::Client<_, Body> = CLIENT.clone();
|
2023-06-06 12:31:25 +12:00
|
|
|
let resp = client.request(request).await.ok()?;
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
// Parse headers - loid header _should_ be saved sent on subsequent token refreshes.
|
|
|
|
// Technically it's not needed, but it's easy for Reddit API to check for this.
|
|
|
|
// It's some kind of header that uniquely identifies the device.
|
|
|
|
if let Some(header) = resp.headers().get("x-reddit-loid") {
|
|
|
|
self.headers_map.insert("x-reddit-loid".to_owned(), header.to_str().ok()?.to_string());
|
|
|
|
}
|
|
|
|
|
2023-12-29 09:37:02 +13:00
|
|
|
// Same with x-reddit-session
|
|
|
|
if let Some(header) = resp.headers().get("x-reddit-session") {
|
|
|
|
self.headers_map.insert("x-reddit-session".to_owned(), header.to_str().ok()?.to_string());
|
|
|
|
}
|
|
|
|
|
2023-06-07 07:05:20 +12:00
|
|
|
// Serialize response
|
2023-06-06 12:31:25 +12:00
|
|
|
let body_bytes = hyper::body::to_bytes(resp.into_body()).await.ok()?;
|
|
|
|
let json: serde_json::Value = serde_json::from_slice(&body_bytes).ok()?;
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
// Save token and expiry
|
2023-06-06 12:31:25 +12:00
|
|
|
self.token = json.get("access_token")?.as_str()?.to_string();
|
2023-06-07 07:05:20 +12:00
|
|
|
self.expires_in = json.get("expires_in")?.as_u64()?;
|
2023-06-06 12:31:25 +12:00
|
|
|
self.headers_map.insert("Authorization".to_owned(), format!("Bearer {}", self.token));
|
2023-06-07 07:05:20 +12:00
|
|
|
|
2023-12-29 09:37:02 +13:00
|
|
|
info!("[✅] Success - Retrieved token \"{}...\", expires in {}", &self.token[..32], self.expires_in);
|
2023-06-07 07:05:20 +12:00
|
|
|
|
2023-06-06 12:31:25 +12:00
|
|
|
Some(())
|
|
|
|
}
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
async fn refresh(&mut self) -> Option<()> {
|
|
|
|
// Refresh is actually just a subsequent login with the same headers (without the old token
|
|
|
|
// or anything). This logic is handled in login, so we just call login again.
|
|
|
|
let refresh = self.login().await;
|
|
|
|
info!("Refreshing OAuth token... {}", if refresh.is_some() { "success" } else { "failed" });
|
|
|
|
refresh
|
|
|
|
}
|
|
|
|
}
|
2023-06-09 06:33:54 +12:00
|
|
|
|
2023-12-29 06:42:06 +13:00
|
|
|
pub async fn token_daemon() {
|
2023-06-07 07:05:20 +12:00
|
|
|
// Monitor for refreshing token
|
|
|
|
loop {
|
|
|
|
// Get expiry time - be sure to not hold the read lock
|
2023-06-09 06:33:54 +12:00
|
|
|
let expires_in = { OAUTH_CLIENT.read().await.expires_in };
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
// sleep for the expiry time minus 2 minutes
|
|
|
|
let duration = Duration::from_secs(expires_in - 120);
|
2023-06-09 06:33:54 +12:00
|
|
|
|
2023-12-29 09:37:02 +13:00
|
|
|
info!("[⏳] Waiting for {duration:?} seconds before refreshing OAuth token...");
|
2023-06-09 06:33:54 +12:00
|
|
|
|
2023-06-07 07:05:20 +12:00
|
|
|
tokio::time::sleep(duration).await;
|
|
|
|
|
2023-12-29 09:37:02 +13:00
|
|
|
info!("[⌛] {duration:?} Elapsed! Refreshing OAuth token...");
|
2023-06-07 07:05:20 +12:00
|
|
|
|
|
|
|
// Refresh token - in its own scope
|
|
|
|
{
|
2023-12-29 06:42:06 +13:00
|
|
|
OAUTH_CLIENT.write().await.refresh().await;
|
2023-06-07 07:05:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-28 17:31:21 +13:00
|
|
|
|
|
|
|
pub async fn force_refresh_token() {
|
|
|
|
OAUTH_CLIENT.write().await.refresh().await;
|
|
|
|
}
|
|
|
|
|
2023-06-09 06:33:54 +12:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2023-06-07 07:05:20 +12:00
|
|
|
struct Device {
|
|
|
|
oauth_id: String,
|
2023-12-29 09:37:02 +13:00
|
|
|
initial_headers: HashMap<String, String>,
|
2023-06-07 07:05:20 +12:00
|
|
|
headers: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Device {
|
|
|
|
fn android() -> Self {
|
2023-06-07 07:26:31 +12:00
|
|
|
// Generate uuid
|
2023-06-07 07:05:20 +12:00
|
|
|
let uuid = uuid::Uuid::new_v4().to_string();
|
2023-06-07 07:26:31 +12:00
|
|
|
|
2023-12-29 09:37:02 +13:00
|
|
|
// Generate random user-agent
|
|
|
|
let android_app_version = choose(ANDROID_APP_VERSION_LIST).to_string();
|
|
|
|
let android_version = fastrand::u8(9..=14);
|
|
|
|
|
|
|
|
let android_user_agent = format!("Reddit/{android_app_version}/Android {android_version}");
|
2023-06-07 07:26:31 +12:00
|
|
|
|
|
|
|
// Android device headers
|
2023-06-07 07:05:20 +12:00
|
|
|
let headers = HashMap::from([
|
|
|
|
("Client-Vendor-Id".into(), uuid.clone()),
|
|
|
|
("X-Reddit-Device-Id".into(), uuid.clone()),
|
|
|
|
("User-Agent".into(), android_user_agent),
|
|
|
|
]);
|
2023-06-07 07:26:31 +12:00
|
|
|
|
2023-12-29 09:37:02 +13:00
|
|
|
info!("[🔄] Spoofing Android client with headers: {headers:?}, uuid: \"{uuid}\", and OAuth ID \"{REDDIT_ANDROID_OAUTH_CLIENT_ID}\"");
|
2023-06-07 07:26:31 +12:00
|
|
|
|
2023-12-29 06:42:06 +13:00
|
|
|
Self {
|
2023-06-07 07:05:20 +12:00
|
|
|
oauth_id: REDDIT_ANDROID_OAUTH_CLIENT_ID.to_string(),
|
2023-12-29 09:37:02 +13:00
|
|
|
headers: headers.clone(),
|
|
|
|
initial_headers: headers,
|
2023-06-07 07:05:20 +12:00
|
|
|
}
|
|
|
|
}
|
2023-12-31 11:28:19 +13:00
|
|
|
fn new() -> Self {
|
|
|
|
// See https://github.com/redlib-org/redlib/issues/8
|
|
|
|
Self::android()
|
2023-06-07 07:05:20 +12:00
|
|
|
}
|
2023-06-06 12:31:25 +12:00
|
|
|
}
|
2023-06-07 07:28:36 +12:00
|
|
|
|
2023-06-09 08:27:36 +12:00
|
|
|
fn choose<T: Copy>(list: &[T]) -> T {
|
2023-12-27 17:15:06 +13:00
|
|
|
*fastrand::choose_multiple(list.iter(), 1)[0]
|
2023-06-09 08:27:36 +12:00
|
|
|
}
|
|
|
|
|
2023-12-31 15:33:27 +13:00
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
2023-06-07 07:28:36 +12:00
|
|
|
async fn test_oauth_client() {
|
2023-06-09 06:33:54 +12:00
|
|
|
assert!(!OAUTH_CLIENT.read().await.token.is_empty());
|
2023-06-07 07:28:36 +12:00
|
|
|
}
|
|
|
|
|
2023-12-31 15:33:27 +13:00
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
2023-06-07 07:28:36 +12:00
|
|
|
async fn test_oauth_client_refresh() {
|
|
|
|
OAUTH_CLIENT.write().await.refresh().await.unwrap();
|
|
|
|
}
|
2023-12-31 15:33:27 +13:00
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
2023-12-29 09:37:02 +13:00
|
|
|
async fn test_oauth_token_exists() {
|
|
|
|
assert!(!OAUTH_CLIENT.read().await.token.is_empty());
|
|
|
|
}
|
|
|
|
|
2023-12-31 15:33:27 +13:00
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
2023-12-29 09:37:02 +13:00
|
|
|
async fn test_oauth_headers_len() {
|
|
|
|
assert!(OAUTH_CLIENT.read().await.headers_map.len() >= 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_creating_device() {
|
2023-12-31 11:28:19 +13:00
|
|
|
Device::new();
|
2023-12-29 09:37:02 +13:00
|
|
|
}
|