The word at the DL 5-min mark was was so GeoIP wins the vote, I need to break this into two parts because… yeah.
One man’s journey through broken links, depreciated instructions and bad web design on a quest to find himself…
Self-hosted GeoIP Lookup: Part 1, The Downloadening
Debrief:
For this to work we need two things, a beefy database and a software solution to use it. Part 1 is about getting the database, part 2 is the easy part.
It appears the only game in town is Maxmind’s geolocation databases licensed under Creative Commons CC BY-SA 4.0. The project started ~18 years ago and if you’re checking them out i’d recommend sticking to the sitemap.
They use a custom binary format for the database to maximize lookup speed so you need a special reader to use it but they provide one for all popular languages and there’s packages in all popular Linux repos that cover all popular use cases (they’re very popular). Lookup data is also available in CSV if you want to send LibreOffice Calc into the next dimension.
There’s 3 database groups:
- GeoLite legacy, which will be discontinued May 2022
- GeoLite2, which is the new hotness
- Geo2, which is more accurate but pay-2-win
We’ll be using GeoLite2 which contrary to every guide on the entire Internet is behind a login wall.
It’s go time:
https://www.maxmind.com/en/geolite2/signup
I tried guerrillamail.com for the email while using ProtonVPN. I couldn’t sign up till i’d dropped the VPN and used a “real” email.
You’ll get an email, use it to set a new password and login to the control panel.
Option 1: Create permalinks so you can wget
the databases server-side
- On the left menu under “Services” click “My License Key”
- Click “Generate new license key”
- Click the “No” radial for “Will this key be used for GeoIP Update?”
- Click “Confirm”
- Copy the license key and save it
- On the left menu under “GeoIP2 / GeoLite2” click “Download Files”
- Option A:
- Use Ulfnic’s script below to download, checksum and rename every database
- Option B.
- Click “Get Permalinks” next to each download
-
wget
each URL replacing YOUR_LICENSE_KEY with the one you saved earlier - Optional: checksum each database
- Optional: Open the checksum file to know the dated filename to use for the database file (so you know if there’s an updated version in future).
- Option A:
Option 2. Download the files locally and upload them to the server
- On the left menu under “GeoIP2 / GeoLite2” click “Download Files”
- Click “Download” on each database
- Optional: checksum each database
- Upload to server
If ls
shows you this, you’re ready for part 2:
Option 1A server-side download script:
#!/usr/bin/env sh
# This script will...
# - Ask for a Maxmind license key
# - Download every GeoLite2 database using that license key
# - Apply the correct, dated filename inside the checksum file to each download instead of a generic filename
# - Perform all checksums
echo -n "Enter your license key: "; read YOUR_LICENSE_KEY
URLS=( \
"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key="$YOUR_LICENSE_KEY"&suffix=tar.gz" \
"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN-CSV&license_key="$YOUR_LICENSE_KEY"&suffix=zip" \
"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key="$YOUR_LICENSE_KEY"&suffix=tar.gz" \
"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&license_key="$YOUR_LICENSE_KEY"&suffix=zip" \
"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key="$YOUR_LICENSE_KEY"&suffix=tar.gz" \
"https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country-CSV&license_key="$YOUR_LICENSE_KEY"&suffix=zip" \
)
for URL in "${URLS[@]}"; do
echo === FETCHING ===
echo $URL
wget -O sha256 "$URL.sha256"
FILENAME=$(cat sha256 | awk '{print $2}')
mv sha256 "$FILENAME.sha256"
wget -O "$FILENAME" "$URL"
echo === CHECKSUM ===
sha256sum -c "$FILENAME.sha256"
done