ruleedit

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 9 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 ruleedit implements the shared "change one forwarding rule" flow used by the MCP write tools and the vmflow rule CLI: read the configured rules snapshot, change a single rule in a copy, then precheck or apply the draft through the daemon management API with optimistic concurrency.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnchanged = errors.New("forwarding rule is already in the requested state")

ErrUnchanged signals a no-op mutation (for example enabling a rule that is already enabled); Mutate reports it without contacting the daemon.

Functions

func Build

func Build(spec RuleSpec) (engine.Rule, error)

Build converts a RuleSpec into a validated engine.Rule.

func Delete

func Delete(rules []engine.Rule, ruleID string) ([]engine.Rule, engine.Rule, error)

Delete removes the rule with the given ID and returns it. The input slice is never modified.

func Finalize

func Finalize(rule *engine.Rule) error

Finalize standardizes rule in place and validates it, wrapping validation failures in "invalid rule". It is applied to rules patched from a snapshot before the draft is submitted.

func IsLoopbackListenAddr

func IsLoopbackListenAddr(addr string) bool

IsLoopbackListenAddr reports whether addr limits listeners to this host. An empty address, 0.0.0.0, and :: listen on every interface, so they count as non-loopback exposure.

func MapWriteError

func MapWriteError(err error) error

MapWriteError converts daemon write failures into actionable errors.

func NormalizeProtocol

func NormalizeProtocol(value string) (engine.Protocol, error)

NormalizeProtocol accepts only the protocols the write tools may configure.

func NormalizeRuleID

func NormalizeRuleID(raw string) (string, error)

NormalizeRuleID trims the requested rule ID and enforces the length bound.

func ParseBandwidth

func ParseBandwidth(in BandwidthSpec) (*engine.BandwidthLimit, error)

ParseBandwidth converts the string quantities of a BandwidthSpec; fields left empty stay at the engine zero value.

func ParseQuota

func ParseQuota(in QuotaSpec) (*engine.TrafficQuota, error)

ParseQuota converts the string quantities of a QuotaSpec; fields left empty stay at the engine zero value and the quota action defaults to "block".

func ParseRate

func ParseRate(value, field string) (engine.ByteRate, error)

ParseRate parses a human-readable rate such as "100 Mbps". An empty value parses as the zero rate.

func ParseSize

func ParseSize(value, field string) (engine.ByteSize, error)

ParseSize parses a human-readable size such as "50GB". An empty value parses as the zero size.

func SetEnabled

func SetEnabled(rules []engine.Rule, ruleID string, enabled bool) ([]engine.Rule, engine.Rule, error)

SetEnabled enables or disables the rule with the given ID. When the rule already has the requested state it returns ErrUnchanged together with the untouched rule. The input slice is never modified.

func Upsert

func Upsert(rules []engine.Rule, rule engine.Rule) ([]engine.Rule, bool)

Upsert replaces the configured rule with the same ID, or appends the rule when the ID is new, and reports whether the rule was newly created. The input slice is never modified.

Types

type BandwidthSpec

type BandwidthSpec struct {
	Rate     string `` /* 145-byte string literal not displayed */
	Upload   string `json:"upload,omitempty" jsonschema:"upload-only limit such as \"50 Mbps\"; cannot be combined with rate"`
	Download string `json:"download,omitempty" jsonschema:"download-only limit such as \"200 Mbps\"; cannot be combined with rate"`
}

BandwidthSpec mirrors engine.BandwidthLimit with string quantities so clients send human-readable values such as "100 Mbps". Quantities parse through engine's own unit tables.

type Client

Client is the daemon management surface needed to change one rule; *controlapi.Client satisfies it directly.

type Mutation

type Mutation struct {
	RuleID string
	DryRun bool
	Mutate func(rules []engine.Rule) (next []engine.Rule, result *engine.Rule, created bool, err error)
}

