Skip to content

Commit e6611f3

Browse files
feat: geolocation headers
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent bc9bbec commit e6611f3

8 files changed

Lines changed: 272 additions & 62 deletions

File tree

‎.cargo/config.toml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
dev = "run -- dev"
55

66
[env]
7-
DUCKDB_STATIC="1"
8-
DUCKDB_DISABLE_EXTENSION_LOAD="1"
9-
DUCKDB_DISABLE_JEMALLOC="1"
7+
DUCKDB_STATIC = "1"
8+
DUCKDB_DISABLE_EXTENSION_LOAD = "1"
9+
DUCKDB_DISABLE_JEMALLOC = "1"
1010

1111
[target.x86_64-unknown-linux-musl]
12-
rustflags=["-C", "target_cpu=x86-64-v3"]
12+
rustflags = ["-C", "target_cpu=x86-64-v3"]

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ Since this is not a library, this changelog focuses on the changes that are rele
2020
2121
## [Unreleased]
2222

23+
### Features
24+
25+
- Added provider presets for client IP headers and support for reading GeoIP data from provider or custom headers
26+
2327
### Bug Fixes
2428

2529
- Tracker requests now avoid CORS preflight requests by sending JSON as `text/plain` and the event API accepts tracker JSON regardless of content type
2630

31+
### Other
32+
33+
- Renamed `trusted_headers` to `client_ip_headers` (the old name remains supported)
34+
2735
## [v1.6.0] - 2026-06-20
2836

2937
### Features

‎audit.toml‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[advisories]
2-
ignore=[]
3-
informational_warnings=["unsound"]
4-
severity_threshold="low"
2+
ignore = []
3+
informational_warnings = ["unsound"]
4+
severity_threshold = "low"
55

66
[target]
7-
arch=["x86_64", "aarch64"]
8-
os=["linux", "windows", "apple"]
7+
arch = ["x86_64", "aarch64"]
8+
os = ["linux", "windows", "apple"]
99

1010
[yanked]
11-
enabled=true
12-
update_index=true
11+
enabled = true
12+
update_index = true

‎data/config.example.toml‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
base_url="http://localhost:9042" # The base URL of the Liwan instance
2-
listen=9042 # The port to listen on (http)
1+
base_url = "http://localhost:9042" # The base URL of the Liwan instance
2+
listen = 9042 # The port to listen on (http)
33

44
# Disable favicon fetching from the DuckDuckGo API.
55
# disable_favicons=false
@@ -11,9 +11,10 @@ listen=9042 # The port to listen on (http)
1111

1212
# Trusted client IP headers (used in order).
1313
# Defaults to all supported headers.
14-
# trusted_headers=["cf-connecting-ip", "fly-client-ip", "true-client-ip", "x-real-ip", "cloudfront-viewer-address", "x-forwarded-for", "forwarded"]
15-
# You can also include custom headers. Names are case-insensitive.
16-
# trusted_headers=["X-Forwarded-For", "X-Client-IP"]
14+
# Presets: cloudflare, fastly, fly, cloudfront, akamai.
15+
# client_ip_headers="cloudflare"
16+
# Provider presets can be combined with standard or custom headers.
17+
# client_ip_headers=["cloudflare", "fly", "X-Forwarded-For", "X-Client-IP"]
1718

1819
# Trusted reverse proxies (IP or CIDR).
1920
# Empty by default, meaning all proxies are trusted.
@@ -29,6 +30,12 @@ listen=9042 # The port to listen on (http)
2930

3031
# GeoIp settings (Optional)
3132
[geoip]
33+
# Read location data supplied by a trusted proxy.
34+
# Presets: cloudflare, cloudfront, vercel.
35+
# headers="cloudflare"
36+
# Multiple presets and custom mappings are also supported, in priority order.
37+
# headers=["cloudflare", { country="X-Country", city="X-City" }]
38+
3239
# # MaxMind account ID and license key
3340
# # Required to automatically download the database and keep it up to date
3441
# maxmind_account_id="MY_ACCOUNT_ID"

‎src/config.rs‎

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::utils::ip_headers::{TrustedHeader, TrustedProxy, deserialize_trusted_headers, deserialize_trusted_proxies};
1+
use crate::utils::ip_headers::{
2+
GeoIpHeaders, TrustedHeader, TrustedProxy, deserialize_client_ip_headers, deserialize_geoip_headers,
3+
deserialize_trusted_proxies,
4+
};
25
use anyhow::{Context, Result, bail};
36
use config::{File, FileFormat, Value};
47
use serde::{Deserialize, Serialize};
@@ -31,8 +34,14 @@ pub struct Config {
3134
#[serde(default)]
3235
pub duckdb: DuckdbConfig,
3336

34-
#[serde(default = "default_trusted_headers", deserialize_with = "deserialize_trusted_headers")]
35-
pub trusted_headers: Vec<TrustedHeader>,
37+
/// Client IP header names or provider presets.
38+
/// Presets: `cloudflare`, `fastly`, `fly`, `cloudfront`, and `akamai`.
39+
#[serde(
40+
default = "default_client_ip_headers",
41+
alias = "trusted_headers",
42+
deserialize_with = "deserialize_client_ip_headers"
43+
)]
44+
pub client_ip_headers: Vec<TrustedHeader>,
3645

