2021-09-20 13:00:31 +00:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2021-09-11 14:34:03 +00:00
|
|
|
use anyhow::{Context, Result};
|
2020-05-22 14:02:31 +00:00
|
|
|
use iptables;
|
2021-09-11 14:34:03 +00:00
|
|
|
use log::*;
|
2020-05-22 14:02:31 +00:00
|
|
|
use regex::Regex;
|
2021-09-20 13:00:31 +00:00
|
|
|
|
|
|
|
use crate::messages;
|
2020-05-21 23:12:53 +00:00
|
|
|
|
2020-07-04 14:59:41 +00:00
|
|
|
pub fn setup(ipt: &iptables::IPTables) -> Result<()> {
|
2023-04-04 16:48:52 +00:00
|
|
|
// ensure we start from a clean state without any rule already set
|
|
|
|
cleanup(ipt)?;
|
|
|
|
|
2023-04-04 17:06:33 +00:00
|
|
|
info!("{}: creating DIPLONAT chain", ipt.cmd);
|
2023-04-04 16:48:52 +00:00
|
|
|
ipt.new_chain("filter", "DIPLONAT")
|
|
|
|
.context("Failed to create new chain")?;
|
|
|
|
ipt.insert_unique("filter", "INPUT", "-j DIPLONAT", 1)
|
|
|
|
.context("Failed to insert jump rule")?;
|
|
|
|
|
|
|
|
Ok(())
|
2020-05-22 16:51:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 14:59:41 +00:00
|
|
|
pub fn open_ports(ipt: &iptables::IPTables, ports: messages::PublicExposedPorts) -> Result<()> {
|
2023-04-04 16:48:52 +00:00
|
|
|
for p in ports.tcp_ports {
|
|
|
|
info!("{}: opening TCP port {}", ipt.cmd, p);
|
|
|
|
ipt.append(
|
|
|
|
"filter",
|
|
|
|
"DIPLONAT",
|
|
|
|
&format!("-p tcp --dport {} -j ACCEPT", p),
|
|
|
|
)
|
|
|
|
.context("Failed to insert port rule")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
for p in ports.udp_ports {
|
|
|
|
info!("{}: opening UDP port {}", ipt.cmd, p);
|
|
|
|
ipt.append(
|
|
|
|
"filter",
|
|
|
|
"DIPLONAT",
|
|
|
|
&format!("-p udp --dport {} -j ACCEPT", p),
|
|
|
|
)
|
|
|
|
.context("Failed to insert port rule")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-05-21 23:12:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 14:59:41 +00:00
|
|
|
pub fn get_opened_ports(ipt: &iptables::IPTables) -> Result<messages::PublicExposedPorts> {
|
2023-04-04 16:48:52 +00:00
|
|
|
let mut ports = messages::PublicExposedPorts {
|
|
|
|
tcp_ports: HashSet::new(),
|
|
|
|
udp_ports: HashSet::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let list = ipt.list("filter", "DIPLONAT")?;
|
|
|
|
let re = Regex::new(r"\-A.*? \-p (\w+).*\-\-dport (\d+).*?\-j ACCEPT")
|
|
|
|
.context("Regex matching open ports encountered an unexpected rule")?;
|
|
|
|
for i in list {
|
2023-04-20 12:29:38 +00:00
|
|
|
debug!("{} list DIPLONAT: got {}", ipt.cmd, i);
|
2023-04-04 16:48:52 +00:00
|
|
|
let caps = re.captures(&i);
|
|
|
|
match caps {
|
|
|
|
Some(c) => {
|
|
|
|
if let (Some(raw_proto), Some(raw_port)) = (c.get(1), c.get(2)) {
|
|
|
|
let proto = String::from(raw_proto.as_str());
|
|
|
|
let number = String::from(raw_port.as_str()).parse::<u16>()?;
|
|
|
|
|
2023-04-20 12:53:20 +00:00
|
|
|
if proto == "tcp" || proto == "6" {
|
2023-04-04 16:48:52 +00:00
|
|
|
ports.tcp_ports.insert(number);
|
2023-04-20 12:53:20 +00:00
|
|
|
} else if proto == "udp" || proto == "17" {
|
2023-04-04 16:48:52 +00:00
|
|
|
ports.udp_ports.insert(number);
|
2023-04-20 12:53:20 +00:00
|
|
|
} else {
|
|
|
|
error!("Unexpected protocol in iptables rule: {}", proto);
|
2023-04-04 16:48:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error!("Unexpected rule found in DIPLONAT chain")
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 12:29:38 +00:00
|
|
|
_ => {
|
|
|
|
debug!("{} rule not parsed: {}", ipt.cmd, i);
|
|
|
|
}
|
2020-05-21 23:12:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 12:53:20 +00:00
|
|
|
debug!("{} ports already openned: {:?}", ipt.cmd, ports);
|
|
|
|
|
2023-04-04 16:48:52 +00:00
|
|
|
Ok(ports)
|
2020-05-21 23:12:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 14:59:41 +00:00
|
|
|
pub fn cleanup(ipt: &iptables::IPTables) -> Result<()> {
|
2023-04-04 16:48:52 +00:00
|
|
|
if ipt.chain_exists("filter", "DIPLONAT")? {
|
|
|
|
info!("{}: removing old DIPLONAT chain", ipt.cmd);
|
|
|
|
ipt.flush_chain("filter", "DIPLONAT")
|
|
|
|
.context("Failed to flush the DIPLONAT chain")?;
|
|
|
|
|
|
|
|
if ipt.exists("filter", "INPUT", "-j DIPLONAT")? {
|
|
|
|
ipt.delete("filter", "INPUT", "-j DIPLONAT")
|
|
|
|
.context("Failed to delete jump rule")?;
|
|
|
|
}
|
2020-07-04 14:59:41 +00:00
|
|
|
|
2023-04-04 16:48:52 +00:00
|
|
|
ipt.delete_chain("filter", "DIPLONAT")
|
|
|
|
.context("Failed to delete chain")?;
|
|
|
|
}
|
2020-05-21 23:12:53 +00:00
|
|
|
|
2023-04-04 16:48:52 +00:00
|
|
|
Ok(())
|
2021-09-11 14:34:03 +00:00
|
|
|
}
|