2020-05-22 16:41:13 +00:00
|
|
|
use anyhow::Result;
|
2020-05-22 17:21:11 +00:00
|
|
|
use tokio::try_join;
|
2021-08-16 09:19:16 +00:00
|
|
|
|
2021-09-20 13:00:31 +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 {
|
2020-05-22 17:21:11 +00:00
|
|
|
consul: ConsulActor,
|
2021-08-16 09:19:16 +00:00
|
|
|
firewall: FirewallActor,
|
2020-05-24 18:40:49 +00:00
|
|
|
igd: IgdActor,
|
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-16 09:19:16 +00:00
|
|
|
let rt_cfg = ConfigOpts::from_env()?;
|
|
|
|
println!("{:#?}", rt_cfg);
|
2021-09-11 14:34:03 +00:00
|
|
|
|
2021-08-16 09:19:16 +00:00
|
|
|
let ca = ConsulActor::new(&rt_cfg.consul.url, &rt_cfg.consul.node_name);
|
2020-05-21 20:25:33 +00:00
|
|
|
|
2021-09-11 14:34:03 +00:00
|
|
|
let fw = FirewallActor::new(rt_cfg.firewall.refresh_time, &ca.rx_open_ports).await?;
|
|
|
|
|
2021-08-16 09:19:16 +00:00
|
|
|
let ia = IgdActor::new(
|
2021-12-25 18:19:19 +00:00
|
|
|
rt_cfg.igd.private_ip.as_ref().map(String::as_str),
|
2021-09-11 14:34:03 +00:00
|
|
|
rt_cfg.igd.refresh_time,
|
|
|
|
rt_cfg.igd.expiration_time,
|
|
|
|
&ca.rx_open_ports,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-05-24 18:40:49 +00:00
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
let ctx = Self {
|
2020-05-22 17:21:11 +00:00
|
|
|
consul: ca,
|
2020-05-24 18:40:49 +00:00
|
|
|
igd: ia,
|
2021-09-11 14:34:03 +00:00
|
|
|
firewall: fw,
|
2020-05-09 14:50:38 +00:00
|
|
|
};
|
2020-05-09 14:27:54 +00:00
|
|
|
|
2021-12-25 18:19:19 +00:00
|
|
|
return Ok(ctx);
|
2020-05-09 14:50:38 +00:00
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
pub async fn listen(&mut self) -> Result<()> {
|
2020-05-22 17:21:11 +00:00
|
|
|
try_join!(
|
|
|
|
self.consul.listen(),
|
2020-05-24 18:40:49 +00:00
|
|
|
self.igd.listen(),
|
|
|
|
self.firewall.listen()
|
2020-05-22 17:21:11 +00:00
|
|
|
)?;
|
2020-05-22 16:41:13 +00:00
|
|
|
|
2021-12-25 18:19:19 +00:00
|
|
|
return Ok(());
|
2020-05-09 14:50:38 +00:00
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|