aboutsummaryrefslogtreecommitdiff
path: root/src/fw_actor.rs
diff options
context:
space:
mode:
authorQuentin <quentin@dufour.io>2021-09-17 10:06:51 +0200
committerQuentin <quentin@dufour.io>2021-09-17 10:06:51 +0200
commit2bbc9109991f8bb79a09a965a1d2779e1749b25b (patch)
tree57dcb5b115b854d651ac6f952466d4109bac2d45 /src/fw_actor.rs
parentfa25c54e47decf9f323ba0c614f4d9de106626d5 (diff)
parentbf226d077ef2bea0567a7b36b4d25ce2d0b5191c (diff)
downloaddiplonat-2bbc9109991f8bb79a09a965a1d2779e1749b25b.tar.gz
diplonat-2bbc9109991f8bb79a09a965a1d2779e1749b25b.zip
Merge pull request 'added rustfmt, a guide about this, and a CI job to enforce code quality' (#10) from adrien/diplonat:meta/formating into main
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/diplonat/pulls/10
Diffstat (limited to 'src/fw_actor.rs')
-rw-r--r--src/fw_actor.rs51
1 files changed, 31 insertions, 20 deletions
diff --git a/src/fw_actor.rs b/src/fw_actor.rs
index b5e4c7e..5147a9c 100644
--- a/src/fw_actor.rs
+++ b/src/fw_actor.rs
@@ -1,28 +1,29 @@
use anyhow::Result;
-use tokio::{
- select,
- sync::watch,
- time::{
- self,
- Duration
-}};
use log::*;
+use tokio::{
+ select,
+ sync::watch,
+ time::{self, Duration},
+};
-use iptables;
-use crate::messages;
use crate::fw;
+use crate::messages;
+use iptables;
use std::collections::HashSet;
pub struct FirewallActor {
pub ipt: iptables::IPTables,
rx_ports: watch::Receiver<messages::PublicExposedPorts>,
last_ports: messages::PublicExposedPorts,
- refresh: Duration
+ refresh: Duration,
}
impl FirewallActor {
- pub async fn new(_refresh: Duration, rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> {
- let ctx = Self {
+ pub async fn new(
+ _refresh: Duration,
+ rxp: &watch::Receiver<messages::PublicExposedPorts>,
+ ) -> Result<Self> {
+ let ctx = Self {
ipt: iptables::new(false)?,
rx_ports: rxp.clone(),
last_ports: messages::PublicExposedPorts::new(),
@@ -30,7 +31,7 @@ impl FirewallActor {
};
fw::setup(&ctx.ipt)?;
-
+
return Ok(ctx);
}
@@ -45,7 +46,9 @@ impl FirewallActor {
};
// 2. Update last ports if needed
- if let Some(p) = new_ports { self.last_ports = p; }
+ if let Some(p) = new_ports {
+ self.last_ports = p;
+ }
// 3. Update firewall rules
match self.do_fw_update().await {
@@ -58,18 +61,26 @@ impl FirewallActor {
pub async fn do_fw_update(&self) -> Result<()> {
let curr_opened_ports = fw::get_opened_ports(&self.ipt)?;
- let diff_tcp = self.last_ports.tcp_ports.difference(&curr_opened_ports.tcp_ports).copied().collect::<HashSet<u16>>();
- let diff_udp = self.last_ports.udp_ports.difference(&curr_opened_ports.udp_ports).copied().collect::<HashSet<u16>>();
+ let diff_tcp = self
+ .last_ports
+ .tcp_ports
+ .difference(&curr_opened_ports.tcp_ports)
+ .copied()
+ .collect::<HashSet<u16>>();
+ let diff_udp = self
+ .last_ports
+ .udp_ports
+ .difference(&curr_opened_ports.udp_ports)
+ .copied()
+ .collect::<HashSet<u16>>();
let ports_to_open = messages::PublicExposedPorts {
- tcp_ports: diff_tcp,
- udp_ports: diff_udp
+ tcp_ports: diff_tcp,
+ udp_ports: diff_udp,
};
fw::open_ports(&self.ipt, ports_to_open)?;
return Ok(());
}
-
}
-