From bca889214764455c1e06556473930499377ec047 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Fri, 22 Mar 2024 17:01:34 +1100 Subject: [PATCH] Added config parsing --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/config.rs | 33 +++++++++++++++++++++++++++++++++ src/domain.rs | 10 +++++++++- src/main.rs | 10 +++++++--- test.ini | 3 +++ 6 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 src/config.rs create mode 100644 test.ini diff --git a/Cargo.lock b/Cargo.lock index 19e3ac4..3bb3b8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "configparser" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec6d3da8e550377a85339063af6e3735f4b1d9392108da4e083a1b3b9820288" + [[package]] name = "data-encoding" version = "2.5.0" @@ -83,6 +89,7 @@ checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5" name = "dns_lookup_project" version = "0.1.0" dependencies = [ + "configparser", "hickory-resolver", ] diff --git a/Cargo.toml b/Cargo.toml index 319bc92..6a5e7f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +configparser = "3.0.4" hickory-resolver = { version = "0.24.0" } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..56c781e --- /dev/null +++ b/src/config.rs @@ -0,0 +1,33 @@ +use configparser::ini::Ini; +use core::fmt; + +pub struct Config { + pub subdomains: Vec, + 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 = 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() + } + } +} \ No newline at end of file diff --git a/src/domain.rs b/src/domain.rs index 45fec5d..d32776d 100644 --- a/src/domain.rs +++ b/src/domain.rs @@ -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, ns_records: Vec, soa_records: Vec, // 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(); diff --git a/src/main.rs b/src/main.rs index 18f8019..d704ebb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); diff --git a/test.ini b/test.ini new file mode 100644 index 0000000..5996892 --- /dev/null +++ b/test.ini @@ -0,0 +1,3 @@ +[General] +wildcard_test=dfjgnkdfjngkdfngjkd +subdomains=www,ftp,mail \ No newline at end of file