anticheat

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package anticheat is the per-class check harness for server-authoritative games: pure-function checks register under a Class enum, are dispatched with InputMode-aware thresholds, and route results (info / flag / hard) to audit + metrics sinks.

The 20-class catalogue (Speed / Teleport / FireRate / RootStunBypass etc.) is game-specific and lives in the consumer; the harness here owns registration, dispatch, severity routing, and metric/audit emission so each consumer doesn't re-invent that scaffold.

Checks must be pure: no I/O, no goroutines, no time.Now() — pass the simulation tick + clock through the input struct.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClassUnregistered = errors.New("anticheat: class has no registered check")
	ErrCheckTypeMismatch = errors.New("anticheat: input type does not match registered check")
)

Errors surfaced by the registry.

Functions

func Register

func Register[T any](r *Registry, class Class, c Check[T])

Register installs a typed Check under class. Panics on duplicate class — registration happens at init / construction time so a programming error should fail loud.

Types

type AuditSink

type AuditSink interface {
	Audit(ctx context.Context, class Class, mode InputMode, result Result)
}

AuditSink is invoked for every Result (regardless of severity).

type AuditSinkFunc

type AuditSinkFunc func(ctx context.Context, class Class, mode InputMode, result Result)

AuditSinkFunc adapts a function to AuditSink.

func (AuditSinkFunc) Audit

func (f AuditSinkFunc) Audit(ctx context.Context, class Class, mode InputMode, result Result)

Audit implements AuditSink.

type Check

type Check[T any] func(ctx context.Context, in T) (Result, error)

Check is the consumer's pure-function check.

type Class

type Class string

Class is the consumer's enum of check identities.

type Config

type Config struct {
	Audit   AuditSink
	Metrics MetricsSink
}

Config configures a Registry.

type InputMode

type InputMode uint8

InputMode caps thresholds per device family. Consumers register one threshold value per (Class, InputMode); the dispatcher picks the right value automatically.

const (
	InputKBM InputMode = iota
	InputController
	InputHybrid
)

type MetricsSink

type MetricsSink interface {
	Inc(class Class, reason string)
}

MetricsSink is invoked for SevFlag and SevHard results.

type MetricsSinkFunc

type MetricsSinkFunc func(class Class, reason string)

MetricsSinkFunc adapts a function to MetricsSink.

func (MetricsSinkFunc) Inc

func (f MetricsSinkFunc) Inc(class Class, reason string)

Inc implements MetricsSink.

type Registry

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

Registry holds the consumer's checks and dispatches Run calls.

func New

func New(cfg Config) *Registry

New constructs a Registry. A nil Audit or Metrics sink is treated as no-op (the caller may not need both).

type Result

type Result struct {
	Pass     bool
	Severity Severity
	Reason   string         // short tag for metric labels — keep cardinality bounded
	Detail   map[string]any // free-form payload for the audit row
}

Result is what a Check returns.

func Run

func Run[T any](r *Registry, ctx context.Context, class Class, mode InputMode, in T) (Result, error)

Run dispatches the registered check for class with the supplied input + InputMode. The result's severity drives audit + metrics routing; the result itself is also returned so the caller can act on SevHard.

type Severity

type Severity uint8

Severity classifies a Result.

const (
	// SevInfo — single-event anomaly. Logged via the audit sink; no
	// metric counter, no hard reject.
	SevInfo Severity = iota
	// SevFlag — recurring anomaly. Audit + metric counter.
	SevFlag
	// SevHard — outright violation. Audit + metric + the dispatcher
	// returns the result so the caller can reject the action.
	SevHard
)

func (Severity) String

func (s Severity) String() string

type Thresholds

type Thresholds[V any] struct {
	// contains filtered or unexported fields
}

Thresholds is a per-(Class, InputMode) value map. Lookups are O(1) after construction with no allocations on the hot path.

func NewThresholds

func NewThresholds[V any]() *Thresholds[V]

NewThresholds constructs an empty Thresholds.

func (*Thresholds[V]) Get

func (t *Thresholds[V]) Get(class Class, mode InputMode) (V, bool)

Get returns the value at (class, mode). Returns the zero value when the pair is unregistered (does not allocate).

func (*Thresholds[V]) Set

func (t *Thresholds[V]) Set(class Class, mode InputMode, v V)

Set installs a value at (class, mode).

Jump to

Keyboard shortcuts

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