Custom CIDR Notation
In addition to predefined IP ranges, Caddy Defender supports custom CIDR notation for precise IP blocking.Basic Syntax
You can specify custom IP ranges directly in your Caddyfile:CIDR Format
CIDR (Classless Inter-Domain Routing) notation consists of:- An IP address:
203.0.113.0 - A slash:
/ - A prefix length:
24
/32= Single IP address (e.g.,192.168.1.1/32)/24= 256 IP addresses (e.g.,192.168.1.0/24)/16= 65,536 IP addresses (e.g.,192.168.0.0/16)/8= 16,777,216 IP addresses (e.g.,10.0.0.0/8)
Validation
Custom CIDR ranges are validated during configuration loading. The validation logic is inconfig.go:228-240:
What Gets Validated
Predefined Keys
Predefined Keys
If a range matches a key in the
IPRanges map (like openai, aws, etc.), it’s used directly without validation.Custom CIDR Blocks
Custom CIDR Blocks
If not found in the predefined map, the value is parsed as CIDR notation using Go’s
net.ParseCIDR.Invalid Ranges
Invalid Ranges
Invalid CIDR notation or unknown keys will cause configuration errors.
Combining Predefined and Custom Ranges
You can mix predefined keys with custom CIDR blocks:IPv6 Support
Caddy Defender supports both IPv4 and IPv6 CIDR notation:Many predefined ranges (like
gcloud, cloudflare) already include both IPv4 and IPv6 ranges.Common CIDR Examples
Single IP
Small Network
Medium Network
Large Network
Real-World Examples
Block Specific Datacenter
Block Internal Network Access
Geographic Blocking (with external list)
Temporary Block During Attack
CIDR Tools and Resources
CIDR Calculators
CIDR Calculators
IP Range Lists
IP Range Lists
- IPdeny - Country-level IP blocks
- Team Cymru - ASN to IP mappings
- MaxMind GeoLite2 - Geographic IP data
Verification Tools
Verification Tools
Test if an IP is in a CIDR range:
Performance Considerations
Optimal Performance: Custom CIDR ranges are processed the same way as predefined ranges, so there’s no performance difference. However:
- Fewer, larger CIDR blocks are more efficient than many individual IPs
- Use
/32for single IPs sparingly - combine into larger blocks when possible - Consider creating a predefined range for frequently used custom blocks
Creating Your Own Predefined Ranges
If you have a set of custom ranges you use frequently, you can add them to the build process:-
Create a new fetcher in
ranges/fetchers/: -
Add it to
ranges/main.go:34-55: -
Rebuild the ranges:
-
Use it in your Caddyfile: