config

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LogSinkStdio = "stdio"
	LogSinkFile  = "file"
)
View Source
const DefaultAlgorithm = "fast"

Variables

View Source
var (
	ErrNoBotRulesDefined                 = errors.New("config: must define at least one (1) bot rule")
	ErrBotMustHaveName                   = errors.New("config.Bot: must set name")
	ErrBotMustHaveUserAgentOrPath        = errors.New("config.Bot: must set either user_agent_regex, path_regex, headers_regex, or remote_addresses")
	ErrBotMustHaveUserAgentOrPathNotBoth = errors.New("config.Bot: must set either user_agent_regex, path_regex, and not both")
	ErrUnknownAction                     = errors.New("config.Bot: unknown action")
	ErrInvalidUserAgentRegex             = errors.New("config.Bot: invalid user agent regex")
	ErrInvalidPathRegex                  = errors.New("config.Bot: invalid path regex")
	ErrInvalidHeadersRegex               = errors.New("config.Bot: invalid headers regex")
	ErrInvalidCIDR                       = errors.New("config.Bot: invalid CIDR")
	ErrRegexEndsWithNewline              = errors.New("config.Bot: regular expression ends with newline (try >- instead of > in yaml)")
	ErrInvalidImportStatement            = errors.New("config.ImportStatement: invalid source file")
	ErrCantSetBotAndImportValuesAtOnce   = errors.New("config.BotOrImport: can't set bot rules and import values at the same time")
	ErrMustSetBotOrImportRules           = errors.New("config.BotOrImport: rule definition is invalid, you must set either bot rules or an import statement, not both")
	ErrStatusCodeNotValid                = errors.New("config.StatusCode: status code not valid, must be between 100 and 599")
)
View Source
var (
	ErrChallengeDifficultyTooLow  = errors.New("config.ChallengeRules: difficulty is too low (must be >= 0)")
	ErrChallengeDifficultyTooHigh = errors.New("config.ChallengeRules: difficulty is too high (must be <= 64)")
	ErrChallengeMustHaveAlgorithm = errors.New("config.ChallengeRules: must have algorithm name set")
)
View Source
var (
	ErrExpressionOrListMustBeStringOrObject = errors.New("config: this must be a string or an object")
	ErrExpressionEmpty                      = errors.New("config: this expression is empty")
	ErrExpressionCantHaveBoth               = errors.New("config: expression block can't contain multiple expression types")
)
View Source
var (
	ErrMissingLoggingFileConfig = errors.New("config.Logging: missing value parameters in logging block")
	ErrInvalidLoggingSink       = errors.New("config.Logging: invalid sink")
	ErrInvalidLoggingFileConfig = errors.New("config.LoggingFileConfig: invalid parameters")
	ErrOutOfRange               = errors.New("config: error out of range")
)
View Source
var (
	ErrInvalidOpenGraphConfig   = errors.New("config.OpenGraph: invalid OpenGraph configuration")
	ErrOpenGraphTTLDoesNotParse = errors.New("config.OpenGraph: ttl does not parse as a Duration, see https://pkg.go.dev/time#ParseDuration (formatted like 5m -> 5 minutes, 2h -> 2 hours, etc)")
	ErrOpenGraphMissingProperty = errors.New("config.OpenGraph: default opengraph tags missing a property")
)
View Source
var (
	ErrNoStoreBackend      = errors.New("config.Store: no backend defined")
	ErrUnknownStoreBackend = errors.New("config.Store: unknown backend")
)
View Source
var (
	ErrNoThresholdRulesDefined             = errors.New("config: no thresholds defined")
	ErrThresholdMustHaveName               = errors.New("config.Threshold: must set name")
	ErrThresholdMustHaveExpression         = errors.New("config.Threshold: must set expression")
	ErrThresholdChallengeMustHaveChallenge = errors.New("config.Threshold: a threshold with the CHALLENGE action must have challenge set")
	ErrThresholdCannotHaveWeighAction      = errors.New("config.Threshold: a threshold cannot have the WEIGH action")

	DefaultThresholds = []Threshold{
		{
			Name: "legacy-anubis-behaviour",
			Expression: &ExpressionOrList{
				Expression: "weight > 0",
			},
			Action: RuleChallenge,
			Challenge: &ChallengeRules{
				Algorithm:  "fast",
				Difficulty: anubis.DefaultDifficulty,
			},
		},
	}
)
View Source
var ErrMissingValue = errors.New("config: missing value")
View Source
var (
	ErrNotCountryCode = errors.New("config.Bot: invalid country code")
)
View Source
var (
	ErrPrivateASN = errors.New("bot.ASNs: you have specified a private use ASN")
)

