The Caddy Defender plugin is a middleware for Caddy that allows you to block or manipulate requests based on the client's IP address. It is particularly useful for preventing unwanted traffic or polluting AI training data by returning garbage responses.
- IP Range Filtering: Block or manipulate requests from specific IP ranges.
- Embedded IP Ranges: Predefined IP ranges for popular AI services (e.g., OpenAI, DeepSeek, GitHub Copilot).
- Custom IP Ranges: Add your own IP ranges via Caddyfile configuration.
- Multiple Responder Backends:
- Block: Return a
403 Forbidden
response. - Custom: Return a custom message.
- Drop: Drops the connection.
- Garbage: Return garbage data to pollute AI training.
- Redirect: Return a
308 Permanent Redirect
response with a custom URL.
- Block: Return a
The easiest way to use the Caddy Defender plugin is by using the pre-built Docker image.
-
Pull the Docker Image:
docker pull ghcr.io/jasonlovesdoggo/caddy-defender:latest
-
Run the Container: Use the following command to run the container with your
Caddyfile
:docker run -d \ --name caddy \ -v /path/to/Caddyfile:/etc/caddy/Caddyfile \ -p 80:80 -p 443:443 \ ghcr.io/jasonlovesdoggo/caddy-defender:latest
Replace
/path/to/Caddyfile
with the path to yourCaddyfile
.
You can also build Caddy with the Caddy Defender plugin using xcaddy
, a tool for building custom Caddy binaries.
-
Install
xcaddy
:go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
-
Build Caddy with the Plugin: Run the following command to build Caddy with the Caddy Defender plugin:
xcaddy build --with github.com/jasonlovesdoggo/caddy-defender
This will produce a
caddy
binary in the current directory. -
Run Caddy: Use the built binary to run Caddy with your configuration:
./caddy run --config Caddyfile
The defender
directive is used to configure the Caddy Defender plugin. It has the following syntax:
defender <responder> {
message <custom message>
ranges <ip_ranges...>
url <url>
}
<responder>
: The responder backend to use. Supported values are:block
: Returns a403 Forbidden
response.custom
: Returns a custom message (requiresmessage
).drop
: Drops the connection.garbage
: Returns garbage data to pollute AI training.redirect
: Returns a308 Permanent Redirect
response (requiresurl
).ratelimit
: Marks requests for rate limiting (requires Caddy-Ratelimit to be installed as well ).
<ip_ranges...>
: An optional list of CIDR ranges or predefined range keys to match against the client's IP. Defaults toaws azurepubliccloud deepseek gcloud githubcopilot openai
.<custom message>
: A custom message to return when using thecustom
responder.<url>
: The URI that theredirect
responder would redirect to.
For examples, check out docs/examples.md
The plugin includes predefined IP ranges for popular AI services. These ranges are embedded in the binary and can be used without additional configuration.
Service | Key | IP Ranges |
---|---|---|
AWS | aws | aws.go |
AWS Region | aws-us-east-1, aws-us-west-1, aws-eu-west-1 | aws_region.go |
DeepSeek | deepseek | deepseek.go |
GitHub Copilot | githubcopilot | github.go |
Google Cloud Platform | gcloud | gcloud.go |
Microsoft Azure | azurepubliccloud | azure.go |
OpenAI | openai | openai.go |
Private | private | private.go |
More are welcome! for a precompiled list, see the embedded results
We welcome contributions! To get started, see CONTRIBUTING.md.
This project is licensed under the MIT License. See the LICENSE file for details.
- The inspiration for this project.
- bart - Karl Gaissmaier's efficient routing table implementation (Balanced ART adaptation) enabling our high-performance IP matching
- Built with ❤️ using Caddy.