From 615f926618471998f85ee184b378b1128340367b Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 4 Apr 2023 18:46:14 +0200 Subject: Add STUN actor that saves autodiscovered IPv4/IPv6 to Consul --- src/config/runtime.rs | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) (limited to 'src/config/runtime.rs') 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}; @@ -35,26 +36,36 @@ pub struct RuntimeConfigIgd { pub refresh_time: Duration, } +#[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, pub consul: RuntimeConfigConsul, pub firewall: RuntimeConfigFirewall, pub igd: RuntimeConfigIgd, + pub stun: RuntimeConfigStun, } impl RuntimeConfig { pub fn new(opts: ConfigOpts) -> Result { - 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 { + pub(super) fn new(opts: &ConfigOptsBase) -> Result { 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 { - let private_ip = opts.private_ip; + pub(super) fn new(opts: &ConfigOptsBase) -> Result { + 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 { + 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, + }) + } +} -- cgit v1.2.3