rat_quickdns 0.2.4

A high-performance, feature-rich DNS client library with support for multiple transport protocols, load balancing, caching, and filtering
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! DNS解析器核心类型定义
//! 
//! 本模块定义了DNS查询过程中使用的核心数据结构

use serde::{Deserialize, Serialize};
use std::net::IpAddr;

/// DNS查询请求
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DnsQueryRequest {
    /// 查询ID(可选,用于追踪)
    pub query_id: Option<String>,
    
    /// 要查询的域名
    pub domain: String,
    
    /// 记录类型
    pub record_type: DnsRecordType,
    
    /// 是否启用EDNS
    pub enable_edns: bool,
    
    /// 客户端地址信息(用于CDN优化)
    pub client_address: Option<String>,
    
    /// 查询超时时间(毫秒)
    pub timeout_ms: Option<u64>,
    
    /// 是否禁用缓存
    pub disable_cache: bool,
    
    /// 是否启用DNSSEC验证
    pub enable_dnssec: bool,
}

impl DnsQueryRequest {
    /// 创建新的DNS查询请求
    pub fn new(domain: impl Into<String>, record_type: DnsRecordType) -> Self {
        Self {
            query_id: None,
            domain: domain.into(),
            record_type,
            enable_edns: true,
            client_address: None,
            timeout_ms: None,
            disable_cache: false,
            enable_dnssec: false,
        }
    }
    
    /// 设置查询ID
    pub fn with_query_id(mut self, id: impl Into<String>) -> Self {
        self.query_id = Some(id.into());
        self
    }
    
    /// 设置客户端地址
    pub fn with_client_address(mut self, address: impl Into<String>) -> Self {
        self.client_address = Some(address.into());
        self
    }
    
    /// 设置超时时间
    pub fn with_timeout(mut self, timeout_ms: u64) -> Self {
        self.timeout_ms = Some(timeout_ms);
        self
    }
    
    /// 禁用缓存
    pub fn disable_cache(mut self) -> Self {
        self.disable_cache = true;
        self
    }
    
    /// 启用DNSSEC验证
    pub fn with_dnssec(mut self, enable: bool) -> Self {
        self.enable_dnssec = enable;
        self
    }
}

/// DNS查询响应
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DnsQueryResponse {
    /// 查询ID
    pub query_id: String,
    
    /// 查询的域名
    pub domain: String,
    
    /// 记录类型
    pub record_type: DnsRecordType,
    
    /// 查询是否成功
    pub success: bool,
    
    /// 错误信息(如果失败)
    pub error: Option<String>,
    
    /// DNS记录列表
    pub records: Vec<DnsRecord>,
    
    /// 查询耗时(毫秒)
    pub duration_ms: u64,
    
    /// 使用的上游服务器
    pub server_used: Option<String>,
    
    /// DNSSEC验证状态
    pub dnssec_status: Option<DnssecStatus>,
    
    /// DNSSEC相关记录(RRSIG、DNSKEY等)
    pub dnssec_records: Vec<DnsRecord>,
}

impl DnsQueryResponse {
    /// 提取IP地址列表
    pub fn ip_addresses(&self) -> Vec<IpAddr> {
        self.records
            .iter()
            .filter_map(|record| {
                if let DnsRecordValue::IpAddr(ip) = &record.value {
                    Some(*ip)
                } else {
                    None
                }
            })
            .collect()
    }
    
    /// 提取域名列表(用于CNAME等记录)
    pub fn domains(&self) -> Vec<String> {
        self.records
            .iter()
            .filter_map(|record| {
                if let DnsRecordValue::Domain(domain) = &record.value {
                    Some(domain.clone())
                } else {
                    None
                }
            })
            .collect()
    }
    
    /// 提取文本列表(用于TXT记录)
    pub fn texts(&self) -> Vec<String> {
        self.records
            .iter()
            .filter_map(|record| {
                if let DnsRecordValue::Text(text) = &record.value {
                    Some(text.clone())
                } else {
                    None
                }
            })
            .collect()
    }
    
