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 ¶
- Variables
- func Build(spec RuleSpec) (engine.Rule, error)
- func Delete(rules []engine.Rule, ruleID string) ([]engine.Rule, engine.Rule, error)
- func Finalize(rule *engine.Rule) error
- func IsLoopbackListenAddr(addr string) bool
- func MapWriteError(err error) error
- func NormalizeProtocol(value string) (engine.Protocol, error)
- func NormalizeRuleID(raw string) (string, error)
- func ParseBandwidth(in BandwidthSpec) (*engine.BandwidthLimit, error)
- func ParseQuota(in QuotaSpec) (*engine.TrafficQuota, error)
- func ParseRate(value, field string) (engine.ByteRate, error)
- func ParseSize(value, field string) (engine.ByteSize, error)
- func SetEnabled(rules []engine.Rule, ruleID string, enabled bool) ([]engine.Rule, engine.Rule, error)
- func Upsert(rules []engine.Rule, rule engine.Rule) ([]engine.Rule, bool)
- type BandwidthSpec
- type Client
- type Mutation
- type NotFoundError
- type QuotaSpec
- type Result
- type RuleSpec
Constants ¶
This section is empty.
Variables ¶
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 Delete ¶
Delete removes the rule with the given ID and returns it. The input slice is never modified.
func Finalize ¶
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 ¶
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 ¶
MapWriteError converts daemon write failures into actionable errors.
func NormalizeProtocol ¶
NormalizeProtocol accepts only the protocols the write tools may configure.
func NormalizeRuleID ¶
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 ¶
ParseRate parses a human-readable rate such as "100 Mbps". An empty value parses as the zero rate.
func ParseSize ¶
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.
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 ¶
type Client interface {
ConfigRules(ctx context.Context) (*controlapi.ConfigRulesResponse, error)
Precheck(ctx context.Context, match string, draft controlapi.ConfigRulesRequest) (*controlapi.PrecheckResponse, error)
Apply(ctx context.Context, match string, draft controlapi.ConfigRulesRequest) (*controlapi.ApplyResponse, error)
}
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.
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.