2021-05-11 18:01:01 -07:00
|
|
|
use std::fmt::{Display, Formatter};
|
|
|
|
use std::num::{NonZeroU16, NonZeroU64};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::sync::atomic::AtomicBool;
|
2021-03-25 18:06:54 -07:00
|
|
|
|
2021-04-17 19:13:36 -07:00
|
|
|
use clap::{crate_authors, crate_description, crate_version, Clap};
|
2021-04-18 20:06:18 -07:00
|
|
|
use url::Url;
|
2021-03-25 18:06:54 -07:00
|
|
|
|
2021-03-25 19:58:07 -07:00
|
|
|
// Validate tokens is an atomic because it's faster than locking on rwlock.
|
|
|
|
pub static VALIDATE_TOKENS: AtomicBool = AtomicBool::new(false);
|
|
|
|
// We use an atomic here because it's better for us to not pass the config
|
|
|
|
// everywhere.
|
|
|
|
pub static SEND_SERVER_VERSION: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
2021-05-22 20:06:05 -07:00
|
|
|
pub static OFFLINE_MODE: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
2021-03-25 21:07:32 -07:00
|
|
|
#[derive(Clap, Clone)]
|
2021-04-17 19:13:36 -07:00
|
|
|
#[clap(version = crate_version!(), author = crate_authors!(), about = crate_description!())]
|
2021-03-25 18:06:54 -07:00
|
|
|
pub struct CliArgs {
|
|
|
|
/// The port to listen on.
|
|
|
|
#[clap(short, long, default_value = "42069", env = "PORT")]
|
|
|
|
pub port: NonZeroU16,
|
|
|
|
/// How large, in bytes, the in-memory cache should be. Note that this does
|
|
|
|
/// not include runtime memory usage.
|
2021-05-11 18:01:01 -07:00
|
|
|
#[clap(long, env = "MEM_CACHE_QUOTA_BYTES", conflicts_with = "low-memory")]
|
|
|
|
pub memory_quota: Option<NonZeroU64>,
|
2021-03-25 18:06:54 -07:00
|
|
|
/// How large, in bytes, the on-disk cache should be. Note that actual
|
|
|
|
/// values may be larger for metadata information.
|
|
|
|
#[clap(long, env = "DISK_CACHE_QUOTA_BYTES")]
|
2021-04-14 19:52:54 -07:00
|
|
|
pub disk_quota: u64,
|
2021-03-25 18:06:54 -07:00
|
|
|
/// Sets the location of the disk cache.
|
|
|
|
#[clap(long, default_value = "./cache", env = "DISK_CACHE_PATH")]
|
|
|
|
pub cache_path: PathBuf,
|
|
|
|
/// The network speed to advertise to Mangadex@Home control server.
|
|
|
|
#[clap(long, env = "MAX_NETWORK_SPEED")]
|
2021-04-14 19:52:54 -07:00
|
|
|
pub network_speed: NonZeroU64,
|
2021-03-25 18:06:54 -07:00
|
|
|
/// Whether or not to provide the Server HTTP header to clients. This is
|
|
|
|
/// useful for debugging, but is generally not recommended for security
|
|
|
|
/// reasons.
|
2021-03-25 19:58:07 -07:00
|
|
|
#[clap(long, env = "ENABLE_SERVER_STRING", takes_value = false)]
|
2021-03-25 18:06:54 -07:00
|
|
|
pub enable_server_string: bool,
|
2021-04-24 20:56:56 -07:00
|
|
|
/// Changes the caching behavior to avoid buffering images in memory, and
|
|
|
|
/// instead use the filesystem as the buffer backing. This is useful for
|
|
|
|
/// clients in low (< 1GB) RAM environments.
|
2021-04-18 14:38:33 -07:00
|
|
|
#[clap(
|
|
|
|
short,
|
|
|
|
long,
|
|
|
|
conflicts_with("memory-quota"),
|
|
|
|
env = "LOW_MEMORY_MODE",
|
|
|
|
takes_value = false
|
|
|
|
)]
|
2021-04-14 19:11:00 -07:00
|
|
|
pub low_memory: bool,
|
2021-04-20 11:12:20 -07:00
|
|
|
/// Changes verbosity. Default verbosity is INFO, while increasing counts of
|
|
|
|
/// verbose flags increases the verbosity to DEBUG and TRACE, respectively.
|
2021-04-17 20:19:27 -07:00
|
|
|
#[clap(short, long, parse(from_occurrences))]
|
|
|
|
pub verbose: usize,
|
2021-04-20 11:12:20 -07:00
|
|
|
/// Changes verbosity. Default verbosity is INFO, while increasing counts of
|
|
|
|
/// quiet flags decreases the verbosity to WARN, ERROR, and no logs,
|
|
|
|
/// respectively.
|
|
|
|
#[clap(short, long, parse(from_occurrences), conflicts_with = "verbose")]
|
|
|
|
pub quiet: usize,
|
2021-04-25 09:55:31 -07:00
|
|
|
#[clap(short = 'Z', long)]
|
|
|
|
pub unstable_options: Vec<UnstableOptions>,
|
2021-04-18 20:06:18 -07:00
|
|
|
#[clap(long)]
|
|
|
|
pub override_upstream: Option<Url>,
|
2021-05-11 18:01:01 -07:00
|
|
|
/// Enables ephemeral disk encryption. Items written to disk are first
|
|
|
|
/// encrypted with a key generated at runtime. There are implications to
|
|
|
|
/// performance, privacy, and usability with this flag enabled.
|
|
|
|
#[clap(short, long)]
|
|
|
|
pub ephemeral_disk_encryption: bool,
|
2021-04-25 09:55:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
pub enum UnstableOptions {
|
|
|
|
/// Overrides the upstream URL to fetch images from. Don't use this unless
|
|
|
|
/// you know what you're dealing with.
|
|
|
|
OverrideUpstream,
|
|
|
|
|
2021-04-24 20:56:56 -07:00
|
|
|
/// Use an LFU implementation for the in-memory cache instead of the default
|
|
|
|
/// LRU implementation.
|
2021-04-25 09:55:31 -07:00
|
|
|
UseLfu,
|
|
|
|
|
|
|
|
/// Disables token validation. Don't use this unless you know the
|
|
|
|
/// ramifications of this command.
|
|
|
|
DisableTokenValidation,
|
2021-05-22 20:06:05 -07:00
|
|
|
|
|
|
|
/// Tries to run without communication to MangaDex.
|
|
|
|
OfflineMode,
|
|
|
|
|
|
|
|
/// Serves HTTP in plaintext
|
|
|
|
DisableTls,
|
2021-04-25 09:55:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for UnstableOptions {
|
2021-05-22 20:06:05 -07:00
|
|
|
type Err = String;
|
2021-04-25 09:55:31 -07:00
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"override-upstream" => Ok(Self::OverrideUpstream),
|
|
|
|
"use-lfu" => Ok(Self::UseLfu),
|
|
|
|
"disable-token-validation" => Ok(Self::DisableTokenValidation),
|
2021-05-22 20:06:05 -07:00
|
|
|
"offline-mode" => Ok(Self::OfflineMode),
|
|
|
|
"disable-tls" => Ok(Self::DisableTls),
|
|
|
|
_ => Err(format!("Unknown unstable option '{}'", s)),
|
2021-04-25 09:55:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for UnstableOptions {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::OverrideUpstream => write!(f, "override-upstream"),
|
|
|
|
Self::UseLfu => write!(f, "use-lfu"),
|
|
|
|
Self::DisableTokenValidation => write!(f, "disable-token-validation"),
|
2021-05-22 20:06:05 -07:00
|
|
|
Self::OfflineMode => write!(f, "offline-mode"),
|
|
|
|
Self::DisableTls => write!(f, "disable-tls"),
|
2021-04-25 09:55:31 -07:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 18:06:54 -07:00
|
|
|
}
|