Last active
March 26, 2021 08:03
-
-
Save Bas-Man/b869526dc3bf085e7c5de14f3fa57494 to your computer and use it in GitHub Desktop.
trust-dns-resolver looking up SOA records in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use trust_dns_resolver::error::ResolveResult; | |
use trust_dns_resolver::Resolver; | |
use trust_dns_resolver::{config::*, lookup::SoaLookup}; | |
fn main() { | |
// Construct a new Resolver with default configuration options | |
let resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default()).unwrap(); | |
let soa_response = resolver.soa_lookup("gmail.com."); | |
display_soa(&soa_response); | |
} | |
fn display_soa(soa_response: &ResolveResult<SoaLookup>) { | |
match soa_response { | |
Err(_) => println!("No SOA."), | |
Ok(soa_response) => { | |
let soa_iter = soa_response.iter(); | |
for record in soa_iter { | |
println!("Admin: {}", record.rname()); | |
let email = convert_rname_to_email_address(&record.rname().to_string()); | |
println!("Admin Email: {}", email); | |
println!("Primary: {}", record.mname()); | |
println!("Serial: {}", record.serial()); | |
} | |
} | |
} | |
} | |
fn convert_rname_to_email_address(rname: &String) -> String { | |
//Solution found on StackOverFlow :) | |
let rname = rname.clone(); | |
let mut email_address: String = String::new(); | |
let mut splitter = rname.splitn(2, "."); | |
email_address.push_str(splitter.next().unwrap()); | |
email_address.push_str("@"); | |
email_address.push_str(splitter.next().unwrap()); | |
// Remove remaining period (.) | |
email_address.pop(); | |
email_address | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment