2021-04-22 15:09:48 -07:00
|
|
|
#![warn(clippy::pedantic, clippy::nursery)]
|
2021-03-22 17:00:21 -07:00
|
|
|
// We're end users, so these is ok
|
2021-04-17 20:19:27 -07:00
|
|
|
#![allow(clippy::module_name_repetitions)]
|
2021-03-17 18:45:16 -07:00
|
|
|
|
2021-05-11 18:01:01 -07:00
|
|
|
use std::env::{self, VarError};
|
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::Display;
|
|
|
|
use std::hint::unreachable_unchecked;
|
|
|
|
use std::num::{NonZeroU64, ParseIntError};
|
2021-03-25 19:58:07 -07:00
|
|
|
use std::process;
|
2021-04-23 15:03:53 -07:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2021-03-22 14:47:56 -07:00
|
|
|
use std::sync::Arc;
|
2021-03-17 18:45:16 -07:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-03-22 14:47:56 -07:00
|
|
|
use actix_web::rt::{spawn, time, System};
|
2021-03-22 20:04:54 -07:00
|
|
|
use actix_web::web::{self, Data};
|
2021-06-24 07:39:12 -07:00
|
|
|
use actix_web::{App, HttpResponse, HttpServer};
|
2021-04-23 14:22:29 -07:00
|
|
|
use cache::{Cache, DiskCache};
|
2021-03-25 18:06:54 -07:00
|
|
|
use clap::Clap;
|
|
|
|
use config::CliArgs;
|
2021-05-19 18:42:55 -07:00
|
|
|
use log::{debug, error, info, warn, LevelFilter};
|
2021-04-18 21:16:13 -07:00
|
|
|
use parking_lot::RwLock;
|
2021-03-22 14:47:56 -07:00
|
|
|
use rustls::{NoClientAuth, ServerConfig};
|
2021-03-17 18:45:16 -07:00
|
|
|
use simple_logger::SimpleLogger;
|
2021-05-19 18:42:55 -07:00
|
|
|
use sodiumoxide::crypto::secretstream::gen_key;
|
2021-03-22 14:47:56 -07:00
|
|
|
use state::{RwLockServerState, ServerState};
|
|
|
|
use stop::send_stop;
|
2021-03-17 18:45:16 -07:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-05-19 19:42:44 -07:00
|
|
|
use crate::cache::mem::{Lfu, Lru};
|
|
|
|
use crate::cache::{MemoryCache, ENCRYPTION_KEY};
|
2021-05-22 20:06:05 -07:00
|
|
|
use crate::config::{UnstableOptions, OFFLINE_MODE};
|
2021-04-23 21:56:58 -07:00
|
|
|
use crate::state::DynamicServerCert;
|
2021-04-22 21:11:30 -07:00
|
|
|
|
2021-03-22 14:47:56 -07:00
|
|
|
mod cache;
|
2021-03-25 18:06:54 -07:00
|
|
|
mod config;
|
2021-05-22 19:10:03 -07:00
|
|
|
mod metrics;
|
2021-03-17 18:45:16 -07:00
|
|
|
mod ping;
|
|
|
|
mod routes;
|
2021-03-22 14:47:56 -07:00
|
|
|
mod state;
|
2021-03-17 18:45:16 -07:00
|
|
|
mod stop;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! client_api_version {
|
|
|
|
() => {
|
2021-05-28 12:30:25 -07:00
|
|
|
"31"
|
2021-03-17 18:45:16 -07:00
|
|
|
};
|
|
|
|
}
|
2021-03-25 19:58:07 -07:00
|
|
|
|
2021-03-17 18:45:16 -07:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
enum ServerError {
|
|
|
|
#[error("There was a failure parsing config")]
|
|
|
|
Config(#[from] VarError),
|
|
|
|
#[error("Failed to parse an int")]
|
|
|
|
ParseInt(#[from] ParseIntError),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_web::main]
|
2021-04-25 09:55:31 -07:00
|
|
|
async fn main() -> Result<(), Box<dyn Error>> {
|
2021-05-11 18:01:01 -07:00
|
|
|
sodiumoxide::init().expect("Failed to initialize crypto");
|
2021-03-22 14:47:56 -07:00
|
|
|
// It's ok to fail early here, it would imply we have a invalid config.
|
2021-03-17 18:45:16 -07:00
|
|
|
dotenv::dotenv().ok();
|
2021-04-17 19:12:02 -07:00
|
|
|
|
2021-05-22 20:06:05 -07:00
|
|
|
//
|
|
|
|
// Config loading
|
|
|
|
//
|
|
|
|
|
|
|
|
let cli_args = CliArgs::parse();
|
2021-03-25 19:58:07 -07:00
|
|
|
let port = cli_args.port;
|
2021-05-11 18:01:01 -07:00
|
|
|
let memory_max_size = cli_args
|
|
|
|
.memory_quota
|
|
|
|
.map(NonZeroU64::get)
|
|
|
|
.unwrap_or_default();
|
2021-03-25 21:07:32 -07:00
|
|
|
let disk_quota = cli_args.disk_quota;
|
|
|
|
let cache_path = cli_args.cache_path.clone();
|
2021-04-14 20:44:13 -07:00
|
|
|
let low_mem_mode = cli_args.low_memory;
|
2021-04-25 09:55:31 -07:00
|
|
|
let use_lfu = cli_args.unstable_options.contains(&UnstableOptions::UseLfu);
|
2021-05-22 20:06:05 -07:00
|
|
|
let disable_tls = cli_args
|
|
|
|
.unstable_options
|
|
|
|
.contains(&UnstableOptions::DisableTls);
|
|
|
|
OFFLINE_MODE.store(
|
|
|
|
cli_args
|
|
|
|
.unstable_options
|
|
|
|
.contains(&UnstableOptions::OfflineMode),
|
|
|
|
Ordering::Release,
|
|
|
|
);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Logging and warnings
|
|
|
|
//
|
2021-03-25 18:06:54 -07:00
|
|
|
|
2021-04-20 11:12:20 -07:00
|
|
|
let log_level = match (cli_args.quiet, cli_args.verbose) {
|
|
|
|
(n, _) if n > 2 => LevelFilter::Off,
|
|
|
|
(2, _) => LevelFilter::Error,
|
|
|
|
(1, _) => LevelFilter::Warn,
|
|
|
|
(0, 0) => LevelFilter::Info,
|
|
|
|
(_, 1) => LevelFilter::Debug,
|
|
|
|
(_, n) if n > 1 => LevelFilter::Trace,
|
2021-05-19 18:42:55 -07:00
|
|
|
// compiler can't figure it out
|
2021-04-23 15:03:53 -07:00
|
|
|
_ => unsafe { unreachable_unchecked() },
|
2021-04-20 11:12:20 -07:00
|
|
|
};
|
|
|
|
|
2021-04-23 15:03:53 -07:00
|
|
|
SimpleLogger::new().with_level(log_level).init()?;
|
2021-03-25 18:06:54 -07:00
|
|
|
|
2021-04-25 09:55:31 -07:00
|
|
|
if let Err(e) = print_preamble_and_warnings(&cli_args) {
|
|
|
|
error!("{}", e);
|
|
|
|
return Err(e);
|
|
|
|
}
|
2021-04-18 20:06:18 -07:00
|
|
|
|
2021-03-25 18:06:54 -07:00
|
|
|
let client_secret = if let Ok(v) = env::var("CLIENT_SECRET") {
|
|
|
|
v
|
|
|
|
} else {
|
2021-03-25 19:58:07 -07:00
|
|
|
error!("Client secret not found in ENV. Please set CLIENT_SECRET.");
|
2021-03-25 18:06:54 -07:00
|
|
|
process::exit(1);
|
|
|
|
};
|
|
|
|
let client_secret_1 = client_secret.clone();
|
|
|
|
|
2021-05-19 18:42:55 -07:00
|
|
|
if cli_args.ephemeral_disk_encryption {
|
|
|
|
info!("Running with at-rest encryption!");
|
|
|
|
ENCRYPTION_KEY.set(gen_key()).unwrap();
|
|
|
|
}
|
|
|
|
|
2021-05-22 20:06:05 -07:00
|
|
|
metrics::init();
|
|
|
|
|
2021-05-22 19:10:03 -07:00
|
|
|
// HTTP Server init
|
|
|
|
|
2021-05-22 20:06:05 -07:00
|
|
|
let server = if OFFLINE_MODE.load(Ordering::Acquire) {
|
|
|
|
ServerState::init_offline()
|
|
|
|
} else {
|
|
|
|
ServerState::init(&client_secret, &cli_args).await?
|
|
|
|
};
|
2021-03-25 19:58:07 -07:00
|
|
|
let data_0 = Arc::new(RwLockServerState(RwLock::new(server)));
|
|
|
|
let data_1 = Arc::clone(&data_0);
|
|
|
|
|
|
|
|
// What's nice is that Rustls only supports TLS 1.2 and 1.3.
|
|
|
|
let mut tls_config = ServerConfig::new(NoClientAuth::new());
|
2021-04-23 21:56:58 -07:00
|
|
|
tls_config.cert_resolver = Arc::new(DynamicServerCert);
|
2021-03-25 19:58:07 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// At this point, the server is ready to start, and starts the necessary
|
|
|
|
// threads.
|
|
|
|
//
|
2021-03-22 14:47:56 -07:00
|
|
|
|
|
|
|
// Set ctrl+c to send a stop message
|
|
|
|
let running = Arc::new(AtomicBool::new(true));
|
2021-04-18 20:06:18 -07:00
|
|
|
let running_1 = running.clone();
|
|
|
|
let system = System::current();
|
2021-03-22 14:47:56 -07:00
|
|
|
ctrlc::set_handler(move || {
|
2021-04-18 20:06:18 -07:00
|
|
|
let system = &system;
|
2021-03-22 14:47:56 -07:00
|
|
|
let client_secret = client_secret.clone();
|
2021-04-18 20:06:18 -07:00
|
|
|
let running_2 = Arc::clone(&running_1);
|
2021-05-22 20:06:05 -07:00
|
|
|
if !OFFLINE_MODE.load(Ordering::Acquire) {
|
|
|
|
System::new().block_on(async move {
|
|
|
|
if running_2.load(Ordering::SeqCst) {
|
|
|
|
send_stop(&client_secret).await;
|
|
|
|
} else {
|
|
|
|
warn!("Got second Ctrl-C, forcefully exiting");
|
|
|
|
system.stop()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-04-18 20:06:18 -07:00
|
|
|
running_1.store(false, Ordering::SeqCst);
|
2021-03-22 14:47:56 -07:00
|
|
|
})
|
|
|
|
.expect("Error setting Ctrl-C handler");
|
|
|
|
|
2021-03-25 19:58:07 -07:00
|
|
|
// Spawn ping task
|
2021-05-22 20:06:05 -07:00
|
|
|
if !OFFLINE_MODE.load(Ordering::Acquire) {
|
|
|
|
spawn(async move {
|
|
|
|
let mut interval = time::interval(Duration::from_secs(90));
|
|
|
|
let mut data = Arc::clone(&data_0);
|
|
|
|
loop {
|
|
|
|
interval.tick().await;
|
|
|
|
debug!("Sending ping!");
|
|
|
|
ping::update_server_state(&client_secret_1, &cli_args, &mut data).await;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-03-17 18:45:16 -07:00
|
|
|
|
2021-05-19 19:42:44 -07:00
|
|
|
let cache = DiskCache::new(disk_quota, cache_path.clone()).await;
|
|
|
|
let cache: Arc<dyn Cache> = if low_mem_mode {
|
|
|
|
cache
|
2021-05-11 18:01:01 -07:00
|
|
|
} else if use_lfu {
|
2021-05-19 19:42:44 -07:00
|
|
|
MemoryCache::<Lfu, _>::new(cache, memory_max_size).await
|
2021-04-22 21:11:30 -07:00
|
|
|
} else {
|
2021-05-19 19:42:44 -07:00
|
|
|
MemoryCache::<Lru, _>::new(cache, memory_max_size).await
|
2021-04-22 21:11:30 -07:00
|
|
|
};
|
|
|
|
|
2021-04-22 10:11:08 -07:00
|
|
|
let cache_0 = Arc::clone(&cache);
|
2021-04-18 14:38:33 -07:00
|
|
|
|
2021-03-25 19:58:07 -07:00
|
|
|
// Start HTTPS server
|
2021-05-22 20:06:05 -07:00
|
|
|
let server = HttpServer::new(move || {
|
2021-03-17 18:45:16 -07:00
|
|
|
App::new()
|
2021-05-27 14:05:50 -07:00
|
|
|
.service(routes::index)
|
2021-03-17 18:45:16 -07:00
|
|
|
.service(routes::token_data)
|
2021-03-22 14:47:56 -07:00
|
|
|
.service(routes::token_data_saver)
|
2021-05-22 20:06:05 -07:00
|
|
|
.service(routes::metrics)
|
2021-06-24 07:39:12 -07:00
|
|
|
.route(
|
|
|
|
"/data/{tail:.*}",
|
|
|
|
web::get().to(HttpResponse::UnavailableForLegalReasons),
|
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/data-saver/{tail:.*}",
|
|
|
|
web::get().to(HttpResponse::UnavailableForLegalReasons),
|
|
|
|
)
|
2021-03-22 14:47:56 -07:00
|
|
|
.route("{tail:.*}", web::get().to(routes::default))
|
2021-03-17 18:45:16 -07:00
|
|
|
.app_data(Data::from(Arc::clone(&data_1)))
|
2021-04-22 10:11:08 -07:00
|
|
|
.app_data(Data::from(Arc::clone(&cache_0)))
|
2021-03-17 18:45:16 -07:00
|
|
|
})
|
2021-05-22 20:06:05 -07:00
|
|
|
.shutdown_timeout(60);
|
|
|
|
|
|
|
|
if disable_tls {
|
|
|
|
server.bind(format!("0.0.0.0:{}", port))?.run().await?;
|
|
|
|
} else {
|
|
|
|
server
|
|
|
|
.bind_rustls(format!("0.0.0.0:{}", port), tls_config)?
|
|
|
|
.run()
|
|
|
|
.await?;
|
|
|
|
}
|
2021-03-22 14:47:56 -07:00
|
|
|
|
|
|
|
// Waiting for us to finish sending stop message
|
|
|
|
while running.load(Ordering::SeqCst) {
|
|
|
|
std::thread::sleep(Duration::from_millis(250));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2021-03-17 18:45:16 -07:00
|
|
|
}
|
2021-04-18 20:06:18 -07:00
|
|
|
|
2021-04-25 09:55:31 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum InvalidCombination {
|
|
|
|
MissingUnstableOption(&'static str, UnstableOptions),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for InvalidCombination {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
InvalidCombination::MissingUnstableOption(opt, arg) => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"The option '{}' requires the unstable option '-Z {}'",
|
|
|
|
opt, arg
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for InvalidCombination {}
|
|
|
|
|
|
|
|
fn print_preamble_and_warnings(args: &CliArgs) -> Result<(), Box<dyn Error>> {
|
2021-04-18 20:06:18 -07:00
|
|
|
println!(concat!(
|
|
|
|
env!("CARGO_PKG_NAME"),
|
|
|
|
" ",
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
2021-06-06 14:48:48 -07:00
|
|
|
" (",
|
2021-06-06 15:17:42 -07:00
|
|
|
env!("VERGEN_GIT_SHA_SHORT"),
|
|
|
|
")",
|
2021-04-18 20:06:18 -07:00
|
|
|
" Copyright (C) 2021 ",
|
|
|
|
env!("CARGO_PKG_AUTHORS"),
|
|
|
|
"\n\n",
|
|
|
|
env!("CARGO_PKG_NAME"),
|
|
|
|
" is free software: you can redistribute it and/or modify\n\
|
|
|
|
it under the terms of the GNU General Public License as published by\n\
|
|
|
|
the Free Software Foundation, either version 3 of the License, or\n\
|
|
|
|
(at your option) any later version.\n\n",
|
|
|
|
env!("CARGO_PKG_NAME"),
|
|
|
|
" is distributed in the hope that it will be useful,\n\
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
|
|
|
|
GNU General Public License for more details.\n\n\
|
|
|
|
You should have received a copy of the GNU General Public License\n\
|
|
|
|
along with ",
|
|
|
|
env!("CARGO_PKG_NAME"),
|
|
|
|
". If not, see <https://www.gnu.org/licenses/>.\n"
|
|
|
|
));
|
2021-04-25 09:55:31 -07:00
|
|
|
|
|
|
|
if !args.unstable_options.is_empty() {
|
|
|
|
warn!("Unstable options are enabled. These options should not be used in production!");
|
|
|
|
}
|
|
|
|
|
2021-05-22 20:06:05 -07:00
|
|
|
if args
|
|
|
|
.unstable_options
|
|
|
|
.contains(&UnstableOptions::OfflineMode)
|
|
|
|
{
|
|
|
|
warn!("Running in offline mode. No communication to MangaDex will be made!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.unstable_options.contains(&UnstableOptions::DisableTls) {
|
|
|
|
warn!("Serving insecure traffic! You better be running this for development only.");
|
|
|
|
}
|
|
|
|
|
2021-04-25 09:55:31 -07:00
|
|
|
if args.override_upstream.is_some()
|
|
|
|
&& !args
|
|
|
|
.unstable_options
|
|
|
|
.contains(&UnstableOptions::OverrideUpstream)
|
|
|
|
{
|
|
|
|
Err(Box::new(InvalidCombination::MissingUnstableOption(
|
|
|
|
"override-upstream",
|
|
|
|
UnstableOptions::OverrideUpstream,
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-18 20:06:18 -07:00
|
|
|
}
|