2021-09-20 13:00:31 +00:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2020-06-28 16:22:23 +00:00
|
|
|
use anyhow::Result;
|
2021-09-20 13:00:31 +00:00
|
|
|
use iptables;
|
2020-06-28 16:22:23 +00:00
|
|
|
use log::*;
|
2021-09-11 14:34:03 +00:00
|
|
|
use tokio::{
|
|
|
|
select,
|
|
|
|
sync::watch,
|
|
|
|
time::{self, Duration},
|
|
|
|
};
|
2020-05-24 18:40:49 +00:00
|
|
|
|
2021-09-20 13:00:31 +00:00
|
|
|
use crate::{fw, messages};
|
2020-05-24 18:40:49 +00:00
|
|
|
|
|
|
|
pub struct FirewallActor {
|
2020-06-28 16:22:23 +00:00
|
|
|
pub ipt: iptables::IPTables,
|
2020-05-24 18:40:49 +00:00
|
|
|
rx_ports: watch::Receiver<messages::PublicExposedPorts>,
|
|
|
|
last_ports: messages::PublicExposedPorts,
|
2021-09-11 14:34:03 +00:00
|
|
|
refresh: Duration,
|
2020-05-24 18:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FirewallActor {
|
2021-09-11 14:34:03 +00:00
|
|
|
pub async fn new(
|
|
|
|
_refresh: Duration,
|
|
|
|
rxp: &watch::Receiver<messages::PublicExposedPorts>,
|
|
|
|
) -> Result<Self> {
|
|
|
|
let ctx = Self {
|
2020-07-04 14:59:41 +00:00
|
|
|
ipt: iptables::new(false)?,
|
2020-05-24 18:40:49 +00:00
|
|
|
rx_ports: rxp.clone(),
|
|
|
|
last_ports: messages::PublicExposedPorts::new(),
|
|
|
|
refresh: _refresh,
|
|
|
|
};
|
|
|
|
|
2020-07-04 14:59:41 +00:00
|
|
|
fw::setup(&ctx.ipt)?;
|
2021-09-11 14:34:03 +00:00
|
|
|
|
2021-12-25 18:19:19 +00:00
|
|
|
return Ok(ctx);
|
2020-05-24 18:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn listen(&mut self) -> Result<()> {
|
|
|
|
let mut interval = time::interval(self.refresh);
|
|
|
|
loop {
|
|
|
|
// 1. Wait for an event
|
|
|
|
let new_ports = select! {
|
2021-12-25 18:19:19 +00:00
|
|
|
_ = self.rx_ports.changed() => Some(self.rx_ports.borrow().clone()),
|
2020-05-24 18:40:49 +00:00
|
|
|
_ = interval.tick() => None,
|
|
|
|
else => return Ok(()) // Sender dropped, terminate loop.
|
|
|
|
};
|
|
|
|
|
|
|
|
// 2. Update last ports if needed
|
2021-09-11 14:34:03 +00:00
|
|
|
if let Some(p) = new_ports {
|
|
|
|
self.last_ports = p;
|
|
|
|
}
|
2020-05-24 18:40:49 +00:00
|
|
|
|
|
|
|
// 3. Update firewall rules
|
|
|
|
match self.do_fw_update().await {
|
|
|
|
Ok(()) => debug!("Successfully updated firewall rules"),
|
|
|
|
Err(e) => error!("An error occured while updating firewall rules. {}", e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn do_fw_update(&self) -> Result<()> {
|
2020-07-04 14:59:41 +00:00
|
|
|
let curr_opened_ports = fw::get_opened_ports(&self.ipt)?;
|
2020-05-24 18:40:49 +00:00
|
|
|
|
2021-09-11 14:34:03 +00:00
|
|
|
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>>();
|
2020-05-24 18:40:49 +00:00
|
|
|
|
|
|
|
let ports_to_open = messages::PublicExposedPorts {
|
2021-09-11 14:34:03 +00:00
|
|
|
tcp_ports: diff_tcp,
|
|
|
|
udp_ports: diff_udp,
|
2020-05-24 18:40:49 +00:00
|
|
|
};
|
|
|
|
|
2020-07-04 14:59:41 +00:00
|
|
|
fw::open_ports(&self.ipt, ports_to_open)?;
|
2020-05-24 18:40:49 +00:00
|
|
|
|
2021-12-25 18:19:19 +00:00
|
|
|
return Ok(());
|
2020-05-24 18:40:49 +00:00
|
|
|
}
|
|
|
|
}
|