lazydns 0.2.63

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
//! GeoIP plugin for geographic IP address matching
//!
//! This plugin provides geographic location matching for IP addresses.
//! It supports a simple text-based database format for easy testing and
//! can be extended to support MaxMind GeoIP2 databases.
//!
//! # Features
//!
//! - **Country code matching**: Match IPs by country code
//! - **Text database**: Simple text-based IP to country mapping
//! - **CIDR support**: Works with CIDR ranges
//! - **Metadata tagging**: Sets geographic metadata in context
//!
//! # Example
//!
//! ```rust
//! use lazydns::plugins::GeoIpPlugin;
//! use lazydns::plugin::Plugin;
//! use std::sync::Arc;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let mut geoip = GeoIpPlugin::new("country_code");
//! // Add IP range for China
//! geoip.add_country_cidr("CN", "1.0.1.0/24".parse()?)?;
//! geoip.add_country_cidr("CN", "1.0.2.0/24".parse()?)?;
//!
//! let plugin: Arc<dyn Plugin> = Arc::new(geoip);
//! # Ok(())
//! # }
//! ```

use crate::RegisterPlugin;
use crate::config::types::PluginConfig;
use crate::dns::RData;
use crate::error::Error;
use crate::plugin::{Context, Plugin};
use async_trait::async_trait;
use ipnet::IpNet;
use std::collections::HashMap;
use std::fmt;
use std::net::IpAddr;
use std::path::PathBuf;
use std::sync::Arc;
use tracing::debug;

/// GeoIP plugin for geographic IP matching
///
/// Matches IP addresses to countries and sets metadata for routing decisions.
#[derive(Clone, RegisterPlugin)]
pub struct GeoIpPlugin {
    /// Metadata key to store country code
    metadata_key: String,
    /// Country code to CIDR ranges mapping
    country_ranges: HashMap<String, Vec<IpNet>>,
}

impl GeoIpPlugin {
    /// Create a new GeoIP plugin
    ///
    /// # Arguments
    ///
    /// * `metadata_key` - Key to use for storing country code in metadata
    ///
    /// # Example
    ///
    /// ```rust
    /// use lazydns::plugins::GeoIpPlugin;
    ///
    /// let geoip = GeoIpPlugin::new("country");
    /// ```
    pub fn new(metadata_key: impl Into<String>) -> Self {
        Self {
            metadata_key: metadata_key.into(),
            country_ranges: HashMap::new(),
        }
    }

    /// Add a CIDR range for a country
    ///
    /// # Arguments
    ///
    /// * `country_code` - ISO 3166-1 alpha-2 country code (e.g., "US", "CN")
    /// * `cidr` - CIDR range for the country
    ///
    /// # Example
    ///
    /// ```rust
    /// use lazydns::plugins::GeoIpPlugin;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut geoip = GeoIpPlugin::new("country");
    /// geoip.add_country_cidr("US", "8.8.8.0/24".parse()?)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn add_country_cidr(&mut self, country_code: &str, cidr: IpNet) -> Result<(), Error> {
        let country = country_code.to_uppercase();
        self.country_ranges.entry(country).or_default().push(cidr);
        Ok(())
    }

    /// Load GeoIP data from a text file
    ///
    /// Format: `<cidr> <country_code>`
    ///
    /// Lines starting with `#` are comments.
    ///
    /// # Example
    ///
    /// ```rust
    /// use lazydns::plugins::GeoIpPlugin;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let mut geoip = GeoIpPlugin::new("country");
    /// let data = r#"
    /// # US ranges
    /// 8.8.8.0/24 US
    /// 8.8.4.0/24 US
    /// # China ranges
    /// 1.0.1.0/24 CN
    /// "#;
    /// geoip.load_from_string(data)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn load_from_string(&mut self, content: &str) -> Result<(), Error> {
        for line in content.lines() {
            let line = line.trim();

            // Skip empty lines and comments
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            // Parse line: <cidr> <country_code>
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() < 2 {
                continue; // Skip malformed lines
            }

            let cidr: IpNet = parts[0]
                .parse()
                .map_err(|e| Error::Config(format!("Invalid CIDR '{}': {}", parts[0], e)))?;

            let country_code = parts[1];
            self.add_country_cidr(country_code, cidr)?;
        }

        Ok(())
    }

    /// Lookup country code for an IP address
    ///
    /// # Returns
    ///
    /// Country code if found, None otherwise
    pub fn lookup(&self, ip: &IpAddr) -> Option<String> {
        for (country, ranges) in &self.country_ranges {
            for range in ranges {
                if range.contains(ip) {
                    return Some(country.clone());
                }
            }
        }
        None
    }

    /// Get the number of countries in the database
    pub fn country_count(&self) -> usize {
        self.country_ranges.len()
    }

    /// Get the total number of CIDR ranges
    pub fn range_count(&self) -> usize {
        self.country_ranges.values().map(|v| v.len()).sum()
    }

    /// Check if the database is empty
    pub fn is_empty(&self) -> bool {
        self.country_ranges.is_empty()
    }

    /// Clear all GeoIP data
    pub fn clear(&mut self) {
        self.country_ranges.clear();
    }

    /// Extract IP address from DNS RData
    fn extract_ip(rdata: &RData) -> Option<IpAddr> {
        match rdata {
            RData::A(ipv4) => Some(IpAddr::V4(*ipv4)),
            RData::AAAA(ipv6) => Some(IpAddr::V6(*ipv6)),
            _ => None,
        }
    }
}

