initial resolve binary

This commit is contained in:
Benjamin Fry 2020-09-17 12:33:11 -07:00
parent 4af665e089
commit 1f0853962b
4 changed files with 212 additions and 1 deletions

111
Cargo.lock generated
View File

@ -284,6 +284,23 @@ dependencies = [
"cache-padded",
]
[[package]]
name = "console"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0b1aacfaffdbff75be81c15a399b4bedf78aaefe840e8af1d299ac2ade885d2"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"regex",
"terminal_size",
"termios",
"unicode-width",
"winapi 0.3.9",
"winapi-util",
]
[[package]]
name = "core-foundation"
version = "0.7.0"
@ -317,6 +334,12 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4d0e2d24e5ee3b23a01de38eefdcd978907890701f08ffffd4cb457ca4ee8d6"
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "endian-type"
version = "0.1.2"
@ -1003,6 +1026,30 @@ version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro-hack"
version = "0.5.18"
@ -1288,6 +1335,30 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6cc388d94ffabf39b5ed5fadddc40147cb21e605f53db6f8f36a625d27489ac5"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e2513111825077552a6751dfad9e11ce0fba07d7276a3943a037d7e93e64c5f"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.41"
@ -1322,6 +1393,25 @@ dependencies = [
"winapi-util",
]
[[package]]
name = "terminal_size"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a14cd9f8c72704232f0bfc8455c0e861f0ad4eb60cc9ec8a170e231414c1e13"
dependencies = [
"libc",
"winapi 0.3.9",
]
[[package]]
name = "termios"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f0fcee7b24a25675de40d5bb4de6e41b0df07bc9856295e7e2b3a3600c400c2"
dependencies = [
"libc",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@ -1392,6 +1482,18 @@ dependencies = [
"num_cpus",
"pin-project-lite",
"slab",
"tokio-macros",
]
[[package]]
name = "tokio-macros"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
@ -1707,10 +1809,13 @@ name = "trust-dns-util"
version = "0.20.0-alpha.2"
dependencies = [
"clap",
"console",
"data-encoding",
"env_logger",
"log",
"openssl",
"structopt",
"tokio",
"trust-dns-client",
"trust-dns-proto",
"trust-dns-resolver",
@ -1800,6 +1905,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "waker-fn"
version = "1.1.0"

View File

@ -6,7 +6,6 @@
// copied, modified, or distributed except according to those terms.
//! Configuration for a resolver
#[cfg(feature = "dns-over-rustls")]
use std::fmt;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::ops::{Deref, DerefMut};
@ -251,6 +250,23 @@ pub enum Protocol {
Mdns,
}
impl fmt::Display for Protocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let protocol = match self {
Protocol::Udp => "udp",
Protocol::Tcp => "tcp",
#[cfg(feature = "dns-over-tls")]
Protocol::Tls => "tls",
#[cfg(feature = "dns-over-https")]
Protocol::Https => "https",
#[cfg(feature = "mdns")]
Protocol::Mdns => "mdns",
};
write!(f, "{}", protocol)
}
}
impl Protocol {
/// Returns true if this is a datagram oriented protocol, e.g. UDP
pub fn is_datagram(self) -> bool {
@ -325,6 +341,18 @@ pub struct NameServerConfig {
pub tls_config: Option<TlsClientConfig>,
}
impl fmt::Display for NameServerConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:", self.protocol)?;
if let Some(ref tls_dns_name) = self.tls_dns_name {
write!(f, "{}@", tls_dns_name)?;
}
write!(f, "{}", self.socket_addr)
}
}
/// A set of name_servers to associate with a [`ResolverConfig`].
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(

View File

@ -47,8 +47,13 @@ path = "src/get_root_ksks.rs"
name = "pem-to-public-dnskey"
path = "src/pem_to_public_dnskey.rs"
[[bin]]
name = "resolve"
path = "src/resolve.rs"
[dependencies]
clap = "2.33.1"
console = "0.12.0"
data-encoding = "2.2.0"
trust-dns-client= { version = "0.20.0-alpha.2", features = ["dnssec-openssl"], path = "../crates/client" }
trust-dns-proto = { version = "0.20.0-alpha.2", features = ["dnssec-openssl"], path = "../crates/proto" }
@ -56,3 +61,5 @@ trust-dns-resolver = { version = "0.20.0-alpha.2", features = ["dnssec-openssl"]
env_logger = "0.7"
log = "0.4"
openssl = { version = "0.10", features = ["v102", "v110"] }
structopt = "0.3"
tokio = { version = "0.2.22", features = ["rt-threaded", "macros"] }

65
util/src/resolve.rs Normal file
View File

@ -0,0 +1,65 @@
// Copyright 2015-2020 Benjamin Fry <benjaminfry@me.com>
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use console::Term;
use structopt::StructOpt;
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
use trust_dns_resolver::proto::rr::RecordType;
use trust_dns_resolver::TokioAsyncResolver;
/// A CLI interface for the trust-dns-resolver.
#[derive(Debug, StructOpt)]
struct Opts {
/// Name to attempt to resolve, if followed by a '.' then it's a fully-qualified-domain-name.
domainname: String,
/// Type of query to issue, e.g. A, AAAA, NS, etc.
#[structopt(short = "t", long = "type", default_value = "A")]
ty: RecordType,
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
let opts: Opts = Opts::from_args();
let term = Term::stdout();
let name = &opts.domainname;
let config = ResolverConfig::default();
let ty = opts.ty;
let name_servers = config.name_servers().iter().map(|n| format!("{}", n)).fold(
String::new(),
|mut names, n| {
if !names.is_empty() {
names.push_str(", ")
}
names.push_str(&n);
names
},
);
term.write_line(&format!(
"Querying for {} {} from {}",
name, ty, name_servers
))?;
let resolver = TokioAsyncResolver::tokio(config, ResolverOpts::default())?;
let lookup = resolver
.lookup(name.to_string(), ty, Default::default())
.await?;
term.write_line(&format!("{:?} succeeeded, records:", lookup.query()))?;
for record in lookup.record_iter() {
term.write_line(&format!("{:?}", record))?;
}
Ok(())
}