Added config parsing

This commit is contained in:
benjamyn committed 2024-03-22 17:01:34 +11:00
1 parent 91db678c55
commit bca8892147
6 files changed
+60 -4

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
use configparser::ini::Ini;
use core::fmt;
pub struct Config {
pub subdomains: Vec<String>,
wildcard_test: String,
}
impl fmt::Display for Config {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Subdomains: {:?}\nWildcard Test: {}", self.subdomains, self.wildcard_test)
}
}
impl Config {
pub fn from_file(file_path: String) -> Config {
let mut config = Ini::new();
let _map = config.load(file_path).unwrap();
let subdomains = config.get("General", "subdomains").unwrap();
let mut subvec: Vec<String> = vec![];
for x in subdomains.split(',') {
subvec.push(x.to_string());
}
let wildcard_test = config.get("General", "wildcard_test");
Config {
subdomains: subvec,
wildcard_test: wildcard_test.unwrap()
}
}
}
+9 -1
View File
@@ -4,6 +4,8 @@ use hickory_resolver::config::*;
use core::fmt;
use crate::config::Config;
// #[derive(Debug)]
pub struct Domain {
domain_name: String,
@@ -14,7 +16,7 @@ pub struct Domain {
mx_records: Vec<MX>,
ns_records: Vec<NS>,
soa_records: Vec<SOA>, // Subdomains CAN have there own SOA records if their zones point elsewhere
resolver: Resolver
resolver: Resolver,
}
impl fmt::Display for Domain {
@@ -74,6 +76,12 @@ impl Domain {
// println!("Added: {}", new_domain);
}
pub fn apply_config(&mut self, config: &Config) {
for subdomain in &config.subdomains {
self.append_subdomain(subdomain.to_string());
}
}
pub fn lookup_all_records(&mut self) {
// Lookup A records
self.lookup_a();
+7 -3
View File
@@ -1,11 +1,15 @@
mod domain;
use crate::domain::Domain;
mod config;
use crate::config::Config;
fn main() {
let mut test = Domain::new("pigandpilgrim.com.au".to_string());
test.append_subdomain("www".to_string());
test.append_subdomain("mail".to_string());
test.append_subdomain("ftp".to_string());
let config = Config::from_file("test.ini".to_string());
test.apply_config(&config);
test.lookup_all_records();
println!("{}", test);