impl fmt::Debug for GeoIpPlugin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("GeoIpPlugin")
            .field("metadata_key", &self.metadata_key)
            .field("countries", &self.country_count())
            .field("ranges", &self.range_count())
            .finish()
    }
}

#[async_trait]
impl Plugin for GeoIpPlugin {
    async fn execute(&self, context: &mut Context) -> Result<(), Error> {
        // Only process if we have a response
        let response = match context.response() {
            Some(r) => r,
            None => return Ok(()),
        };

        // Check answer section for IP addresses
        for record in response.answers() {
            if let Some(ip) = Self::extract_ip(record.rdata())
                && let Some(country) = self.lookup(&ip)
            {
                // Set country code in metadata
                context.set_metadata(self.metadata_key.clone(), country.clone());

                debug!("GeoIP: IP {} belongs to country {}", ip, country);

                // Return after first match
                return Ok(());
            }
        }

        Ok(())
    }

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

    fn priority(&self) -> i32 {
        // Run after response is set but before routing decisions
        -20
    }

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

        let args = config.effective_args();

        let metadata_key = match args.get("metadata_key") {
            Some(Value::String(s)) => s.clone(),
            _ => "country".to_string(),
        };

        let mut geoip = GeoIpPlugin::new(metadata_key);

        // Load from files
        if let Some(Value::Sequence(seq)) = args.get("files") {
            for file_val in seq {
                if let Some(file_str) = file_val.as_str() {
                    let file = PathBuf::from(file_str);
                    let content = std::fs::read_to_string(&file).map_err(|e| {
                        Error::Config(format!(
                            "Failed to read GeoIP file '{}': {}",
                            file.display(),
                            e
                        ))
                    })?;
                    geoip.load_from_string(&content)?;
                }
            }
        }

        // Load inline data
        if let Some(Value::Sequence(seq)) = args.get("data") {
            for entry_val in seq {
                if let Some(entry) = entry_val.as_str() {
                    geoip.load_from_string(entry)?;
                }
            }
        }

        Ok(Arc::new(geoip))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dns::{Message, RecordClass, RecordType, ResourceRecord};
    use std::net::{Ipv4Addr, Ipv6Addr};

    #[test]
    fn test_geoip_plugin_creation() {
        let geoip = GeoIpPlugin::new("country");
        assert!(geoip.is_empty());
        assert_eq!(geoip.country_count(), 0);
        assert_eq!(geoip.range_count(), 0);
    }