Mutation describes one single-rule change. RuleID selects the rule in the apply response; Mutate receives a copy of the configured rules and returns the replacement slice plus the resulting rule (nil when deleted).

type NotFoundError

type NotFoundError struct {
	RuleID string
}

NotFoundError reports that no configured rule matches the requested ID.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type QuotaSpec

type QuotaSpec struct {
	Daily    string `json:"daily,omitempty" jsonschema:"calendar-day upload+download quota such as \"50GB\"; daily or monthly is required"`
	Monthly  string `json:"monthly,omitempty" jsonschema:"calendar-month quota such as \"1TB\""`
	Timezone string `json:"timezone,omitempty" jsonschema:"IANA timezone for quota windows; defaults to UTC"`
}

QuotaSpec mirrors engine.TrafficQuota with string quantities.

type Result

type Result struct {
	Created, Unchanged, Applied, DryRun bool
	Revision                            string
	Rule                                *engine.Rule
	Diff                                []controlapi.ConfigRuleDiff
	Precheck                            *precheck.Result
	ApplyResult                         *engine.ApplyResult
}

Result reports the outcome of one Mutate call.

func Mutate

func Mutate(ctx context.Context, client Client, mutation Mutation) (Result, error)

Mutate reads the current snapshot, checks that the session may write, applies one single-rule mutation to a copy, and either prechecks or applies the resulting draft with optimistic concurrency.

type RuleSpec

type RuleSpec struct {
	RuleID       string         `json:"rule_id" jsonschema:"unique rule ID; upserting an existing rule keeps its identity but replaces its configuration"`
	Name         string         `json:"name" jsonschema:"human-readable rule name"`
	Protocol     string         `json:"protocol" jsonschema:"protocol: tcp, udp, or tcp+udp"`
	ListenAddr   string         `` /* 136-byte string literal not displayed */
	ListenPort   int            `json:"listen_port" jsonschema:"listen port between 1 and 65535"`
	TargetAddr   string         `json:"target_addr" jsonschema:"target address to forward traffic to"`
	TargetPort   int            `json:"target_port" jsonschema:"target port between 1 and 65535"`
	Enabled      *bool          `json:"enabled,omitempty" jsonschema:"whether the rule runs; defaults to true"`
	SpeedLimit   int64          `` /* 132-byte string literal not displayed */
	Bandwidth    *BandwidthSpec `json:"bandwidth_limit,omitempty" jsonschema:"aggregate whole-rule bandwidth policy"`
	TrafficQuota *QuotaSpec     `json:"traffic_quota,omitempty" jsonschema:"durable daily and monthly traffic quota"`
	MaxConn      int            `json:"max_conn,omitempty" jsonschema:"maximum concurrent TCP connections; 0 means unlimited"`
	IdleTimeout  int            `json:"idle_timeout,omitempty" jsonschema:"idle timeout in seconds; 0 uses the daemon default"`
	SourceIPMode string         `json:"source_ip_mode,omitempty" jsonschema:"source policy: allowlist or denylist; required when source_ips is set"`
	SourceIPs    []string       `json:"source_ips,omitempty" jsonschema:"IPv4/IPv6 addresses or CIDRs matched by source_ip_mode"`
	Domains      []string       `json:"domains,omitempty" jsonschema:"domains bound to this rule (currently only used by https rules)"`
	Remark       string         `json:"remark,omitempty" jsonschema:"free-form note stored with the rule"`
	// AllowNonLoopback and DryRun are request options, not rule fields. They
	// live here so the MCP tool keeps its historical JSON input shape. Build
	// reads AllowNonLoopback and ignores DryRun.
	AllowNonLoopback bool `json:"allow_non_loopback,omitempty" jsonschema:"must be true to accept a listen address reachable from other hosts"`
	DryRun           bool `json:"dry_run,omitempty" jsonschema:"validate the change through daemon precheck without applying it"`
}

RuleSpec is the complete desired state of one forwarding rule.

Jump to

Keyboard shortcuts

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