Minor stylistic changes
This commit is contained in:
parent
bfe1c3db57
commit
9850109326
@ -20,12 +20,12 @@ use crate::server::RequestExt;
|
||||
|
||||
const REDDIT_URL_BASE: &str = "https://oauth.reddit.com";
|
||||
|
||||
pub(crate) static CLIENT: Lazy<Client<HttpsConnector<HttpConnector>>> = Lazy::new(|| {
|
||||
pub static CLIENT: Lazy<Client<HttpsConnector<HttpConnector>>> = Lazy::new(|| {
|
||||
let https = hyper_rustls::HttpsConnectorBuilder::new().with_native_roots().https_only().enable_http1().build();
|
||||
client::Client::builder().build(https)
|
||||
});
|
||||
|
||||
pub(crate) static OAUTH_CLIENT: Lazy<RwLock<Oauth>> = Lazy::new(|| {
|
||||
pub static OAUTH_CLIENT: Lazy<RwLock<Oauth>> = Lazy::new(|| {
|
||||
let client = block_on(Oauth::new());
|
||||
tokio::spawn(token_daemon());
|
||||
RwLock::new(client)
|
||||
|
@ -7,11 +7,11 @@ use std::{env::var, fs::read_to_string};
|
||||
//
|
||||
// This is the local static that is initialized at runtime (technically at
|
||||
// first request) and contains the instance settings.
|
||||
pub(crate) static CONFIG: Lazy<Config> = Lazy::new(Config::load);
|
||||
pub static CONFIG: Lazy<Config> = Lazy::new(Config::load);
|
||||
|
||||
// This serves as the frontend for the Pushshift API - on removed comments, this URL will
|
||||
// be the base of a link, to display removed content (on another site).
|
||||
pub(crate) const DEFAULT_PUSHSHIFT_FRONTEND: &str = "www.unddit.com";
|
||||
pub const DEFAULT_PUSHSHIFT_FRONTEND: &str = "www.unddit.com";
|
||||
|
||||
/// Stores the configuration parsed from the environment variables and the
|
||||
/// config file. `Config::Default()` contains None for each setting.
|
||||
@ -104,7 +104,7 @@ impl Config {
|
||||
pub fn load() -> Self {
|
||||
let load_config = |name: &str| {
|
||||
let new_file = read_to_string(name);
|
||||
new_file.ok().and_then(|new_file| toml::from_str::<Config>(&new_file).ok())
|
||||
new_file.ok().and_then(|new_file| toml::from_str::<Self>(&new_file).ok())
|
||||
};
|
||||
|
||||
let config = load_config("redlib.toml").or(load_config("libreddit.toml")).unwrap_or_default();
|
||||
@ -168,7 +168,7 @@ fn get_setting_from_config(name: &str, config: &Config) -> Option<String> {
|
||||
}
|
||||
|
||||
/// Retrieves setting from environment variable or config file.
|
||||
pub(crate) fn get_setting(name: &str) -> Option<String> {
|
||||
pub fn get_setting(name: &str) -> Option<String> {
|
||||
get_setting_from_config(name, &CONFIG)
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@ use time::OffsetDateTime;
|
||||
// This is the local static that is intialized at runtime (technically at
|
||||
// the first request to the info endpoint) and contains the data
|
||||
// retrieved from the info endpoint.
|
||||
pub(crate) static INSTANCE_INFO: Lazy<InstanceInfo> = Lazy::new(InstanceInfo::new);
|
||||
pub static INSTANCE_INFO: Lazy<InstanceInfo> = Lazy::new(InstanceInfo::new);
|
||||
|
||||
/// Handles instance info endpoint
|
||||
pub async fn instance_info(req: Request<Body>) -> Result<Response<Body>, String> {
|
||||
@ -84,7 +84,7 @@ fn info_html(req: Request<Body>) -> Result<Response<Body>, Error> {
|
||||
Response::builder().status(200).header("content-type", "text/html; charset=utf8").body(Body::from(message))
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
pub(crate) struct InstanceInfo {
|
||||
pub struct InstanceInfo {
|
||||
package_name: String,
|
||||
crate_version: String,
|
||||
git_commit: String,
|
||||
|
25
src/oauth.rs
25
src/oauth.rs
@ -13,24 +13,24 @@ static REDDIT_IOS_OAUTH_CLIENT_ID: &str = "LNDo9k1o8UAEUw";
|
||||
static AUTH_ENDPOINT: &str = "https://accounts.reddit.com";
|
||||
|
||||
// Various Android user agents - build numbers from valid APK variants
|
||||
pub(crate) static ANDROID_USER_AGENT: [&str; 3] = [
|
||||
pub static ANDROID_USER_AGENT: [&str; 3] = [
|
||||
"Reddit/Version 2023.21.0/Build 956283/Android 13",
|
||||
"Reddit/Version 2023.21.0/Build 968223/Android 10",
|
||||
"Reddit/Version 2023.21.0/Build 946732/Android 12",
|
||||
];
|
||||
|
||||
// Various iOS user agents - iOS versions.
|
||||
pub(crate) static IOS_USER_AGENT: [&str; 3] = [
|
||||
pub static IOS_USER_AGENT: [&str; 3] = [
|
||||
"Reddit/Version 2023.22.0/Build 613580/iOS Version 17.0 (Build 21A5248V)",
|
||||
"Reddit/Version 2023.22.0/Build 613580/iOS Version 16.0 (Build 20A5328h)",
|
||||
"Reddit/Version 2023.22.0/Build 613580/iOS Version 16.5",
|
||||
];
|
||||
// Various iOS device codes. iPhone 11 displays as `iPhone12,1`
|
||||
// I just changed the number a few times for some plausible values
|
||||
pub(crate) static IOS_DEVICES: [&str; 5] = ["iPhone8,1", "iPhone11,1", "iPhone12,1", "iPhone13,1", "iPhone14,1"];
|
||||
pub static IOS_DEVICES: [&str; 5] = ["iPhone8,1", "iPhone11,1", "iPhone12,1", "iPhone13,1", "iPhone14,1"];
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub(crate) struct Oauth {
|
||||
pub struct Oauth {
|
||||
// Currently unused, may be necessary if we decide to support GQL in the future
|
||||
pub(crate) headers_map: HashMap<String, String>,
|
||||
pub(crate) token: String,
|
||||
@ -40,7 +40,7 @@ pub(crate) struct Oauth {
|
||||
|
||||
impl Oauth {
|
||||
pub(crate) async fn new() -> Self {
|
||||
let mut oauth = Oauth::default();
|
||||
let mut oauth = Self::default();
|
||||
oauth.login().await;
|
||||
oauth
|
||||
}
|
||||
@ -49,7 +49,7 @@ impl Oauth {
|
||||
let device = Device::random();
|
||||
let headers = device.headers.clone();
|
||||
// For now, just insert headers - no token request
|
||||
Oauth {
|
||||
Self {
|
||||
headers_map: headers,
|
||||
token: String::new(),
|
||||
expires_in: 0,
|
||||
@ -123,7 +123,7 @@ impl Oauth {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn token_daemon() {
|
||||
pub async fn token_daemon() {
|
||||
// Monitor for refreshing token
|
||||
loop {
|
||||
// Get expiry time - be sure to not hold the read lock
|
||||
@ -140,8 +140,7 @@ pub(crate) async fn token_daemon() {
|
||||
|
||||
// Refresh token - in its own scope
|
||||
{
|
||||
let mut client = OAUTH_CLIENT.write().await;
|
||||
client.refresh().await;
|
||||
OAUTH_CLIENT.write().await.refresh().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -168,7 +167,7 @@ impl Device {
|
||||
|
||||
info!("Spoofing Android client with headers: {headers:?}, uuid: \"{uuid}\", and OAuth ID \"{REDDIT_ANDROID_OAUTH_CLIENT_ID}\"");
|
||||
|
||||
Device {
|
||||
Self {
|
||||
oauth_id: REDDIT_ANDROID_OAUTH_CLIENT_ID.to_string(),
|
||||
headers,
|
||||
}
|
||||
@ -194,7 +193,7 @@ impl Device {
|
||||
|
||||
info!("Spoofing iOS client {ios_device} with headers: {headers:?}, uuid: \"{uuid}\", and OAuth ID \"{REDDIT_IOS_OAUTH_CLIENT_ID}\"");
|
||||
|
||||
Device {
|
||||
Self {
|
||||
oauth_id: REDDIT_IOS_OAUTH_CLIENT_ID.to_string(),
|
||||
headers,
|
||||
}
|
||||
@ -202,9 +201,9 @@ impl Device {
|
||||
// Randomly choose a device
|
||||
fn random() -> Self {
|
||||
if fastrand::bool() {
|
||||
Device::android()
|
||||
Self::android()
|
||||
} else {
|
||||
Device::ios()
|
||||
Self::ios()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,11 +47,11 @@ impl CompressionType {
|
||||
/// Returns a `CompressionType` given a content coding
|
||||
/// in [RFC 7231](https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.4)
|
||||
/// format.
|
||||
fn parse(s: &str) -> Option<CompressionType> {
|
||||
fn parse(s: &str) -> Option<Self> {
|
||||
let c = match s {
|
||||
// Compressors we support.
|
||||
"gzip" => CompressionType::Gzip,
|
||||
"br" => CompressionType::Brotli,
|
||||
"gzip" => Self::Gzip,
|
||||
"br" => Self::Brotli,
|
||||
|
||||
// The wildcard means that we can choose whatever
|
||||
// compression we prefer. In this case, use the
|
||||
@ -69,8 +69,8 @@ impl CompressionType {
|
||||
impl ToString for CompressionType {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
CompressionType::Gzip => "gzip".to_string(),
|
||||
CompressionType::Brotli => "br".to_string(),
|
||||
Self::Gzip => "gzip".to_string(),
|
||||
Self::Brotli => "br".to_string(),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
@ -195,7 +195,7 @@ impl Route<'_> {
|
||||
|
||||
impl Server {
|
||||
pub fn new() -> Self {
|
||||
Server {
|
||||
Self {
|
||||
default_headers: HeaderMap::new(),
|
||||
router: Router::new(),
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user