Functions

This section is empty.

Types

type ASNs

type ASNs struct {
	Match []uint32 `json:"match"`
}

func (*ASNs) Valid

func (a *ASNs) Valid() error

type BotConfig

type BotConfig struct {
	UserAgentRegex *string           `json:"user_agent_regex,omitempty" yaml:"user_agent_regex,omitempty"`
	PathRegex      *string           `json:"path_regex,omitempty" yaml:"path_regex,omitempty"`
	HeadersRegex   map[string]string `json:"headers_regex,omitempty" yaml:"headers_regex,omitempty"`
	Expression     *ExpressionOrList `json:"expression,omitempty" yaml:"expression,omitempty"`
	Challenge      *ChallengeRules   `json:"challenge,omitempty" yaml:"challenge,omitempty"`
	Weight         *Weight           `json:"weight,omitempty" yaml:"weight,omitempty"`

	// Thoth features
	GeoIP *GeoIP `json:"geoip,omitempty"`
	ASNs  *ASNs  `json:"asns,omitempty"`

	Name       string   `json:"name" yaml:"name"`
	Action     Rule     `json:"action" yaml:"action"`
	RemoteAddr []string `json:"remote_addresses,omitempty" yaml:"remote_addresses,omitempty"`
}

func (*BotConfig) Valid

func (b *BotConfig) Valid() error

func (BotConfig) Zero

func (b BotConfig) Zero() bool

type BotOrImport

type BotOrImport struct {
	*BotConfig       `json:",inline"`
	*ImportStatement `json:",inline"`
}

func (*BotOrImport) Valid

func (boi *BotOrImport) Valid() error

type ChallengeRules

type ChallengeRules struct {
	Algorithm  string `json:"algorithm,omitempty" yaml:"algorithm,omitempty"`
	Difficulty int    `json:"difficulty,omitempty" yaml:"difficulty,omitempty"`
	ReportAs   int    `json:"report_as,omitempty" yaml:"report_as,omitempty"`
}

func (ChallengeRules) Valid

func (cr ChallengeRules) Valid() error

type Config

type Config struct {
	Impressum   *Impressum
	Store       *Store
	OpenGraph   OpenGraph
	Bots        []BotConfig
	Thresholds  []Threshold
	StatusCodes StatusCodes
	Logging     *Logging
	DNSBL       bool
	DNSTTL      DnsTTL
}

func Load

func Load(fin io.Reader, fname string) (*Config, error)

func (Config) Valid

func (c Config) Valid() error

type DnsTTL

type DnsTTL struct {
	Forward int `json:"forward"`
	Reverse int `json:"reverse"`
}

func (DnsTTL) Valid

func (sc DnsTTL) Valid() error

type ExpressionOrList

type ExpressionOrList struct {
	Expression string   `json:"-" yaml:"-"`
	All        []string `json:"all,omitempty" yaml:"all,omitempty"`
	Any        []string `json:"any,omitempty" yaml:"any,omitempty"`
}

func (ExpressionOrList) Equal

func (eol ExpressionOrList) Equal(rhs *ExpressionOrList) bool

func (*ExpressionOrList) MarshalJSON

func (eol *ExpressionOrList) MarshalJSON() ([]byte, error)

func (*ExpressionOrList) MarshalYAML

func (eol *ExpressionOrList) MarshalYAML() (any, error)

