aboutsummaryrefslogtreecommitdiff
path: root/src/fw_actor.rs
diff options
context:
space:
mode:
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(());
}
-
}
-