2021-11-22 16:28:30 +00:00
|
|
|
use crate::xx::*;
|
2022-06-08 01:31:05 +00:00
|
|
|
use crate::*;
|
2022-02-07 02:18:42 +00:00
|
|
|
use serde::*;
|
|
|
|
|
2022-05-31 23:54:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2022-07-12 12:02:22 +00:00
|
|
|
pub type ConfigCallbackReturn = Result<Box<dyn core::any::Any + Send>, VeilidAPIError>;
|
|
|
|
pub type ConfigCallback = Arc<dyn Fn(String) -> ConfigCallbackReturn + Send + Sync>;
|
2021-11-22 16:28:30 +00:00
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigHTTPS {
|
|
|
|
pub enabled: bool,
|
|
|
|
pub listen_address: String,
|
2021-12-09 21:00:47 +00:00
|
|
|
pub path: String,
|
|
|
|
pub url: Option<String>, // Fixed URL is not optional for TLS-based protocols and is dynamically validated
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigHTTP {
|
|
|
|
pub enabled: bool,
|
|
|
|
pub listen_address: String,
|
2021-12-09 21:00:47 +00:00
|
|
|
pub path: String,
|
|
|
|
pub url: Option<String>,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigApplication {
|
|
|
|
pub https: VeilidConfigHTTPS,
|
|
|
|
pub http: VeilidConfigHTTP,
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigUDP {
|
|
|
|
pub enabled: bool,
|
|
|
|
pub socket_pool_size: u32,
|
|
|
|
pub listen_address: String,
|
|
|
|
pub public_address: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigTCP {
|
|
|
|
pub connect: bool,
|
|
|
|
pub listen: bool,
|
|
|
|
pub max_connections: u32,
|
|
|
|
pub listen_address: String,
|
|
|
|
pub public_address: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigWS {
|
|
|
|
pub connect: bool,
|
|
|
|
pub listen: bool,
|
|
|
|
pub max_connections: u32,
|
|
|
|
pub listen_address: String,
|
|
|
|
pub path: String,
|
2021-12-09 21:00:47 +00:00
|
|
|
pub url: Option<String>,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigWSS {
|
|
|
|
pub connect: bool,
|
|
|
|
pub listen: bool,
|
|
|
|
pub max_connections: u32,
|
|
|
|
pub listen_address: String,
|
|
|
|
pub path: String,
|
2021-12-09 21:00:47 +00:00
|
|
|
pub url: Option<String>, // Fixed URL is not optional for TLS-based protocols and is dynamically validated
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigProtocol {
|
|
|
|
pub udp: VeilidConfigUDP,
|
|
|
|
pub tcp: VeilidConfigTCP,
|
|
|
|
pub ws: VeilidConfigWS,
|
|
|
|
pub wss: VeilidConfigWSS,
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigTLS {
|
|
|
|
pub certificate_path: String,
|
|
|
|
pub private_key_path: String,
|
2022-01-27 14:53:01 +00:00
|
|
|
pub connection_initial_timeout_ms: u32,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigDHT {
|
2022-01-27 14:53:01 +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-01-27 14:53:01 +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-01-27 14:53:01 +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-01-27 14:53:01 +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-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigRPC {
|
|
|
|
pub concurrency: u32,
|
|
|
|
pub queue_size: u32,
|
2022-01-27 14:53:01 +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-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2022-03-24 14:14:50 +00:00
|
|
|
pub struct VeilidConfigRoutingTable {
|
|
|
|
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,
|
|
|
|
}
|
2021-11-22 16:28:30 +00:00
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigNetwork {
|
2022-01-27 14:53:01 +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,
|
2022-04-03 16:58:06 +00:00
|
|
|
pub client_whitelist_timeout_ms: u32,
|
2022-04-16 15:18:54 +00:00
|
|
|
pub reverse_connection_receipt_time_ms: u32,
|
|
|
|
pub hole_punch_receipt_time_ms: u32,
|
2022-05-31 23:54:52 +00:00
|
|
|
pub node_id: DHTKey,
|
|
|
|
pub node_id_secret: DHTKeySecret,
|
2021-11-22 16:28:30 +00:00
|
|
|
pub bootstrap: Vec<String>,
|
2022-05-16 15:52:48 +00:00
|
|
|
pub bootstrap_nodes: Vec<String>,
|
2022-03-24 14:14:50 +00:00
|
|
|
pub routing_table: VeilidConfigRoutingTable,
|
2021-11-22 16:28:30 +00:00
|
|
|
pub rpc: VeilidConfigRPC,
|
|
|
|
pub dht: VeilidConfigDHT,
|
|
|
|
pub upnp: bool,
|
|
|
|
pub natpmp: bool,
|
2022-08-07 18:55:48 +00:00
|
|
|
pub detect_address_changes: bool,
|
2021-12-26 15:28:28 +00:00
|
|
|
pub enable_local_peer_scope: bool,
|
2021-11-24 01:19:16 +00:00
|
|
|
pub restricted_nat_retries: u32,
|
2021-11-22 16:28:30 +00:00
|
|
|
pub tls: VeilidConfigTLS,
|
|
|
|
pub application: VeilidConfigApplication,
|
|
|
|
pub protocol: VeilidConfigProtocol,
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigTableStore {
|
|
|
|
pub directory: String,
|
2022-01-19 02:21:11 +00:00
|
|
|
pub delete: bool,
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2022-01-19 02:21:11 +00:00
|
|
|
pub struct VeilidConfigBlockStore {
|
|
|
|
pub directory: String,
|
|
|
|
pub delete: bool,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2022-01-09 04:33:25 +00:00
|
|
|
pub struct VeilidConfigProtectedStore {
|
|
|
|
pub allow_insecure_fallback: bool,
|
|
|
|
pub always_use_insecure_storage: bool,
|
|
|
|
pub insecure_fallback_directory: String,
|
2022-01-19 02:21:11 +00:00
|
|
|
pub delete: bool,
|
2022-01-09 04:33:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigCapabilities {
|
|
|
|
pub protocol_udp: bool,
|
|
|
|
pub protocol_connect_tcp: bool,
|
|
|
|
pub protocol_accept_tcp: bool,
|
|
|
|
pub protocol_connect_ws: bool,
|
|
|
|
pub protocol_accept_ws: bool,
|
|
|
|
pub protocol_connect_wss: bool,
|
|
|
|
pub protocol_accept_wss: bool,
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:18:42 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
2022-02-01 03:47:17 +00:00
|
|
|
pub enum VeilidConfigLogLevel {
|
|
|
|
Off,
|
|
|
|
Error,
|
|
|
|
Warn,
|
|
|
|
Info,
|
|
|
|
Debug,
|
|
|
|
Trace,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VeilidConfigLogLevel {
|
2022-06-08 01:31:05 +00:00
|
|
|
pub fn to_veilid_log_level(&self) -> Option<VeilidLogLevel> {
|
2022-02-01 03:47:17 +00:00
|
|
|
match self {
|
2022-06-08 01:31:05 +00:00
|
|
|
Self::Off => None,
|
|
|
|
Self::Error => Some(VeilidLogLevel::Error),
|
|
|
|
Self::Warn => Some(VeilidLogLevel::Warn),
|
|
|
|
Self::Info => Some(VeilidLogLevel::Info),
|
|
|
|
Self::Debug => Some(VeilidLogLevel::Debug),
|
|
|
|
Self::Trace => Some(VeilidLogLevel::Trace),
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 01:51:38 +00:00
|
|
|
pub fn to_tracing_level_filter(&self) -> level_filters::LevelFilter {
|
|
|
|
match self {
|
|
|
|
Self::Off => level_filters::LevelFilter::OFF,
|
|
|
|
Self::Error => level_filters::LevelFilter::ERROR,
|
|
|
|
Self::Warn => level_filters::LevelFilter::WARN,
|
|
|
|
Self::Info => level_filters::LevelFilter::INFO,
|
|
|
|
Self::Debug => level_filters::LevelFilter::DEBUG,
|
|
|
|
Self::Trace => level_filters::LevelFilter::TRACE,
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 01:31:05 +00:00
|
|
|
pub fn from_veilid_log_level(level: Option<VeilidLogLevel>) -> Self {
|
|
|
|
match level {
|
|
|
|
None => Self::Off,
|
|
|
|
Some(VeilidLogLevel::Error) => Self::Error,
|
|
|
|
Some(VeilidLogLevel::Warn) => Self::Warn,
|
|
|
|
Some(VeilidLogLevel::Info) => Self::Info,
|
|
|
|
Some(VeilidLogLevel::Debug) => Self::Debug,
|
|
|
|
Some(VeilidLogLevel::Trace) => Self::Trace,
|
2022-02-01 03:47:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:13:52 +00:00
|
|
|
pub fn from_tracing_level_filter(level: level_filters::LevelFilter) -> Self {
|
|
|
|
match level {
|
|
|
|
level_filters::LevelFilter::OFF => Self::Off,
|
|
|
|
level_filters::LevelFilter::ERROR => Self::Error,
|
|
|
|
level_filters::LevelFilter::WARN => Self::Warn,
|
|
|
|
level_filters::LevelFilter::INFO => Self::Info,
|
|
|
|
level_filters::LevelFilter::DEBUG => Self::Debug,
|
|
|
|
level_filters::LevelFilter::TRACE => Self::Trace,
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 03:47:17 +00:00
|
|
|
}
|
|
|
|
impl Default for VeilidConfigLogLevel {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Off
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 15:18:54 +00:00
|
|
|
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct VeilidConfigInner {
|
2022-01-09 04:33:25 +00:00
|
|
|
pub program_name: String,
|
2021-11-22 16:28:30 +00:00
|
|
|
pub namespace: String,
|
|
|
|
pub capabilities: VeilidConfigCapabilities,
|
2022-01-09 04:33:25 +00:00
|
|
|
pub protected_store: VeilidConfigProtectedStore,
|
|
|
|
pub table_store: VeilidConfigTableStore,
|
2022-01-19 02:21:11 +00:00
|
|
|
pub block_store: VeilidConfigBlockStore,
|
2021-11-22 16:28:30 +00:00
|
|
|
pub network: VeilidConfigNetwork,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct VeilidConfig {
|
|
|
|
inner: Arc<RwLock<VeilidConfigInner>>,
|
|
|
|
}
|
|
|
|
|
2022-06-10 21:07:10 +00:00
|
|
|
impl fmt::Debug for VeilidConfig {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let inner = self.inner.read();
|
|
|
|
f.debug_struct("VeilidConfig")
|
|
|
|
.field("inner", &*inner)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 17:44:21 +00:00
|
|
|
impl Default for VeilidConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 16:28:30 +00:00
|
|
|
impl VeilidConfig {
|
|
|
|
fn new_inner() -> VeilidConfigInner {
|
|
|
|
VeilidConfigInner::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
inner: Arc::new(RwLock::new(Self::new_inner())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 03:15:51 +00:00
|
|
|
pub fn setup_from_json(&mut self, config: String) -> Result<(), VeilidAPIError> {
|
2022-02-09 15:00:19 +00:00
|
|
|
{
|
|
|
|
let mut inner = self.inner.write();
|
2022-07-07 03:15:51 +00:00
|
|
|
*inner = serde_json::from_str(&config).map_err(VeilidAPIError::generic)?;
|
2022-02-09 15:00:19 +00:00
|
|
|
}
|
2022-02-09 14:47:36 +00:00
|
|
|
|
|
|
|
// Validate settings
|
2022-03-09 03:32:12 +00:00
|
|
|
self.validate()?;
|
2022-02-09 14:47:36 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-07 03:15:51 +00:00
|
|
|
pub fn setup(&mut self, cb: ConfigCallback) -> Result<(), VeilidAPIError> {
|
2021-11-22 16:28:30 +00:00
|
|
|
macro_rules! get_config {
|
|
|
|
($key:expr) => {
|
|
|
|
let keyname = &stringify!($key)[6..];
|
2021-12-09 21:00:47 +00:00
|
|
|
$key = *cb(keyname.to_owned())?.downcast().map_err(|_| {
|
2022-07-07 03:15:51 +00:00
|
|
|
let err = format!("incorrect type for key {}", keyname);
|
2021-12-09 21:00:47 +00:00
|
|
|
debug!("{}", err);
|
2022-07-07 03:15:51 +00:00
|
|
|
VeilidAPIError::generic(err)
|
2021-12-09 21:00:47 +00:00
|
|
|
})?;
|
2021-11-22 16:28:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let mut inner = self.inner.write();
|
2022-01-09 04:33:25 +00:00
|
|
|
get_config!(inner.program_name);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.namespace);
|
|
|
|
get_config!(inner.capabilities.protocol_udp);
|
|
|
|
get_config!(inner.capabilities.protocol_connect_tcp);
|
|
|
|
get_config!(inner.capabilities.protocol_accept_tcp);
|
|
|
|
get_config!(inner.capabilities.protocol_connect_ws);
|
|
|
|
get_config!(inner.capabilities.protocol_accept_ws);
|
|
|
|
get_config!(inner.capabilities.protocol_connect_wss);
|
|
|
|
get_config!(inner.capabilities.protocol_accept_wss);
|
2022-01-09 04:33:25 +00:00
|
|
|
get_config!(inner.table_store.directory);
|
2022-01-19 02:21:11 +00:00
|
|
|
get_config!(inner.table_store.delete);
|
|
|
|
get_config!(inner.block_store.directory);
|
|
|
|
get_config!(inner.block_store.delete);
|
2022-01-09 04:33:25 +00:00
|
|
|
get_config!(inner.protected_store.allow_insecure_fallback);
|
|
|
|
get_config!(inner.protected_store.always_use_insecure_storage);
|
|
|
|
get_config!(inner.protected_store.insecure_fallback_directory);
|
2022-01-19 02:21:11 +00:00
|
|
|
get_config!(inner.protected_store.delete);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.node_id);
|
|
|
|
get_config!(inner.network.node_id_secret);
|
2022-01-27 14:53:01 +00:00
|
|
|
get_config!(inner.network.connection_initial_timeout_ms);
|
2022-03-19 22:19:40 +00:00
|
|
|
get_config!(inner.network.connection_inactivity_timeout_ms);
|
2022-05-05 00:40:10 +00:00
|
|
|
get_config!(inner.network.max_connections_per_ip4);
|
|
|
|
get_config!(inner.network.max_connections_per_ip6_prefix);
|
|
|
|
get_config!(inner.network.max_connections_per_ip6_prefix_size);
|
|
|
|
get_config!(inner.network.max_connection_frequency_per_min);
|
2022-04-03 16:58:06 +00:00
|
|
|
get_config!(inner.network.client_whitelist_timeout_ms);
|
2022-05-24 21:13:52 +00:00
|
|
|
get_config!(inner.network.reverse_connection_receipt_time_ms);
|
|
|
|
get_config!(inner.network.hole_punch_receipt_time_ms);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.bootstrap);
|
2022-05-16 15:52:48 +00:00
|
|
|
get_config!(inner.network.bootstrap_nodes);
|
2022-03-24 14:14:50 +00:00
|
|
|
get_config!(inner.network.routing_table.limit_over_attached);
|
|
|
|
get_config!(inner.network.routing_table.limit_fully_attached);
|
|
|
|
get_config!(inner.network.routing_table.limit_attached_strong);
|
|
|
|
get_config!(inner.network.routing_table.limit_attached_good);
|
|
|
|
get_config!(inner.network.routing_table.limit_attached_weak);
|
2022-01-27 14:53:01 +00:00
|
|
|
get_config!(inner.network.dht.resolve_node_timeout_ms);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.dht.resolve_node_count);
|
|
|
|
get_config!(inner.network.dht.resolve_node_fanout);
|
|
|
|
get_config!(inner.network.dht.max_find_node_count);
|
2022-01-27 14:53:01 +00:00
|
|
|
get_config!(inner.network.dht.get_value_timeout_ms);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.dht.get_value_count);
|
|
|
|
get_config!(inner.network.dht.get_value_fanout);
|
2022-01-27 14:53:01 +00:00
|
|
|
get_config!(inner.network.dht.set_value_timeout_ms);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.dht.set_value_count);
|
|
|
|
get_config!(inner.network.dht.set_value_fanout);
|
|
|
|
get_config!(inner.network.dht.min_peer_count);
|
2022-01-27 14:53:01 +00:00
|
|
|
get_config!(inner.network.dht.min_peer_refresh_time_ms);
|
|
|
|
get_config!(inner.network.dht.validate_dial_info_receipt_time_ms);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.rpc.concurrency);
|
|
|
|
get_config!(inner.network.rpc.queue_size);
|
2022-01-27 14:53:01 +00:00
|
|
|
get_config!(inner.network.rpc.max_timestamp_behind_ms);
|
|
|
|
get_config!(inner.network.rpc.max_timestamp_ahead_ms);
|
|
|
|
get_config!(inner.network.rpc.timeout_ms);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.rpc.max_route_hop_count);
|
|
|
|
get_config!(inner.network.upnp);
|
|
|
|
get_config!(inner.network.natpmp);
|
2022-08-07 18:55:48 +00:00
|
|
|
get_config!(inner.network.detect_address_changes);
|
2021-12-26 15:28:28 +00:00
|
|
|
get_config!(inner.network.enable_local_peer_scope);
|
2021-11-24 01:19:16 +00:00
|
|
|
get_config!(inner.network.restricted_nat_retries);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.tls.certificate_path);
|
|
|
|
get_config!(inner.network.tls.private_key_path);
|
2022-01-27 14:53:01 +00:00
|
|
|
get_config!(inner.network.tls.connection_initial_timeout_ms);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.application.https.enabled);
|
|
|
|
get_config!(inner.network.application.https.listen_address);
|
2021-12-09 21:00:47 +00:00
|
|
|
get_config!(inner.network.application.https.path);
|
|
|
|
get_config!(inner.network.application.https.url);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.application.http.enabled);
|
|
|
|
get_config!(inner.network.application.http.listen_address);
|
2021-12-09 21:00:47 +00:00
|
|
|
get_config!(inner.network.application.http.path);
|
|
|
|
get_config!(inner.network.application.http.url);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.protocol.udp.enabled);
|
|
|
|
get_config!(inner.network.protocol.udp.socket_pool_size);
|
|
|
|
get_config!(inner.network.protocol.udp.listen_address);
|
|
|
|
get_config!(inner.network.protocol.udp.public_address);
|
|
|
|
get_config!(inner.network.protocol.tcp.connect);
|
|
|
|
get_config!(inner.network.protocol.tcp.listen);
|
|
|
|
get_config!(inner.network.protocol.tcp.max_connections);
|
|
|
|
get_config!(inner.network.protocol.tcp.listen_address);
|
|
|
|
get_config!(inner.network.protocol.tcp.public_address);
|
|
|
|
get_config!(inner.network.protocol.ws.connect);
|
|
|
|
get_config!(inner.network.protocol.ws.listen);
|
|
|
|
get_config!(inner.network.protocol.ws.max_connections);
|
|
|
|
get_config!(inner.network.protocol.ws.listen_address);
|
|
|
|
get_config!(inner.network.protocol.ws.path);
|
2021-12-09 21:00:47 +00:00
|
|
|
get_config!(inner.network.protocol.ws.url);
|
2021-11-22 16:28:30 +00:00
|
|
|
get_config!(inner.network.protocol.wss.connect);
|
|
|
|
get_config!(inner.network.protocol.wss.listen);
|
|
|
|
get_config!(inner.network.protocol.wss.max_connections);
|
|
|
|
get_config!(inner.network.protocol.wss.listen_address);
|
|
|
|
get_config!(inner.network.protocol.wss.path);
|
2021-12-09 21:00:47 +00:00
|
|
|
get_config!(inner.network.protocol.wss.url);
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
2021-12-09 21:00:47 +00:00
|
|
|
// Validate settings
|
2022-03-09 03:32:12 +00:00
|
|
|
self.validate()?;
|
2021-12-09 21:00:47 +00:00
|
|
|
|
|
|
|
Ok(())
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self) -> RwLockReadGuard<VeilidConfigInner> {
|
|
|
|
self.inner.read()
|
|
|
|
}
|
|
|
|
|
2022-03-09 03:32:12 +00:00
|
|
|
pub fn get_mut(&self) -> RwLockWriteGuard<VeilidConfigInner> {
|
|
|
|
self.inner.write()
|
|
|
|
}
|
|
|
|
|
2022-07-07 03:15:51 +00:00
|
|
|
pub fn get_key_json(&self, key: &str) -> Result<String, VeilidAPIError> {
|
2022-03-09 03:32:12 +00:00
|
|
|
let c = self.get();
|
|
|
|
|
|
|
|
// Generate json from whole config
|
2022-07-07 03:15:51 +00:00
|
|
|
let jc = serde_json::to_string(&*c).map_err(VeilidAPIError::generic)?;
|
|
|
|
let jvc = json::parse(&jc).map_err(VeilidAPIError::generic)?;
|
2022-03-09 03:32:12 +00:00
|
|
|
|
|
|
|
// Find requested subkey
|
2022-03-10 14:51:53 +00:00
|
|
|
if key.is_empty() {
|
|
|
|
Ok(jvc.to_string())
|
|
|
|
} else {
|
|
|
|
// Split key into path parts
|
|
|
|
let keypath: Vec<&str> = key.split('.').collect();
|
|
|
|
let mut out = &jvc;
|
|
|
|
for k in keypath {
|
|
|
|
if !out.has_key(k) {
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_parse!(format!("invalid subkey in key '{}'", key), k);
|
2022-03-10 14:51:53 +00:00
|
|
|
}
|
|
|
|
out = &out[k];
|
2022-03-09 03:32:12 +00:00
|
|
|
}
|
2022-03-10 14:51:53 +00:00
|
|
|
Ok(out.to_string())
|
2022-03-09 03:32:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-07 03:15:51 +00:00
|
|
|
pub fn set_key_json(&self, key: &str, value: &str) -> Result<(), VeilidAPIError> {
|
2022-03-09 03:32:12 +00:00
|
|
|
let mut c = self.get_mut();
|
|
|
|
|
|
|
|
// Split key into path parts
|
|
|
|
let keypath: Vec<&str> = key.split('.').collect();
|
|
|
|
|
|
|
|
// Convert value into jsonvalue
|
2022-07-07 03:15:51 +00:00
|
|
|
let newval = json::parse(value).map_err(VeilidAPIError::generic)?;
|
2022-03-09 03:32:12 +00:00
|
|
|
|
|
|
|
// Generate json from whole config
|
2022-07-07 03:15:51 +00:00
|
|
|
let jc = serde_json::to_string(&*c).map_err(VeilidAPIError::generic)?;
|
|
|
|
let mut jvc = json::parse(&jc).map_err(VeilidAPIError::generic)?;
|
2022-03-09 03:32:12 +00:00
|
|
|
|
|
|
|
// Find requested subkey
|
|
|
|
let newconfigstring = if let Some((objkeyname, objkeypath)) = keypath.split_last() {
|
|
|
|
// Replace subkey
|
|
|
|
let mut out = &mut jvc;
|
|
|
|
for k in objkeypath {
|
2022-03-10 14:51:53 +00:00
|
|
|
if !out.has_key(*k) {
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_parse!(format!("invalid subkey in key '{}'", key), k);
|
2022-03-09 03:32:12 +00:00
|
|
|
}
|
2022-03-10 14:51:53 +00:00
|
|
|
out = &mut out[*k];
|
2022-03-09 03:32:12 +00:00
|
|
|
}
|
|
|
|
if !out.has_key(objkeyname) {
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_parse!(format!("invalid subkey in key '{}'", key), objkeyname);
|
2022-03-09 03:32:12 +00:00
|
|
|
}
|
|
|
|
out[*objkeyname] = newval;
|
|
|
|
jvc.to_string()
|
|
|
|
} else {
|
|
|
|
newval.to_string()
|
|
|
|
};
|
|
|
|
// Generate and validate new config
|
|
|
|
let mut newconfig = VeilidConfig::new();
|
|
|
|
newconfig.setup_from_json(newconfigstring)?;
|
|
|
|
// Replace whole config
|
|
|
|
*c = newconfig.get().clone();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-07-07 03:15:51 +00:00
|
|
|
fn validate(&self) -> Result<(), VeilidAPIError> {
|
2021-12-09 21:00:47 +00:00
|
|
|
let inner = self.inner.read();
|
2022-01-09 04:33:25 +00:00
|
|
|
|
|
|
|
if inner.program_name.is_empty() {
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("Program name must not be empty in 'program_name'");
|
2022-01-09 04:33:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-09 21:27:17 +00:00
|
|
|
// if inner.network.protocol.udp.enabled {
|
|
|
|
// // Validate UDP settings
|
|
|
|
// }
|
2021-12-09 21:00:47 +00:00
|
|
|
if inner.network.protocol.tcp.listen {
|
|
|
|
// Validate TCP settings
|
|
|
|
if inner.network.protocol.tcp.max_connections == 0 {
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("TCP max connections must be > 0 in config key 'network.protocol.tcp.max_connections'");
|
2021-12-09 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if inner.network.protocol.ws.listen {
|
|
|
|
// Validate WS settings
|
|
|
|
if inner.network.protocol.ws.max_connections == 0 {
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("WS max connections must be > 0 in config key 'network.protocol.ws.max_connections'");
|
2021-12-09 21:00:47 +00:00
|
|
|
}
|
|
|
|
if inner.network.application.https.enabled
|
|
|
|
&& inner.network.application.https.path == inner.network.protocol.ws.path
|
|
|
|
{
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("WS path conflicts with HTTPS application path in config key 'network.protocol.ws.path'");
|
2021-12-09 21:00:47 +00:00
|
|
|
}
|
|
|
|
if inner.network.application.http.enabled
|
|
|
|
&& inner.network.application.http.path == inner.network.protocol.ws.path
|
|
|
|
{
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("WS path conflicts with HTTP application path in config key 'network.protocol.ws.path'");
|
2021-12-09 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if inner.network.protocol.wss.listen {
|
|
|
|
// Validate WSS settings
|
|
|
|
if inner.network.protocol.wss.max_connections == 0 {
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("WSS max connections must be > 0 in config key 'network.protocol.wss.max_connections'");
|
2021-12-09 21:00:47 +00:00
|
|
|
}
|
|
|
|
if inner
|
|
|
|
.network
|
|
|
|
.protocol
|
|
|
|
.wss
|
|
|
|
.url
|
|
|
|
.as_ref()
|
|
|
|
.map(|u| u.is_empty())
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!(
|
|
|
|
"WSS URL must be specified in config key 'network.protocol.wss.url'"
|
2021-12-09 21:00:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if inner.network.application.https.enabled
|
|
|
|
&& inner.network.application.https.path == inner.network.protocol.wss.path
|
|
|
|
{
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("WSS path conflicts with HTTPS application path in config key 'network.protocol.ws.path'");
|
2021-12-09 21:00:47 +00:00
|
|
|
}
|
|
|
|
if inner.network.application.http.enabled
|
|
|
|
&& inner.network.application.http.path == inner.network.protocol.wss.path
|
|
|
|
{
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("WSS path conflicts with HTTP application path in config key 'network.protocol.ws.path'");
|
2021-12-09 21:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if inner.network.application.https.enabled {
|
|
|
|
// Validate HTTPS settings
|
|
|
|
if inner
|
|
|
|
.network
|
|
|
|
.application
|
|
|
|
.https
|
|
|
|
.url
|
|
|
|
.as_ref()
|
|
|
|
.map(|u| u.is_empty())
|
|
|
|
.unwrap_or_default()
|
|
|
|
{
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!(
|
2021-12-09 21:00:47 +00:00
|
|
|
"HTTPS URL must be specified in config key 'network.application.https.url'"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:28:30 +00:00
|
|
|
// Get the node id from config if one is specified
|
2022-01-09 04:33:25 +00:00
|
|
|
// Must be done -after- protected store startup
|
2022-07-07 03:15:51 +00:00
|
|
|
pub async fn init_node_id(
|
|
|
|
&self,
|
|
|
|
protected_store: intf::ProtectedStore,
|
|
|
|
) -> Result<(), VeilidAPIError> {
|
2022-01-09 05:13:47 +00:00
|
|
|
let mut node_id = self.inner.read().network.node_id;
|
|
|
|
let mut node_id_secret = self.inner.read().network.node_id_secret;
|
2021-11-22 16:28:30 +00:00
|
|
|
// See if node id was previously stored in the protected store
|
|
|
|
if !node_id.valid {
|
|
|
|
debug!("pulling node id from storage");
|
2022-07-07 03:15:51 +00:00
|
|
|
if let Some(s) = protected_store
|
|
|
|
.load_user_secret_string("node_id")
|
|
|
|
.await
|
|
|
|
.map_err(VeilidAPIError::internal)?
|
|
|
|
{
|
2021-11-22 16:28:30 +00:00
|
|
|
debug!("node id found in storage");
|
2022-07-07 03:15:51 +00:00
|
|
|
node_id = DHTKey::try_decode(s.as_str()).map_err(VeilidAPIError::internal)?
|
2021-11-22 16:28:30 +00:00
|
|
|
} else {
|
|
|
|
debug!("node id not found in storage");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if node id secret was previously stored in the protected store
|
|
|
|
if !node_id_secret.valid {
|
|
|
|
debug!("pulling node id secret from storage");
|
2022-01-09 04:33:25 +00:00
|
|
|
if let Some(s) = protected_store
|
|
|
|
.load_user_secret_string("node_id_secret")
|
2022-07-07 03:15:51 +00:00
|
|
|
.await
|
|
|
|
.map_err(VeilidAPIError::internal)?
|
2021-11-22 16:28:30 +00:00
|
|
|
{
|
|
|
|
debug!("node id secret found in storage");
|
2022-07-07 03:15:51 +00:00
|
|
|
node_id_secret =
|
|
|
|
DHTKeySecret::try_decode(s.as_str()).map_err(VeilidAPIError::internal)?
|
2021-11-22 16:28:30 +00:00
|
|
|
} else {
|
|
|
|
debug!("node id secret not found in storage");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a node id from storage, check it
|
|
|
|
if node_id.valid && node_id_secret.valid {
|
|
|
|
// Validate node id
|
2022-06-08 01:31:05 +00:00
|
|
|
if !dht::validate_key(&node_id, &node_id_secret) {
|
2022-07-07 03:15:51 +00:00
|
|
|
apibail_generic!("node id secret and node id key don't match");
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still don't have a valid node id, generate one
|
|
|
|
if !node_id.valid || !node_id_secret.valid {
|
|
|
|
debug!("generating new node id");
|
2022-05-31 23:54:52 +00:00
|
|
|
let (i, s) = generate_secret();
|
2021-11-22 16:28:30 +00:00
|
|
|
node_id = i;
|
|
|
|
node_id_secret = s;
|
|
|
|
}
|
|
|
|
info!("Node Id is {}", node_id.encode());
|
|
|
|
// info!("Node Id Secret is {}", node_id_secret.encode());
|
|
|
|
|
|
|
|
// Save the node id / secret in storage
|
2022-01-09 04:33:25 +00:00
|
|
|
protected_store
|
|
|
|
.save_user_secret_string("node_id", node_id.encode().as_str())
|
2022-07-07 03:15:51 +00:00
|
|
|
.await
|
|
|
|
.map_err(VeilidAPIError::internal)?;
|
2022-01-09 04:33:25 +00:00
|
|
|
protected_store
|
|
|
|
.save_user_secret_string("node_id_secret", node_id_secret.encode().as_str())
|
2022-07-07 03:15:51 +00:00
|
|
|
.await
|
|
|
|
.map_err(VeilidAPIError::internal)?;
|
2021-11-22 16:28:30 +00:00
|
|
|
|
2022-01-09 05:13:47 +00:00
|
|
|
self.inner.write().network.node_id = node_id;
|
|
|
|
self.inner.write().network.node_id_secret = node_id_secret;
|
2021-11-22 16:28:30 +00:00
|
|
|
|
|
|
|
trace!("init_node_id complete");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|