veilid/veilid-server/src/settings.rs

1566 lines
56 KiB
Rust
Raw Normal View History

2021-11-28 02:38:24 +00:00
#![allow(clippy::bool_assert_comparison)]
2021-11-28 02:31:01 +00:00
2021-11-22 16:28:30 +00:00
use directories::*;
use parking_lot::*;
2021-11-28 02:31:01 +00:00
2021-11-22 16:28:30 +00:00
use serde_derive::*;
use std::ffi::OsStr;
2022-03-10 14:51:53 +00:00
use std::net::SocketAddr;
2021-11-22 16:28:30 +00:00
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use url::Url;
2022-03-10 14:51:53 +00:00
use veilid_core::xx::*;
2021-11-22 16:28:30 +00:00
2022-03-10 16:10:11 +00:00
pub fn load_default_config() -> Result<config::Config, config::ConfigError> {
2021-11-22 16:28:30 +00:00
let default_config = String::from(
r#"---
2022-05-16 15:52:48 +00:00
daemon:
enabled: false
2021-11-22 16:28:30 +00:00
client_api:
enabled: true
2022-01-15 23:24:37 +00:00
listen_address: 'localhost:5959'
2022-01-01 03:09:30 +00:00
auto_attach: true
2022-05-16 15:52:48 +00:00
logging:
system:
enabled: false
level: 'info'
2021-11-22 16:28:30 +00:00
terminal:
enabled: true
2022-01-15 23:24:37 +00:00
level: 'info'
2021-11-22 16:28:30 +00:00
file:
enabled: false
2022-01-15 23:24:37 +00:00
path: ''
2021-11-22 16:28:30 +00:00
append: true
2022-01-15 23:24:37 +00:00
level: 'info'
2022-06-08 01:31:05 +00:00
api:
2022-05-16 15:52:48 +00:00
enabled: false
2022-01-15 23:24:37 +00:00
level: 'info'
2021-11-22 16:28:30 +00:00
testing:
subnode_index: 0
core:
2022-01-09 04:33:25 +00:00
protected_store:
2022-01-09 13:55:33 +00:00
allow_insecure_fallback: true
2022-05-16 15:52:48 +00:00
always_use_insecure_storage: true
2022-01-15 23:24:37 +00:00
insecure_fallback_directory: '%INSECURE_FALLBACK_DIRECTORY%'
2022-01-19 02:21:11 +00:00
delete: false
2022-01-09 04:33:25 +00:00
table_store:
2022-01-15 23:24:37 +00:00
directory: '%TABLE_STORE_DIRECTORY%'
2022-01-19 02:21:11 +00:00
delete: false
block_store:
directory: '%BLOCK_STORE_DIRECTORY%'
delete: false
2021-11-22 16:28:30 +00:00
network:
2022-03-04 01:45:39 +00:00
connection_initial_timeout_ms: 2000
2022-03-19 22:19:40 +00:00
connection_inactivity_timeout_ms: 60000
2022-05-05 00:40:10 +00:00
max_connections_per_ip4: 8
max_connections_per_ip6_prefix: 8
max_connections_per_ip6_prefix_size: 56
max_connection_frequency_per_min: 8
client_whitelist_timeout_ms: 300000
2022-05-16 15:52:48 +00:00
reverse_connection_receipt_time_ms: 5000
hole_punch_receipt_time_ms: 5000
2022-01-15 23:24:37 +00:00
node_id: ''
node_id_secret: ''
bootstrap: ['bootstrap-dev.veilid.net']
2022-05-16 15:52:48 +00:00
bootstrap_nodes: []
2022-03-24 14:14:50 +00:00
routing_table:
limit_over_attached: 64
limit_fully_attached: 32
limit_attached_strong: 16
limit_attached_good: 8
limit_attached_weak: 4
2021-11-22 16:28:30 +00:00
rpc:
concurrency: 0
queue_size: 1024
2022-03-04 01:45:39 +00:00
max_timestamp_behind_ms: 10000
max_timestamp_ahead_ms: 10000
timeout_ms: 10000
2021-11-22 16:28:30 +00:00
max_route_hop_count: 7
dht:
resolve_node_timeout:
resolve_node_count: 20
resolve_node_fanout: 3
max_find_node_count: 20
get_value_timeout:
get_value_count: 20
get_value_fanout: 3
set_value_timeout:
set_value_count: 20
set_value_fanout: 5
2022-05-28 14:07:57 +00:00
min_peer_count: 1 # 20
2022-03-04 01:45:39 +00:00
min_peer_refresh_time_ms: 2000
2022-05-28 20:11:50 +00:00
validate_dial_info_receipt_time_ms: 2000
2021-11-22 16:28:30 +00:00
upnp: false
natpmp: false
enable_local_peer_scope: false
2022-05-28 20:11:50 +00:00
restricted_nat_retries: 0
2021-11-22 16:28:30 +00:00
tls:
2022-06-09 00:07:26 +00:00
certificate_path: '%CERTIFICATE_PATH%'
private_key_path: '%PRIVATE_KEY_PATH%'
2022-03-04 01:45:39 +00:00
connection_initial_timeout_ms: 2000
2021-11-22 16:28:30 +00:00
application:
https:
enabled: false
2022-03-10 14:51:53 +00:00
listen_address: ':5150'
2022-01-15 23:24:37 +00:00
path: 'app'
# url: 'https://localhost:5150'
2021-11-22 16:28:30 +00:00
http:
enabled: false
2022-03-10 14:51:53 +00:00
listen_address: ':5150'
2022-01-15 23:24:37 +00:00
path: 'app'
# url: 'http://localhost:5150'
2021-11-22 16:28:30 +00:00
protocol:
udp:
enabled: true
socket_pool_size: 0
2022-03-10 14:51:53 +00:00
listen_address: ':5150'
2022-01-15 23:24:37 +00:00
# public_address: ''
2021-11-22 16:28:30 +00:00
tcp:
connect: true
listen: true
max_connections: 32
2022-03-10 14:51:53 +00:00
listen_address: ':5150'
2022-01-15 23:24:37 +00:00
#'public_address: ''
2021-11-22 16:28:30 +00:00
ws:
connect: true
listen: true
max_connections: 16
2022-03-10 14:51:53 +00:00
listen_address: ':5150'
2022-01-15 23:24:37 +00:00
path: 'ws'
# url: 'ws://localhost:5150/ws'
2021-11-22 16:28:30 +00:00
wss:
connect: true
listen: false
2021-11-22 16:28:30 +00:00
max_connections: 16
2022-03-10 14:51:53 +00:00
listen_address: ':5150'
2022-01-15 23:24:37 +00:00
path: 'ws'
# url: ''
2021-11-22 16:28:30 +00:00
"#,
)
.replace(
2022-01-09 04:33:25 +00:00
"%TABLE_STORE_DIRECTORY%",
2021-11-22 16:28:30 +00:00
&Settings::get_default_table_store_path().to_string_lossy(),
2022-01-09 04:33:25 +00:00
)
2022-01-19 02:21:11 +00:00
.replace(
"%BLOCK_STORE_DIRECTORY%",
&Settings::get_default_block_store_path().to_string_lossy(),
)
2022-01-09 04:33:25 +00:00
.replace(
"%INSECURE_FALLBACK_DIRECTORY%",
&Settings::get_default_protected_store_insecure_fallback_directory().to_string_lossy(),
2022-05-28 14:07:57 +00:00
)
.replace(
2022-06-09 00:07:26 +00:00
"%CERTIFICATE_PATH%",
&Settings::get_default_certificate_directory()
.join("server.crt")
.to_string_lossy(),
2022-05-28 14:07:57 +00:00
)
.replace(
2022-06-09 00:07:26 +00:00
"%PRIVATE_KEY_PATH%",
&Settings::get_default_private_key_directory()
.join("server.key")
.to_string_lossy(),
2021-11-22 16:28:30 +00:00
);
2022-03-10 16:10:11 +00:00
config::Config::builder()
.add_source(config::File::from_str(
&default_config,
config::FileFormat::Yaml,
))
.build()
2021-11-22 16:28:30 +00:00
}
pub fn load_config(
2022-03-10 16:10:11 +00:00
cfg: config::Config,
2021-11-22 16:28:30 +00:00
config_file: &Path,
2022-03-10 16:10:11 +00:00
) -> Result<config::Config, config::ConfigError> {
2021-11-22 16:28:30 +00:00
if let Some(config_file_str) = config_file.to_str() {
2022-03-10 16:10:11 +00:00
config::Config::builder()
.add_source(cfg)
.add_source(config::File::new(config_file_str, config::FileFormat::Yaml))
.build()
2021-11-22 16:28:30 +00:00
} else {
Err(config::ConfigError::Message(
"config file path is not valid UTF-8".to_owned(),
))
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum LogLevel {
Error,
Warn,
Info,
Debug,
Trace,
}
impl<'de> serde::Deserialize<'de> for LogLevel {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.to_ascii_lowercase().as_str() {
"error" => Ok(LogLevel::Error),
"warn" => Ok(LogLevel::Warn),
"info" => Ok(LogLevel::Info),
"debug" => Ok(LogLevel::Debug),
"trace" => Ok(LogLevel::Trace),
_ => Err(serde::de::Error::custom(format!(
"Invalid log level: {}",
s
))),
}
}
}
2022-01-06 01:32:43 +00:00
impl serde::Serialize for LogLevel {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let s = match self {
LogLevel::Error => "error",
LogLevel::Warn => "warn",
LogLevel::Info => "info",
LogLevel::Debug => "debug",
LogLevel::Trace => "trace",
};
s.serialize(serializer)
}
}
2022-06-08 01:31:05 +00:00
pub fn convert_loglevel(log_level: LogLevel) -> veilid_core::VeilidLogLevel {
2021-11-22 16:28:30 +00:00
match log_level {
2022-06-08 01:31:05 +00:00
LogLevel::Error => veilid_core::VeilidLogLevel::Error,
LogLevel::Warn => veilid_core::VeilidLogLevel::Warn,
LogLevel::Info => veilid_core::VeilidLogLevel::Info,
LogLevel::Debug => veilid_core::VeilidLogLevel::Debug,
LogLevel::Trace => veilid_core::VeilidLogLevel::Trace,
2021-11-22 16:28:30 +00:00
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedUrl {
2021-11-22 16:28:30 +00:00
pub urlstring: String,
pub url: Url,
}
impl ParsedUrl {
pub fn offset_port(&mut self, offset: u16) -> Result<(), ()> {
// Bump port on url
self.url.set_port(Some(self.url.port().unwrap() + offset))?;
self.urlstring = self.url.to_string();
Ok(())
}
}
2021-11-22 16:28:30 +00:00
impl FromStr for ParsedUrl {
type Err = url::ParseError;
fn from_str(s: &str) -> Result<ParsedUrl, url::ParseError> {
let mut url = Url::parse(s)?;
if url.scheme().to_lowercase() == "http" && url.port().is_none() {
url.set_port(Some(80))
.map_err(|_| url::ParseError::InvalidPort)?
}
if url.scheme().to_lowercase() == "https" && url.port().is_none() {
url.set_port(Some(443))
.map_err(|_| url::ParseError::InvalidPort)?;
}
let parsed_urlstring = url.to_string();
2021-11-22 16:28:30 +00:00
Ok(Self {
urlstring: parsed_urlstring,
2021-11-28 02:31:01 +00:00
url,
2021-11-22 16:28:30 +00:00
})
}
}
impl<'de> serde::Deserialize<'de> for ParsedUrl {
2021-11-22 16:28:30 +00:00
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
ParsedUrl::from_str(s.as_str()).map_err(serde::de::Error::custom)
2021-11-22 16:28:30 +00:00
}
}
2022-01-06 01:32:43 +00:00
impl serde::Serialize for ParsedUrl {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.urlstring.serialize(serializer)
}
}
2022-01-01 03:09:30 +00:00
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedNodeDialInfo {
pub node_dial_info_string: String,
pub node_dial_info: veilid_core::NodeDialInfo,
}
// impl ParsedNodeDialInfo {
// pub fn offset_port(&mut self, offset: u16) -> Result<(), ()> {
// // Bump port on dial_info
// self.node_dial_info
// .dial_info
// .set_port(self.node_dial_info.dial_info.port() + 1);
// self.node_dial_info_string = self.node_dial_info.to_string();
// Ok(())
// }
// }
impl FromStr for ParsedNodeDialInfo {
type Err = veilid_core::VeilidAPIError;
fn from_str(
node_dial_info_string: &str,
) -> Result<ParsedNodeDialInfo, veilid_core::VeilidAPIError> {
let node_dial_info = veilid_core::NodeDialInfo::from_str(node_dial_info_string)?;
Ok(Self {
node_dial_info_string: node_dial_info_string.to_owned(),
node_dial_info,
})
}
}
impl<'de> serde::Deserialize<'de> for ParsedNodeDialInfo {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
ParsedNodeDialInfo::from_str(s.as_str()).map_err(serde::de::Error::custom)
}
}
2022-01-06 01:32:43 +00:00
impl serde::Serialize for ParsedNodeDialInfo {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.node_dial_info_string.serialize(serializer)
}
}
2021-11-22 16:28:30 +00:00
#[derive(Debug, PartialEq)]
pub struct NamedSocketAddrs {
pub name: String,
pub addrs: Vec<SocketAddr>,
}
impl FromStr for NamedSocketAddrs {
type Err = std::io::Error;
fn from_str(s: &str) -> Result<NamedSocketAddrs, std::io::Error> {
2022-03-10 14:51:53 +00:00
let addr_iter = listen_address_to_socket_addrs(s)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;
2021-11-22 16:28:30 +00:00
Ok(NamedSocketAddrs {
name: s.to_owned(),
2022-03-10 14:51:53 +00:00
addrs: addr_iter,
2021-11-22 16:28:30 +00:00
})
}
}
impl<'de> serde::Deserialize<'de> for NamedSocketAddrs {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
2021-11-28 02:31:01 +00:00
NamedSocketAddrs::from_str(s.as_str()).map_err(serde::de::Error::custom)
2021-11-22 16:28:30 +00:00
}
}
2022-01-06 01:32:43 +00:00
impl serde::Serialize for NamedSocketAddrs {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.name.serialize(serializer)
}
}
2021-11-22 16:28:30 +00:00
impl NamedSocketAddrs {
pub fn offset_port(&mut self, offset: u16) -> Result<(), ()> {
// Bump port on name
if let Some(split) = self.name.rfind(':') {
let hoststr = &self.name[0..split];
let portstr = &self.name[split + 1..];
let port: u16 = portstr.parse::<u16>().map_err(drop)? + offset;
2022-03-13 16:45:36 +00:00
self.name = format!("{}:{}", hoststr, port);
2021-11-22 16:28:30 +00:00
} else {
return Err(());
}
// Bump port on addresses
for addr in self.addrs.iter_mut() {
addr.set_port(addr.port() + offset);
}
Ok(())
}
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct Terminal {
pub enabled: bool,
pub level: LogLevel,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct File {
pub enabled: bool,
pub path: String,
pub append: bool,
pub level: LogLevel,
}
2022-05-16 15:52:48 +00:00
#[derive(Debug, Deserialize, Serialize)]
pub struct System {
pub enabled: bool,
pub level: LogLevel,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2022-06-08 01:31:05 +00:00
pub struct Api {
2021-12-11 01:14:33 +00:00
pub enabled: bool,
pub level: LogLevel,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct ClientApi {
pub enabled: bool,
pub listen_address: NamedSocketAddrs,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct Logging {
2022-05-16 15:52:48 +00:00
pub system: System,
2021-11-22 16:28:30 +00:00
pub terminal: Terminal,
pub file: File,
2022-06-08 01:31:05 +00:00
pub api: Api,
2021-11-22 16:28:30 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Https {
2021-11-22 16:28:30 +00:00
pub enabled: bool,
pub listen_address: NamedSocketAddrs,
pub path: PathBuf,
pub url: Option<ParsedUrl>,
2021-11-22 16:28:30 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Http {
2021-11-22 16:28:30 +00:00
pub enabled: bool,
pub listen_address: NamedSocketAddrs,
pub path: PathBuf,
pub url: Option<ParsedUrl>,
2021-11-22 16:28:30 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct Application {
2021-11-29 01:08:50 +00:00
pub https: Https,
pub http: Http,
2021-11-22 16:28:30 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Udp {
2021-11-22 16:28:30 +00:00
pub enabled: bool,
pub socket_pool_size: u32,
pub listen_address: NamedSocketAddrs,
pub public_address: Option<NamedSocketAddrs>,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Tcp {
2021-11-22 16:28:30 +00:00
pub connect: bool,
pub listen: bool,
pub max_connections: u32,
pub listen_address: NamedSocketAddrs,
pub public_address: Option<NamedSocketAddrs>,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Ws {
2021-11-22 16:28:30 +00:00
pub connect: bool,
pub listen: bool,
pub max_connections: u32,
pub listen_address: NamedSocketAddrs,
pub path: PathBuf,
pub url: Option<ParsedUrl>,
2021-11-22 16:28:30 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Wss {
2021-11-22 16:28:30 +00:00
pub connect: bool,
pub listen: bool,
pub max_connections: u32,
pub listen_address: NamedSocketAddrs,
pub path: PathBuf,
pub url: Option<ParsedUrl>,
2021-11-22 16:28:30 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct Protocol {
2021-11-29 01:08:50 +00:00
pub udp: Udp,
pub tcp: Tcp,
pub ws: Ws,
pub wss: Wss,
2021-11-22 16:28:30 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Tls {
2021-11-22 16:28:30 +00:00
pub certificate_path: PathBuf,
pub private_key_path: PathBuf,
2022-03-04 01:45:39 +00:00
pub connection_initial_timeout_ms: u32,
2021-11-22 16:28:30 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Rpc {
2021-11-22 16:28:30 +00:00
pub concurrency: u32,
pub queue_size: u32,
2022-03-04 01:45:39 +00:00
pub max_timestamp_behind_ms: Option<u32>,
pub max_timestamp_ahead_ms: Option<u32>,
pub timeout_ms: u32,
2021-11-22 16:28:30 +00:00
pub max_route_hop_count: u8,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-29 01:08:50 +00:00
pub struct Dht {
2022-03-04 01:45:39 +00:00
pub resolve_node_timeout_ms: Option<u32>,
2021-11-22 16:28:30 +00:00
pub resolve_node_count: u32,
pub resolve_node_fanout: u32,
pub max_find_node_count: u32,
2022-03-04 01:45:39 +00:00
pub get_value_timeout_ms: Option<u32>,
2021-11-22 16:28:30 +00:00
pub get_value_count: u32,
pub get_value_fanout: u32,
2022-03-04 01:45:39 +00:00
pub set_value_timeout_ms: Option<u32>,
2021-11-22 16:28:30 +00:00
pub set_value_count: u32,
pub set_value_fanout: u32,
pub min_peer_count: u32,
2022-03-04 01:45:39 +00:00
pub min_peer_refresh_time_ms: u32,
pub validate_dial_info_receipt_time_ms: u32,
2021-11-22 16:28:30 +00:00
}
2022-03-24 14:14:50 +00:00
#[derive(Debug, Deserialize, Serialize)]
pub struct RoutingTable {
pub limit_over_attached: u32,
pub limit_fully_attached: u32,
pub limit_attached_strong: u32,
pub limit_attached_good: u32,
pub limit_attached_weak: u32,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct Network {
2022-03-04 01:45:39 +00:00
pub connection_initial_timeout_ms: u32,
2022-03-19 22:19:40 +00:00
pub connection_inactivity_timeout_ms: u32,
2022-05-05 00:40:10 +00:00
pub max_connections_per_ip4: u32,
pub max_connections_per_ip6_prefix: u32,
pub max_connections_per_ip6_prefix_size: u32,
pub max_connection_frequency_per_min: u32,
pub client_whitelist_timeout_ms: u32,
2022-05-16 15:52:48 +00:00
pub reverse_connection_receipt_time_ms: u32,
pub hole_punch_receipt_time_ms: u32,
2021-11-22 16:28:30 +00:00
pub node_id: veilid_core::DHTKey,
pub node_id_secret: veilid_core::DHTKeySecret,
2022-05-16 15:52:48 +00:00
pub bootstrap: Vec<String>,
pub bootstrap_nodes: Vec<ParsedNodeDialInfo>,
2022-03-24 14:14:50 +00:00
pub routing_table: RoutingTable,
2021-11-29 01:08:50 +00:00
pub rpc: Rpc,
pub dht: Dht,
2021-11-22 16:28:30 +00:00
pub upnp: bool,
pub natpmp: bool,
pub enable_local_peer_scope: bool,
2021-11-24 01:19:16 +00:00
pub restricted_nat_retries: u32,
2021-11-29 01:08:50 +00:00
pub tls: Tls,
2021-11-22 16:28:30 +00:00
pub application: Application,
pub protocol: Protocol,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct Testing {
pub subnode_index: u16,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct TableStore {
pub directory: PathBuf,
2022-01-19 02:21:11 +00:00
pub delete: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct BlockStore {
pub directory: PathBuf,
pub delete: bool,
2021-11-22 16:28:30 +00:00
}
2022-01-09 04:33:25 +00:00
#[derive(Debug, Deserialize, Serialize)]
pub struct ProtectedStore {
pub allow_insecure_fallback: bool,
pub always_use_insecure_storage: bool,
pub insecure_fallback_directory: PathBuf,
2022-01-19 02:21:11 +00:00
pub delete: bool,
2022-01-09 04:33:25 +00:00
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct Core {
2022-01-09 04:33:25 +00:00
pub protected_store: ProtectedStore,
pub table_store: TableStore,
2022-01-19 02:21:11 +00:00
pub block_store: BlockStore,
2021-11-22 16:28:30 +00:00
pub network: Network,
}
2022-05-16 15:52:48 +00:00
#[derive(Debug, Deserialize, Serialize)]
pub struct Daemon {
pub enabled: bool,
pub pid_file: Option<String>,
pub chroot: Option<String>,
pub working_directory: Option<String>,
pub user: Option<String>,
pub group: Option<String>,
pub stdout_file: Option<String>,
pub stderr_file: Option<String>,
}
2022-01-06 01:32:43 +00:00
#[derive(Debug, Deserialize, Serialize)]
2021-11-22 16:28:30 +00:00
pub struct SettingsInner {
2022-05-16 15:52:48 +00:00
pub daemon: Daemon,
2021-11-22 16:28:30 +00:00
pub client_api: ClientApi,
pub auto_attach: bool,
pub logging: Logging,
pub testing: Testing,
pub core: Core,
}
2022-01-15 23:24:37 +00:00
#[derive(Clone, Debug)]
2021-11-22 16:28:30 +00:00
pub struct Settings {
2022-01-15 23:24:37 +00:00
inner: Arc<RwLock<SettingsInner>>,
2021-11-22 16:28:30 +00:00
}
impl Settings {
2022-03-10 16:10:11 +00:00
pub fn new(config_file: Option<&OsStr>) -> Result<Self, config::ConfigError> {
2021-11-22 16:28:30 +00:00
// Load the default config
2022-03-10 16:10:11 +00:00
let mut cfg = load_default_config()?;
2021-11-22 16:28:30 +00:00
// Merge in the config file if we have one
2022-03-10 16:10:11 +00:00
if let Some(config_file) = config_file {
let config_file_path = Path::new(config_file);
2021-11-22 16:28:30 +00:00
// If the user specifies a config file on the command line then it must exist
2022-03-10 16:10:11 +00:00
cfg = load_config(cfg, config_file_path)?;
2021-11-22 16:28:30 +00:00
}
// Generate config
2022-03-10 16:10:11 +00:00
let inner: SettingsInner = cfg.try_deserialize()?;
//
2021-11-22 16:28:30 +00:00
Ok(Self {
inner: Arc::new(RwLock::new(inner)),
2021-11-22 16:28:30 +00:00
})
}
pub fn read(&self) -> RwLockReadGuard<SettingsInner> {
self.inner.read()
}
pub fn write(&self) -> RwLockWriteGuard<SettingsInner> {
self.inner.write()
}
pub fn apply_subnode_index(&self) -> Result<(), ()> {
let mut settingsrw = self.write();
let idx = settingsrw.testing.subnode_index;
if idx == 0 {
return Ok(());
}
// bump client api port
(*settingsrw).client_api.listen_address.offset_port(idx)?;
// bump protocol ports
(*settingsrw)
.core
.network
.protocol
.udp
.listen_address
.offset_port(idx)?;
(*settingsrw)
.core
.network
.protocol
.tcp
.listen_address
.offset_port(idx)?;
(*settingsrw)
.core
.network
.protocol
.ws
.listen_address
.offset_port(idx)?;
if let Some(url) = &mut (*settingsrw).core.network.protocol.ws.url {
url.offset_port(idx)?;
}
2021-11-22 16:28:30 +00:00
(*settingsrw)
.core
.network
.protocol
.wss
.listen_address
.offset_port(idx)?;
if let Some(url) = &mut (*settingsrw).core.network.protocol.wss.url {
url.offset_port(idx)?;
}
2021-11-22 16:28:30 +00:00
// bump application ports
(*settingsrw)
.core
.network
.application
.http
.listen_address
.offset_port(idx)?;
if let Some(url) = &mut (*settingsrw).core.network.application.http.url {
url.offset_port(idx)?;
}
2021-11-22 16:28:30 +00:00
(*settingsrw)
.core
.network
.application
.https
.listen_address
.offset_port(idx)?;
if let Some(url) = &mut (*settingsrw).core.network.application.https.url {
url.offset_port(idx)?;
}
2021-11-22 16:28:30 +00:00
Ok(())
}
2022-05-28 14:07:57 +00:00
pub fn get_default_config_path() -> PathBuf {
#[cfg(unix)]
{
let globalpath = PathBuf::from("/etc/veilid-server/veilid-server.conf");
if globalpath.exists() {
return globalpath;
2022-05-16 15:52:48 +00:00
}
}
2022-05-28 14:07:57 +00:00
let mut cfg_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid")
{
2022-05-16 15:52:48 +00:00
PathBuf::from(my_proj_dirs.config_dir())
} else {
PathBuf::from("./")
};
2022-05-28 14:07:57 +00:00
cfg_path.push("veilid-server.conf");
2022-05-16 15:52:48 +00:00
2022-05-28 14:07:57 +00:00
cfg_path
2021-11-22 16:28:30 +00:00
}
pub fn get_default_table_store_path() -> PathBuf {
2022-05-28 14:07:57 +00:00
#[cfg(unix)]
{
let globalpath = PathBuf::from("/var/db/veilid-server/table_store");
if globalpath.exists() {
return globalpath;
}
}
let mut ts_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
2022-05-16 15:52:48 +00:00
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
2022-05-28 14:07:57 +00:00
ts_path.push("table_store");
2022-01-09 04:33:25 +00:00
2022-05-28 14:07:57 +00:00
ts_path
2022-01-09 04:33:25 +00:00
}
2022-01-19 02:21:11 +00:00
pub fn get_default_block_store_path() -> PathBuf {
2022-05-28 14:07:57 +00:00
#[cfg(unix)]
{
let globalpath = PathBuf::from("/var/db/veilid-server/block_store");
if globalpath.exists() {
return globalpath;
}
}
let mut bs_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
2022-05-16 15:52:48 +00:00
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
2022-05-28 14:07:57 +00:00
bs_path.push("block_store");
2022-01-19 02:21:11 +00:00
2022-05-28 14:07:57 +00:00
bs_path
2022-01-19 02:21:11 +00:00
}
2022-01-09 04:33:25 +00:00
pub fn get_default_protected_store_insecure_fallback_directory() -> PathBuf {
2022-05-28 14:07:57 +00:00
#[cfg(unix)]
{
let globalpath = PathBuf::from("/var/db/veilid-server/protected_store");
if globalpath.exists() {
return globalpath;
}
}
let mut ps_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
2022-05-16 15:52:48 +00:00
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
2022-05-28 14:07:57 +00:00
ps_path.push("protected_store");
2021-11-22 16:28:30 +00:00
2022-05-28 14:07:57 +00:00
ps_path
}
pub fn get_default_certificate_directory() -> PathBuf {
#[cfg(unix)]
{
let mut globalpath = PathBuf::from("/etc/veilid-server");
if globalpath.exists() {
globalpath.push("ssl");
globalpath.push("certs");
return globalpath;
}
}
let mut c_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
c_path.push("ssl");
c_path.push("certs");
c_path
}
pub fn get_default_private_key_directory() -> PathBuf {
#[cfg(unix)]
{
let mut globalpath = PathBuf::from("/etc/veilid-server");
if globalpath.exists() {
globalpath.push("ssl");
globalpath.push("keys");
return globalpath;
}
}
let mut pk_path = if let Some(my_proj_dirs) = ProjectDirs::from("org", "Veilid", "Veilid") {
PathBuf::from(my_proj_dirs.data_local_dir())
} else {
PathBuf::from("./")
};
pk_path.push("ssl");
pk_path.push("keys");
pk_path
2021-11-22 16:28:30 +00:00
}
2022-06-08 15:18:17 +00:00
pub fn set(&self, key: &str, value: &str) -> Result<(), String> {
let mut inner = self.inner.write();
macro_rules! set_config_value {
($innerkey:expr, $value:expr) => {{
let innerkeyname = &stringify!($innerkey)[6..];
if innerkeyname == key {
match veilid_core::deserialize_json(value) {
Ok(v) => {
$innerkey = v;
return Ok(());
}
Err(e) => {
return Err(format!(
"invalid type for key {}, value: {}: {}",
key, value, e
))
}
}
}
}};
}
set_config_value!(inner.daemon.enabled, value);
set_config_value!(inner.client_api.enabled, value);
set_config_value!(inner.client_api.listen_address, value);
set_config_value!(inner.auto_attach, value);
set_config_value!(inner.logging.system.enabled, value);
set_config_value!(inner.logging.system.level, value);
set_config_value!(inner.logging.terminal.enabled, value);
set_config_value!(inner.logging.terminal.level, value);
set_config_value!(inner.logging.file.enabled, value);
set_config_value!(inner.logging.file.path, value);
set_config_value!(inner.logging.file.append, value);
set_config_value!(inner.logging.file.level, value);
set_config_value!(inner.logging.api.enabled, value);
set_config_value!(inner.logging.api.level, value);
set_config_value!(inner.testing.subnode_index, value);
set_config_value!(inner.core.protected_store.allow_insecure_fallback, value);
set_config_value!(
inner.core.protected_store.always_use_insecure_storage,
value
);
set_config_value!(
inner.core.protected_store.insecure_fallback_directory,
value
);
set_config_value!(inner.core.protected_store.delete, value);
set_config_value!(inner.core.table_store.directory, value);
set_config_value!(inner.core.table_store.delete, value);
set_config_value!(inner.core.block_store.directory, value);
set_config_value!(inner.core.block_store.delete, value);
set_config_value!(inner.core.network.connection_initial_timeout_ms, value);
set_config_value!(inner.core.network.connection_inactivity_timeout_ms, value);
set_config_value!(inner.core.network.max_connections_per_ip4, value);
set_config_value!(inner.core.network.max_connections_per_ip6_prefix, value);
set_config_value!(
inner.core.network.max_connections_per_ip6_prefix_size,
value
);
set_config_value!(inner.core.network.max_connection_frequency_per_min, value);
set_config_value!(inner.core.network.client_whitelist_timeout_ms, value);
set_config_value!(inner.core.network.reverse_connection_receipt_time_ms, value);
set_config_value!(inner.core.network.hole_punch_receipt_time_ms, value);
set_config_value!(inner.core.network.node_id, value);
set_config_value!(inner.core.network.node_id_secret, value);
set_config_value!(inner.core.network.bootstrap, value);
set_config_value!(inner.core.network.bootstrap_nodes, value);
set_config_value!(inner.core.network.routing_table.limit_over_attached, value);
set_config_value!(inner.core.network.routing_table.limit_fully_attached, value);
set_config_value!(
inner.core.network.routing_table.limit_attached_strong,
value
);
set_config_value!(inner.core.network.routing_table.limit_attached_good, value);
set_config_value!(inner.core.network.routing_table.limit_attached_weak, value);
set_config_value!(inner.core.network.rpc.concurrency, value);
set_config_value!(inner.core.network.rpc.queue_size, value);
set_config_value!(inner.core.network.rpc.max_timestamp_behind_ms, value);
set_config_value!(inner.core.network.rpc.max_timestamp_ahead_ms, value);
set_config_value!(inner.core.network.rpc.timeout_ms, value);
set_config_value!(inner.core.network.rpc.max_route_hop_count, value);
set_config_value!(inner.core.network.dht.resolve_node_timeout_ms, value);
set_config_value!(inner.core.network.dht.resolve_node_count, value);
set_config_value!(inner.core.network.dht.resolve_node_fanout, value);
set_config_value!(inner.core.network.dht.max_find_node_count, value);
set_config_value!(inner.core.network.dht.get_value_timeout_ms, value);
set_config_value!(inner.core.network.dht.get_value_count, value);
set_config_value!(inner.core.network.dht.get_value_fanout, value);
set_config_value!(inner.core.network.dht.set_value_timeout_ms, value);
set_config_value!(inner.core.network.dht.set_value_count, value);
set_config_value!(inner.core.network.dht.set_value_fanout, value);
set_config_value!(inner.core.network.dht.min_peer_count, value);
set_config_value!(inner.core.network.dht.min_peer_refresh_time_ms, value);
set_config_value!(
inner.core.network.dht.validate_dial_info_receipt_time_ms,
value
);
set_config_value!(inner.core.network.upnp, value);
set_config_value!(inner.core.network.natpmp, value);
set_config_value!(inner.core.network.enable_local_peer_scope, value);
set_config_value!(inner.core.network.restricted_nat_retries, value);
set_config_value!(inner.core.network.tls.certificate_path, value);
set_config_value!(inner.core.network.tls.private_key_path, value);
set_config_value!(inner.core.network.tls.connection_initial_timeout_ms, value);
set_config_value!(inner.core.network.application.https.enabled, value);
set_config_value!(inner.core.network.application.https.listen_address, value);
set_config_value!(inner.core.network.application.https.path, value);
set_config_value!(inner.core.network.application.https.url, value);
set_config_value!(inner.core.network.application.http.enabled, value);
set_config_value!(inner.core.network.application.http.listen_address, value);
set_config_value!(inner.core.network.application.http.path, value);
set_config_value!(inner.core.network.application.http.url, value);
set_config_value!(inner.core.network.protocol.udp.enabled, value);
set_config_value!(inner.core.network.protocol.udp.socket_pool_size, value);
set_config_value!(inner.core.network.protocol.udp.listen_address, value);
set_config_value!(inner.core.network.protocol.udp.public_address, value);
set_config_value!(inner.core.network.protocol.tcp.connect, value);
set_config_value!(inner.core.network.protocol.tcp.listen, value);
set_config_value!(inner.core.network.protocol.tcp.max_connections, value);
set_config_value!(inner.core.network.protocol.tcp.listen_address, value);
set_config_value!(inner.core.network.protocol.tcp.public_address, value);
set_config_value!(inner.core.network.protocol.ws.connect, value);
set_config_value!(inner.core.network.protocol.ws.listen, value);
set_config_value!(inner.core.network.protocol.ws.max_connections, value);
set_config_value!(inner.core.network.protocol.ws.listen_address, value);
set_config_value!(inner.core.network.protocol.ws.path, value);
set_config_value!(inner.core.network.protocol.ws.url, value);
set_config_value!(inner.core.network.protocol.wss.connect, value);
set_config_value!(inner.core.network.protocol.wss.listen, value);
set_config_value!(inner.core.network.protocol.wss.max_connections, value);
set_config_value!(inner.core.network.protocol.wss.listen_address, value);
set_config_value!(inner.core.network.protocol.wss.path, value);
set_config_value!(inner.core.network.protocol.wss.url, value);
Err("settings key not found".to_owned())
}
2021-11-22 16:28:30 +00:00
pub fn get_core_config_callback(&self) -> veilid_core::ConfigCallback {
let inner = self.inner.clone();
Arc::new(move |key: String| {
let inner = inner.read();
2022-01-21 03:32:22 +00:00
let out: Result<Box<dyn core::any::Any + Send>, String> = match key.as_str() {
2022-01-09 04:33:25 +00:00
"program_name" => Ok(Box::new("veilid-server".to_owned())),
2021-11-22 16:28:30 +00:00
"namespace" => Ok(Box::new(if inner.testing.subnode_index == 0 {
"".to_owned()
} else {
format!("subnode{}", inner.testing.subnode_index)
})),
2022-06-08 01:31:05 +00:00
"api_log_level" => Ok(Box::new(if inner.logging.api.enabled {
veilid_core::VeilidConfigLogLevel::from_veilid_log_level(Some(
convert_loglevel(inner.logging.api.level),
))
} else {
veilid_core::VeilidConfigLogLevel::Off
})),
2021-11-22 16:28:30 +00:00
"capabilities.protocol_udp" => Ok(Box::new(true)),
"capabilities.protocol_connect_tcp" => Ok(Box::new(true)),
"capabilities.protocol_accept_tcp" => Ok(Box::new(true)),
"capabilities.protocol_connect_ws" => Ok(Box::new(true)),
"capabilities.protocol_accept_ws" => Ok(Box::new(true)),
"capabilities.protocol_connect_wss" => Ok(Box::new(true)),
"capabilities.protocol_accept_wss" => Ok(Box::new(true)),
2022-01-09 04:33:25 +00:00
"protected_store.allow_insecure_fallback" => {
Ok(Box::new(inner.core.protected_store.allow_insecure_fallback))
}
"protected_store.always_use_insecure_storage" => Ok(Box::new(
inner.core.protected_store.always_use_insecure_storage,
)),
"protected_store.insecure_fallback_directory" => Ok(Box::new(
2021-11-22 16:28:30 +00:00
inner
.core
2022-01-09 04:33:25 +00:00
.protected_store
.insecure_fallback_directory
.to_string_lossy()
.to_string(),
)),
2022-01-19 02:21:11 +00:00
"protected_store.delete" => Ok(Box::new(inner.core.protected_store.delete)),
2022-01-09 04:33:25 +00:00
"table_store.directory" => Ok(Box::new(
inner
.core
.table_store
2021-11-22 16:28:30 +00:00
.directory
.to_string_lossy()
.to_string(),
)),
2022-01-19 02:21:11 +00:00
"table_store.delete" => Ok(Box::new(inner.core.table_store.delete)),
"block_store.directory" => Ok(Box::new(
inner
.core
.block_store
.directory
.to_string_lossy()
.to_string(),
)),
"block_store.delete" => Ok(Box::new(inner.core.block_store.delete)),
2022-03-04 01:45:39 +00:00
"network.connection_initial_timeout_ms" => {
Ok(Box::new(inner.core.network.connection_initial_timeout_ms))
2021-11-22 16:28:30 +00:00
}
2022-03-19 22:19:40 +00:00
"network.connection_inactivity_timeout_ms" => Ok(Box::new(
inner.core.network.connection_inactivity_timeout_ms,
)),
2022-05-05 00:40:10 +00:00
"network.max_connections_per_ip4" => {
Ok(Box::new(inner.core.network.max_connections_per_ip4))
}
"network.max_connections_per_ip6_prefix" => {
Ok(Box::new(inner.core.network.max_connections_per_ip6_prefix))
}
"network.max_connections_per_ip6_prefix_size" => Ok(Box::new(
inner.core.network.max_connections_per_ip6_prefix_size,
)),
"network.max_connection_frequency_per_min" => Ok(Box::new(
inner.core.network.max_connection_frequency_per_min,
)),
"network.client_whitelist_timeout_ms" => {
Ok(Box::new(inner.core.network.client_whitelist_timeout_ms))
}
2022-05-16 15:52:48 +00:00
"network.reverse_connection_receipt_time_ms" => Ok(Box::new(
inner.core.network.reverse_connection_receipt_time_ms,
)),
"network.hole_punch_receipt_time_ms" => {
Ok(Box::new(inner.core.network.hole_punch_receipt_time_ms))
}
2021-11-22 16:28:30 +00:00
"network.node_id" => Ok(Box::new(inner.core.network.node_id)),
"network.node_id_secret" => Ok(Box::new(inner.core.network.node_id_secret)),
2022-05-16 15:52:48 +00:00
"network.bootstrap" => Ok(Box::new(inner.core.network.bootstrap.clone())),
"network.bootstrap_nodes" => Ok(Box::new(
2021-11-22 16:28:30 +00:00
inner
.core
.network
2022-05-16 15:52:48 +00:00
.bootstrap_nodes
2021-11-22 16:28:30 +00:00
.clone()
.into_iter()
2022-01-01 03:09:30 +00:00
.map(|e| e.node_dial_info_string)
2021-11-22 16:28:30 +00:00
.collect::<Vec<String>>(),
)),
2022-03-24 14:14:50 +00:00
"network.routing_table.limit_over_attached" => Ok(Box::new(
inner.core.network.routing_table.limit_over_attached,
)),
"network.routing_table.limit_fully_attached" => Ok(Box::new(
inner.core.network.routing_table.limit_fully_attached,
)),
"network.routing_table.limit_attached_strong" => Ok(Box::new(
inner.core.network.routing_table.limit_attached_strong,
)),
"network.routing_table.limit_attached_good" => Ok(Box::new(
inner.core.network.routing_table.limit_attached_good,
)),
"network.routing_table.limit_attached_weak" => Ok(Box::new(
inner.core.network.routing_table.limit_attached_weak,
)),
2021-11-22 16:28:30 +00:00
"network.rpc.concurrency" => Ok(Box::new(inner.core.network.rpc.concurrency)),
"network.rpc.queue_size" => Ok(Box::new(inner.core.network.rpc.queue_size)),
2022-03-04 01:45:39 +00:00
"network.rpc.max_timestamp_behind_ms" => {
Ok(Box::new(inner.core.network.rpc.max_timestamp_behind_ms))
2021-11-22 16:28:30 +00:00
}
2022-03-04 01:45:39 +00:00
"network.rpc.max_timestamp_ahead_ms" => {
Ok(Box::new(inner.core.network.rpc.max_timestamp_ahead_ms))
2021-11-22 16:28:30 +00:00
}
2022-03-04 01:45:39 +00:00
"network.rpc.timeout_ms" => Ok(Box::new(inner.core.network.rpc.timeout_ms)),
2021-11-22 16:28:30 +00:00
"network.rpc.max_route_hop_count" => {
Ok(Box::new(inner.core.network.rpc.max_route_hop_count))
}
2022-03-04 01:45:39 +00:00
"network.dht.resolve_node_timeout_ms" => {
Ok(Box::new(inner.core.network.dht.resolve_node_timeout_ms))
2021-11-22 16:28:30 +00:00
}
"network.dht.resolve_node_count" => {
Ok(Box::new(inner.core.network.dht.resolve_node_count))
}
"network.dht.resolve_node_fanout" => {
Ok(Box::new(inner.core.network.dht.resolve_node_fanout))
}
"network.dht.max_find_node_count" => {
Ok(Box::new(inner.core.network.dht.max_find_node_count))
}
2022-03-04 01:45:39 +00:00
"network.dht.get_value_timeout_ms" => {
Ok(Box::new(inner.core.network.dht.get_value_timeout_ms))
2021-11-22 16:28:30 +00:00
}
"network.dht.get_value_count" => {
Ok(Box::new(inner.core.network.dht.get_value_count))
}
"network.dht.get_value_fanout" => {
Ok(Box::new(inner.core.network.dht.get_value_fanout))
}
2022-03-04 01:45:39 +00:00
"network.dht.set_value_timeout_ms" => {
Ok(Box::new(inner.core.network.dht.set_value_timeout_ms))
2021-11-22 16:28:30 +00:00
}
"network.dht.set_value_count" => {
Ok(Box::new(inner.core.network.dht.set_value_count))
}
"network.dht.set_value_fanout" => {
Ok(Box::new(inner.core.network.dht.set_value_fanout))
}
"network.dht.min_peer_count" => Ok(Box::new(inner.core.network.dht.min_peer_count)),
2022-03-04 01:45:39 +00:00
"network.dht.min_peer_refresh_time_ms" => {
Ok(Box::new(inner.core.network.dht.min_peer_refresh_time_ms))
2021-11-22 16:28:30 +00:00
}
2022-03-04 01:45:39 +00:00
"network.dht.validate_dial_info_receipt_time_ms" => Ok(Box::new(
inner.core.network.dht.validate_dial_info_receipt_time_ms,
2021-11-22 16:28:30 +00:00
)),
"network.upnp" => Ok(Box::new(inner.core.network.upnp)),
"network.natpmp" => Ok(Box::new(inner.core.network.natpmp)),
"network.enable_local_peer_scope" => {
Ok(Box::new(inner.core.network.enable_local_peer_scope))
}
2021-11-24 01:19:16 +00:00
"network.restricted_nat_retries" => {
Ok(Box::new(inner.core.network.restricted_nat_retries))
}
2021-11-22 16:28:30 +00:00
"network.tls.certificate_path" => Ok(Box::new(
inner
.core
.network
.tls
.certificate_path
.to_string_lossy()
.to_string(),
)),
"network.tls.private_key_path" => Ok(Box::new(
inner
.core
.network
.tls
.private_key_path
.to_string_lossy()
.to_string(),
)),
2022-03-04 01:45:39 +00:00
"network.tls.connection_initial_timeout_ms" => Ok(Box::new(
inner.core.network.tls.connection_initial_timeout_ms,
)),
"network.application.https.enabled" => {
Ok(Box::new(inner.core.network.application.https.enabled))
}
"network.application.https.listen_address" => Ok(Box::new(
2021-11-22 16:28:30 +00:00
inner
.core
.network
.application
.https
.listen_address
.name
.clone(),
)),
"network.application.https.path" => Ok(Box::new(
inner
.core
.network
.application
.https
2021-11-22 16:28:30 +00:00
.path
.to_string_lossy()
.to_string(),
)),
"network.application.https.url" => Ok(Box::new(
2021-11-22 16:28:30 +00:00
inner
.core
.network
.application
.https
.url
.as_ref()
.map(|a| a.urlstring.clone()),
2021-11-22 16:28:30 +00:00
)),
"network.application.http.enabled" => {
Ok(Box::new(inner.core.network.application.http.enabled))
}
"network.application.http.listen_address" => Ok(Box::new(
inner
.core
.network
.application
.http
.listen_address
.name
.clone(),
)),
"network.application.http.path" => Ok(Box::new(
inner
.core
.network
.application
.http
.path
.to_string_lossy()
.to_string(),
)),
"network.application.http.url" => Ok(Box::new(
inner
.core
.network
.application
.http
.url
.as_ref()
.map(|a| a.urlstring.clone()),
)),
2021-11-22 16:28:30 +00:00
"network.protocol.udp.enabled" => {
Ok(Box::new(inner.core.network.protocol.udp.enabled))
}
"network.protocol.udp.socket_pool_size" => {
Ok(Box::new(inner.core.network.protocol.udp.socket_pool_size))
}
"network.protocol.udp.listen_address" => Ok(Box::new(
inner.core.network.protocol.udp.listen_address.name.clone(),
)),
"network.protocol.udp.public_address" => Ok(Box::new(
2021-11-28 02:38:24 +00:00
inner
.core
.network
.protocol
.udp
.public_address
.as_ref()
.map(|a| a.name.clone()),
2021-11-22 16:28:30 +00:00
)),
"network.protocol.tcp.connect" => {
Ok(Box::new(inner.core.network.protocol.tcp.connect))
}
"network.protocol.tcp.listen" => {
Ok(Box::new(inner.core.network.protocol.tcp.listen))
}
"network.protocol.tcp.max_connections" => {
Ok(Box::new(inner.core.network.protocol.tcp.max_connections))
}
"network.protocol.tcp.listen_address" => Ok(Box::new(
inner.core.network.protocol.tcp.listen_address.name.clone(),
)),
"network.protocol.tcp.public_address" => Ok(Box::new(
2021-11-28 02:38:24 +00:00
inner
.core
.network
.protocol
.tcp
.public_address
.as_ref()
.map(|a| a.name.clone()),
2021-11-22 16:28:30 +00:00
)),
"network.protocol.ws.connect" => {
Ok(Box::new(inner.core.network.protocol.ws.connect))
}
"network.protocol.ws.listen" => Ok(Box::new(inner.core.network.protocol.ws.listen)),
"network.protocol.ws.max_connections" => {
Ok(Box::new(inner.core.network.protocol.ws.max_connections))
}
"network.protocol.ws.listen_address" => Ok(Box::new(
inner.core.network.protocol.ws.listen_address.name.clone(),
)),
"network.protocol.ws.path" => Ok(Box::new(
2021-11-28 02:38:24 +00:00
inner
.core
.network
.protocol
.ws
.path
.to_string_lossy()
.to_string(),
)),
"network.protocol.ws.url" => Ok(Box::new(
inner
.core
.network
.protocol
.ws
.url
2021-11-28 02:38:24 +00:00
.as_ref()
.map(|a| a.urlstring.clone()),
2021-11-22 16:28:30 +00:00
)),
"network.protocol.wss.connect" => {
Ok(Box::new(inner.core.network.protocol.wss.connect))
}
"network.protocol.wss.listen" => {
Ok(Box::new(inner.core.network.protocol.wss.listen))
}
"network.protocol.wss.max_connections" => {
Ok(Box::new(inner.core.network.protocol.wss.max_connections))
}
"network.protocol.wss.listen_address" => Ok(Box::new(
inner.core.network.protocol.wss.listen_address.name.clone(),
)),
"network.protocol.wss.path" => Ok(Box::new(
2021-11-28 02:38:24 +00:00
inner
.core
.network
.protocol
.wss
.path
.to_string_lossy()
.to_string(),
2021-11-22 16:28:30 +00:00
)),
2021-12-09 21:27:17 +00:00
"network.protocol.wss.url" => Ok(Box::new(
inner
.core
.network
.protocol
.wss
.url
.as_ref()
.map(|a| a.urlstring.clone()),
)),
2021-11-22 16:28:30 +00:00
_ => Err(format!("config key '{}' doesn't exist", key)),
};
out
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
#[test]
#[serial]
fn test_default_config() {
2022-03-10 16:10:11 +00:00
let cfg = load_default_config().unwrap();
let inner = cfg.try_deserialize::<SettingsInner>().unwrap();
2021-11-22 16:28:30 +00:00
println!("default settings: {:?}", inner);
}
#[test]
#[serial]
fn test_default_config_settings() {
2022-03-10 16:10:11 +00:00
let settings = Settings::new(None).unwrap();
2021-11-22 16:28:30 +00:00
let s = settings.read();
2022-05-16 15:52:48 +00:00
assert_eq!(s.daemon.enabled, false);
assert_eq!(s.daemon.pid_file, None);
assert_eq!(s.daemon.chroot, None);
assert_eq!(s.daemon.working_directory, None);
assert_eq!(s.daemon.user, None);
assert_eq!(s.daemon.group, None);
assert_eq!(s.daemon.stdout_file, None);
assert_eq!(s.daemon.stderr_file, None);
2021-11-22 16:28:30 +00:00
assert_eq!(s.client_api.enabled, true);
assert_eq!(s.client_api.listen_address.name, "localhost:5959");
assert_eq!(
s.client_api.listen_address.addrs,
2022-03-10 14:51:53 +00:00
listen_address_to_socket_addrs("localhost:5959").unwrap()
2021-11-22 16:28:30 +00:00
);
2022-01-01 03:09:30 +00:00
assert_eq!(s.auto_attach, true);
2022-05-26 00:56:13 +00:00
assert_eq!(s.logging.system.enabled, false);
assert_eq!(s.logging.system.level, LogLevel::Info);
2021-11-22 16:28:30 +00:00
assert_eq!(s.logging.terminal.enabled, true);
assert_eq!(s.logging.terminal.level, LogLevel::Info);
assert_eq!(s.logging.file.enabled, false);
assert_eq!(s.logging.file.path, "");
assert_eq!(s.logging.file.append, true);
assert_eq!(s.logging.file.level, LogLevel::Info);
2022-06-08 01:31:05 +00:00
assert_eq!(s.logging.api.enabled, false);
assert_eq!(s.logging.api.level, LogLevel::Info);
2021-11-22 16:28:30 +00:00
assert_eq!(s.testing.subnode_index, 0);
2022-01-19 02:21:11 +00:00
2021-11-22 16:28:30 +00:00
assert_eq!(
2022-01-09 04:33:25 +00:00
s.core.table_store.directory,
2021-11-22 16:28:30 +00:00
Settings::get_default_table_store_path()
);
2022-01-19 02:21:11 +00:00
assert_eq!(s.core.table_store.delete, false);
assert_eq!(
s.core.block_store.directory,
Settings::get_default_block_store_path()
);
assert_eq!(s.core.block_store.delete, false);
2022-01-09 04:33:25 +00:00
assert_eq!(s.core.protected_store.allow_insecure_fallback, true);
2022-05-16 15:52:48 +00:00
assert_eq!(s.core.protected_store.always_use_insecure_storage, true);
2022-01-09 04:33:25 +00:00
assert_eq!(
2022-01-09 13:55:33 +00:00
s.core.protected_store.insecure_fallback_directory,
2022-01-09 04:33:25 +00:00
Settings::get_default_protected_store_insecure_fallback_directory()
);
2022-01-19 02:21:11 +00:00
assert_eq!(s.core.protected_store.delete, false);
2022-03-04 01:45:39 +00:00
assert_eq!(s.core.network.connection_initial_timeout_ms, 2_000u32);
2022-03-19 22:19:40 +00:00
assert_eq!(s.core.network.connection_inactivity_timeout_ms, 60_000u32);
2022-05-05 00:40:10 +00:00
assert_eq!(s.core.network.max_connections_per_ip4, 8u32);
assert_eq!(s.core.network.max_connections_per_ip6_prefix, 8u32);
assert_eq!(s.core.network.max_connections_per_ip6_prefix_size, 56u32);
assert_eq!(s.core.network.max_connection_frequency_per_min, 8u32);
assert_eq!(s.core.network.client_whitelist_timeout_ms, 300_000u32);
2022-05-16 15:52:48 +00:00
assert_eq!(s.core.network.reverse_connection_receipt_time_ms, 5_000u32);
assert_eq!(s.core.network.hole_punch_receipt_time_ms, 5_000u32);
2021-11-22 16:28:30 +00:00
assert_eq!(s.core.network.node_id, veilid_core::DHTKey::default());
assert_eq!(
s.core.network.node_id_secret,
veilid_core::DHTKeySecret::default()
);
//
2022-05-16 15:52:48 +00:00
assert_eq!(
s.core.network.bootstrap,
2022-06-09 00:07:26 +00:00
vec!["bootstrap-dev.veilid.net".to_owned()]
2022-05-16 15:52:48 +00:00
);
assert_eq!(s.core.network.bootstrap_nodes, vec![]);
2021-11-22 16:28:30 +00:00
//
assert_eq!(s.core.network.rpc.concurrency, 0);
assert_eq!(s.core.network.rpc.queue_size, 1024);
2022-03-04 01:45:39 +00:00
assert_eq!(s.core.network.rpc.max_timestamp_behind_ms, Some(10_000u32));
assert_eq!(s.core.network.rpc.max_timestamp_ahead_ms, Some(10_000u32));
assert_eq!(s.core.network.rpc.timeout_ms, 10_000u32);
2021-11-22 16:28:30 +00:00
assert_eq!(s.core.network.rpc.max_route_hop_count, 7);
//
2022-03-04 01:45:39 +00:00
assert_eq!(s.core.network.dht.resolve_node_timeout_ms, None);
2021-11-22 16:28:30 +00:00
assert_eq!(s.core.network.dht.resolve_node_count, 20u32);
assert_eq!(s.core.network.dht.resolve_node_fanout, 3u32);
assert_eq!(s.core.network.dht.max_find_node_count, 20u32);
2022-03-04 01:45:39 +00:00
assert_eq!(s.core.network.dht.get_value_timeout_ms, None);
2021-11-22 16:28:30 +00:00
assert_eq!(s.core.network.dht.get_value_count, 20u32);
assert_eq!(s.core.network.dht.get_value_fanout, 3u32);
2022-03-04 01:45:39 +00:00
assert_eq!(s.core.network.dht.set_value_timeout_ms, None);
2021-11-22 16:28:30 +00:00
assert_eq!(s.core.network.dht.set_value_count, 20u32);
assert_eq!(s.core.network.dht.set_value_fanout, 5u32);
2022-06-09 00:07:26 +00:00
assert_eq!(s.core.network.dht.min_peer_count, 1u32);
2022-03-04 01:45:39 +00:00
assert_eq!(s.core.network.dht.min_peer_refresh_time_ms, 2_000u32);
2021-11-22 16:28:30 +00:00
assert_eq!(
2022-03-04 01:45:39 +00:00
s.core.network.dht.validate_dial_info_receipt_time_ms,
2022-06-09 00:07:26 +00:00
2_000u32
2021-11-22 16:28:30 +00:00
);
//
assert_eq!(s.core.network.upnp, false);
assert_eq!(s.core.network.natpmp, false);
assert_eq!(s.core.network.enable_local_peer_scope, false);
2022-06-09 00:07:26 +00:00
assert_eq!(s.core.network.restricted_nat_retries, 0u32);
2021-11-22 16:28:30 +00:00
//
assert_eq!(
s.core.network.tls.certificate_path,
2022-06-09 00:07:26 +00:00
Settings::get_default_certificate_directory().join("server.crt")
2021-11-22 16:28:30 +00:00
);
assert_eq!(
s.core.network.tls.private_key_path,
2022-06-09 00:07:26 +00:00
Settings::get_default_private_key_directory().join("server.key")
2021-11-22 16:28:30 +00:00
);
2022-03-04 01:45:39 +00:00
assert_eq!(s.core.network.tls.connection_initial_timeout_ms, 2_000u32);
2021-11-22 16:28:30 +00:00
//
assert_eq!(s.core.network.application.https.enabled, false);
2021-11-22 16:28:30 +00:00
assert_eq!(
s.core.network.application.https.listen_address.name,
2022-03-10 14:51:53 +00:00
":5150"
2021-11-22 16:28:30 +00:00
);
assert_eq!(
s.core.network.application.https.listen_address.addrs,
2022-03-10 14:51:53 +00:00
listen_address_to_socket_addrs(":5150").unwrap()
2021-11-22 16:28:30 +00:00
);
assert_eq!(
s.core.network.application.https.path,
std::path::PathBuf::from("app")
);
assert_eq!(s.core.network.application.https.url, None);
assert_eq!(s.core.network.application.http.enabled, false);
2022-03-10 14:51:53 +00:00
assert_eq!(s.core.network.application.http.listen_address.name, ":5150");
2021-11-22 16:28:30 +00:00
assert_eq!(
s.core.network.application.http.listen_address.addrs,
2022-03-10 14:51:53 +00:00
listen_address_to_socket_addrs(":5150").unwrap()
2021-11-22 16:28:30 +00:00
);
assert_eq!(
s.core.network.application.http.path,
std::path::PathBuf::from("app")
);
assert_eq!(s.core.network.application.http.url, None);
2021-11-22 16:28:30 +00:00
//
assert_eq!(s.core.network.protocol.udp.enabled, true);
assert_eq!(s.core.network.protocol.udp.socket_pool_size, 0);
2022-03-10 16:10:11 +00:00
assert_eq!(s.core.network.protocol.udp.listen_address.name, ":5150");
2021-11-22 16:28:30 +00:00
assert_eq!(
s.core.network.protocol.udp.listen_address.addrs,
2022-03-10 14:51:53 +00:00
listen_address_to_socket_addrs(":5150").unwrap()
2021-11-22 16:28:30 +00:00
);
assert_eq!(s.core.network.protocol.udp.public_address, None);
//
assert_eq!(s.core.network.protocol.tcp.connect, true);
assert_eq!(s.core.network.protocol.tcp.listen, true);
assert_eq!(s.core.network.protocol.tcp.max_connections, 32);
2022-03-10 16:10:11 +00:00
assert_eq!(s.core.network.protocol.tcp.listen_address.name, ":5150");
2021-11-22 16:28:30 +00:00
assert_eq!(
s.core.network.protocol.tcp.listen_address.addrs,
2022-03-10 14:51:53 +00:00
listen_address_to_socket_addrs(":5150").unwrap()
2021-11-22 16:28:30 +00:00
);
assert_eq!(s.core.network.protocol.tcp.public_address, None);
//
assert_eq!(s.core.network.protocol.ws.connect, true);
assert_eq!(s.core.network.protocol.ws.listen, true);
assert_eq!(s.core.network.protocol.ws.max_connections, 16);
2022-03-10 16:10:11 +00:00
assert_eq!(s.core.network.protocol.ws.listen_address.name, ":5150");
2021-11-22 16:28:30 +00:00
assert_eq!(
s.core.network.protocol.ws.listen_address.addrs,
2022-03-10 14:51:53 +00:00
listen_address_to_socket_addrs(":5150").unwrap()
2021-11-22 16:28:30 +00:00
);
assert_eq!(
s.core.network.protocol.ws.path,
std::path::PathBuf::from("ws")
);
assert_eq!(s.core.network.protocol.ws.url, None);
2021-11-22 16:28:30 +00:00
//
assert_eq!(s.core.network.protocol.wss.connect, true);
assert_eq!(s.core.network.protocol.wss.listen, false);
2021-11-22 16:28:30 +00:00
assert_eq!(s.core.network.protocol.wss.max_connections, 16);
2022-03-10 16:10:11 +00:00
assert_eq!(s.core.network.protocol.wss.listen_address.name, ":5150");
2021-11-22 16:28:30 +00:00
assert_eq!(
s.core.network.protocol.wss.listen_address.addrs,
2022-03-10 14:51:53 +00:00
listen_address_to_socket_addrs(":5150").unwrap()
2021-11-22 16:28:30 +00:00
);
assert_eq!(
s.core.network.protocol.wss.path,
std::path::PathBuf::from("ws")
);
assert_eq!(s.core.network.protocol.wss.url, None);
//
2021-11-22 16:28:30 +00:00
}
}