2021-12-29 23:47:31 -05:00
|
|
|
use lazy_static::lazy_static;
|
2021-12-30 22:07:21 -05:00
|
|
|
use log::*;
|
2021-12-29 23:13:02 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-12-29 23:47:31 -05:00
|
|
|
use std::sync::RwLock;
|
2022-02-12 10:29:25 -05:00
|
|
|
use std::time::Duration;
|
2021-12-29 23:47:31 -05:00
|
|
|
|
|
|
|
// initialize a singleton default configuration
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref SETTINGS: RwLock<Settings> = RwLock::new(Settings::default());
|
|
|
|
}
|
2021-12-29 23:13:02 -05:00
|
|
|
|
2022-01-05 10:10:44 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2022-01-03 18:42:24 -05:00
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Info {
|
2022-01-03 22:03:30 -05:00
|
|
|
pub relay_url: Option<String>,
|
2022-01-03 18:42:24 -05:00
|
|
|
pub name: Option<String>,
|
2022-01-03 22:03:30 -05:00
|
|
|
pub description: Option<String>,
|
2022-01-03 18:42:24 -05:00
|
|
|
pub pubkey: Option<String>,
|
2022-01-16 09:34:19 -05:00
|
|
|
pub contact: Option<String>,
|
2022-01-03 18:42:24 -05:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:51:57 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Database {
|
|
|
|
pub data_directory: String,
|
2022-01-25 21:39:24 -05:00
|
|
|
pub min_conn: u32,
|
|
|
|
pub max_conn: u32,
|
2021-12-31 12:51:57 -05:00
|
|
|
}
|
|
|
|
|
2021-12-29 23:13:02 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Network {
|
|
|
|
pub port: u16,
|
|
|
|
pub address: String,
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:47:31 -05:00
|
|
|
//
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Options {
|
|
|
|
pub reject_future_seconds: Option<usize>, // if defined, reject any events with a timestamp more than X seconds in the future
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:13:02 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Retention {
|
|
|
|
// TODO: implement
|
2021-12-30 22:07:21 -05:00
|
|
|
pub max_events: Option<usize>, // max events
|
|
|
|
pub max_bytes: Option<usize>, // max size
|
|
|
|
pub persist_days: Option<usize>, // oldest message
|
|
|
|
pub whitelist_addresses: Option<Vec<String>>, // whitelisted addresses (never delete)
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Limits {
|
2021-12-30 22:07:21 -05:00
|
|
|
pub messages_per_sec: Option<u32>, // Artificially slow down event writing to limit disk consumption (averaged over 1 minute)
|
2021-12-31 16:19:35 -05:00
|
|
|
pub max_event_bytes: Option<usize>, // Maximum size of an EVENT message
|
2021-12-29 23:13:02 -05:00
|
|
|
pub max_ws_message_bytes: Option<usize>,
|
|
|
|
pub max_ws_frame_bytes: Option<usize>,
|
|
|
|
pub broadcast_buffer: usize, // events to buffer for subscribers (prevents slow readers from consuming memory)
|
|
|
|
pub event_persist_buffer: usize, // events to buffer for database commits (block senders if database writes are too slow)
|
|
|
|
}
|
|
|
|
|
2022-01-26 22:39:03 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Authorization {
|
|
|
|
pub pubkey_whitelist: Option<Vec<String>>, // If present, only allow these pubkeys to publish events
|
|
|
|
}
|
|
|
|
|
2022-02-12 10:29:25 -05:00
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
|
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
pub enum VerifiedUsersMode {
|
|
|
|
Enabled,
|
|
|
|
Passive,
|
|
|
|
Disabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct VerifiedUsers {
|
|
|
|
pub mode: VerifiedUsersMode, // Mode of operation: "enabled" (enforce) or "passive" (check only). If none, this is simply disabled.
|
|
|
|
pub domain_whitelist: Option<Vec<String>>, // If present, only allow verified users from these domains can publish events
|
|
|
|
pub domain_blacklist: Option<Vec<String>>, // If present, allow all verified users from any domain except these
|
|
|
|
pub verify_expiration: Option<String>, // how long a verification is cached for before no longer being used
|
|
|
|
pub verify_update_frequency: Option<String>, // how often to attempt to update verification
|
|
|
|
pub verify_expiration_duration: Option<Duration>, // internal result of parsing verify_expiration
|
|
|
|
pub verify_update_frequency_duration: Option<Duration>, // internal result of parsing verify_update_frequency
|
|
|
|
pub max_consecutive_failures: usize, // maximum number of verification failures in a row, before ceasing future checks
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VerifiedUsers {
|
|
|
|
pub fn init(&mut self) {
|
|
|
|
self.verify_expiration_duration = self.verify_expiration_duration();
|
|
|
|
self.verify_update_frequency_duration = self.verify_update_duration();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_enabled(&self) -> bool {
|
|
|
|
self.mode == VerifiedUsersMode::Enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_active(&self) -> bool {
|
|
|
|
self.mode == VerifiedUsersMode::Enabled || self.mode == VerifiedUsersMode::Passive
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_passive(&self) -> bool {
|
|
|
|
self.mode == VerifiedUsersMode::Passive
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verify_expiration_duration(&self) -> Option<Duration> {
|
|
|
|
self.verify_expiration
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|x| parse_duration::parse(x).ok())
|
|
|
|
}
|
|
|
|
pub fn verify_update_duration(&self) -> Option<Duration> {
|
|
|
|
self.verify_update_frequency
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|x| parse_duration::parse(x).ok())
|
|
|
|
}
|
|
|
|
pub fn is_valid(&self) -> bool {
|
|
|
|
self.verify_expiration_duration().is_some() && self.verify_update_duration().is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 23:13:02 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Settings {
|
2022-01-03 18:42:24 -05:00
|
|
|
pub info: Info,
|
2021-12-31 12:51:57 -05:00
|
|
|
pub database: Database,
|
2021-12-29 23:13:02 -05:00
|
|
|
pub network: Network,
|
|
|
|
pub limits: Limits,
|
2022-01-26 22:39:03 -05:00
|
|
|
pub authorization: Authorization,
|
2022-02-12 10:29:25 -05:00
|
|
|
pub verified_users: VerifiedUsers,
|
2021-12-29 23:13:02 -05:00
|
|
|
pub retention: Retention,
|
2021-12-29 23:47:31 -05:00
|
|
|
pub options: Options,
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let d = Self::default();
|
|
|
|
// attempt to construct settings with file
|
2021-12-30 22:07:21 -05:00
|
|
|
// Self::new_from_default(&d).unwrap_or(d)
|
|
|
|
let from_file = Self::new_from_default(&d);
|
|
|
|
match from_file {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Error reading config file ({:?})", e);
|
|
|
|
d
|
|
|
|
}
|
|
|
|
}
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_from_default(default: &Settings) -> Result<Self, config::ConfigError> {
|
|
|
|
let config: config::Config = config::Config::new();
|
2022-02-12 10:29:25 -05:00
|
|
|
let mut settings: Settings = config
|
2021-12-29 23:13:02 -05:00
|
|
|
// use defaults
|
|
|
|
.with_merged(config::Config::try_from(default).unwrap())?
|
|
|
|
// override with file contents
|
|
|
|
.with_merged(config::File::with_name("config"))?
|
|
|
|
.try_into()?;
|
2022-01-25 21:39:24 -05:00
|
|
|
// ensure connection pool size is logical
|
|
|
|
if settings.database.min_conn > settings.database.max_conn {
|
|
|
|
panic!(
|
|
|
|
"Database min_conn setting ({}) cannot exceed max_conn ({})",
|
|
|
|
settings.database.min_conn, settings.database.max_conn
|
|
|
|
);
|
|
|
|
}
|
2022-02-12 10:29:25 -05:00
|
|
|
// ensure durations parse
|
|
|
|
if !settings.verified_users.is_valid() {
|
|
|
|
panic!("VerifiedUsers time settings could not be parsed");
|
|
|
|
}
|
|
|
|
// initialize durations for verified users
|
|
|
|
settings.verified_users.init();
|
2021-12-29 23:13:02 -05:00
|
|
|
Ok(settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Settings {
|
2022-01-03 18:42:24 -05:00
|
|
|
info: Info {
|
2022-01-03 22:03:30 -05:00
|
|
|
relay_url: None,
|
2022-01-03 18:42:24 -05:00
|
|
|
name: Some("Unnamed nostr-rs-relay".to_owned()),
|
2022-01-03 22:03:30 -05:00
|
|
|
description: None,
|
2022-01-03 18:42:24 -05:00
|
|
|
pubkey: None,
|
2022-01-16 09:34:19 -05:00
|
|
|
contact: None,
|
2022-01-03 18:42:24 -05:00
|
|
|
},
|
2021-12-31 12:51:57 -05:00
|
|
|
database: Database {
|
|
|
|
data_directory: ".".to_owned(),
|
2022-01-25 21:39:24 -05:00
|
|
|
min_conn: 4,
|
|
|
|
max_conn: 128,
|
2021-12-31 12:51:57 -05:00
|
|
|
},
|
2021-12-29 23:13:02 -05:00
|
|
|
network: Network {
|
|
|
|
port: 8080,
|
|
|
|
address: "0.0.0.0".to_owned(),
|
|
|
|
},
|
|
|
|
limits: Limits {
|
|
|
|
messages_per_sec: None,
|
|
|
|
max_event_bytes: Some(2 << 17), // 128K
|
|
|
|
max_ws_message_bytes: Some(2 << 17), // 128K
|
|
|
|
max_ws_frame_bytes: Some(2 << 17), // 128K
|
2022-02-12 10:29:25 -05:00
|
|
|
broadcast_buffer: 16384,
|
|
|
|
event_persist_buffer: 4096,
|
2021-12-29 23:13:02 -05:00
|
|
|
},
|
2022-01-26 22:39:03 -05:00
|
|
|
authorization: Authorization {
|
|
|
|
pubkey_whitelist: None, // Allow any address to publish
|
|
|
|
},
|
2022-02-12 10:29:25 -05:00
|
|
|
verified_users: VerifiedUsers {
|
|
|
|
mode: VerifiedUsersMode::Disabled,
|
|
|
|
domain_whitelist: None,
|
|
|
|
domain_blacklist: None,
|
|
|
|
verify_expiration: Some("1 week".to_owned()),
|
|
|
|
verify_update_frequency: Some("1 day".to_owned()),
|
|
|
|
verify_expiration_duration: None,
|
|
|
|
verify_update_frequency_duration: None,
|
|
|
|
max_consecutive_failures: 20,
|
|
|
|
},
|
2021-12-29 23:13:02 -05:00
|
|
|
retention: Retention {
|
2021-12-30 22:07:21 -05:00
|
|
|
max_events: None, // max events
|
|
|
|
max_bytes: None, // max size
|
|
|
|
persist_days: None, // oldest message
|
|
|
|
whitelist_addresses: None, // whitelisted addresses (never delete)
|
2021-12-29 23:13:02 -05:00
|
|
|
},
|
2021-12-29 23:47:31 -05:00
|
|
|
options: Options {
|
|
|
|
reject_future_seconds: Some(30 * 60), // Reject events 30min in the future or greater
|
|
|
|
},
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|