lazydns 0.3.20

A light and fast DNS server/forwarder implementation in Rust
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! WebUI server configuration
//!
//! Configuration for the WebUI HTTP server with real-time streaming.

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

/// Default WebUI listen address
fn default_listen_addr() -> String {
    "127.0.0.1:8080".to_string()
}

/// Default event bus capacity
fn default_event_bus_capacity() -> usize {
    1024
}

/// Default metrics window in seconds
fn default_metrics_window_secs() -> u64 {
    300 // 5 minutes
}

/// Default top-N count
fn default_top_n() -> usize {
    10
}

/// Default SSE keepalive interval in seconds
fn default_sse_keepalive_secs() -> u64 {
    30
}

/// Default max SSE connections
fn default_max_sse_connections() -> usize {
    100
}

/// Default max WebSocket connections
fn default_max_ws_connections() -> usize {
    50
}

/// WebUI server configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebConfig {
    /// Whether WebUI is enabled
    #[serde(default)]
    pub enabled: bool,

    /// Listen address for the WebUI server
    #[serde(default = "default_listen_addr")]
    pub listen: String,

    /// Path to static files directory (for development)
    #[serde(default)]
    pub static_dir: Option<String>,

    /// Event bus capacity (number of events to buffer)
    #[serde(default = "default_event_bus_capacity")]
    pub event_bus_capacity: usize,

    /// Metrics configuration
    #[serde(default)]
    pub metrics: MetricsConfig,

    /// SSE (Server-Sent Events) configuration
    #[serde(default)]
    pub sse: SseConfig,

    /// WebSocket configuration
    #[serde(default)]
    pub websocket: WebSocketConfig,

    /// Alert configuration
    #[serde(default)]
    pub alerts: AlertConfig,

    /// CORS configuration
    #[serde(default)]
    pub cors: CorsConfig,
}

impl Default for WebConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            listen: default_listen_addr(),
            static_dir: None,
            event_bus_capacity: default_event_bus_capacity(),
            metrics: MetricsConfig::default(),
            sse: SseConfig::default(),
            websocket: WebSocketConfig::default(),
            alerts: AlertConfig::default(),
            cors: CorsConfig::default(),
        }
    }
}

impl WebConfig {
    /// Parse listen address to SocketAddr
    pub fn socket_addr(&self) -> Result<SocketAddr, std::net::AddrParseError> {
        self.listen.parse()
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<(), String> {
        // Validate listen address
        self.socket_addr()
            .map_err(|e| format!("Invalid listen address '{}': {}", self.listen, e))?;

        // Validate event bus capacity
        if self.event_bus_capacity == 0 {
            return Err("event_bus_capacity must be greater than 0".to_string());
        }

        // Validate metrics
        self.metrics.validate()?;

        // Validate SSE
        self.sse.validate()?;

        // Validate WebSocket
        self.websocket.validate()?;

        Ok(())
    }
}

/// Metrics collection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsConfig {
    /// Time window for metrics aggregation (seconds)
    #[serde(default = "default_metrics_window_secs")]
    pub window_secs: u64,

    /// Number of top items to track (domains, clients, and others)
    #[serde(default = "default_top_n")]
    pub top_n: usize,

    /// Whether to track per-client statistics
    #[serde(default = "default_true")]
    pub track_clients: bool,

    /// Whether to track per-domain statistics
    #[serde(default = "default_true")]
    pub track_domains: bool,

    /// Whether to track latency distribution
    #[serde(default = "default_true")]
    pub track_latency: bool,
}

fn default_true() -> bool {
    true
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            window_secs: default_metrics_window_secs(),
            top_n: default_top_n(),
            track_clients: true,
            track_domains: true,
            track_latency: true,
        }
    }
}

impl MetricsConfig {
    fn validate(&self) -> Result<(), String> {
        if self.window_secs == 0 {
            return Err("metrics.window_secs must be greater than 0".to_string());
        }
        if self.top_n == 0 {
            return Err("metrics.top_n must be greater than 0".to_string());
        }
        Ok(())
    }
}

/// SSE (Server-Sent Events) configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SseConfig {
    /// Maximum number of concurrent SSE connections
    #[serde(default = "default_max_sse_connections")]
    pub max_connections: usize,

    /// Keepalive interval in seconds (sends comment to keep connection alive)
    #[serde(default = "default_sse_keepalive_secs")]
    pub keepalive_secs: u64,

    /// Buffer size for SSE events per connection
    #[serde(default = "default_sse_buffer")]
    pub buffer_size: usize,
}

fn default_sse_buffer() -> usize {
    100
}

impl Default for SseConfig {
    fn default() -> Self {
        Self {
            max_connections: default_max_sse_connections(),
            keepalive_secs: default_sse_keepalive_secs(),
            buffer_size: default_sse_buffer(),
        }
    }
}

impl SseConfig {
    fn validate(&self) -> Result<(), String> {
        if self.max_connections == 0 {
            return Err("sse.max_connections must be greater than 0".to_string());
        }
        if self.keepalive_secs == 0 {
            return Err("sse.keepalive_secs must be greater than 0".to_string());
        }
        Ok(())
    }
}

