mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
remove reference types and macros
This commit is contained in:
parent
ceb2646502
commit
ce5f0b4931
7 changed files with 39 additions and 168 deletions
|
@ -1,8 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
use super::object::{AddressReference, ServiceReference};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct Firewall {
|
||||
pub forward_rules: Vec<ForwardRule>,
|
||||
|
@ -13,9 +11,9 @@ pub struct Firewall {
|
|||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct ForwardRule {
|
||||
pub name: String,
|
||||
pub services: Vec<ServiceReference>,
|
||||
pub source_addresses: Vec<AddressReference>,
|
||||
pub destination_addresses: Vec<AddressReference>,
|
||||
pub services: Vec<String>,
|
||||
pub source_addresses: Vec<String>,
|
||||
pub destination_addresses: Vec<String>,
|
||||
pub comment: String,
|
||||
pub counter: bool,
|
||||
pub verdict: Verdict,
|
||||
|
@ -24,21 +22,21 @@ pub struct ForwardRule {
|
|||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct DestinationNATRule {
|
||||
pub name: String,
|
||||
pub services: Vec<ServiceReference>,
|
||||
pub source_addresses: Vec<AddressReference>,
|
||||
pub destination_addresses: Vec<AddressReference>,
|
||||
pub services: Vec<String>,
|
||||
pub source_addresses: Vec<String>,
|
||||
pub destination_addresses: Vec<String>,
|
||||
pub comment: String,
|
||||
pub counter: bool,
|
||||
pub dnat_address: Option<AddressReference>,
|
||||
pub dnat_service: Option<ServiceReference>,
|
||||
pub dnat_address: Option<String>,
|
||||
pub dnat_service: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct SourceNATRule {
|
||||
pub name: String,
|
||||
pub services: Vec<ServiceReference>,
|
||||
pub source_addresses: Vec<AddressReference>,
|
||||
pub destination_addresses: Vec<AddressReference>,
|
||||
pub services: Vec<String>,
|
||||
pub source_addresses: Vec<String>,
|
||||
pub destination_addresses: Vec<String>,
|
||||
pub comment: String,
|
||||
pub counter: bool,
|
||||
pub snat_type: SNATType,
|
||||
|
@ -56,8 +54,8 @@ pub enum Verdict {
|
|||
#[serde(rename_all = "lowercase")]
|
||||
pub enum SNATType {
|
||||
SNAT {
|
||||
address: Option<AddressReference>,
|
||||
service: Option<ServiceReference>,
|
||||
address: Option<String>,
|
||||
service: Option<String>,
|
||||
},
|
||||
Masquerade,
|
||||
}
|
||||
|
|
|
@ -2,77 +2,9 @@ use self::config::Config;
|
|||
|
||||
pub mod config;
|
||||
pub mod firewall;
|
||||
pub mod macro_db;
|
||||
pub mod network;
|
||||
pub mod object;
|
||||
pub mod service;
|
||||
pub mod system;
|
||||
pub mod vpn;
|
||||
|
||||
pub trait Referenceable<T> {
|
||||
fn named_get(&self, name: String) -> T;
|
||||
fn named_exists(&self, name: String) -> bool;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_referenceable_trait {
|
||||
($typ:ident, $ele:ty) => {
|
||||
pub type $typ = Vec<$ele>;
|
||||
|
||||
impl Referenceable<$ele> for $typ {
|
||||
fn named_get(&self, name: String) -> $ele {
|
||||
let index = self.iter().position(|e| *e.name == name);
|
||||
|
||||
match index {
|
||||
Some(i) => self[i].clone(),
|
||||
// This is fine since the config always has to validated before commiting
|
||||
None => panic!("Referenced Thing: '{:?}' does not exist ", name),
|
||||
}
|
||||
}
|
||||
|
||||
fn named_exists(&self, name: String) -> bool {
|
||||
let index = self.iter().position(|e| *e.name == name);
|
||||
index.is_some()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub trait References<T> {
|
||||
fn get_ref(&self, config: Config) -> T;
|
||||
fn ref_exists(&self, config: Config) -> bool;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! impl_references_trait {
|
||||
($thing:ident, $referenced:ty, $( $path:ident ).+) => {
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
|
||||
#[serde(from = "String")]
|
||||
#[serde(into = "String")]
|
||||
pub struct $thing {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Into<String> for $thing {
|
||||
fn into(self) -> String {
|
||||
self.name
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for $thing {
|
||||
fn from(value: String) -> Self {
|
||||
$thing { name: value }
|
||||
}
|
||||
}
|
||||
|
||||
impl References<$referenced> for $thing {
|
||||
fn get_ref(&self, config: Config) -> $referenced {
|
||||
config.$($path).+.named_get(self.clone().into())
|
||||
}
|
||||
|
||||
fn ref_exists(&self, config: Config) -> bool {
|
||||
config.$($path).+.named_exists(self.clone().into())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -3,26 +3,12 @@ use serde::{Deserialize, Serialize};
|
|||
use std::net::IpAddr;
|
||||
use validator::Validate;
|
||||
|
||||
use crate::definitions::Referenceable;
|
||||
use crate::{impl_referenceable_trait, impl_references_trait};
|
||||
|
||||
use super::config::Config;
|
||||
use super::object::AddressReference;
|
||||
use super::References;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct Network {
|
||||
pub interfaces: NetworkInterfaces,
|
||||
pub interfaces: Vec<NetworkInterface>,
|
||||
pub static_routes: Vec<StaticRoute>,
|
||||
}
|
||||
|
||||
impl_referenceable_trait!(NetworkInterfaces, NetworkInterface);
|
||||
impl_references_trait!(
|
||||
NetworkInterfaceReference,
|
||||
NetworkInterface,
|
||||
network.interfaces
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct NetworkInterface {
|
||||
pub name: String,
|
||||
|
@ -36,33 +22,24 @@ pub struct NetworkInterface {
|
|||
#[serde(rename_all = "snake_case")]
|
||||
pub enum NetworkInterfaceType {
|
||||
// TODO figure out how to validate the device since it needs to soft fail
|
||||
Hardware {
|
||||
device: String,
|
||||
},
|
||||
Vlan {
|
||||
id: i32,
|
||||
parent: NetworkInterfaceReference,
|
||||
},
|
||||
Bond {
|
||||
members: Vec<NetworkInterfaceReference>,
|
||||
},
|
||||
Bridge {
|
||||
members: Vec<NetworkInterfaceReference>,
|
||||
},
|
||||
Hardware { device: String },
|
||||
Vlan { id: i32, parent: String },
|
||||
Bond { members: Vec<String> },
|
||||
Bridge { members: Vec<String> },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum AddressingMode {
|
||||
None,
|
||||
Static { address: AddressReference },
|
||||
Static { address: String },
|
||||
DHCP,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct StaticRoute {
|
||||
pub name: String,
|
||||
pub interface: NetworkInterfaceReference,
|
||||
pub interface: String,
|
||||
// TODO make this a Address Object Reference?
|
||||
pub gateway: IpAddr,
|
||||
pub destination: IpNet,
|
||||
|
|
|
@ -3,21 +3,12 @@ use serde::{Deserialize, Serialize};
|
|||
use std::net::IpAddr;
|
||||
use validator::Validate;
|
||||
|
||||
// Referencing
|
||||
use crate::definitions::config::Config;
|
||||
use crate::definitions::Referenceable;
|
||||
use crate::definitions::References;
|
||||
use crate::{impl_referenceable_trait, impl_references_trait};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct Object {
|
||||
pub addresses: Addresses,
|
||||
pub services: Services,
|
||||
pub addresses: Vec<Address>,
|
||||
pub services: Vec<Service>,
|
||||
}
|
||||
|
||||
impl_referenceable_trait!(Addresses, Address);
|
||||
impl_references_trait!(AddressReference, Address, object.addresses);
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct Address {
|
||||
pub name: String,
|
||||
|
@ -31,12 +22,9 @@ pub enum AddressType {
|
|||
Host { address: IpAddr },
|
||||
Range { range: IpAddr },
|
||||
Network { network: IpNet },
|
||||
Group { members: Vec<AddressReference> },
|
||||
Group { members: Vec<String> },
|
||||
}
|
||||
|
||||
impl_referenceable_trait!(Services, Service);
|
||||
impl_references_trait!(ServiceReference, Service, object.services);
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct Service {
|
||||
pub name: String,
|
||||
|
@ -59,7 +47,7 @@ pub enum ServiceType {
|
|||
code: u8,
|
||||
},
|
||||
Group {
|
||||
members: Vec<ServiceReference>,
|
||||
members: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -3,8 +3,6 @@ use macaddr::MacAddr8;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
use super::{network::NetworkInterfaceReference, object::AddressReference};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct Service {
|
||||
pub dhcp_servers: Vec<DHCPServer>,
|
||||
|
@ -14,8 +12,9 @@ pub struct Service {
|
|||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct DHCPServer {
|
||||
pub interface: NetworkInterfaceReference,
|
||||
pub pool: Vec<AddressReference>,
|
||||
pub name: String,
|
||||
pub interface: String,
|
||||
pub pool: Vec<String>,
|
||||
pub lease_time: time::Duration,
|
||||
pub gateway_mode: GatewayMode,
|
||||
pub dns_server_mode: DNSServerMode,
|
||||
|
@ -26,13 +25,15 @@ pub struct DHCPServer {
|
|||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct DNSServer {
|
||||
pub interface: NetworkInterfaceReference,
|
||||
pub name: String,
|
||||
pub interface: String,
|
||||
pub comment: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct NTPServer {
|
||||
pub interface: NetworkInterfaceReference,
|
||||
pub name: String,
|
||||
pub interface: String,
|
||||
pub comment: String,
|
||||
}
|
||||
|
||||
|
@ -41,7 +42,7 @@ pub struct NTPServer {
|
|||
pub enum GatewayMode {
|
||||
None,
|
||||
Interface,
|
||||
Specify { gateway: AddressReference },
|
||||
Specify { gateway: String },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
|
@ -49,7 +50,7 @@ pub enum GatewayMode {
|
|||
pub enum DNSServerMode {
|
||||
None,
|
||||
Interface,
|
||||
Specify { dns_servers: Vec<AddressReference> },
|
||||
Specify { dns_servers: Vec<String> },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
|
@ -57,12 +58,12 @@ pub enum DNSServerMode {
|
|||
pub enum NTPServerMode {
|
||||
None,
|
||||
Interface,
|
||||
Specify { ntp_servers: Vec<AddressReference> },
|
||||
Specify { ntp_servers: Vec<String> },
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct Reservation {
|
||||
pub ip_address: AddressReference,
|
||||
pub ip_address: String,
|
||||
pub hardware_address: MacAddr8,
|
||||
pub comment: String,
|
||||
}
|
||||
|
|
|
@ -1,20 +1,11 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
// Referencing
|
||||
use crate::definitions::config::Config;
|
||||
use crate::definitions::Referenceable;
|
||||
use crate::definitions::References;
|
||||
use crate::{impl_referenceable_trait, impl_references_trait};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct System {
|
||||
pub users: Vec<User>,
|
||||
}
|
||||
|
||||
impl_referenceable_trait!(Users, User);
|
||||
impl_references_trait!(UserReference, User, system.users);
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct User {
|
||||
pub name: String,
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
// Referencing
|
||||
use crate::definitions::config::Config;
|
||||
use crate::definitions::Referenceable;
|
||||
use crate::definitions::References;
|
||||
use crate::{impl_referenceable_trait, impl_references_trait};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct VPN {
|
||||
pub wireguard: Wireguard,
|
||||
|
@ -14,30 +8,20 @@ pub struct VPN {
|
|||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Default, Debug)]
|
||||
pub struct Wireguard {
|
||||
pub interfaces: WireguardInterfaces,
|
||||
pub peers: WireguardPeers,
|
||||
pub interfaces: Vec<WireguardInterface>,
|
||||
pub peers: Vec<WireguardPeer>,
|
||||
}
|
||||
|
||||
impl_referenceable_trait!(WireguardInterfaces, WireguardInterface);
|
||||
impl_references_trait!(
|
||||
WireguardInterfaceReference,
|
||||
WireguardInterface,
|
||||
vpn.wireguard.interfaces
|
||||
);
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct WireguardInterface {
|
||||
pub name: String,
|
||||
pub public_key: String,
|
||||
pub private_key: String,
|
||||
pub listen_port: u64,
|
||||
pub peers: Vec<WireguardPeerReference>,
|
||||
pub peers: Vec<String>,
|
||||
pub comment: String,
|
||||
}
|
||||
|
||||
impl_referenceable_trait!(WireguardPeers, WireguardPeer);
|
||||
impl_references_trait!(WireguardPeerReference, WireguardPeer, vpn.wireguard.peers);
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Validate, Debug)]
|
||||
pub struct WireguardPeer {
|
||||
pub name: String,
|
||||
|
|
Loading…
Add table
Reference in a new issue