How IP Ranges Work
Caddy Defender includes a comprehensive database of IP ranges from various cloud providers, AI services, and other sources. These ranges are fetched at build time and embedded directly into the binary, ensuring fast lookups without external dependencies.IPRanges Map Structure
All IP ranges are stored in a centralizedIPRanges map located in ranges/data/generated.go. This file is automatically generated by the build process:
Map Key Reference
The map uses lowercase string keys to identify different IP range sources:Cloud Providers
aws, gcloud, azure, oci, vultr, digitalocean, linode, cloudflare, aliyunAI Services
openai, deepseek, githubcopilot, mistralSpecial Ranges
private, all, vpn, tor (optional)AWS Regions
aws-us-east-1, aws-us-west-1, and other regional variantsBuild-Time Generation
IP ranges are fetched and embedded during the build process using theranges/main.go tool:
Generation Process
- Concurrent Fetching: The tool fetches IP ranges from multiple sources simultaneously using goroutines
- Data Sources: Each fetcher connects to official APIs or endpoints (e.g., AWS IP ranges JSON, OpenAI bot endpoints)
- Map Population: Results are stored in the
IPRangesmap with lowercase keys - Code Generation: The map is serialized to Go code using text templates
- Binary Embedding: The generated file is compiled into the final Caddy binary
The generated file contains a comment:
// Code generated by pkg.jsn.cam/caddy-defender/blob/main/ranges/main.go; DO NOT EDIT.Using IP Ranges in Configuration
In your Caddyfile, you can reference IP ranges by their map key:Validation
When Caddy Defender validates your configuration (seeconfig.go:228-240):
- Check for Predefined Keys: If a range matches a key in
IPRanges, it’s used directly - Parse Custom CIDRs: If not found in the map, it’s validated as a CIDR notation
- Reject Invalid Values: Invalid CIDRs or unknown keys result in configuration errors
Performance Benefits
Zero Runtime Fetching
Zero Runtime Fetching
Since IP ranges are embedded at build time, there’s no need to fetch data from external APIs during runtime. This eliminates:
- Network latency
- External API dependencies
- Rate limiting concerns
- Potential points of failure
Fast In-Memory Lookups
Fast In-Memory Lookups
The
IPRanges map provides O(1) lookup time for predefined ranges, making IP matching extremely fast even with thousands of CIDR blocks.No External Dependencies
No External Dependencies
Your Caddy server doesn’t need internet access or external API keys to use IP range blocking - everything is self-contained.
Updating IP Ranges
To update the IP ranges in your Caddy Defender build:-
Rebuild the ranges data:
-
Rebuild Caddy with the updated ranges:
Consider automating this process in your CI/CD pipeline to keep IP ranges up-to-date.