diff options
author | adrien <adrien@luxeylab.net> | 2021-08-25 17:20:31 +0200 |
---|---|---|
committer | adrien <adrien@luxeylab.net> | 2021-08-26 16:05:39 +0200 |
commit | 76fe63791baea5577bde3f83243f85ba781f46fe (patch) | |
tree | 1f7ffed5671f30cbd13093db02d7462183bf1952 /src/diplonat.rs | |
parent | fa25c54e47decf9f323ba0c614f4d9de106626d5 (diff) | |
download | diplonat-76fe63791baea5577bde3f83243f85ba781f46fe.tar.gz diplonat-76fe63791baea5577bde3f83243f85ba781f46fe.zip |
Rewrote the configuration options to make Diplonat modular: IGD and Firewall modules will only be enabled if correponding `DIPLONAT_*_ENABLE` is set. (Breaking changes!)
Also rearranged imports throughout the project, because I couldn't resist, sorry.
Diffstat (limited to 'src/diplonat.rs')
-rw-r--r-- | src/diplonat.rs | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/src/diplonat.rs b/src/diplonat.rs index 7049530..6334e5b 100644 --- a/src/diplonat.rs +++ b/src/diplonat.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Result, anyhow}; use tokio::try_join; use crate::config::ConfigOpts; @@ -8,43 +8,60 @@ use crate::igd_actor::IgdActor; pub struct Diplonat { consul: ConsulActor, - firewall: FirewallActor, - igd: IgdActor, + + firewall: Option<FirewallActor>, + igd: Option<IgdActor>, } impl Diplonat { pub async fn new() -> Result<Self> { - let rt_cfg = ConfigOpts::from_env()?; - println!("{:#?}", rt_cfg); + let config = ConfigOpts::from_env()?; + println!("{:#?}", config); - let ca = ConsulActor::new(&rt_cfg.consul.url, &rt_cfg.consul.node_name); + let consul_actor = ConsulActor::new(config.consul); - let fw = FirewallActor::new( - rt_cfg.firewall.refresh_time, - &ca.rx_open_ports + let firewall_actor = FirewallActor::new( + config.firewall, + &consul_actor.rx_open_ports ).await?; - let ia = IgdActor::new( - &rt_cfg.igd.private_ip, - rt_cfg.igd.refresh_time, - rt_cfg.igd.expiration_time, - &ca.rx_open_ports + let igd_actor = IgdActor::new( + config.igd, + &consul_actor.rx_open_ports ).await?; + if firewall_actor.is_none() && igd_actor.is_none() { + return Err(anyhow!( + "At least enable *one* module, otherwise it's boring!")); + } + let ctx = Self { - consul: ca, - igd: ia, - firewall: fw + consul: consul_actor, + firewall: firewall_actor, + igd: igd_actor, }; return Ok(ctx); } pub async fn listen(&mut self) -> Result<()> { + let firewall = &mut self.firewall; + let igd = &mut self.igd; + try_join!( self.consul.listen(), - self.igd.listen(), - self.firewall.listen() + async { + match firewall { + Some(x) => x.listen().await, + None => Ok(()) + } + }, + async { + match igd { + Some(x) => x.listen().await, + None => Ok(()) + } + }, )?; return Ok(()); |