/// WebSocket configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSocketConfig {
    /// Maximum number of concurrent WebSocket connections
    #[serde(default = "default_max_ws_connections")]
    pub max_connections: usize,

    /// Heartbeat interval in seconds
    #[serde(default = "default_ws_heartbeat")]
    pub heartbeat_secs: u64,

    /// Connection timeout in seconds (no heartbeat response)
    #[serde(default = "default_ws_timeout")]
    pub timeout_secs: u64,

    /// Maximum message size in bytes
    #[serde(default = "default_ws_max_message_size")]
    pub max_message_size: usize,
}

fn default_ws_heartbeat() -> u64 {
    30
}

fn default_ws_timeout() -> u64 {
    60
}

fn default_ws_max_message_size() -> usize {
    64 * 1024 // 64KB
}

impl Default for WebSocketConfig {
    fn default() -> Self {
        Self {
            max_connections: default_max_ws_connections(),
            heartbeat_secs: default_ws_heartbeat(),
            timeout_secs: default_ws_timeout(),
            max_message_size: default_ws_max_message_size(),
        }
    }
}

impl WebSocketConfig {
    fn validate(&self) -> Result<(), String> {
        if self.max_connections == 0 {
            return Err("websocket.max_connections must be greater than 0".to_string());
        }
        if self.heartbeat_secs == 0 {
            return Err("websocket.heartbeat_secs must be greater than 0".to_string());
        }
        if self.timeout_secs <= self.heartbeat_secs {
            return Err("websocket.timeout_secs must be greater than heartbeat_secs".to_string());
        }
        Ok(())
    }
}

/// Alert engine configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertConfig {
    /// Whether alerting is enabled
    #[serde(default)]
    pub enabled: bool,

    /// Alert rules
    #[serde(default)]
    pub rules: Vec<AlertRule>,

    /// Deduplication window in seconds
    #[serde(default = "default_dedup_window")]
    pub dedup_window_secs: u64,

    /// Maximum alerts to keep in memory
    #[serde(default = "default_max_alerts")]
    pub max_alerts: usize,

    /// Webhook configuration
    #[serde(default)]
    pub webhook: Option<WebhookConfig>,
}

fn default_dedup_window() -> u64 {
    300 // 5 minutes
}

fn default_max_alerts() -> usize {
    1000
}

impl Default for AlertConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            rules: Vec::new(),
            dedup_window_secs: default_dedup_window(),
            max_alerts: default_max_alerts(),
            webhook: None,
        }
    }
}

/// Alert rule definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertRule {
    /// Rule name
    pub name: String,

    /// Condition type
    pub condition: AlertCondition,

    /// Severity level
    #[serde(default)]
    pub severity: AlertSeverity,

    /// Optional message template
    #[serde(default)]
    pub message: Option<String>,
}

/// Alert condition types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AlertCondition {
    /// Trigger on security event type
    SecurityEvent { event_type: String },
    /// Trigger when rate exceeds threshold
    RateThreshold {
        metric: String,
        threshold: f64,
        window_secs: u64,
    },
    /// Trigger on upstream health change
    UpstreamHealth { status: String },
    /// Trigger when error rate exceeds threshold
    ErrorRate { threshold: f64, window_secs: u64 },
}

/// Alert severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum AlertSeverity {
    Info,
    #[default]
    Warning,
    Error,
    Critical,
}

/// Webhook notification configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookConfig {
    /// Webhook URL
    pub url: String,

    /// Optional authorization header
    #[serde(default)]
    pub auth_header: Option<String>,

    /// Timeout in seconds
    #[serde(default = "default_webhook_timeout")]
    pub timeout_secs: u64,

    /// Retry count on failure
    #[serde(default = "default_webhook_retries")]
    pub retries: u32,
}

fn default_webhook_timeout() -> u64 {
    10
}

fn default_webhook_retries() -> u32 {
    3
}

/// CORS configuration
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CorsConfig {
    /// Allowed origins (empty = allow all)
    #[serde(default)]
    pub allowed_origins: Vec<String>,

    /// Whether to allow credentials
    #[serde(default)]
    pub allow_credentials: bool,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = WebConfig::default();
        assert!(!config.enabled);
        assert_eq!(config.listen, "127.0.0.1:8080");
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_parse_socket_addr() {
        let config = WebConfig::default();
        let addr = config.socket_addr().unwrap();
        assert_eq!(addr.port(), 8080);
    }

    #[test]
    fn test_invalid_listen_addr() {
        let config = WebConfig {
            listen: "invalid".to_string(),
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_invalid_metrics_config() {
        let mut config = WebConfig::default();
        config.metrics.top_n = 0;
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_websocket_timeout_validation() {
        let mut config = WebConfig::default();
        config.websocket.timeout_secs = 10;
        config.websocket.heartbeat_secs = 20;
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_alert_rule_deserialization() {
        let yaml = r#"
            name: high_rate_limit
            condition:
              type: security_event
              event_type: rate_limit_exceeded
            severity: warning
        "#;
        let rule: AlertRule = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(rule.name, "high_rate_limit");
        assert_eq!(rule.severity, AlertSeverity::Warning);
    }
}