WIP: rewrote body of 'new(...)' for FirewallActor and IgdActor using match; clearer intent
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
adrien 2021-09-22 14:47:58 +02:00
parent f5ac36e21f
commit 006f84e3b0
3 changed files with 32 additions and 35 deletions

View file

@ -19,7 +19,7 @@ impl Diplonat {
let consul_actor = ConsulActor::new(config.consul);
let firewall_actor = FirewallActor::new(config.firewall, &consul_actor.rx_open_ports).await?;
let firewall_actor = FirewallActor::new(config.firewall, &consul_actor.rx_open_ports)?;
let igd_actor = IgdActor::new(config.igd, &consul_actor.rx_open_ports).await?;

View file

@ -21,25 +21,24 @@ pub struct FirewallActor {
}
impl FirewallActor {
pub async fn new(
pub fn new(
config: Option<RuntimeConfigFirewall>,
rxp: &watch::Receiver<messages::PublicExposedPorts>,
) -> Result<Option<Self>> {
if config.is_none() {
return Ok(None)
match config {
None => Ok(None),
Some(c) => {
let ctx = Self {
ipt: iptables::new(false)?,
last_ports: messages::PublicExposedPorts::new(),
refresh: c.refresh_time,
rx_ports: rxp.clone(),
};
fw::setup(&ctx.ipt)?;
Ok(Some(ctx))
}
}
let config = config.unwrap();
let ctx = Self {
ipt: iptables::new(false)?,
last_ports: messages::PublicExposedPorts::new(),
refresh: config.refresh_time,
rx_ports: rxp.clone(),
};
fw::setup(&ctx.ipt)?;
return Ok(Some(ctx))
}
pub async fn listen(&mut self) -> Result<()> {

View file

@ -26,26 +26,24 @@ impl IgdActor {
config: Option<RuntimeConfigIgd>,
rxp: &watch::Receiver<messages::PublicExposedPorts>,
) -> Result<Option<Self>> {
if config.is_none() {
return Ok(None)
match config {
None => Ok(None),
Some(c) => {
let gw = search_gateway(Default::default())
.await
.context("Failed to find IGD gateway")?;
info!("IGD gateway: {}", gw);
Ok(Some(Self {
expire: c.expiration_time,
gateway: gw,
last_ports: messages::PublicExposedPorts::new(),
private_ip: c.private_ip,
refresh: c.refresh_time,
rx_ports: rxp.clone(),
}))
}
}
let config = config.unwrap();
let gw = search_gateway(Default::default())
.await
.context("Failed to find IGD gateway")?;
info!("IGD gateway: {}", gw);
let ctx = Self {
expire: config.expiration_time,
gateway: gw,
last_ports: messages::PublicExposedPorts::new(),
private_ip: config.private_ip,
refresh: config.refresh_time,
rx_ports: rxp.clone(),
};
return Ok(Some(ctx))
}
pub async fn listen(&mut self) -> Result<()> {