aboutsummaryrefslogtreecommitdiff
path: root/src/config
diff options
context:
space:
mode:
Diffstat (limited to 'src/config')
-rw-r--r--src/config/mod.rs3
-rw-r--r--src/config/options.rs4
-rw-r--r--src/config/options_test.rs4
-rw-r--r--src/config/runtime.rs48
4 files changed, 50 insertions, 9 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 2bf8f66..a9cbd13 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -5,9 +5,10 @@ mod runtime;
pub use options::{ConfigOpts, ConfigOptsAcme, ConfigOptsBase, ConfigOptsConsul};
pub use runtime::{
- RuntimeConfig, RuntimeConfigAcme, RuntimeConfigConsul, RuntimeConfigFirewall, RuntimeConfigIgd,
+ RuntimeConfig, RuntimeConfigAcme, RuntimeConfigConsul, RuntimeConfigFirewall, RuntimeConfigIgd, RuntimeConfigStun
};
pub const EXPIRATION_TIME: u16 = 300;
pub const REFRESH_TIME: u16 = 60;
pub const CONSUL_URL: &str = "http://127.0.0.1:8500";
+pub const STUN_SERVER: &str = "stun.nextcloud.com:443";
diff --git a/src/config/options.rs b/src/config/options.rs
index 793838a..08cdd15 100644
--- a/src/config/options.rs
+++ b/src/config/options.rs
@@ -17,6 +17,8 @@ pub struct ConfigOptsBase {
pub expiration_time: Option<u16>,
/// Refresh time for IGD and Firewall rules [default: 300]
pub refresh_time: Option<u16>,
+ /// STUN server [default: stun.nextcloud.com:443]
+ pub stun_server: Option<String>,
}
/// ACME configuration options
@@ -69,7 +71,7 @@ impl ConfigOpts {
}
// Currently only used in tests
- #[allow(dead_code)]
+ #[cfg(test)]
pub fn from_iter<Iter: Clone>(iter: Iter) -> Result<RuntimeConfig>
where
Iter: IntoIterator<Item = (String, String)>,
diff --git a/src/config/options_test.rs b/src/config/options_test.rs
index 6b91235..427b70e 100644
--- a/src/config/options_test.rs
+++ b/src/config/options_test.rs
@@ -21,6 +21,10 @@ fn all_valid_options() -> HashMap<String, String> {
let mut opts = minimal_valid_options();
opts.insert("DIPLONAT_EXPIRATION_TIME".to_string(), "30".to_string());
opts.insert(
+ "DIPLONAT_STUN_SERVER".to_string(),
+ "stun.nextcloud.com:443".to_string(),
+ );
+ opts.insert(
"DIPLONAT_PRIVATE_IP".to_string(),
"172.123.43.555".to_string(),
);
diff --git a/src/config/runtime.rs b/src/config/runtime.rs
index 2e7b573..50624de 100644
--- a/src/config/runtime.rs
+++ b/src/config/runtime.rs
@@ -1,6 +1,7 @@
use std::fs::File;
use std::io::Read;
use std::time::Duration;
+use std::net::{SocketAddr, ToSocketAddrs};
use anyhow::{anyhow, bail, Result};
@@ -36,25 +37,35 @@ pub struct RuntimeConfigIgd {
}
#[derive(Debug)]
+pub struct RuntimeConfigStun {
+ pub stun_server_v4: SocketAddr,
+ pub stun_server_v6: SocketAddr,
+ pub refresh_time: Duration,
+}
+
+#[derive(Debug)]
pub struct RuntimeConfig {
pub acme: Option<RuntimeConfigAcme>,
pub consul: RuntimeConfigConsul,
pub firewall: RuntimeConfigFirewall,
pub igd: RuntimeConfigIgd,
+ pub stun: RuntimeConfigStun,
}
impl RuntimeConfig {
pub fn new(opts: ConfigOpts) -> Result<Self> {
- let acme = RuntimeConfigAcme::new(opts.acme.clone())?;
- let consul = RuntimeConfigConsul::new(opts.consul.clone())?;
- let firewall = RuntimeConfigFirewall::new(opts.base.clone())?;
- let igd = RuntimeConfigIgd::new(opts.base.clone())?;
+ let acme = RuntimeConfigAcme::new(opts.acme)?;
+ let consul = RuntimeConfigConsul::new(opts.consul)?;
+ let firewall = RuntimeConfigFirewall::new(&opts.base)?;
+ let igd = RuntimeConfigIgd::new(&opts.base)?;
+ let stun = RuntimeConfigStun::new(&opts.base)?;
Ok(Self {
acme,
consul,
firewall,
igd,
+ stun,
})
}
}
@@ -115,7 +126,7 @@ impl RuntimeConfigConsul {
}
impl RuntimeConfigFirewall {
- pub(super) fn new(opts: ConfigOptsBase) -> Result<Self> {
+ pub(super) fn new(opts: &ConfigOptsBase) -> Result<Self> {
let refresh_time = Duration::from_secs(opts.refresh_time.unwrap_or(super::REFRESH_TIME).into());
Ok(Self { refresh_time })
@@ -123,8 +134,8 @@ impl RuntimeConfigFirewall {
}
impl RuntimeConfigIgd {
- pub(super) fn new(opts: ConfigOptsBase) -> Result<Self> {
- let private_ip = opts.private_ip;
+ pub(super) fn new(opts: &ConfigOptsBase) -> Result<Self> {
+ let private_ip = opts.private_ip.clone();
let expiration_time = Duration::from_secs(
opts
.expiration_time
@@ -149,3 +160,26 @@ impl RuntimeConfigIgd {
})
}
}
+
+impl RuntimeConfigStun {
+ pub(super) fn new(opts: &ConfigOptsBase) -> Result<Self> {
+ let mut stun_server_v4 = None;
+ let mut stun_server_v6 = None;
+ for addr in opts.stun_server.as_deref().unwrap_or(super::STUN_SERVER).to_socket_addrs()? {
+ if addr.is_ipv4() {
+ stun_server_v4 = Some(addr);
+ }
+ if addr.is_ipv6() {
+ stun_server_v6 = Some(addr);
+ }
+ }
+
+ let refresh_time = Duration::from_secs(opts.refresh_time.unwrap_or(super::REFRESH_TIME).into());
+
+ Ok(Self {
+ stun_server_v4: stun_server_v4.ok_or(anyhow!("Unable to resolve STUN server's IPv4 address"))?,
+ stun_server_v6: stun_server_v6.ok_or(anyhow!("Unable to resolve STUN server's IPv6 address"))?,
+ refresh_time,
+ })
+ }
+}