2021-04-04 12:30:31 +00:00
|
|
|
use std::env;
|
|
|
|
|
2022-02-26 23:34:57 +00:00
|
|
|
use std::path::PathBuf;
|
2022-11-04 10:37:10 +00:00
|
|
|
use std::str::FromStr;
|
2022-09-30 12:38:33 +00:00
|
|
|
use time::ext::NumericalDuration;
|
2022-02-27 00:50:29 +00:00
|
|
|
use time::Duration;
|
2022-02-26 23:34:57 +00:00
|
|
|
use tokio::fs;
|
2021-04-04 12:30:31 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Config {
|
2021-12-08 17:54:55 +00:00
|
|
|
pub static_dir: PathBuf,
|
2021-04-04 12:30:31 +00:00
|
|
|
pub files_dir: PathBuf,
|
|
|
|
pub max_file_size: Option<u64>,
|
2021-04-07 22:03:02 +00:00
|
|
|
pub no_auth_limits: Option<NoAuthLimits>,
|
2022-08-21 16:44:12 +00:00
|
|
|
pub enable_rate_limit: bool,
|
|
|
|
pub proxied: bool,
|
2022-10-15 12:31:54 +00:00
|
|
|
pub rate_limit_replenish_seconds: u64,
|
2022-08-21 16:44:12 +00:00
|
|
|
pub rate_limit_burst: u32,
|
2021-04-07 22:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct NoAuthLimits {
|
|
|
|
pub auth_password: String,
|
2021-08-18 21:22:50 +00:00
|
|
|
pub max_time: Duration,
|
|
|
|
pub large_file_max_time: Duration,
|
2021-04-07 22:03:02 +00:00
|
|
|
pub large_file_size: u64,
|
2021-04-04 12:30:31 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 10:37:10 +00:00
|
|
|
pub async fn from_env() -> Config {
|
2021-04-04 12:30:31 +00:00
|
|
|
let max_file_size = env::var("UPLOAD_MAX_BYTES")
|
|
|
|
.ok()
|
|
|
|
.and_then(|variable| variable.parse().ok())
|
2022-09-30 12:38:33 +00:00
|
|
|
.or(Some(8 * 1024 * 1024))
|
|
|
|
.filter(|&max_file_size| max_file_size != 0);
|
2021-04-04 12:30:31 +00:00
|
|
|
|
2021-12-08 17:54:55 +00:00
|
|
|
let static_dir =
|
|
|
|
PathBuf::from(env::var("STATIC_DIR").unwrap_or_else(|_| "./static".to_owned()));
|
2021-04-04 12:30:31 +00:00
|
|
|
let files_dir = PathBuf::from(env::var("FILES_DIR").unwrap_or_else(|_| "./files".to_owned()));
|
|
|
|
fs::create_dir_all(&files_dir)
|
|
|
|
.await
|
|
|
|
.expect("could not create directory for storing files");
|
|
|
|
|
2021-08-18 21:22:50 +00:00
|
|
|
let no_auth_limits = get_no_auth_limits();
|
|
|
|
|
2022-10-15 12:31:54 +00:00
|
|
|
// default to 480requests/8h
|
2022-08-21 16:44:12 +00:00
|
|
|
let enable_rate_limit = matches!(env::var("RATE_LIMIT").as_deref(), Ok("true") | Err(_));
|
2022-10-15 12:31:54 +00:00
|
|
|
let proxied = env::var("RATE_LIMIT_PROXIED").as_deref() == Ok("true");
|
|
|
|
let rate_limit_replenish_seconds = env::var("RATE_LIMIT_REPLENISH_SECONDS")
|
2022-08-21 16:44:12 +00:00
|
|
|
.ok()
|
|
|
|
.and_then(|rate_limit| rate_limit.parse().ok())
|
|
|
|
.unwrap_or(60);
|
|
|
|
let rate_limit_burst = env::var("RATE_LIMIT_BURST")
|
|
|
|
.ok()
|
2022-10-15 12:31:54 +00:00
|
|
|
.and_then(|burst| burst.parse().ok())
|
|
|
|
.unwrap_or(480);
|
2022-08-21 16:44:12 +00:00
|
|
|
|
2021-08-18 21:22:50 +00:00
|
|
|
Config {
|
2021-12-08 17:54:55 +00:00
|
|
|
static_dir,
|
2021-08-18 21:22:50 +00:00
|
|
|
files_dir,
|
|
|
|
max_file_size,
|
|
|
|
no_auth_limits,
|
2022-08-21 16:44:12 +00:00
|
|
|
enable_rate_limit,
|
|
|
|
proxied,
|
2022-10-15 12:31:54 +00:00
|
|
|
rate_limit_replenish_seconds,
|
2022-08-21 16:44:12 +00:00
|
|
|
rate_limit_burst,
|
2021-08-18 21:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_no_auth_limits() -> Option<NoAuthLimits> {
|
|
|
|
match (
|
2021-04-07 22:03:02 +00:00
|
|
|
env::var("AUTH_PASSWORD").ok(),
|
2022-11-04 10:37:10 +00:00
|
|
|
env_number::<i64>("NO_AUTH_MAX_TIME"),
|
|
|
|
env_number::<i64>("NO_AUTH_LARGE_FILE_MAX_TIME"),
|
2021-04-07 22:03:02 +00:00
|
|
|
env_number("NO_AUTH_LARGE_FILE_SIZE"),
|
|
|
|
) {
|
|
|
|
(Some(auth_password), Some(max_time), Some(large_file_max_time), Some(large_file_size)) => {
|
|
|
|
Some(NoAuthLimits {
|
|
|
|
auth_password,
|
2022-11-04 10:37:10 +00:00
|
|
|
max_time: max_time.seconds(),
|
|
|
|
large_file_max_time: large_file_max_time.seconds(),
|
2021-04-07 22:03:02 +00:00
|
|
|
large_file_size,
|
|
|
|
})
|
|
|
|
}
|
2021-08-18 21:22:50 +00:00
|
|
|
(None, None, None, None) => None,
|
2021-12-08 17:54:55 +00:00
|
|
|
_ => {
|
|
|
|
panic!("Incomplete NO_AUTH configuration: All environment variables must be specified")
|
|
|
|
}
|
2021-04-04 12:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 22:03:02 +00:00
|
|
|
|
2022-11-04 10:37:10 +00:00
|
|
|
fn env_number<T: FromStr>(variable: &str) -> Option<T> {
|
|
|
|
env::var(variable).ok().and_then(|n| n.parse::<T>().ok())
|
2021-04-07 22:03:02 +00:00
|
|
|
}
|