caddy_fail2ban

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2025 License: AGPL-3.0 Imports: 12 Imported by: 0

README

caddy-fail2ban

A simple package to add fail2ban support to caddy. This simple module adds a fail2ban HTTP matcher based on a text file of IP addresses.

Getting Started

First, make sure to build your caddy with support for this module:

RUN xcaddy build \
    --with github.com/Javex/caddy-fail2ban@main

Then insert this into your Caddyfile:

@banned {
	fail2ban ./banned-ips
}
handle @banned {
	abort
}

The right place for it depends on your setup, but you can find more complete examples in the examples/ directory.

Next, you will need to create the fail2ban action. You can copy the suggested one if you like:

$ cp fail2ban/caddy-banfile.conf /etc/fail2ban/actions.d/caddy-banfile.conf

Now in any of your jails if you want to block requests at the HTTP layer, you can use the action:

action = caddy-banfile[banfile_path="/etc/caddy/banned-ips"]

The above path is the default so you can omit the banfile_path parameter if you like.

Running tests

First run the go unit tests, then spin up a docker container to test the integration with fail2ban

go build -v ./...
go test -v ./...

sudo docker build . -t caddy-fail2ban
sudo docker run --rm --name caddy-fail2ban --detach -v $PWD/test/Caddyfile:/etc/caddy/Caddyfile caddy-fail2ban
sudo docker exec -it caddy-fail2ban /usr/local/bin/caddy-fail2ban-test.sh
sudo docker logs caddy-fail2ban
sudo docker stop caddy-fail2ban

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Banlist

type Banlist struct {
	// contains filtered or unexported fields
}

func NewBanlist

func NewBanlist(ctx caddy.Context, logger *zap.Logger, banfile *string) Banlist

func (*Banlist) IsBanned

func (b *Banlist) IsBanned(remote_ip string) bool

func (*Banlist) Reload

func (b *Banlist) Reload()

func (*Banlist) Start

func (b *Banlist) Start()

type Fail2Ban

type Fail2Ban struct {
	Banfile string `json:"banfile"`
	// contains filtered or unexported fields
}

Fail2Ban implements an HTTP handler that checks a specified file for banned IPs and matches if they are found

func (Fail2Ban) CaddyModule

func (Fail2Ban) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*Fail2Ban) Match

func (m *Fail2Ban) Match(req *http.Request) bool

func (*Fail2Ban) Provision

func (m *Fail2Ban) Provision(ctx caddy.Context) error

Provision implements caddy.Provisioner.

func (*Fail2Ban) UnmarshalCaddyfile

func (m *Fail2Ban) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL