web_analyzer/
domain_dns.rs1use serde::{Deserialize, Serialize};
2use std::time::Instant;
3use tokio::process::Command;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct DnsRecords {
9 pub a: Vec<String>,
10 pub aaaa: Vec<String>,
11 pub mx: Vec<String>,
12 pub ns: Vec<String>,
13 pub soa: Vec<String>,
14 pub txt: Vec<String>,
15 pub cname: Vec<String>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct DomainDnsResult {
20 pub timestamp: String,
21 pub domain: String,
22 pub records: DnsRecords,
23 pub response_time_ms: u128,
24}
25
26async fn resolve_record(domain: &str, record_type: &str) -> Vec<String> {
29 if let Ok(output) = Command::new("dig")
30 .arg("+short")
31 .arg(record_type)
32 .arg(domain)
33 .output()
34 .await
35 {
36 if let Ok(text) = String::from_utf8(output.stdout) {
37 return text
38 .lines()
39 .filter_map(|s| {
40 let s = s.trim();
41 if !s.is_empty() && !s.starts_with(';') {
42 Some(s.to_string())
43 } else {
44 None
45 }
46 })
47 .collect();
48 }
49 }
50 vec![]
51}
52
53pub async fn get_dns_records(
56 domain: &str,
57) -> Result<DomainDnsResult, Box<dyn std::error::Error + Send + Sync>> {
58 let start_time = Instant::now();
59
60 let (a, aaaa, mx, ns, soa, txt, cname) = tokio::join!(
62 resolve_record(domain, "A"),
63 resolve_record(domain, "AAAA"),
64 resolve_record(domain, "MX"),
65 resolve_record(domain, "NS"),
66 resolve_record(domain, "SOA"),
67 resolve_record(domain, "TXT"),
68 resolve_record(domain, "CNAME"),
69 );
70
71 let duration = start_time.elapsed().as_millis();
72
73 Ok(DomainDnsResult {
74 timestamp: chrono::Utc::now().to_rfc3339(),
75 domain: domain.to_string(),
76 records: DnsRecords {
77 a,
78 aaaa,
79 mx,
80 ns,
81 soa,
82 txt,
83 cname,
84 },
85 response_time_ms: duration,
86 })
87}