vopono_core 0.1.12

Library code for running VPN connections in network namespaces
Documentation
use crate::network::firewall::Firewall;
use crate::network::netns::NetworkNamespace;
use std::net::IpAddr;

pub fn open_hosts(netns_name: &str, hosts: &[IpAddr], firewall: Firewall) -> anyhow::Result<()> {
    for host in hosts {
        match firewall {
            Firewall::IpTables => {
                NetworkNamespace::exec(
                    netns_name,
                    &[
                        "iptables",
                        "-I",
                        "OUTPUT",
                        "1",
                        "-d",
                        &host.to_string(),
                        "-j",
                        "ACCEPT",
                    ],
                )?;
            }
            Firewall::NfTables => {
                NetworkNamespace::exec(
                    netns_name,
                    &[
                        "nft",
                        "insert",
                        "rule",
                        "inet",
                        netns_name,
                        "output",
                        "ip",
                        "daddr",
                        &host.to_string(),
                        "counter",
                        "accept",
                    ],
                )?;
            }
        }
    }
    Ok(())
}