    /// 提取MX记录
    pub fn mx_records(&self) -> Vec<(u16, String)> {
        self.records
            .iter()
            .filter_map(|record| {
                if let DnsRecordValue::Mx { priority, exchange } = &record.value {
                    Some((*priority, exchange.clone()))
                } else {
                    None
                }
            })
            .collect()
    }
    
    /// 检查是否有DNSSEC记录
    pub fn has_dnssec_records(&self) -> bool {
        !self.dnssec_records.is_empty() || 
        self.records.iter().any(|r| r.record_type.is_dnssec_record())
    }
    
    /// 获取DNSSEC状态描述
    pub fn dnssec_status_description(&self) -> String {
        match &self.dnssec_status {
            Some(DnssecStatus::Secure) => "🔒 DNSSEC验证通过".to_string(),
            Some(DnssecStatus::Insecure) => "🔓 未启用DNSSEC".to_string(),
            Some(DnssecStatus::Bogus) => "⚠️ DNSSEC验证失败".to_string(),
            Some(DnssecStatus::Indeterminate) => "❓ DNSSEC状态不确定".to_string(),
            None => "➖ 无DNSSEC信息".to_string(),
        }
    }
    
    /// 提取DNSSEC相关记录
    pub fn dnssec_record_summary(&self) -> String {
        let dnssec_records: Vec<_> = self.records.iter()
            .filter(|r| r.record_type.is_dnssec_record())
            .collect();
        
        if dnssec_records.is_empty() {
            "无DNSSEC记录".to_string()
        } else {
            let mut summary = Vec::new();
            let mut counts = std::collections::HashMap::new();
            
            for record in &dnssec_records {
                *counts.entry(record.record_type).or_insert(0) += 1;
            }
            
            for (record_type, count) in counts {
                summary.push(format!("{}: {}", record_type.as_str(), count));
            }
            
            summary.join(", ")
        }
    }
}

/// DNSSEC验证状态
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DnssecStatus {
    /// 安全 - DNSSEC验证通过
    Secure,
    
    /// 不安全 - 域名未启用DNSSEC
    Insecure,
    
    /// 伪造 - DNSSEC验证失败
    Bogus,
    
    /// 不确定 - 无法验证DNSSEC状态
    Indeterminate,
}

/// DNS记录类型
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DnsRecordType {
    /// A记录 - IPv4地址
    A,
    
    /// AAAA记录 - IPv6地址
    AAAA,
    
    /// CNAME记录 - 别名
    CNAME,
    
    /// MX记录 - 邮件交换
    MX,
    
    /// TXT记录 - 文本记录
    TXT,
    
    /// NS记录 - 名称服务器
    NS,
    
    /// PTR记录 - 反向解析
    PTR,
    
    /// SRV记录 - 服务记录
    SRV,
    
    /// SOA记录 - 授权开始
    SOA,
    
    /// DNSSEC相关记录类型
    /// RRSIG记录 - 资源记录签名
    RRSIG,
    
    /// DNSKEY记录 - DNS公钥
    DNSKEY,
    
    /// DS记录 - 委托签名者
    DS,
    
    /// NSEC记录 - 下一个安全记录
    NSEC,
    
    /// NSEC3记录 - 下一个安全记录版本3
    NSEC3,
}

