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
//! Rate limiting plugin for DNS queries
//!
//! Implements rate limiting to prevent DoS attacks and resource exhaustion.

use crate::config::PluginConfig;
use crate::dns::ResponseCode;
use crate::plugin::{BackgroundTask, Context, Plugin, RETURN_FLAG};
use crate::{RegisterPlugin, Result};
use async_trait::async_trait;
use dashmap::DashMap;
use std::any::Any;
use std::net::IpAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::{debug, warn};

// Auto-register plugin builder

/// Rate limiter entry tracking request history
#[derive(Debug)]
struct RateLimitEntry {
    /// Number of requests in current window
    count: u32,
    /// Window start time
    window_start: Instant,
}

/// Rate limiting plugin
///
/// Limits the number of queries from a single IP address within a time window.
///
/// # YAML configuration example
///
/// ```yaml
/// # plugins: (top-level list of plugins)
/// - name: my_rate_limiter
///   type: rate_limit
///   args:
///     max_queries: 100     # max queries per window per IP (integer)
///     window_secs: 60      # window length in seconds (integer)
/// ```
///
/// # Rust example
///
/// ```rust
/// use lazydns::plugins::RateLimitPlugin;
/// use lazydns::plugin::Plugin;
///
/// // Allow 100 queries per 60 seconds per IP
/// let rate_limiter = RateLimitPlugin::new(100, 60);
/// assert_eq!(rate_limiter.name(), "rate_limit");
/// ```
#[derive(Debug, Clone, RegisterPlugin)]
pub struct RateLimitPlugin {
    /// Maximum queries per window
    max_queries: u32,
    /// Time window in seconds
    window_secs: u64,
    /// Storage for rate limit tracking
    limits: Arc<DashMap<IpAddr, RateLimitEntry>>,
}

impl RateLimitPlugin {
    /// Create a new rate limit plugin
    ///
    /// # Arguments
    ///
    /// * `max_queries` - Maximum number of queries allowed per window
    /// * `window_secs` - Time window in seconds
    pub fn new(max_queries: u32, window_secs: u64) -> Self {
        Self {
            max_queries,
            window_secs,
            limits: Arc::new(DashMap::new()),
        }
    }

    /// Check if a request should be rate limited
    fn should_limit(&self, client_ip: IpAddr) -> bool {
        let now = Instant::now();
        let window_duration = Duration::from_secs(self.window_secs);

        let mut should_limit = false;

        self.limits
            .entry(client_ip)
            .and_modify(|entry| {
                // Check if we need to reset the window
                if now.duration_since(entry.window_start) >= window_duration {
                    entry.count = 1;
                    entry.window_start = now;
                } else {
                    entry.count += 1;
                    if entry.count > self.max_queries {
                        should_limit = true;
                    }
                }
            })
            .or_insert(RateLimitEntry {
                count: 1,
                window_start: now,
            });

        should_limit
    }

    /// Clean up old entries
    pub fn cleanup(&self) {
        let now = Instant::now();
        let window_duration = Duration::from_secs(self.window_secs);

        self.limits
            .retain(|_, entry| now.duration_since(entry.window_start) < window_duration * 2);
    }

    /// Spawn a background cleanup task that periodically removes expired entries
    ///
    /// This prevents the DashMap from growing unboundedly when there are many
    /// unique client IPs. The cleanup runs every `window_secs * 2` seconds.
    ///
    /// # Returns
    ///
    /// A JoinHandle that can be used to abort the cleanup task if needed.
    pub fn spawn_cleanup_task(self: Arc<Self>) -> tokio::task::JoinHandle<()> {
        let cleanup_interval = Duration::from_secs(self.window_secs.max(30) * 2);
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(cleanup_interval);
            loop {
                interval.tick().await;
                let before = self.limits.len();
                self.cleanup();
                let after = self.limits.len();
                if before > after {
                    debug!(
                        "RateLimitPlugin cleanup: removed {} entries ({} -> {})",
                        before - after,
                        before,
                        after
                    );
                }
            }
        })
    }
}