3746
#[serde(default, deserialize_with = "deserialize_trusted_proxies")]
3847
pub trusted_proxies: Vec<TrustedProxy>,
@@ -54,7 +63,7 @@ impl Default for Config {
5463
disable_favicons: false,
5564
listen: None,
5665
port: None,
57-
trusted_headers: default_trusted_headers(),
66+
client_ip_headers: default_client_ip_headers(),
5867
trusted_proxies: Vec::new(),
5968
use_forward_headers: default_use_forward_headers(),
6069
visitor_group_rotation_hour: default_visitor_group_rotation_hour(),
@@ -64,6 +73,9 @@ impl Default for Config {
6473

6574
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
6675
pub struct GeoIpConfig {
76+
/// GeoIP header mappings or provider presets. Presets: `cloudflare`, `cloudfront`, and `vercel`.
77+
#[serde(default, deserialize_with = "deserialize_geoip_headers")]
78+
pub headers: Vec<GeoIpHeaders>,
6779
#[serde(default)]
6880
pub maxmind_db_path: Option<String>,
6981
#[serde(default, deserialize_with = "deserialize_string_from_number")]
@@ -108,7 +120,7 @@ fn default_data_dir() -> String {
108120
}
109121
}
110122

111-
fn default_trusted_headers() -> Vec<TrustedHeader> {
123+
fn default_client_ip_headers() -> Vec<TrustedHeader> {
112124
TrustedHeader::all().to_vec()
113125
}
114126

@@ -221,6 +233,9 @@ impl Config {
221233

222234
fn map_env_key(key: &str) -> Option<String> {
223235
let key = key.strip_prefix("LIWAN_")?.to_ascii_lowercase();
236+
if key == "geoip_headers" {
237+
return Some("geoip.headers".to_string());
238+
}
224239
const NESTED_PREFIXES: &[(&str, &str)] = &[("maxmind_", "geoip.maxmind_"), ("duckdb_", "duckdb.")];
225240

226241
for (prefix, mapped_prefix) in NESTED_PREFIXES {
@@ -346,7 +361,7 @@ mod test {
346361
assert_eq!(config.data_dir, "/data");
347362
assert_eq!(config.base_url, "https://example.com");
348363
assert_eq!(config.geoip.maxmind_account_id, Some("123".to_string()));
349-
assert_eq!(config.trusted_headers, vec![TrustedHeader::XForwardedFor, TrustedHeader::Forwarded]);
364+
assert_eq!(config.client_ip_headers, vec![TrustedHeader::XForwardedFor, TrustedHeader::Forwarded]);
350365
assert_eq!(
351366
config.trusted_proxies,
352367
vec![TrustedProxy::Ip("127.0.0.1".parse().unwrap()), TrustedProxy::Cidr("10.0.0.0/8".parse().unwrap())]
@@ -357,7 +372,48 @@ mod test {
357372
#[test]
358373
fn test_env_custom_trusted_header() {
359374
let config = Config::load(None, vec![("LIWAN_TRUSTED_HEADERS", "X_CLIENT_IP")]).expect("failed to load config");
360-
assert_eq!(config.trusted_headers, vec![TrustedHeader::Other("x-client-ip".to_string())]);
375+
assert_eq!(config.client_ip_headers, vec![TrustedHeader::Other("x-client-ip".to_string())]);
376+
}
377+
378+
#[test]
379+
fn test_header_presets_and_geoip_mappings() {
380+
let (_temp_dir, config_path) = temp_config(
381+
"headers.config.toml",
382+
r#"
383+
client_ip_headers = ["cloudflare", "fastly", "fly", "akamai", "X-Client-IP"]
384+
385+
[geoip]
386+
headers = ["cloudflare", { country = "X-Country", city = "X-City" }]
387+
"#,
388+
);
389+
390+
let config = Config::load(Some(config_path), Vec::<(String, String)>::new()).expect("failed to load config");
391+
assert_eq!(
392+
config.client_ip_headers,
393+
vec![
394+
TrustedHeader::CfConnectingIp,
395+
TrustedHeader::FastlyClientIp,
396+
TrustedHeader::FlyClientIp,
397+
TrustedHeader::TrueClientIp,
398+
TrustedHeader::Other("x-client-ip".to_string())
399+
]
400+
);
401+
assert_eq!(config.geoip.headers.len(), 2);
402+
assert_eq!(config.geoip.headers[0].country(), Some("cf-ipcountry"));
403+
assert_eq!(config.geoip.headers[0].city(), Some("cf-ipcity"));
404+
assert_eq!(config.geoip.headers[1].country(), Some("X-Country"));
405+
assert_eq!(config.geoip.headers[1].city(), Some("X-City"));
406+
}
407+
408+
#[test]
409+
fn test_legacy_trusted_headers_alias() {
410+
let (_temp_dir, config_path) = temp_config("legacy.config.toml", "trusted_headers = \"fly\"");
411+
let config = Config::load(Some(config_path), Vec::<(String, String)>::new()).expect("failed to load config");
412+
413+
assert_eq!(config.client_ip_headers, vec![TrustedHeader::FlyClientIp]);
414+
let serialized = serde_json::to_value(config).expect("failed to serialize config");
415+
assert!(serialized.get("client_ip_headers").is_some());
416+
assert!(serialized.get("trusted_headers").is_none());
361417
}
362418

363419
#[test]

0 commit comments

Comments
 (0)