ate_comms/dns/
query.rs

1use std::net::IpAddr;
2use std::str::FromStr;
3use ate_crypto::AteHash;
4#[allow(unused_imports, dead_code)]
5use tracing::{debug, error, info, trace, warn};
6
7pub use trust_dns_client::rr::*;
8
9impl super::Dns
10{
11    pub async fn dns_certs(&mut self, name: &str) -> Vec<AteHash> {
12        match name.to_lowercase().as_str() {
13            "localhost" => {
14                return Vec::new();
15            }
16            _ => {}
17        };
18
19        if let Ok(_) = IpAddr::from_str(name) {
20            return Vec::new();
21        }
22
23        trace!("dns_query for {}", name);
24        
25        let mut txts = Vec::new();
26        if let Some(response) = self
27            .query(Name::from_str(name).unwrap(), DNSClass::IN, RecordType::TXT)
28            .await
29            .ok()
30        {
31            for answer in response.answers() {
32                if let RData::TXT(ref txt) = *answer.rdata() {
33                    txts.push(txt.to_string());
34                }
35            }
36        }
37
38        let prefix = "ate-cert-";
39
40        let mut certs = Vec::new();
41        for txt in txts {
42            let txt = txt.replace(" ", "");
43            if txt.trim().starts_with(prefix) {
44                let start = prefix.len();
45                let hash = &txt.trim()[start..];
46                if let Some(hash) = AteHash::from_hex_string(hash) {
47                    trace!("found certificate({}) for {}", hash, name);
48                    certs.push(hash);
49                }
50            }
51        }
52        trace!(
53            "dns_query for {} returned {} certificates",
54            name,
55            certs.len()
56        );
57
58        certs
59    }
60}