Skip to main content

Crate geoipsed

Crate geoipsed 

Source
Expand description

The geoipsed library for IP address extraction and geolocation tagging.

This library provides functionality for finding and decorating IP addresses with geolocation information from various MMDB databases.

§Examples

Creating and registering a custom MMDB provider:

use geoipsed::{MmdbProvider, ProviderRegistry, TemplateField, template::Template};
use std::path::{Path, PathBuf};
use anyhow::Result;

// Implement the MmdbProvider trait for your custom provider
#[derive(Debug)]
struct CustomProvider {
    name: String,
    initialized: bool,
}

impl MmdbProvider for CustomProvider {
    fn name(&self) -> &str {
        &self.name
    }

    fn default_path(&self) -> PathBuf {
        PathBuf::from("/path/to/mmdb")
    }

    fn required_files(&self) -> Vec<String> {
        vec!["custom.mmdb".to_string()]
    }

    fn available_fields(&self) -> Vec<TemplateField> {
        vec![
            TemplateField {
                name: "ip".to_string(),
                description: "The IP address".to_string(),
                example: "93.184.216.34".to_string(),
            },
        ]
    }

    fn initialize(&mut self, _path: &Path) -> Result<()> {
        self.initialized = true;
        Ok(())
    }

    fn lookup(
        &self,
        _ip: std::net::IpAddr,
        ip_str: &str,
        template: &Template,
    ) -> Result<String> {
        Ok(template.render(|field| match field {
            "ip" => ip_str,
            _ => "",
        }))
    }

    fn lookup_and_write(
        &self,
        wtr: &mut dyn std::io::Write,
        _ip: std::net::IpAddr,
        ip_str: &str,
        template: &Template,
    ) -> Result<()> {
        template.write(wtr, |out, field| match field {
            "ip" => out.write_all(ip_str.as_bytes()),
            _ => Ok(()),
        })?;
        Ok(())
    }

    fn has_asn(&self, _ip: std::net::IpAddr) -> bool {
        false
    }
}

// Register with a registry
let mut registry = ProviderRegistry::default();
registry.register(
    "custom".to_string(),
    Box::new(CustomProvider {
        name: "My Custom Provider".to_string(),
        initialized: false,
    })
);

Re-exports§

pub use crate::error::Error;
pub use crate::mmdb::MmdbProvider;
pub use crate::mmdb::ProviderRegistry;
pub use crate::mmdb::TemplateField;

Modules§

error
files
geoip
input
mmdb
template

Structs§

Extractor
Re-export IP extraction types from the sub-crate The main IP address extractor.
ExtractorBuilder
Re-export IP extraction types from the sub-crate A builder for configuring IP extraction behavior.
IpMatch
Re-export IP extraction types from the sub-crate A validated IP address match within a haystack.
Tag
Re-export IP extraction types from the sub-crate A tag representing an IP address found in text.
Tagged
Re-export IP extraction types from the sub-crate A line of text with tags.
TextData
Re-export IP extraction types from the sub-crate Represents the text data for JSON serialization.

Enums§

IpKind
Re-export IP extraction types from the sub-crate Whether a validated IP match is IPv4 or IPv6.