cloudflare-dns-updater/src/config.rs

141 lines
4.4 KiB
Rust
Raw Normal View History

2023-12-25 15:24:18 +00:00
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::net::Ipv6Addr;
2023-12-26 03:18:58 +00:00
use ipnet::{IpBitOr, IpSub};
use std::str::FromStr;
2023-12-25 15:24:18 +00:00
use log::{error, warn};
use serde_derive::{Deserialize, Serialize};
use systemd_journal_logger::connected_to_journal;
use crate::cloudflare::DnsRecordType;
use crate::cloudflare::DnsRecordType::{A, AAAA};
2023-12-25 15:24:18 +00:00
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct InterfaceConfig {
pub(crate) host_address: Ipv6Addr,
pub(crate) interfaces: HashMap<String, Ipv6Addr>,
2023-12-25 15:24:18 +00:00
}
impl InterfaceConfig {
pub(crate) fn load() -> Result<Self, Box<dyn Error>> {
let cfg: Self = match confy::load(env!("CARGO_PKG_NAME"),"interfaces") {
Ok(data) => data,
Err(e) => {
2023-12-26 03:06:38 +00:00
match connected_to_journal() {
true => error!("[ERROR] {e}"),
false => eprintln!("[ERROR] {e}")
}
2023-12-25 15:24:18 +00:00
return Err(Box::new(e));
}
};
Ok(cfg)
}
pub(crate) fn full_v6(&self, interface_name: &String, host_v6: Ipv6Addr) -> Result<Ipv6Addr, ()> {
let host_range = Ipv6Addr::from(host_v6.saturating_sub(self.host_address));
let interface_address = match self.interfaces.get(interface_name) {
2023-12-26 03:18:58 +00:00
Some(address) => *address,
None => {
let err_msg = "Malformed IP in interfaces.toml";
match connected_to_journal() {
true => error!("[ERROR] {err_msg}"),
false => eprintln!("[ERROR] {err_msg}"),
}
return Err(());
}
};
let interface_ip = host_range.bitor(interface_address);
Ok(interface_ip)
}
2023-12-25 15:24:18 +00:00
}
impl Default for InterfaceConfig {
fn default() -> Self {
InterfaceConfig {
host_address: Ipv6Addr::from_str("::").expect("Malformed literal in code"),
2023-12-26 03:18:58 +00:00
interfaces: HashMap::from([(" ".to_owned(), Ipv6Addr::from(0))]),
2023-12-25 15:24:18 +00:00
}
}
}
///////////////
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct ZoneEntry {
pub(crate) name: String,
pub(crate) r#type: Vec<DnsRecordType>,
2023-12-25 15:24:18 +00:00
pub(crate) interface: String,
}
impl Default for ZoneEntry {
fn default() -> Self {
ZoneEntry {
2023-12-26 03:18:58 +00:00
name: " ".to_owned(),
r#type: vec![A, AAAA],
2023-12-26 03:18:58 +00:00
interface: " ".to_owned(),
2023-12-25 15:24:18 +00:00
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct ZoneConfig {
pub(crate) email: String,
pub(crate) name: String,
2023-12-25 15:24:18 +00:00
pub(crate) id: String,
#[serde(alias="entry")]
pub(crate) entries: Vec<ZoneEntry>
}
impl ZoneConfig {
pub(crate) fn load() -> Result<Vec<Self>, Box<dyn Error>> {
let path = confy::get_configuration_file_path(env!("CARGO_PKG_NAME"), "interfaces").expect("Something went wrong with confy");
let zones_dir = path.parent().expect("Something went wrong with confy").join("zones.d/");
let zones = fs::read_dir(zones_dir).unwrap();
let mut zone_configs: Vec<Self> = vec![];
for entry in zones {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
2023-12-26 03:06:38 +00:00
let warn_msg = "Subdirectory in zones.d detected, this should not be the case";
match connected_to_journal() {
true => warn!("[WARN] {warn_msg}"),
false => eprintln!("[WARN] {warn_msg}"),
}
2023-12-25 15:24:18 +00:00
}
else {
let zone_config_path = format!("zones.d/{}", path.file_stem()
.expect("stem could not be extracted from filename").to_str()
.expect("&OsStr could not be converted to &str"));
match confy::load(env!("CARGO_PKG_NAME"), zone_config_path.as_str()) {
Ok(data) => zone_configs.push(data),
Err(e) => {
2023-12-26 03:06:38 +00:00
match connected_to_journal() {
true => error!("[ERROR] {e}"),
false => eprintln!("[ERROR] {e}"),
}
2023-12-25 15:24:18 +00:00
return Err(Box::new(e));
}
};
}
}
Ok(zone_configs)
}
}
impl Default for ZoneConfig {
fn default() -> Self {
ZoneConfig {
2023-12-26 03:18:58 +00:00
email: " ".to_owned(),
name: " ".to_owned(),
id: " ".to_owned(),
2023-12-25 15:24:18 +00:00
entries: vec![ZoneEntry::default()],
}
}
}