view src/geoip.rs @ 77:fd0237049be0

Show paths instead of tags & apply rust fixes
author Lewin Bormann <lbo@spheniscida.de>
date Mon, 31 Jul 2023 09:31:37 +0200
parents 0c5f8caf6736
children d3368a7142ef
line wrap: on
line source

use anyhow::{Error};
use log::warn;
use maxminddb::{
    geoip2::{City},
    Reader,
};

use std::collections::BTreeMap;
use std::net::IpAddr;
use std::path::Path;

pub struct GeoIP {
    r: Reader<Vec<u8>>,
}

impl GeoIP {
    pub fn new<P: AsRef<Path>>(path: P) -> Result<GeoIP, Error> {
        Ok(GeoIP {
            r: Reader::open_readfile(path)?,
        })
    }

    pub fn lookup(&self, addr: IpAddr) -> Option<(String, String)> {
        match self.r.lookup::<City>(addr) {
            Ok(c) => Some((
                c.country
                    .map(|c| c.iso_code.unwrap_or(""))
                    .unwrap_or("")
                    .to_string(),
                c.city
                    .map(|c| {
                        c.names
                            .unwrap_or_else(BTreeMap::new)
                            .get("en")
                            .unwrap_or(&"")
                            .to_string()
                    })
                    .unwrap_or(String::new()),
            )),
            Err(e) => {
                warn!("GeoIP lookup failed: {}", e);
                None
            }
        }
    }
}