You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yuurei/src/config.rs

95 lines
2.5 KiB

use super::*;
use std::{
borrow::Cow,
net::{
SocketAddr,
Ipv4Addr,
},
};
use tokio::{
time,
};
use cidr::Cidr;
//TODO: Use tokio Watcher instead, to allow hotreloading?
use once_cell::sync::OnceCell;
static INSTANCE: OnceCell<Config> = OnceCell::new();
/// Set the global config instance.
/// This can only be done once for the duration of the program's runtime
pub fn set(conf: Config)
{
INSTANCE.set(conf).expect("Failed to set global config state: Already set");
}
/// Get the global config instance.
/// `set()` must have been previously called or this function panics.
pub fn get() -> &'static Config
{
INSTANCE.get().expect("Global config tried to be accessed before it was set")
}
/// Get the global config instance or return default config instance.
pub fn get_or_default() -> Cow<'static, Config>
{
match INSTANCE.get() {
Some(e) => Cow::Borrowed(e),
_ => {
warn!("Config instance not initialised, using default.");
Cow::Owned(Config::default())
}
}
}
/// A salt for secure tripcode
pub type Salt = [u8; khash::salt::SIZE];
/// Config for this run
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Config
{
/// Name for nanashi
pub anon_name: Cow<'static, str>,
/// The server will listen on this address
pub listen: SocketAddr,
/// Accept all connections in this match
pub accept_mask: Vec<cidr::IpCidr>,
/// Deny all connections in this match, even if previously matched by allow
pub deny_mask: Vec<cidr::IpCidr>,
/// Accept by default
pub accept_default: bool,
/// The number of connections allowed to be processed at once on one route
pub dos_max: usize,
/// The timeout for any routing dispatch
pub req_timeout_local: Option<time::Duration>,
/// The timeout for *all* routing dispatchs
pub req_timeout_global: Option<time::Duration>,
/// The salt for secure tripcode
pub tripcode_salt: Salt,
}
impl Default for Config
{
#[inline]
fn default() -> Self
{
Self {
anon_name: Cow::Borrowed("Nanashi"),
listen: SocketAddr::from(([0,0,0,0], 8088)),
accept_mask: vec![cidr::Ipv4Cidr::new(Ipv4Addr::new(127,0,0,1), 32).unwrap().into()],
deny_mask: Vec::new(),
accept_default: false,
dos_max: 16,
req_timeout_local: Some(time::Duration::from_millis(500)),
req_timeout_global: Some(time::Duration::from_secs(1)),
tripcode_salt: hex!("770d64c6bf46da1a812d067fd224bbe671b7607d3274265abfcdda45c44ca3c1"),
}
}
}