diplonat/src/diplonat.rs

70 lines
1.4 KiB
Rust
Raw Permalink Normal View History

use anyhow::{Result, anyhow};
2020-05-22 17:21:11 +00:00
use tokio::try_join;
use crate::config::ConfigOpts;
2020-05-22 16:41:13 +00:00
use crate::consul_actor::ConsulActor;
use crate::fw_actor::FirewallActor;
use crate::igd_actor::IgdActor;
2020-05-21 20:25:33 +00:00
2020-05-22 16:41:13 +00:00
pub struct Diplonat {
2020-05-22 17:21:11 +00:00
consul: ConsulActor,
firewall: Option<FirewallActor>,
igd: Option<IgdActor>,
}
2020-05-22 16:41:13 +00:00
impl Diplonat {
pub async fn new() -> Result<Self> {
let config = ConfigOpts::from_env()?;
println!("{:#?}", config);
let consul_actor = ConsulActor::new(config.consul);
2020-05-21 20:25:33 +00:00
let firewall_actor = FirewallActor::new(
config.firewall,
&consul_actor.rx_open_ports
).await?;
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!"));
}
2020-05-22 16:41:13 +00:00
let ctx = Self {
consul: consul_actor,
firewall: firewall_actor,
igd: igd_actor,
2020-05-09 14:50:38 +00:00
};
2020-05-09 14:27:54 +00:00
2020-05-09 14:50:38 +00:00
return Ok(ctx);
}
2020-05-22 16:41:13 +00:00
pub async fn listen(&mut self) -> Result<()> {
let firewall = &mut self.firewall;
let igd = &mut self.igd;
2020-05-22 17:21:11 +00:00
try_join!(
self.consul.listen(),
async {
match firewall {
Some(x) => x.listen().await,
None => Ok(())
}
},
async {
match igd {
Some(x) => x.listen().await,
None => Ok(())
}
},
2020-05-22 17:21:11 +00:00
)?;
2020-05-22 16:41:13 +00:00
2020-05-21 13:22:45 +00:00
return Ok(());
2020-05-09 14:50:38 +00:00
}
}