2021-09-22 13:52:50 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
|
2021-09-20 09:30:00 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2021-09-22 13:52:50 +00:00
|
|
|
use futures::future::{try_join_all, Future};
|
2020-05-22 17:21:11 +00:00
|
|
|
use tokio::try_join;
|
2021-08-16 09:19:16 +00:00
|
|
|
|
2021-09-20 14:07:58 +00:00
|
|
|
use crate::{
|
|
|
|
config::ConfigOpts, consul_actor::ConsulActor, fw_actor::FirewallActor, igd_actor::IgdActor,
|
|
|
|
};
|
2020-05-21 20:25:33 +00:00
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
pub struct Diplonat {
|
2021-09-22 13:52:50 +00:00
|
|
|
actors: Vec<Pin<Box<dyn Future<Output = Result<()>>>>>,
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
impl Diplonat {
|
|
|
|
pub async fn new() -> Result<Self> {
|
2021-08-25 15:20:31 +00:00
|
|
|
let config = ConfigOpts::from_env()?;
|
|
|
|
println!("{:#?}", config);
|
2021-09-20 09:30:00 +00:00
|
|
|
|
2021-08-25 15:20:31 +00:00
|
|
|
let consul_actor = ConsulActor::new(config.consul);
|
2021-09-22 13:52:50 +00:00
|
|
|
let mut actors = vec![Box::pin(consul_actor.listen())];
|
2020-05-21 20:25:33 +00:00
|
|
|
|
2021-09-22 13:52:50 +00:00
|
|
|
if let Some(actor) = FirewallActor::new(config.firewall, &consul_actor.rx_open_ports)? {
|
|
|
|
actors.push(Box::pin(actor.listen()));
|
|
|
|
}
|
|
|
|
if let Some(actor) = IgdActor::new(config.igd, &consul_actor.rx_open_ports).await? {
|
|
|
|
actors.push(Box::pin(actor.listen()));
|
|
|
|
}
|
2020-05-24 18:40:49 +00:00
|
|
|
|
2021-09-22 13:52:50 +00:00
|
|
|
if actors.len() == 1 {
|
2021-08-25 15:20:31 +00:00
|
|
|
return Err(anyhow!(
|
2021-09-20 09:30:00 +00:00
|
|
|
"At least enable *one* module, otherwise it's boring!"
|
2021-09-20 14:07:58 +00:00
|
|
|
))
|
2021-08-25 15:20:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 13:52:50 +00:00
|
|
|
Ok(Self { actors })
|
2020-05-09 14:50:38 +00:00
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
|
2021-09-22 13:52:50 +00:00
|
|
|
pub async fn listen(&self) -> Result<()> {
|
|
|
|
try_join_all(self.actors);
|
|
|
|
Ok(())
|
2020-05-09 14:50:38 +00:00
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|