Skip to content

Commit 28df26f

Browse files
committed
Simplify geoIP DB fetching
Use the automatically-updated CDN download link from https://github.com/wp-statistics/GeoLite2-Country?tab=readme-ov-file to avoid manual date shenanigans.
1 parent 4c450cc commit 28df26f

2 files changed

Lines changed: 4 additions & 47 deletions

File tree

payjoin-mailroom/config.example.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
# --- Access-control (requires `access-control` feature) ---
3131
# [access_control]
3232

33-
# Optional path to a MaxMind GeoIP2 / DB-IP Country database.
34-
# If omitted but blocked_regions is non-empty, a free DB-IP Lite
33+
# Optional path to a MaxMind GeoIP2 / GeoLite2 Country database.
34+
# If omitted but blocked_regions is non-empty, the free GeoLite2-Country
3535
# database will be fetched automatically and cached under storage_dir.
3636
# geo_db_path = "/path/to/GeoIP2-Country.mmdb"
3737

payjoin-mailroom/src/access_control.rs

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,10 @@ pub fn spawn_address_list_updater(
124124
async fn fetch_geoip_db(dest: &Path) -> anyhow::Result<()> {
125125
use std::io::Read;
126126

127-
let now = chrono_month_year();
128-
let url =
129-
format!("https://download.db-ip.com/free/dbip-country-lite-{}-{}.mmdb.gz", now.0, now.1);
127+
let url = "https://cdn.jsdelivr.net/npm/geolite2-country/GeoLite2-Country.mmdb.gz";
130128
tracing::info!("Fetching GeoIP database from {}", url);
131129

132-
let response = reqwest::get(&url).await?;
130+
let response = reqwest::get(url).await?;
133131
if !response.status().is_success() {
134132
anyhow::bail!("Failed to fetch GeoIP database: HTTP {}", response.status());
135133
}
@@ -146,31 +144,6 @@ async fn fetch_geoip_db(dest: &Path) -> anyhow::Result<()> {
146144
Ok(())
147145
}
148146

149-
/// Returns (year, month) as strings for the DB-IP download URL.
150-
fn chrono_month_year() -> (String, String) {
151-
let now = std::time::SystemTime::now()
152-
.duration_since(std::time::UNIX_EPOCH)
153-
.expect("system time should be after UNIX_EPOCH");
154-
let days_since_epoch = (now.as_secs() / 86_400) as i64;
155-
let (year, month) = year_month_from_days_since_epoch(days_since_epoch);
156-
(year.to_string(), format!("{month:02}"))
157-
}
158-
159-
fn year_month_from_days_since_epoch(days_since_epoch: i64) -> (i32, u32) {
160-
// Exact conversion from Unix days to Gregorian year/month in UTC.
161-
// Based on Howard Hinnant's civil calendar algorithm.
162-
let z = days_since_epoch + 719_468;
163-
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
164-
let doe = z - era * 146_097;
165-
let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
166-
let y = yoe + era * 400;
167-
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
168-
let mp = (5 * doy + 2) / 153;
169-
let month = (mp + if mp < 10 { 3 } else { -9 }) as u32;
170-
let year = (y + if month <= 2 { 1 } else { 0 }) as i32;
171-
(year, month)
172-
}
173-
174147
#[cfg(test)]
175148
mod tests {
176149
use super::*;
@@ -260,20 +233,4 @@ mod tests {
260233
assert!(ac.check_ip("192.0.2.1".parse().unwrap()));
261234
assert!(ac.check_ip("2001:db8::1".parse().unwrap()));
262235
}
263-
264-
#[test]
265-
fn year_month_conversion_handles_leap_day() {
266-
// 2024-02-29 00:00:00 UTC
267-
let days = 19_782;
268-
let (year, month) = year_month_from_days_since_epoch(days);
269-
assert_eq!((year, month), (2024, 2));
270-
}
271-
272-
#[test]
273-
fn year_month_conversion_handles_year_start() {
274-
// 2024-01-01 00:00:00 UTC
275-
let days = 19_723;
276-
let (year, month) = year_month_from_days_since_epoch(days);
277-
assert_eq!((year, month), (2024, 1));
278-
}
279236
}

0 commit comments

Comments
 (0)