impl BackgroundTask for RateLimitPlugin {
    fn background_task_interval(&self) -> Duration {
        Duration::from_secs(self.window_secs.max(30) * 2)
    }

    fn run_background_task(&self) {
        let before = self.limits.len();
        self.cleanup();
        let after = self.limits.len();
        if before > after {
            debug!(
                "RateLimitPlugin cleanup: removed {} entries ({} -> {})",
                before - after,
                before,
                after
            );
        }
    }

    fn background_task_name(&self) -> &str {
        "ratelimit_cleanup"
    }
}

#[async_trait]
impl Plugin for RateLimitPlugin {
    async fn execute(&self, ctx: &mut Context) -> Result<()> {
        // Try to get client IP from metadata (set by server)
        let client_ip: IpAddr = match ctx.get_metadata::<IpAddr>("client_ip") {
            Some(ip) => *ip,
            None => {
                // Skip rate limiting if no client IP available
                return Ok(());
            }
        };

        if self.should_limit(client_ip) {
            warn!("Rate limit exceeded for IP: {}", client_ip);

            #[cfg(feature = "web")]
            // Log security event
            crate::plugins::AUDIT_LOGGER
                .log_security_event(
                    crate::plugins::SecurityEventType::RateLimitExceeded,
                    format!("Rate limit exceeded for client {}", client_ip),
                    Some(client_ip),
                    None,
                )
                .await;

            // Create a REFUSED response. Echo the request's question section and
            // id so the client gets a well-formed reply, and set RETURN_FLAG so
            // downstream plugins (ttl/forward/...) cannot overwrite the refusal.
            let mut response = crate::dns::Message::new();
            response.set_id(ctx.request().id());
            response.set_response(true);
            response.set_response_code(ResponseCode::Refused);
            let request_questions = ctx.request().questions().to_vec();
            *response.questions_mut() = request_questions;

            ctx.set_response(Some(response));
            ctx.set_metadata(RETURN_FLAG, true);
        }

        Ok(())
    }

    fn name(&self) -> &str {
        "rate_limit"
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn spawn_background_task(&self) -> Option<tokio::task::JoinHandle<()>> {
        Some(Arc::new(self.clone()).spawn_background_task())
    }

    fn init(config: &PluginConfig) -> Result<Arc<dyn Plugin>> {
        use serde_yaml::Value;
        use std::sync::Arc;

        // defaults
        let mut max_queries: u32 = 100;
        let mut window_secs: u64 = 60;

        let args = config.effective_args();
        if let Some(v) = args.get("max_queries") {
            match v {
                Value::Number(n) => {
                    if let Some(u) = n.as_u64() {
                        max_queries = u as u32;
                    } else if let Some(i) = n.as_i64() {
                        max_queries = i as u32;
                    } else {
                        return Err(crate::Error::Config(
                            "Invalid 'max_queries' value".to_string(),
                        ));
                    }
                }
                Value::String(s) => {
                    max_queries = s.parse::<u32>().map_err(|_| {
                        crate::Error::Config("Invalid 'max_queries' string value".to_string())
                    })?;
                }
                _ => {
                    return Err(crate::Error::Config(
                        "Invalid 'max_queries' type".to_string(),
                    ));
                }
            }
        }

        if let Some(v) = args.get("window_secs") {
            match v {
                Value::Number(n) => {
                    if let Some(u) = n.as_u64() {
                        window_secs = u;
                    } else if let Some(i) = n.as_i64() {
                        window_secs = i as u64;
                    } else {
                        return Err(crate::Error::Config(
                            "Invalid 'window_secs' value".to_string(),
                        ));
                    }
                }
                Value::String(s) => {
                    window_secs = s.parse::<u64>().map_err(|_| {
                        crate::Error::Config("Invalid 'window_secs' string value".to_string())
                    })?;
                }
                _ => {
                    return Err(crate::Error::Config(
                        "Invalid 'window_secs' type".to_string(),
                    ));
                }
            }
        }

        Ok(Arc::new(RateLimitPlugin::new(max_queries, window_secs)))
    }
}

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