impl DnsRecordType {
    /// 获取记录类型的字符串表示
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::A => "A",
            Self::AAAA => "AAAA",
            Self::CNAME => "CNAME",
            Self::MX => "MX",
            Self::TXT => "TXT",
            Self::NS => "NS",
            Self::PTR => "PTR",
            Self::SRV => "SRV",
            Self::SOA => "SOA",
            Self::RRSIG => "RRSIG",
            Self::DNSKEY => "DNSKEY",
            Self::DS => "DS",
            Self::NSEC => "NSEC",
            Self::NSEC3 => "NSEC3",
        }
    }
    
    /// 从字符串解析记录类型
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_uppercase().as_str() {
            "A" => Some(Self::A),
            "AAAA" => Some(Self::AAAA),
            "CNAME" => Some(Self::CNAME),
            "MX" => Some(Self::MX),
            "TXT" => Some(Self::TXT),
            "NS" => Some(Self::NS),
            "PTR" => Some(Self::PTR),
            "SRV" => Some(Self::SRV),
            "SOA" => Some(Self::SOA),
            "RRSIG" => Some(Self::RRSIG),
            "DNSKEY" => Some(Self::DNSKEY),
            "DS" => Some(Self::DS),
            "NSEC" => Some(Self::NSEC),
            "NSEC3" => Some(Self::NSEC3),
            _ => None,
        }
    }
    
    /// 检查是否为DNSSEC相关记录类型
    pub fn is_dnssec_record(&self) -> bool {
        matches!(self, Self::RRSIG | Self::DNSKEY | Self::DS | Self::NSEC | Self::NSEC3)
    }
}

/// DNS记录
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DnsRecord {
    /// 记录名称
    pub name: String,
    
    /// 记录类型
    pub record_type: DnsRecordType,
    
    /// 记录值
    pub value: DnsRecordValue,
    
    /// TTL(生存时间)
    pub ttl: u32,
}

/// DNS记录值
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DnsRecordValue {
    /// IP地址(A/AAAA记录)
    IpAddr(IpAddr),
    
    /// 域名(CNAME/NS/PTR记录)
    Domain(String),
    
    /// 文本(TXT记录)
    Text(String),
    
    /// MX记录
    Mx {
        /// 优先级,数值越小优先级越高
        priority: u16,
        /// 邮件服务器域名
        exchange: String,
    },
    
    /// SRV记录
    Srv {
        /// 优先级,数值越小优先级越高
        priority: u16,
        /// 权重,用于负载均衡
        weight: u16,
        /// 服务端口号
        port: u16,
        /// 目标主机名
        target: String,
    },
    
    /// SOA记录
    Soa {
        /// 主名称服务器
        mname: String,
        /// 管理员邮箱
        rname: String,
        /// 序列号
        serial: u32,
        /// 刷新间隔(秒)
        refresh: u32,
        /// 重试间隔(秒)
        retry: u32,
        /// 过期时间(秒)
        expire: u32,
        /// 最小TTL(秒)
        minimum: u32,
    },
}

impl DnsRecord {
    /// 创建A记录
    pub fn a(name: impl Into<String>, ip: std::net::Ipv4Addr, ttl: u32) -> Self {
        Self {
            name: name.into(),
            record_type: DnsRecordType::A,
            value: DnsRecordValue::IpAddr(IpAddr::V4(ip)),
            ttl,
        }
    }
    
    /// 创建AAAA记录
    pub fn aaaa(name: impl Into<String>, ip: std::net::Ipv6Addr, ttl: u32) -> Self {
        Self {
            name: name.into(),
            record_type: DnsRecordType::AAAA,
            value: DnsRecordValue::IpAddr(IpAddr::V6(ip)),
            ttl,
        }
    }
    
    /// 创建CNAME记录
    pub fn cname(name: impl Into<String>, target: impl Into<String>, ttl: u32) -> Self {
        Self {
            name: name.into(),
            record_type: DnsRecordType::CNAME,
            value: DnsRecordValue::Domain(target.into()),
            ttl,
        }
    }
    
    /// 创建TXT记录
    pub fn txt(name: impl Into<String>, text: impl Into<String>, ttl: u32) -> Self {
        Self {
            name: name.into(),
            record_type: DnsRecordType::TXT,
            value: DnsRecordValue::Text(text.into()),
            ttl,
        }
    }
}