Rewrite Cloudflare module, minor changes to config module

This commit is contained in:
Neshura 2023-12-26 04:10:00 +01:00
parent fbf31a4115
commit ab23597218
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 388 additions and 9 deletions

View file

@ -2,13 +2,19 @@ use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::hash::Hash;
use std::net::Ipv6Addr;
use ipnet::{IpAdd, IpBitAnd, IpBitOr, IpSub};
use std::str::FromStr;
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};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct InterfaceConfig {
pub(crate) host_address: String,
pub(crate) interfaces: HashMap<String, String>,
pub(crate) host_address: Ipv6Addr,
pub(crate) interfaces: HashMap<String, Ipv6Addr>,
}
impl InterfaceConfig {
@ -26,13 +32,30 @@ impl InterfaceConfig {
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) {
Some(address) => address.clone(),
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)
}
}
impl Default for InterfaceConfig {
fn default() -> Self {
InterfaceConfig {
host_address: "::".to_string(),
interfaces: HashMap::from([(" ".to_string(), "::".to_string())]),
host_address: Ipv6Addr::from_str("::").expect("Malformed literal in code"),
interfaces: HashMap::from([(" ".to_string(), Ipv6Addr::from(0))]),
}
}
}
@ -42,7 +65,7 @@ impl Default for InterfaceConfig {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct ZoneEntry {
pub(crate) name: String,
pub(crate) rtype: u8,
pub(crate) r#type: Vec<DnsRecordType>,
pub(crate) interface: String,
}
@ -50,7 +73,7 @@ impl Default for ZoneEntry {
fn default() -> Self {
ZoneEntry {
name: " ".to_string(),
rtype: 10,
r#type: vec![A, AAAA],
interface: " ".to_string(),
}
}
@ -59,7 +82,7 @@ impl Default for ZoneEntry {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct ZoneConfig {
pub(crate) email: String,
pub(crate) zone: String,
pub(crate) name: String,
pub(crate) id: String,
#[serde(alias="entry")]
pub(crate) entries: Vec<ZoneEntry>
@ -109,7 +132,7 @@ impl Default for ZoneConfig {
fn default() -> Self {
ZoneConfig {
email: " ".to_string(),
zone: " ".to_string(),
name: " ".to_string(),
id: " ".to_string(),
entries: vec![ZoneEntry::default()],
}