Use new MaxMind env vars on Labrinth (#4573)

* Bring in modrinth-maxmind

* integrate modrinth-maxmind into labrinth

* Fix CI
This commit is contained in:
aecsocket
2025-10-18 11:38:19 -07:00
committed by GitHub
parent d1ffed564d
commit fa7d1d7942
17 changed files with 655 additions and 111 deletions

View File

@@ -1,87 +1,23 @@
use flate2::read::GzDecoder;
use maxminddb::geoip2::Country;
use std::io::{Cursor, Read};
use modrinth_maxmind::{MaxMind, geoip2};
use std::net::Ipv6Addr;
use tar::Archive;
use tokio::sync::RwLock;
use tracing::warn;
pub struct MaxMindIndexer {
pub reader: RwLock<Option<maxminddb::Reader<Vec<u8>>>>,
pub maxmind: MaxMind,
}
impl MaxMindIndexer {
pub async fn new() -> Result<Self, reqwest::Error> {
let reader = MaxMindIndexer::inner_index(false).await.ok().flatten();
Ok(MaxMindIndexer {
reader: RwLock::new(reader),
})
}
pub async fn index(&self) -> Result<(), reqwest::Error> {
let reader = MaxMindIndexer::inner_index(false).await?;
if let Some(reader) = reader {
let mut reader_new = self.reader.write().await;
*reader_new = Some(reader);
}
Ok(())
}
async fn inner_index(
should_panic: bool,
) -> Result<Option<maxminddb::Reader<Vec<u8>>>, reqwest::Error> {
let response = reqwest::get(
format!(
"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key={}&suffix=tar.gz",
dotenvy::var("MAXMIND_LICENSE_KEY").unwrap()
)
).await?.bytes().await.unwrap().to_vec();
let tarfile = GzDecoder::new(Cursor::new(response));
let mut archive = Archive::new(tarfile);
if let Ok(entries) = archive.entries() {
for mut file in entries.flatten() {
if let Ok(path) = file.header().path()
&& path.extension().and_then(|x| x.to_str()) == Some("mmdb")
{
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
let reader = maxminddb::Reader::from_source(buf).unwrap();
return Ok(Some(reader));
}
}
}
if should_panic {
panic!(
"Unable to download maxmind database- did you get a license key?"
)
} else {
warn!("Unable to download maxmind database.");
Ok(None)
pub async fn new() -> Self {
Self {
maxmind: MaxMind::new().await,
}
}
pub async fn query(&self, ip: Ipv6Addr) -> Option<String> {
let maxmind = self.reader.read().await;
if let Some(ref maxmind) = *maxmind {
maxmind
.lookup::<Country>(ip.into())
.ok()
.flatten()
.and_then(|x| {
x.country.and_then(|x| x.iso_code.map(|x| x.to_string()))
})
} else {
None
}
let reader = self.maxmind.reader.as_ref()?;
reader
.lookup::<geoip2::Country>(ip.into())
.ok()?
.and_then(|c| c.country)
.and_then(|c| c.iso_code.map(|s| s.to_string()))
}
}