    #[test]
    fn test_rate_limit_creation() {
        let limiter = RateLimitPlugin::new(10, 60);
        assert_eq!(limiter.max_queries, 10);
        assert_eq!(limiter.window_secs, 60);
    }

    #[test]
    fn test_rate_limit_allows_within_limit() {
        let limiter = RateLimitPlugin::new(5, 60);
        let ip: IpAddr = "192.168.1.1".parse().unwrap();

        for _ in 0..5 {
            assert!(!limiter.should_limit(ip));
        }
    }

    #[test]
    fn test_rate_limit_blocks_over_limit() {
        let limiter = RateLimitPlugin::new(3, 60);
        let ip: IpAddr = "192.168.1.2".parse().unwrap();

        // First 3 should pass
        for _ in 0..3 {
            assert!(!limiter.should_limit(ip));
        }

        // 4th should be blocked
        assert!(limiter.should_limit(ip));
    }

    #[test]
    fn test_rate_limit_different_ips() {
        let limiter = RateLimitPlugin::new(2, 60);
        let ip1: IpAddr = "192.168.1.1".parse().unwrap();
        let ip2: IpAddr = "192.168.1.2".parse().unwrap();

        // Each IP should have independent limits
        assert!(!limiter.should_limit(ip1));
        assert!(!limiter.should_limit(ip2));
        assert!(!limiter.should_limit(ip1));
        assert!(!limiter.should_limit(ip2));

        // Both should now be at limit
        assert!(limiter.should_limit(ip1));
        assert!(limiter.should_limit(ip2));
    }

    #[test]
    fn test_cleanup() {
        let limiter = RateLimitPlugin::new(10, 1);
        let ip: IpAddr = "192.168.1.3".parse().unwrap();

        limiter.should_limit(ip);
        assert_eq!(limiter.limits.len(), 1);

        limiter.cleanup();
        assert_eq!(limiter.limits.len(), 1); // Still there

        std::thread::sleep(std::time::Duration::from_secs(3));
        limiter.cleanup();
        assert_eq!(limiter.limits.len(), 0); // Cleaned up
    }

    #[tokio::test]
    async fn test_rate_limit_plugin_execute() {
        let limiter = RateLimitPlugin::new(2, 60);
        let mut ctx = Context::new(Message::new());
        ctx.set_metadata("client_ip", "192.168.1.1".parse::<IpAddr>().unwrap());

        // First two should pass
        limiter.execute(&mut ctx).await.unwrap();
        assert!(ctx.response().is_none());

        limiter.execute(&mut ctx).await.unwrap();
        assert!(ctx.response().is_none());

        // Third should be rate limited
        limiter.execute(&mut ctx).await.unwrap();
        assert!(ctx.response().is_some());
        assert_eq!(
            ctx.response().unwrap().response_code(),
            ResponseCode::Refused
        );
    }
}

#[cfg(test)]
mod builder_init_tests {
    use super::*;
    use serde_yaml::Mapping;
    use serde_yaml::Value;

    #[test]
    fn test_init_from_config() {
        let mut args_map = Mapping::new();
        args_map.insert(
            Value::String("max_queries".to_string()),
            Value::Number(100.into()),
        );
        args_map.insert(
            Value::String("window_secs".to_string()),
            Value::Number(60.into()),
        );

        let cfg = crate::config::types::PluginConfig {
            tag: None,
            plugin_type: "rate_limit".to_string(),
            args: Value::Mapping(args_map),
        };

        let plugin = RateLimitPlugin::init(&cfg).expect("init");
        assert_eq!(plugin.name(), "rate_limit");
        if let Some(rl) = plugin.as_ref().as_any().downcast_ref::<RateLimitPlugin>() {
            assert_eq!(rl.max_queries, 100);
            assert_eq!(rl.window_secs, 60);
        } else {
            panic!("unexpected plugin type");
        }
    }
}