New config module
This commit is contained in:
parent
478dc4dd3a
commit
7efa41a48c
2 changed files with 111 additions and 1 deletions
108
src/config.rs
Normal file
108
src/config.rs
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs;
|
||||||
|
use std::hash::Hash;
|
||||||
|
use log::{error, warn};
|
||||||
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub(crate) struct InterfaceConfig {
|
||||||
|
pub(crate) host_address: String,
|
||||||
|
pub(crate) interfaces: HashMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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) => {
|
||||||
|
error!("[ERROR] {}", e);
|
||||||
|
return Err(Box::new(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(cfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for InterfaceConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
InterfaceConfig {
|
||||||
|
host_address: "::".to_string(),
|
||||||
|
interfaces: HashMap::from([(" ".to_string(), "::".to_string())]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub(crate) struct ZoneEntry {
|
||||||
|
pub(crate) name: String,
|
||||||
|
pub(crate) rtype: u8,
|
||||||
|
pub(crate) interface: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ZoneEntry {
|
||||||
|
fn default() -> Self {
|
||||||
|
ZoneEntry {
|
||||||
|
name: " ".to_string(),
|
||||||
|
rtype: 10,
|
||||||
|
interface: " ".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
pub(crate) struct ZoneConfig {
|
||||||
|
pub(crate) email: String,
|
||||||
|
pub(crate) zone: String,
|
||||||
|
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() {
|
||||||
|
warn!("[WARN] Subdirectory in zones.d detected, this should not be the case");
|
||||||
|
}
|
||||||
|
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) => {
|
||||||
|
error!("[ERROR] {}", e);
|
||||||
|
return Err(Box::new(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(zone_configs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ZoneConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
ZoneConfig {
|
||||||
|
email: " ".to_string(),
|
||||||
|
zone: " ".to_string(),
|
||||||
|
id: " ".to_string(),
|
||||||
|
entries: vec![ZoneEntry::default()],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ use reqwest::blocking::get;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use std::{fs, thread::{sleep}};
|
use std::{fs, thread::{sleep}};
|
||||||
use chrono::{Utc, Duration};
|
use chrono::{Utc, Duration};
|
||||||
|
mod config;
|
||||||
mod cloudflare;
|
mod cloudflare;
|
||||||
|
|
||||||
fn default_key() -> String {
|
fn default_key() -> String {
|
||||||
|
@ -359,7 +360,8 @@ fn main() {
|
||||||
let mut now = Utc::now() - Duration::seconds(59);
|
let mut now = Utc::now() - Duration::seconds(59);
|
||||||
let mut current = now;
|
let mut current = now;
|
||||||
|
|
||||||
loop {
|
let ifaces = config::InterfaceConfig::load().unwrap();
|
||||||
|
let zone_cfgs= config::ZoneConfig::load().unwrap();
|
||||||
now = Utc::now();
|
now = Utc::now();
|
||||||
if now >= current + Duration::seconds(60) {
|
if now >= current + Duration::seconds(60) {
|
||||||
current = now;
|
current = now;
|
||||||
|
|
Loading…
Reference in a new issue