mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-07-21 00:06:03 +02:00
Added a templating helper
This commit is contained in:
118
src/simple_dns.rs
Normal file
118
src/simple_dns.rs
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* This module wraps the trust_dns_resolver library
|
||||
* to generate results thaat are ready for serializing
|
||||
* or templating.
|
||||
* It does not aim to be reusable for any other purpose,
|
||||
* the trust_dns_resolver library already does that.
|
||||
*/
|
||||
|
||||
use trust_dns_resolver::{
|
||||
TokioAsyncResolver,
|
||||
error::*,
|
||||
};
|
||||
|
||||
use std::net::IpAddr;
|
||||
|
||||
|
||||
/* Data Structures */
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
#[derive(Default)]
|
||||
pub struct DnsLookupResult {
|
||||
a: Vec<IpAddr>,
|
||||
aaaa: Vec<IpAddr>,
|
||||
mx: Vec<MxRecord>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize)]
|
||||
pub struct MxRecord {
|
||||
preference: u16,
|
||||
exchange: String,
|
||||
}
|
||||
|
||||
/* Lookup Functions*/
|
||||
|
||||
pub async fn reverse_lookup(
|
||||
resolver: &TokioAsyncResolver,
|
||||
address: &IpAddr,
|
||||
) -> Option<String> {
|
||||
let revese_res = resolver.reverse_lookup(*address);
|
||||
match revese_res.await {
|
||||
Ok(lookup) => {
|
||||
for name in lookup {
|
||||
return Some(name.to_string())
|
||||
}
|
||||
None
|
||||
}
|
||||
Err(e) => {
|
||||
let kind = e.kind();
|
||||
match kind {
|
||||
ResolveErrorKind::NoRecordsFound { .. } => {
|
||||
//Ignore, that just happens …
|
||||
}
|
||||
_ => {
|
||||
println!("Reverse lookup on {address} failed: {kind}");
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This function takes a resolver, a domain name and returns a DnsLookupResult.
|
||||
// If do_full_lookup is false only the A and AAAA (CNAMEs planned for the future)
|
||||
// records will be fetched.
|
||||
pub async fn lookup(
|
||||
resolver: &TokioAsyncResolver,
|
||||
name: &String,
|
||||
do_full_lookup: bool,
|
||||
) -> DnsLookupResult {
|
||||
let ipv4_lookup_res = resolver.ipv4_lookup(name);
|
||||
let ipv6_lookup_res = resolver.ipv6_lookup(name);
|
||||
|
||||
// initlize an empty lookup result
|
||||
let mut dig_result: DnsLookupResult = Default::default();
|
||||
|
||||
match ipv4_lookup_res.await {
|
||||
Ok(lookup) => {
|
||||
for address in lookup {
|
||||
dig_result.a.push(std::net::IpAddr::V4(address));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("There was an error while looking A up {name}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
match ipv6_lookup_res.await {
|
||||
Ok(lookup) => {
|
||||
for address in lookup {
|
||||
dig_result.aaaa.push(std::net::IpAddr::V6(address));
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("There was an error while looking AAAA up {name}: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
if do_full_lookup {
|
||||
let mx_lookup_res = resolver.mx_lookup(name);
|
||||
|
||||
match mx_lookup_res.await {
|
||||
Ok(lookup) => {
|
||||
for mx in lookup {
|
||||
dig_result.mx.push(MxRecord{
|
||||
preference: mx.preference(),
|
||||
exchange: mx.exchange().to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("There was an error while looking MX up {name}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dig_result
|
||||
|
||||
}
|
Reference in New Issue
Block a user