Note: The module already exists for multiple years now, but I wanted to open a topic here in addition to the Repository as well. This was my very first caddy module I built.
Disclaimer: I am not affiliated with IPInfo in any way or form.
Repository: oltdaniel/caddy-ipinfo-free
This is an easy way to use the free (or now called “lite”) IPInfo Country Database.
{
# Required as a third-party handler
order ipinfo_free first
# Configure module
ipinfo_free_config {
url https://ipinfo.io/data/ipinfo_lite.mmdb?token=magicduck
cron "10 16 * * *"
path /tmp/caddy_ipinfo
}
}
# curl localhost:8080/?ip=1.1.1.1
:8080 {
ipinfo_free "{http.request.uri.query.ip}"
header Content-Type text/plain
respond <<TEXT
IP: {ipinfo_free.ip}
Country: {ipinfo_free.country} ({ipinfo_free.country_code})
Continent: {ipinfo_free.continent} ({ipinfo_free.continent_code})
ASN: {ipinfo_free.asn} {ipinfo_free.as_name} {ipinfo_free.as_domain}
TEXT 200
}
Flexibility thanks to placeholders
Making use of the caddy placeholder functionality, allows for many use-cases. I have a few examples in my repository that crossed my mind during testing. One common use-case I see is geo-blocking.
ipinfo_free
header Content-Type text/plain
@dach expression ({ipinfo_free.country_code} in ["DE", "CH", "AT"])
respond @dach "Hallo Besucher aus der DACH-Region!"
respond "Access denied for your location" 451
Some background
Initially I just built it, because it was a rather simple use-case for my first caddy module. However, over time I received some stars here and there on the repository and I keep maintaining it to this day.
If you have any feedback in regards to the code, or have a cool use-case I could add to my README, feel free to comment.