func (ExpressionOrList) String

func (eol ExpressionOrList) String() string

func (*ExpressionOrList) UnmarshalJSON

func (eol *ExpressionOrList) UnmarshalJSON(data []byte) error

func (*ExpressionOrList) Valid

func (eol *ExpressionOrList) Valid() error

type GeoIP

type GeoIP struct {
	Countries []string `json:"countries"`
}

func (*GeoIP) Valid

func (g *GeoIP) Valid() error

type ImportStatement

type ImportStatement struct {
	Import string `json:"import"`
	Bots   []BotConfig
}

func (*ImportStatement) Valid

func (is *ImportStatement) Valid() error

type Impressum

type Impressum struct {
	Footer string        `json:"footer" yaml:"footer"`
	Page   ImpressumPage `json:"page" yaml:"page"`
}

func (Impressum) Render

func (i Impressum) Render(_ context.Context, w io.Writer) error

func (Impressum) Valid

func (i Impressum) Valid() error

type ImpressumPage

type ImpressumPage struct {
	Title string `json:"title" yaml:"title"`
	Body  string `json:"body" yaml:"body"`
}

func (ImpressumPage) Render

func (ip ImpressumPage) Render(_ context.Context, w io.Writer) error

func (ImpressumPage) Valid

func (ip ImpressumPage) Valid() error

type Logging

type Logging struct {
	Sink       string             `json:"sink"`       // Logging sink, either "stdio" or "file"
	Level      *slog.Level        `json:"level"`      // Log level, if set supercedes the level in flags
	Parameters *LoggingFileConfig `json:"parameters"` // Logging parameters, to be dynamic in the future
}

func (Logging) Default

func (Logging) Default() *Logging

func (*Logging) Valid

func (l *Logging) Valid() error

type LoggingFileConfig

type LoggingFileConfig struct {
	Filename     string `json:"file"`
	MaxBackups   int    `json:"maxBackups"`
	MaxBytes     int64  `json:"maxBytes"`
	MaxAge       int    `json:"maxAge"`
	Compress     bool   `json:"compress"`
	UseLocalTime bool   `json:"useLocalTime"`
}

func (LoggingFileConfig) Default

func (*LoggingFileConfig) Valid

func (lfc *LoggingFileConfig) Valid() error

func (LoggingFileConfig) Zero

func (lfc LoggingFileConfig) Zero() bool

type OpenGraph

type OpenGraph struct {
	Override     map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
	TimeToLive   time.Duration     `json:"ttl" yaml:"ttl"`
	Enabled      bool              `json:"enabled" yaml:"enabled"`
	ConsiderHost bool              `json:"considerHost" yaml:"enabled"`
}

type Rule

type Rule string
const (
	RuleUnknown   Rule = ""
	RuleAllow     Rule = "ALLOW"
	RuleDeny      Rule = "DENY"
	RuleChallenge Rule = "CHALLENGE"
	RuleWeigh     Rule = "WEIGH"
	RuleBenchmark Rule = "DEBUG_BENCHMARK"
)

func (Rule) Valid

func (r Rule) Valid() error

type StatusCodes

type StatusCodes struct {
	Challenge int `json:"CHALLENGE"`
	Deny      int `json:"DENY"`
}

func (StatusCodes) Valid

func (sc StatusCodes) Valid() error

type Store

type Store struct {
	Backend    string          `json:"backend"`
	Parameters json.RawMessage `json:"parameters"`
}

func (*Store) Valid

func (s *Store) Valid() error

type Threshold

type Threshold struct {
	Expression *ExpressionOrList `json:"expression" yaml:"expression"`
	Challenge  *ChallengeRules   `json:"challenge" yaml:"challenge"`
	Name       string            `json:"name" yaml:"name"`
	Action     Rule              `json:"action" yaml:"action"`
}

func (Threshold) Valid

func (t Threshold) Valid() error

type Weight

type Weight struct {
	Adjust int `json:"adjust" yaml:"adjust"`
}

Jump to

Keyboard shortcuts

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