Overview
The GeoIP filtering system blocks or allows requests based on the geographic location of the client IP address using MaxMind GeoLite2 database.Functions
loadGeoIP()
Loads the MaxMind GeoIP2 database from the specified file path.main.go:264-271
File path to GeoLite2-Country.mmdb database
Usage Example
From main.go:370-373:getCountryCode()
Returns the ISO country code for a given IP address.main.go:273-283
IP address as string (e.g.,
"192.168.1.100" or "2001:db8::1")Two-letter ISO country code (e.g.,
"US", "CN", "DE")Error if IP is invalid or lookup fails
Example
geoFilter()
Filters requests based on geographic allow/block lists.main.go:296-315
HTTP request containing RemoteAddr
Set of allowed country codes (empty map = allow all)
Set of blocked country codes
true if request should be allowed, false if blocked
Country code if allowed, or reason string if blocked
Filtering Logic
- Lookup fails: Return
false, "geo lookup failed" - Country in blocklist: Return
false, "geo blocked" - Allowlist non-empty AND country not in allowlist: Return
false, "geo not allowed" - Otherwise: Return
true, countryCode
Blocklist takes precedence over allowlist. If both are specified, blocked countries are rejected even if in the allowlist.
Usage Example
From main.go:432-439:parseCSVSet()
Parses a comma-separated string of country codes into a set.main.go:285-294
Comma-separated country codes (e.g.,
"US,GB,DE")Set of uppercase country codes
Features
- Automatically converts to uppercase
- Trims whitespace
- Skips empty values
- Returns empty map for empty input
Usage Example
From main.go:417-420:Global Variables
main.go:47-53
MaxMind GeoIP2 database reader instance
Whether GeoIP filtering is enabled
Set of allowed country codes
Set of blocked country codes
Environment Variables
See Environment Variables for configuration:GEO_BLOCK_ENABLED- Enable/disable GeoIP filteringGEO_ALLOW_COUNTRIES- Comma-separated allowlistGEO_BLOCK_COUNTRIES- Comma-separated blocklist
Complete Example
Configuration
Behavior
| IP Location | Result | Reason |
|---|---|---|
| United States (US) | Allowed | In allowlist |
| Canada (CA) | Allowed | In allowlist |
| China (CN) | Blocked | In blocklist (takes precedence) |
| Russia (RU) | Blocked | In blocklist |
| Japan (JP) | Blocked | Not in allowlist |
| Germany (DE) | Allowed | In allowlist |
Code Flow
Database Requirements
Requires MaxMind GeoLite2 or GeoIP2 Country database in
.mmdb format. Download from MaxMind./app/GeoLite2-Country.mmdb