Added config parsing

This commit is contained in:
Benjamyn Love 2024-03-22 17:01:34 +11:00
parent 91db678c55
commit bca8892147
6 changed files with 60 additions and 4 deletions

7
Cargo.lock generated
View File

@ -73,6 +73,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "configparser"
version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec6d3da8e550377a85339063af6e3735f4b1d9392108da4e083a1b3b9820288"
[[package]] [[package]]
name = "data-encoding" name = "data-encoding"
version = "2.5.0" version = "2.5.0"
@ -83,6 +89,7 @@ checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5"
name = "dns_lookup_project" name = "dns_lookup_project"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"configparser",
"hickory-resolver", "hickory-resolver",
] ]

View File

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
configparser = "3.0.4"
hickory-resolver = { version = "0.24.0" } hickory-resolver = { version = "0.24.0" }

33
src/config.rs Normal file
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()
}
}
}

View File

@ -4,6 +4,8 @@ use hickory_resolver::config::*;
use core::fmt; use core::fmt;
use crate::config::Config;
// #[derive(Debug)] // #[derive(Debug)]
pub struct Domain { pub struct Domain {
domain_name: String, domain_name: String,
@ -14,7 +16,7 @@ pub struct Domain {
mx_records: Vec<MX>, mx_records: Vec<MX>,
ns_records: Vec<NS>, ns_records: Vec<NS>,
soa_records: Vec<SOA>, // Subdomains CAN have there own SOA records if their zones point elsewhere 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 { impl fmt::Display for Domain {
@ -74,6 +76,12 @@ impl Domain {
// println!("Added: {}", new_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) { pub fn lookup_all_records(&mut self) {
// Lookup A records // Lookup A records
self.lookup_a(); self.lookup_a();

View File

@ -1,11 +1,15 @@
mod domain; mod domain;
use crate::domain::Domain; use crate::domain::Domain;
mod config;
use crate::config::Config;
fn main() { fn main() {
let mut test = Domain::new("pigandpilgrim.com.au".to_string()); let mut test = Domain::new("pigandpilgrim.com.au".to_string());
test.append_subdomain("www".to_string()); let config = Config::from_file("test.ini".to_string());
test.append_subdomain("mail".to_string()); test.apply_config(&config);
test.append_subdomain("ftp".to_string());
test.lookup_all_records(); test.lookup_all_records();
println!("{}", test); println!("{}", test);

3
test.ini Normal file
View File

@ -0,0 +1,3 @@
[General]
wildcard_test=dfjgnkdfjngkdfngjkd
subdomains=www,ftp,mail