diff options
Diffstat (limited to 'src/config/runtime.rs')
-rw-r--r-- | src/config/runtime.rs | 48 |
1 files changed, 41 insertions, 7 deletions
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, + }) + } +} |