traefik_plugin_fail2ban

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Fail2Ban middleware plugin for traefik reverse proxy

Continuous Integration Status

This plugin is a small but growing implementation of a fail2ban instance as a middleware plugin for traefik. It is inspired by other implementations similar in the goal but is tailored to our needs.

Inspirations taken from:

Installation

Installation instructions are provided via the traefik Plugin Catalog.

CAUTION: Breaking Changes
Version 0.2.0
  • traefik v2.10+ is required due to now having a vendored dependency which results in go routine panics in previous traefik versions.

Configuration

All configuration options may be specified either in config files or as CLI parameters.

Always allowing or blocking certain IPs(/IP-ranges)

There can be configured certain ip addresses or ranges which are either always allowed or always denied access. Blocking always takes precedence before allowing access and allowing access takes precedence before executing other fail2ban rules.

testData:
    alwaysAllowed:
        ip: "::1,127.0.0.1"
    alwaysDenied:
        ip: "192.168.0.0/24"
Restricting logging messages

In order to help managing the use of this plugin the level of logged messages can be adjusted.

testData:
    logLevel: "INFO"
Fail2Ban rules

The ultimate goal is to support any rule matcher fail2ban supports themselves but implementation follows the direct needs of our projects.

Currently the implemented settings consist of:

testData:
    rules:
        banTime: "3h"
        findTime: "10m"
        maxRetries: 4
        response:
            statusCodes: "400,401,403-499"
            errorCode: "403"

Processing requests

Prior to executing the defined rules if the Remote IP is in the alwaysDenied-list the request will be immediately denied. This applies for the alwaysAllowed-list accordingly.

In the first request from an unknown IP address they are added to the pool starting the findTime timer:

In every subsequent request (while the findTime is not exceeded) the IP address counter in the pool is incremented and the rules are checked.

How to develop in this project

  • First clean install vendor dependencies: make clean vendor

Documentation

Overview

Package traefik_plugin_fail2ban plugin for traefik reverse proxy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error)

New creates a Fail2Ban plugin instance.

func NewLogger added in v0.2.0

func NewLogger(logLevel string) *logf.Logger

NewLogger creates new instance of logger.

Types

type Cache

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

Cache implementation for caching of IP entries.

func NewCache

func NewCache() *Cache

NewCache creates new instance of caching.

func (*Cache) CleanEntries

func (c *Cache) CleanEntries(findTime, banTime time.Duration) (removedEntries int)

CleanEntries garbage collect entries.

func (*Cache) CleanEntryIfPossible

func (c *Cache) CleanEntryIfPossible(key string, findTime, banTime time.Duration, now time.Time)

CleanEntryIfPossible removes entry from cache by key and limits.

func (*Cache) CreateEntry

func (c *Cache) CreateEntry(key string, firstSeen time.Time) *CacheEntry

CreateEntry retrieve entry from cache or create new entry with firstSeen.

func (*Cache) GetEntry

func (c *Cache) GetEntry(key string) *CacheEntry

GetEntry retrieve entry from cache.

type CacheEntry

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

CacheEntry represents single entry/IP in cache.

func (*CacheEntry) GetFirstSeen

func (ce *CacheEntry) GetFirstSeen() time.Time

GetFirstSeen gets firstSeen.

func (*CacheEntry) GetLastSeen

func (ce *CacheEntry) GetLastSeen() time.Time

GetLastSeen gets lastSeen.

func (*CacheEntry) GetTimesSeen

func (ce *CacheEntry) GetTimesSeen() uint32

GetTimesSeen gets timesSeen.

func (*CacheEntry) IncrementTimesSeen

func (ce *CacheEntry) IncrementTimesSeen()

IncrementTimesSeen increments timesSeen.

func (*CacheEntry) IsBanned

func (ce *CacheEntry) IsBanned() bool

IsBanned checks if entry is banned.

func (*CacheEntry) IssueBan

func (ce *CacheEntry) IssueBan()

IssueBan issues a ban for the entry/ip.

func (*CacheEntry) SetFirstSeen

func (ce *CacheEntry) SetFirstSeen(t time.Time)

SetFirstSeen sets firstSeen.

func (*CacheEntry) SetLastSeen

func (ce *CacheEntry) SetLastSeen(t time.Time)

SetLastSeen sets lastSeen.

type Config

type Config struct {
	Enabled       bool         `yaml:"enabled"`
	AlwaysAllowed configIPSpec `yaml:"alwaysAllowed"`
	AlwaysDenied  configIPSpec `yaml:"alwaysDenied"`
	LogLevel      string       `yaml:"logLevel"`
	Rules         configRules  `yaml:"rules"`
}

Config plugin configuration.

func CreateConfig

func CreateConfig() *Config

CreateConfig creates the default plugin configuration.

type Fail2Ban

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

Fail2Ban plugin data structure.

func (*Fail2Ban) ServeHTTP

func (a *Fail2Ban) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request)

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriter wrapping original ResponseWriter with response check handling.

func (*ResponseWriter) Write

func (rw *ResponseWriter) Write(data []byte) (status int, err error)

Write wraps original Write whilst checking for response body.

func (*ResponseWriter) WriteHeader

func (rw *ResponseWriter) WriteHeader(code int)

WriteHeader wraps original WriteHeader whilst checking for response status code.

Jump to

Keyboard shortcuts

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