add support for --reverse in trust-dns-util

* Previous code allowed for reverse lookups but required the user to
  pass `in-addr.arpa` hostnames.  Passing `--reverse` makes the
  conversion automatically.

* Minor changes to output to make the query type explicit.
This commit is contained in:
Italo Cunha 2022-09-27 09:37:23 -03:00 committed by Benjamin Fry
parent 29eeef4593
commit b1b386f59a
2 changed files with 32 additions and 9 deletions

View File

@ -429,6 +429,12 @@ macro_rules! lookup_type {
}
}
impl From<$l> for Lookup {
fn from(revlookup: $l) -> Self {
revlookup.0
}
}
/// An iterator over the Lookup type
pub struct $i<'i>(LookupIter<'i>);

View File

@ -50,9 +50,13 @@ struct Opts {
ty: RecordType,
/// Happy eye balls lookup, ipv4 and ipv6
#[clap(short = 'e', long = "happy", conflicts_with("ty"))]
#[clap(short = 'e', long = "happy", conflicts_with_all(&["reverse", "ty"]))]
happy: bool,
/// Reverse DNS lookup
#[clap(short = 'r', long = "reverse", conflicts_with_all(&["happy", "ty"]))]
reverse: bool,
/// Use system configuration, e.g. /etc/resolv.conf, instead of defaults
#[clap(short = 's', long = "system")]
system: bool,
@ -225,18 +229,31 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
let resolver = TokioAsyncResolver::tokio(config, options)?;
// execute query
println!(
"Querying for {name} {ty} from {ns}",
name = style(name).yellow(),
ty = style(ty).yellow(),
ns = style(name_servers).blue()
);
let lookup = if opts.happy {
println!(
"Querying for {name} {ty} from {ns}",
name = style(name).yellow(),
ty = style("A+AAAA").yellow(),
ns = style(name_servers).blue()
);
let lookup = resolver.lookup_ip(name.to_string()).await?;
lookup.into()
} else if opts.reverse {
let v4addr = name.parse::<IpAddr>()?;
println!(
"Querying {reverse} for {name} from {ns}",
reverse = style("reverse").yellow(),
name = style(name).yellow(),
ns = style(name_servers).blue()
);
resolver.reverse_lookup(v4addr).await?.into()
} else {
println!(
"Querying for {name} {ty} from {ns}",
name = style(name).yellow(),
ty = style(ty).yellow(),
ns = style(name_servers).blue()
);
resolver.lookup(name.to_string(), ty).await?
};