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

77 lines
1.9 KiB

use super::*;
use std::{
borrow::Cow,
net::{
SocketAddr,
Ipv4Addr,
},
};
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())
}
}
}
/// 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,
}
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,
}
}
}