2023-04-20 12:14:30 +00:00
|
|
|
use anyhow::{Context, Result};
|
|
|
|
use futures::future::FutureExt;
|
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::{
|
2023-04-04 16:48:52 +00:00
|
|
|
config::ConfigOpts, consul_actor::ConsulActor, fw_actor::FirewallActor, igd_actor::IgdActor,
|
|
|
|
stun_actor::StunActor,
|
2021-09-20 13:00:31 +00:00
|
|
|
};
|
2020-05-21 20:25:33 +00:00
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
pub struct Diplonat {
|
2023-04-04 16:48:52 +00:00
|
|
|
consul: ConsulActor,
|
|
|
|
firewall: FirewallActor,
|
2023-04-05 07:47:58 +00:00
|
|
|
igd: Option<IgdActor>,
|
2023-04-04 16:48:52 +00:00
|
|
|
stun: StunActor,
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 16:41:13 +00:00
|
|
|
impl Diplonat {
|
2023-04-04 16:48:52 +00:00
|
|
|
pub async fn new() -> Result<Self> {
|
2023-04-20 12:14:30 +00:00
|
|
|
let rt_cfg = ConfigOpts::from_env().context("Parse configuration")?;
|
2023-04-04 16:48:52 +00:00
|
|
|
println!("{:#?}", rt_cfg);
|
|
|
|
|
|
|
|
let ca = ConsulActor::new(&rt_cfg.consul, &rt_cfg.consul.node_name);
|
|
|
|
|
2023-04-05 07:47:58 +00:00
|
|
|
let fw = FirewallActor::new(
|
|
|
|
rt_cfg.firewall.ipv6_only,
|
|
|
|
rt_cfg.firewall.refresh_time,
|
2023-04-04 16:48:52 +00:00
|
|
|
&ca.rx_open_ports,
|
|
|
|
)
|
2023-04-20 12:14:30 +00:00
|
|
|
.await
|
|
|
|
.context("Setup fireall actor")?;
|
2023-04-04 16:48:52 +00:00
|
|
|
|
2023-04-05 07:47:58 +00:00
|
|
|
let ia = match rt_cfg.igd {
|
|
|
|
Some(igdc) => Some(
|
|
|
|
IgdActor::new(
|
|
|
|
igdc.private_ip,
|
|
|
|
igdc.refresh_time,
|
|
|
|
igdc.expiration_time,
|
|
|
|
&ca.rx_open_ports,
|
|
|
|
)
|
2023-04-20 12:14:30 +00:00
|
|
|
.await
|
|
|
|
.context("Setup IGD actor")?,
|
2023-04-05 07:47:58 +00:00
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2023-04-04 16:48:52 +00:00
|
|
|
let sa = StunActor::new(&rt_cfg.consul, &rt_cfg.stun, &rt_cfg.consul.node_name);
|
|
|
|
|
|
|
|
let ctx = Self {
|
|
|
|
consul: ca,
|
|
|
|
igd: ia,
|
|
|
|
firewall: fw,
|
|
|
|
stun: sa,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn listen(&mut self) -> Result<()> {
|
2023-04-05 07:47:58 +00:00
|
|
|
let igd_opt = &mut self.igd;
|
|
|
|
|
2023-04-04 16:48:52 +00:00
|
|
|
try_join!(
|
2023-04-20 12:14:30 +00:00
|
|
|
self.consul.listen().map(|x| x.context("Run consul actor")),
|
2023-04-05 07:47:58 +00:00
|
|
|
async {
|
|
|
|
if let Some(igd) = igd_opt {
|
2023-04-20 12:14:30 +00:00
|
|
|
igd.listen().await.context("Run IGD actor")
|
2023-04-05 07:47:58 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
},
|
2023-04-20 12:14:30 +00:00
|
|
|
self.firewall
|
|
|
|
.listen()
|
|
|
|
.map(|x| x.context("Run firewall actor")),
|
|
|
|
self.stun.listen().map(|x| x.context("Run STUN actor")),
|
2023-04-04 16:48:52 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-09 13:53:22 +00:00
|
|
|
}
|