2022-02-12 10:29:31 -05:00
|
|
|
//! Configuration file and settings management
|
2022-02-26 12:06:23 -05:00
|
|
|
use config::{Config, ConfigError, File};
|
2021-12-29 23:13:02 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-02-12 10:29:25 -05:00
|
|
|
use std::time::Duration;
|
2022-09-28 08:19:59 -04:00
|
|
|
use tracing::warn;
|
2021-12-29 23:47:31 -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
|
|
|
}
|
|
|
|
|
2022-09-06 07:12:07 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-12-31 12:51:57 -05:00
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Database {
|
|
|
|
pub data_directory: String,
|
2022-09-06 07:06:01 -04:00
|
|
|
pub in_memory: bool,
|
2022-01-25 21:39:24 -05:00
|
|
|
pub min_conn: u32,
|
|
|
|
pub max_conn: u32,
|
2021-12-31 12:51:57 -05:00
|
|
|
}
|
|
|
|
|
2022-09-06 07:12:07 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-12-29 23:13:02 -05:00
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Network {
|
|
|
|
pub port: u16,
|
|
|
|
pub address: String,
|
2022-11-04 19:05:01 -04:00
|
|
|
pub remote_ip_header: Option<String>, // retrieve client IP from this HTTP header if present
|
2022-11-05 08:42:08 -04:00
|
|
|
pub ping_interval_seconds: u32,
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
|
2022-09-06 07:12:07 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-12-29 23:47:31 -05:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
|
2022-09-06 07:12:07 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-12-29 23:13:02 -05:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2022-09-06 07:12:07 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-12-29 23:13:02 -05:00
|
|
|
#[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)
|
2022-12-17 10:27:29 -05:00
|
|
|
pub subscriptions_per_min: Option<u32>, // Artificially slow down request (db query) creation to prevent abuse (averaged over 1 minute)
|
2022-12-18 00:19:48 -05:00
|
|
|
pub db_conns_per_client: Option<u32>, // How many concurrent database queries (not subscriptions) may a client have?
|
2022-12-18 10:14:04 -05:00
|
|
|
pub max_blocking_threads: usize,
|
|
|
|
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-09-06 07:12:07 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2022-01-26 22:39:03 -05:00
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Authorization {
|
|
|
|
pub pubkey_whitelist: Option<Vec<String>>, // If present, only allow these pubkeys to publish events
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:44:45 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Diagnostics {
|
|
|
|
pub tracing: bool, // enables tokio console-subscriber
|
|
|
|
}
|
|
|
|
|
2022-09-24 09:30:22 -04:00
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Copy)]
|
2022-02-12 10:29:25 -05:00
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
pub enum VerifiedUsersMode {
|
|
|
|
Enabled,
|
|
|
|
Passive,
|
|
|
|
Disabled,
|
|
|
|
}
|
|
|
|
|
2022-09-06 07:12:07 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2022-02-12 10:29:25 -05:00
|
|
|
#[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();
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:01:09 -04:00
|
|
|
#[must_use]
|
2022-02-12 10:29:25 -05:00
|
|
|
pub fn is_enabled(&self) -> bool {
|
|
|
|
self.mode == VerifiedUsersMode::Enabled
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:01:09 -04:00
|
|
|
#[must_use]
|
2022-02-12 10:29:25 -05:00
|
|
|
pub fn is_active(&self) -> bool {
|
|
|
|
self.mode == VerifiedUsersMode::Enabled || self.mode == VerifiedUsersMode::Passive
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:01:09 -04:00
|
|
|
#[must_use]
|
2022-02-12 10:29:25 -05:00
|
|
|
pub fn is_passive(&self) -> bool {
|
|
|
|
self.mode == VerifiedUsersMode::Passive
|
|
|
|
}
|
|
|
|
|
2022-09-24 10:01:09 -04:00
|
|
|
#[must_use]
|
2022-02-12 10:29:25 -05:00
|
|
|
pub fn verify_expiration_duration(&self) -> Option<Duration> {
|
|
|
|
self.verify_expiration
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|x| parse_duration::parse(x).ok())
|
|
|
|
}
|
2022-09-24 10:01:09 -04:00
|
|
|
|
|
|
|
#[must_use]
|
2022-02-12 10:29:25 -05:00
|
|
|
pub fn verify_update_duration(&self) -> Option<Duration> {
|
|
|
|
self.verify_update_frequency
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|x| parse_duration::parse(x).ok())
|
|
|
|
}
|
2022-09-24 10:01:09 -04:00
|
|
|
|
|
|
|
#[must_use]
|
2022-02-12 10:29:25 -05:00
|
|
|
pub fn is_valid(&self) -> bool {
|
|
|
|
self.verify_expiration_duration().is_some() && self.verify_update_duration().is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 07:12:07 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-12-29 23:13:02 -05:00
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Settings {
|
2022-01-03 18:42:24 -05:00
|
|
|
pub info: Info,
|
2022-09-11 13:44:45 -04:00
|
|
|
pub diagnostics: Diagnostics,
|
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 {
|
2022-09-24 10:01:09 -04:00
|
|
|
#[must_use]
|
2021-12-29 23:13:02 -05:00
|
|
|
pub fn new() -> Self {
|
2022-02-26 12:06:23 -05:00
|
|
|
let default_settings = Self::default();
|
2021-12-29 23:13:02 -05:00
|
|
|
// attempt to construct settings with file
|
2022-02-26 12:06:23 -05:00
|
|
|
let from_file = Self::new_from_default(&default_settings);
|
2021-12-30 22:07:21 -05:00
|
|
|
match from_file {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Error reading config file ({:?})", e);
|
2022-02-26 12:06:23 -05:00
|
|
|
default_settings
|
2021-12-30 22:07:21 -05:00
|
|
|
}
|
|
|
|
}
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
|
2022-02-26 12:06:23 -05:00
|
|
|
fn new_from_default(default: &Settings) -> Result<Self, ConfigError> {
|
|
|
|
let builder = Config::builder();
|
|
|
|
let config: Config = builder
|
2021-12-29 23:13:02 -05:00
|
|
|
// use defaults
|
2022-02-26 12:06:23 -05:00
|
|
|
.add_source(Config::try_from(default)?)
|
2021-12-29 23:13:02 -05:00
|
|
|
// override with file contents
|
2022-09-06 07:12:07 -04:00
|
|
|
.add_source(File::with_name("config.toml"))
|
2022-09-02 11:18:16 -04:00
|
|
|
.build()?;
|
2022-02-26 10:55:12 -05:00
|
|
|
let mut settings: Settings = config.try_deserialize()?;
|
2022-01-25 21:39:24 -05:00
|
|
|
// ensure connection pool size is logical
|
2022-09-24 10:01:09 -04:00
|
|
|
assert!(
|
|
|
|
settings.database.min_conn <= settings.database.max_conn,
|
|
|
|
"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
|
2022-09-24 10:01:09 -04:00
|
|
|
assert!(
|
|
|
|
settings.verified_users.is_valid(),
|
|
|
|
"VerifiedUsers time settings could not be parsed"
|
|
|
|
);
|
2022-02-12 10:29:25 -05:00
|
|
|
// 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
|
|
|
},
|
2022-09-11 13:44:45 -04:00
|
|
|
diagnostics: Diagnostics { tracing: false },
|
2021-12-31 12:51:57 -05:00
|
|
|
database: Database {
|
|
|
|
data_directory: ".".to_owned(),
|
2022-09-06 07:06:01 -04:00
|
|
|
in_memory: false,
|
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,
|
2022-11-05 08:42:08 -04:00
|
|
|
ping_interval_seconds: 300,
|
2021-12-29 23:13:02 -05:00
|
|
|
address: "0.0.0.0".to_owned(),
|
2022-11-04 19:05:01 -04:00
|
|
|
remote_ip_header: None,
|
2021-12-29 23:13:02 -05:00
|
|
|
},
|
|
|
|
limits: Limits {
|
|
|
|
messages_per_sec: None,
|
2022-12-17 10:27:29 -05:00
|
|
|
subscriptions_per_min: None,
|
2022-12-18 00:19:48 -05:00
|
|
|
db_conns_per_client: None,
|
2022-12-18 10:14:04 -05:00
|
|
|
max_blocking_threads: 64,
|
2021-12-29 23:13:02 -05:00
|
|
|
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 {
|
2022-09-10 21:40:10 -04:00
|
|
|
reject_future_seconds: None, // Reject events in the future if defined
|
2021-12-29 23:47:31 -05:00
|
|
|
},
|
2021-12-29 23:13:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|