engine

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 9 Imported by: ? 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 engine turns per-source signal counts into a ban list. It is a pure decision package: Step reads its inputs and returns what it would do, and the engine changes only when Commit is called after the caller has recorded the new bans.

Index

Constants

View Source
const (
	SourceScorer = "scorer"
	SourceManual = "manual"
)

Entry sources.

View Source
const (
	SuppressedByAllowlist = "allowlist"
	SuppressedByLift      = "lift"
)

What suppressed an entry.

View Source
const (
	DeferHourCap       = "hour_cap"
	DeferFeederCap     = "feeder_cap"
	DeferMaxEntries    = "max_entries"
	DeferCoverage      = "coverage"
	DeferGenerationCap = "generation_cap"
)

Deferral reasons.

Variables

DeferReasons lists every deferral reason.

Functions

This section is empty.

Types

type Ban

type Ban struct {
	Unit    netip.Prefix
	Issued  time.Time
	Expires time.Time
	Points  float64
	// Number is 1 for a first ban, 2 for the next within the repeat
	// window, and so on.
	Number  int
	Feeders []string
	Signals map[string]float64
	Reason  string
}

Ban is one scored ban.

type Caps

type Caps struct {
	MaxEntries             int    `json:"maxEntries"`
	MaxAddedPerGeneration  int    `json:"maxAddedPerGeneration"`
	MaxNewPerHour          int    `json:"maxNewPerHour"`
	MaxNewPerFeederPerHour int    `json:"maxNewPerFeederPerHour"`
	MaxIPv4Addresses       uint64 `json:"maxIPv4Addresses"`
	MaxIPv6Networks        uint64 `json:"maxIPv6Networks"`
}

Caps bound what one step may publish and ban.

type Config

type Config struct {
	Threshold    float64
	Window       time.Duration
	IPv6Prefix   int
	BanBase      time.Duration
	BanMax       time.Duration
	RepeatWindow time.Duration
	Caps         Caps
	Signals      []Signal
}

Config is the scoring policy.

type Engine

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

Engine holds the bans issued so far.

func New

func New(cfg Config) *Engine

New returns an engine with no history.

func (*Engine) Commit

func (e *Engine) Commit(r Result)

Commit applies a step's new bans and manual entries after the caller has recorded them, and forgets what has aged out.

func (*Engine) NewTally added in v0.2.0

func (e *Engine) NewTally() *Tally

NewTally returns an empty tally of this engine's sources.

func (*Engine) Published

func (e *Engine) Published(entries []Entry)

Published tells the engine what consumers now hold, for the next step's generation cap.

func (*Engine) Restore

func (e *Engine) Restore(bans []Ban, manualKeys, published []string)

Restore loads what was recorded before a restart: the bans, the manual entries already recorded, and the prefixes of the last published list, which the next step's additions are counted against.

func (*Engine) Step

func (e *Engine) Step(in Inputs) Result

Step computes the next list from in without changing the engine.

type Entry

type Entry struct {
	CIDR    string `json:"cidr"`
	Expires int64  `json:"expires"`
	Source  string `json:"source"`
	Reason  string `json:"reason"`
	Commit  string `json:"commit"`
}

Entry is one published prefix.

type Inputs

type Inputs struct {
	Now  time.Time
	Mode Mode
	// Tallies are the signals' rows, by signal name. A signal without one
	// gives no points.
	Tallies map[string]*Tally
	Allow   *netaddr.Set
	Manual  []manual.Entry
	// NoNewDecisions issues no bans and reports no newly seen manual
	// entries, for a step whose decisions cannot be recorded. Its list still
	// drops what has expired, been lifted or been allowed.
	NoNewDecisions bool
}

Inputs is everything one step reads.

type Mode

type Mode string

Mode is the kill switch's position.

const (
	// Enforce publishes the list; consumers apply it in their own mode.
	Enforce Mode = "enforce"
	// Shadow publishes the list and tells consumers to drop nothing.
	Shadow Mode = "shadow"
	// Empty publishes an empty list.
	Empty Mode = "empty"
)

func ParseMode

func ParseMode(s string) (Mode, error)

ParseMode accepts enforce, shadow or empty.

type Result

type Result struct {
	Now       time.Time
	NewBans   []Ban
	NewManual []manual.Entry
	Entries   []Entry
	// Suppressed are sources over the threshold, bans in force and manual
	// bans that the allowlist or a lift keeps off the list.
	Suppressed []Suppression
	// Deferred are entries a cap holds back, by reason. They are considered
	// again at the next step.
	Deferred map[string][]netip.Prefix
	// WouldBan are sources a shadow signal would have banned, by signal.
	WouldBan map[string][]netip.Prefix
	// Scored counts sources with any points from counting signals.
	Scored int
	// Invalid counts rows that could not be scored: their address did not
	// parse, or their attempts were not a count.
	Invalid int
	// ActiveManual counts manual entries in force, by action.
	ActiveManual map[string]int
}

Result is what one step would do.

type Row

type Row struct {
	Address  string
	Feeder   string
	Attempts float64
}

Row is one signal's count for one address and feeder over the window. The feeder is whoever reported the attempts, such as a host; caps on new bans are kept per feeder.

type Signal

type Signal struct {
	Name   string
	Weight float64
	Cap    float64
	Shadow bool
}

Signal weighs one kind of evidence. Points from a signal are Weight per attempt, at most Cap per source over the window (0 means no cap). A shadow signal is scored and reported but never counts toward a ban.

type Suppression

type Suppression struct {
	Prefix netip.Prefix
	Points float64
	// By is SuppressedByAllowlist or SuppressedByLift.
	By     string
	Manual bool
}

Suppression is a source, a ban in force or a manual ban kept off the list.

type Tally added in v0.2.0

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

Tally sums one signal's rows per source and feeder as they are read, so what a step holds grows with the sources, not with how many addresses of one source the query returns.

func (*Tally) Add added in v0.2.0

func (t *Tally) Add(r Row)

Add counts one row. A row whose address does not parse, or whose attempts are NaN (as NULL arrives), infinite or negative, counts as invalid.

Jump to

Keyboard shortcuts

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