Signed-off-by: 0xf8 <0xf8.dev@proton.me>
This commit is contained in:
0xf8 2023-02-25 12:05:07 -05:00
parent c8ad7f0086
commit c1c31367e0
Signed by: 0xf8
GPG Key ID: 446580D758689584
4 changed files with 367 additions and 2 deletions

14
.fleet/run.json Normal file
View File

@ -0,0 +1,14 @@
{
"configurations": [
{
"type": "cargo",
"name": "Debug",
"cargoArgs": ["run"],
},
{
"type": "cargo",
"name": "Release",
"cargoArgs": ["run", "-r"],
},
]
}

181
Cargo.lock generated
View File

@ -2,6 +2,187 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "argparse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f8ebf5827e4ac4fd5946560e6a99776ea73b596d80898f357007317a7141e47"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "idna"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e"
dependencies = [
"matches",
"unicode-bidi",
"unicode-normalization",
]
[[package]]
name = "json"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "log"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
dependencies = [
"log 0.4.17",
]
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "matches"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5"
[[package]]
name = "rand"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
dependencies = [
"libc",
"rand 0.4.6",
]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "resdns"
version = "0.1.0"
dependencies = [
"argparse",
"json",
"resolve",
]
[[package]]
name = "resolve"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19526b305899bea65f26edda78a64f5313958494321ee0ab66bd94b32958614a"
dependencies = [
"idna",
"libc",
"log 0.3.9",
"rand 0.3.23",
]
[[package]]
name = "tinyvec"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "unicode-bidi"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54675592c1dbefd78cbd98db9bacd89886e1ca50692a0692baefffdeb92dd58"
[[package]]
name = "unicode-normalization"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
dependencies = [
"tinyvec",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -6,3 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
argparse = "0.2.2"
json = "0.12.4"
resolve = "0.2.0"

View File

@ -1,5 +1,172 @@
use argparse::{ArgumentParser, List, Store};
use resolve::{record, DnsConfig, DnsResolver};
use std::time::Duration;
#[derive(Debug, Clone)]
struct Record {
pub host: String,
pub addr: String,
}
#[derive(Debug)]
enum OutputParser {
Plain,
JSON { pretty: bool },
JSONPretty { pretty: bool },
CSV,
}
struct Resolver {
resolver: DnsResolver,
}
impl Resolver {
pub fn init() -> Resolver {
let config = DnsConfig {
timeout: Duration::from_secs(3),
attempts: 2,
use_inet6: false,
..DnsConfig::load_default().expect("Couldn't get default DnsConfig")
};
Resolver {
resolver: DnsResolver::new(config).expect("Couldn't create DNS resolver"),
}
}
pub fn resolve(&self, host: &str) -> Vec<Record> {
let rec4 = self
.resolver
.resolve_record::<record::A>(host)
.unwrap_or(vec![]);
let rec6 = self
.resolver
.resolve_record::<record::AAAA>(host)
.unwrap_or(vec![]);
let mut recs: Vec<Record> = vec![];
for r in rec4 {
recs.insert(
0,
Record {
host: host.to_owned(),
addr: r.address.to_string(),
},
)
}
for r in rec6 {
recs.insert(
0,
Record {
host: host.to_owned(),
addr: r.address.to_string(),
},
)
}
recs.sort_unstable_by(|a, b| a.addr.len().cmp(&b.addr.len()));
recs
}
}
fn main() {
println!("Hello, world!");
}
let mut output_flag = String::from("Default");
let mut hosts: Vec<String> = vec![];
// Argument parser
{
let mut args = ArgumentParser::new();
args.set_description("Resolve Host(s) to IP Address(es).");
// -o, --output, --parser
args.refer(&mut output_flag).add_option(
&["-o", "--output", "--parser"],
Store,
"Select how the response is outputted: [Default, Plain, JSON, JSON Pretty, CSV]",
);
// Positional arg: hosts
args.refer(&mut hosts)
.add_argument("hosts", List, "Hosts to resolve")
.required();
args.parse_args_or_exit();
}
// OutputParser
let output = match output_flag.to_lowercase().as_str() {
"default" | "plain" | "" => OutputParser::Plain,
"json" => OutputParser::JSON { pretty: false },
"json-pretty" | "json_pretty" | "json pretty" | "jsonpretty" => {
OutputParser::JSONPretty { pretty: true }
}
"csv" => OutputParser::CSV,
_ => {
eprintln!("[FATAL] Invalid output parser: \"{output_flag}\"");
std::process::exit(1)
}
};
// No hosts
if hosts.is_empty() {
eprintln!("[Fatal] No hosts given");
std::process::exit(1)
}
// Resolver
let resolver: Resolver = Resolver::init();
let mut record_pile: Vec<Record> = vec![];
for h in hosts {
record_pile.append(&mut resolver.resolve(h.as_str()));
}
match output {
// Plain / Default output
OutputParser::Plain => {
for r in record_pile {
println!("{}: {}", r.host, r.addr)
}
}
// JSON / JSON Pretty output
OutputParser::JSON { pretty } | OutputParser::JSONPretty { pretty } => {
let mut rj = json::object! { records: {} };
for r in record_pile {
if rj["records"][&r.host].is_null() {
rj["records"]
.insert(
&r.host,
json::parse(&format!("[\"{}\"]", r.addr))
.expect("Unable to parse JSON"),
)
.expect("Unable to insert record into JSON");
} else {
rj["records"][&r.host]
.push(r.addr)
.expect("Unable to insert record into JSON");
}
}
println!(
"{}",
match pretty {
true => rj.pretty(2),
false => rj.dump(),
}
);
}
// CSV output
OutputParser::CSV => {
println!("\"Host\", \"Address\"");
for r in record_pile {
println!("\"{}\", \"{}\"", r.host, r.addr)
}
}
// _ => (),
}
}