lbprotect

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 20 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.

Documentation

Overview

Package lbprotect is a project's traffic protection on its load balancers (Phase 25): Traefik rate limits (shared through a Valkey, with a fallback to per-node counters while it is down), concurrent-request and body limits, block and bypass lists, and a network guard on the project's own load-balancer nodes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSecret

func NewSecret() string

NewSecret makes a bypass secret.

func ValidFirewallGuard

func ValidFirewallGuard(g *agentv1.FirewallGuard) error

ValidFirewallGuard reports whether a guard renders (for tests).

Types

type Bypass

type Bypass struct {
	IPs    []string `json:"ips"`
	Header string   `json:"header"` // header name; its value is the secret
	// HasSecret is set when a secret is stored (it is never returned).
	HasSecret bool     `json:"hasSecret"`
	Paths     []string `json:"paths"`
}

Bypass are requests that skip the limits (never the block list).

type ClientIP

type ClientIP struct {
	// Source: connection (its address), forwarded (X-Forwarded-For at
	// Depth, behind a proxy or CDN) or header (e.g. CF-Connecting-IP).
	Source string `json:"source"`
	Depth  int    `json:"depth"`
	Header string `json:"header"`
}

ClientIP says how clients are told apart.

type ComponentState

type ComponentState struct{ ID, Name, State, Error string }

ComponentState mirrors alerts.ComponentState (this package does not depend on alerts).

type Guard

type Guard struct {
	// Rate is new connections per second and source (0 = no limit).
	Rate           int `json:"rate"`
	Burst          int `json:"burst"`
	MaxConnections int `json:"maxConnections"` // concurrent per source (0 = no cap)
	// BanMinutes blocks a source over the guard on that node (0 = no ban).
	BanMinutes int `json:"banMinutes"`
}

Guard is the network guard on the project's own load-balancer nodes.

type Manager

type Manager struct {

	// Valkey looks a Valkey up (nil: no shared counters).
	Valkey func(ctx context.Context, id string) (dbs.ValkeyTarget, error)
	// IngressNodes are a project's own load-balancer nodes (by project name).
	IngressNodes func(project string) []string
	// Assigned maps each load-balancer node to the projects it serves.
	Assigned func() map[string][]string
	// OnChange runs after a change that firewalls must follow (guards).
	OnChange func()
	// contains filtered or unexported fields
}

Manager keeps every project's protection in memory: Traefik's config asks for it every poll.

func New

func New(st *store.Store, box *secrets.Box, log *slog.Logger) *Manager

func (*Manager) AutoBan

func (m *Manager) AutoBan(node string, port uint32) (time.Duration, string, bool)

AutoBan is the ban of a source over a node's guard on 80/443.

func (*Manager) Components

func (m *Manager) Components() []ComponentState

Components report each project whose shared counters fell back, for the system component alert.

func (*Manager) Get

func (m *Manager) Get(project string) Spec

Get returns a project's protection (the default when none is saved).

func (*Manager) Guards

func (m *Manager) Guards(node string) []*agentv1.FirewallGuard

Guards are the network guards of a node's 80/443 (its projects').

func (*Manager) Load

func (m *Manager) Load(ctx context.Context) error

Load reads every project's protection.

func (*Manager) Middlewares

func (m *Manager) Middlewares() map[string]traefik.Middleware

Middlewares are every project's protection middlewares.

func (*Manager) Protection

func (m *Manager) Protection(project string) *traefik.Protection

Protection is what a project's routes carry (nil: none).

func (*Manager) Put

func (m *Manager) Put(ctx context.Context, p store.Project, s Spec, sec SecretUpdate) (Spec, error)

Put saves a project's protection.

func (*Manager) Redis

func (m *Manager) Redis(project string) RedisState

Redis returns a project's shared-counter state.

func (*Manager) Run

func (m *Manager) Run(ctx context.Context)

Run refreshes the Valkeys' health every 10 seconds, switching projects to per-node counters while theirs is down (Traefik answers 500 when it cannot reach Redis) and back once it is healthy.

type RateLimit

type RateLimit struct {
	Average int    `json:"average"`
	Burst   int    `json:"burst"`
	Period  string `json:"period"` // 1s | 1m | 1h
}

RateLimit is requests per Period and client (Average 0 = off).

type Redis

type Redis struct {
	DatabaseID string `json:"databaseId"`
}

Redis is the Valkey that keeps shared counters ("" = per node).

type RedisState

type RedisState struct {
	Name     string `json:"name,omitempty"`
	Health   string `json:"health,omitempty"`
	Active   bool   `json:"active"`   // counters are shared now
	Fallback bool   `json:"fallback"` // its Valkey is down: per-node counters
}

RedisState is a project's shared-counter state.

type SecretUpdate

type SecretUpdate struct {
	Set   string
	Clear bool
}

SecretUpdate changes the bypass secret: Keep it, Set a new one, or Clear it.

type Spec

type Spec struct {
	// Enabled off keeps the settings but applies none of them.
	Enabled   bool      `json:"enabled"`
	RateLimit RateLimit `json:"rateLimit"`
	ClientIP  ClientIP  `json:"clientIp"`
	// MaxConcurrent caps a client's requests in flight (0 = no cap).
	MaxConcurrent int `json:"maxConcurrent"`
	// MaxBodyMB caps request bodies (0 = no cap).
	MaxBodyMB int `json:"maxBodyMb"`
	// Block are client IPs/CIDRs answered with 403.
	Block  []string `json:"block"`
	Bypass Bypass   `json:"bypass"`
	// Redis shares the rate-limit counters between load balancers.
	Redis Redis `json:"redis"`
	// Guard limits connections per source on the project's own nodes.
	Guard Guard `json:"guard"`
}

Spec is a project's protection.

func Default

func Default() Spec

Default is off, with sensible values ready to switch on.

func (*Spec) Validate

func (s *Spec) Validate() error

Validate normalizes and checks a spec.

Jump to

Keyboard shortcuts

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