    #[test]
    fn test_add_country_cidr() {
        let mut geoip = GeoIpPlugin::new("country");
        geoip
            .add_country_cidr("US", "8.8.8.0/24".parse().unwrap())
            .unwrap();

        assert_eq!(geoip.country_count(), 1);
        assert_eq!(geoip.range_count(), 1);
    }

    #[test]
    fn test_lookup() {
        let mut geoip = GeoIpPlugin::new("country");
        geoip
            .add_country_cidr("US", "8.8.8.0/24".parse().unwrap())
            .unwrap();
        geoip
            .add_country_cidr("CN", "1.0.1.0/24".parse().unwrap())
            .unwrap();

        // Test US IP
        let ip_us = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8));
        assert_eq!(geoip.lookup(&ip_us), Some("US".to_string()));

        // Test China IP
        let ip_cn = IpAddr::V4(Ipv4Addr::new(1, 0, 1, 1));
        assert_eq!(geoip.lookup(&ip_cn), Some("CN".to_string()));

        // Test unknown IP
        let ip_unknown = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
        assert_eq!(geoip.lookup(&ip_unknown), None);
    }

    #[test]
    fn test_load_from_string() {
        let mut geoip = GeoIpPlugin::new("country");
        let data = r#"
# US ranges
8.8.8.0/24 US
8.8.4.0/24 US
# China ranges
1.0.1.0/24 CN
1.0.2.0/24 CN
        "#;

        geoip.load_from_string(data).unwrap();

        assert_eq!(geoip.country_count(), 2);
        assert_eq!(geoip.range_count(), 4);

        // Verify lookups
        assert_eq!(
            geoip.lookup(&IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))),
            Some("US".to_string())
        );
        assert_eq!(
            geoip.lookup(&IpAddr::V4(Ipv4Addr::new(1, 0, 1, 1))),
            Some("CN".to_string())
        );
    }

    #[test]
    fn test_ipv6_support() {
        let mut geoip = GeoIpPlugin::new("country");
        geoip
            .add_country_cidr("US", "2001:4860::/32".parse().unwrap())
            .unwrap();

        let ip = IpAddr::V6(Ipv6Addr::new(0x2001, 0x4860, 0, 0, 0, 0, 0, 1));
        assert_eq!(geoip.lookup(&ip), Some("US".to_string()));
    }

    #[test]
    fn test_clear() {
        let mut geoip = GeoIpPlugin::new("country");
        geoip
            .add_country_cidr("US", "8.8.8.0/24".parse().unwrap())
            .unwrap();

        assert!(!geoip.is_empty());

        geoip.clear();

        assert!(geoip.is_empty());
        assert_eq!(geoip.country_count(), 0);
    }

    #[tokio::test]
    async fn test_geoip_plugin_execution() {
        let mut geoip = GeoIpPlugin::new("country");
        geoip
            .add_country_cidr("US", "8.8.8.0/24".parse().unwrap())
            .unwrap();

        // Create response with US IP
        let mut response = Message::new();
        response.set_response(true);
        response.add_answer(ResourceRecord::new(
            "example.com".to_string(),
            RecordType::A,
            RecordClass::IN,
            300,
            RData::A(Ipv4Addr::new(8, 8, 8, 8)),
        ));

        let mut context = Context::new(Message::new());
        context.set_response(Some(response));

        geoip.execute(&mut context).await.unwrap();

        // Check that country code was set
        let country: Option<&String> = context.get_metadata("country");
        assert_eq!(country, Some(&"US".to_string()));
    }

    #[tokio::test]
    async fn test_geoip_plugin_no_response() {
        let mut geoip = GeoIpPlugin::new("country");
        geoip
            .add_country_cidr("US", "8.8.8.0/24".parse().unwrap())
            .unwrap();

        let mut context = Context::new(Message::new());
        // No response set

        geoip.execute(&mut context).await.unwrap();

        // Should not set metadata
        let country: Option<&String> = context.get_metadata("country");
        assert_eq!(country, None);
    }
}