feat: evolve rate-filter to a more generic filter

This commit is contained in:
2026-07-28 20:40:12 +08:00
parent a3e3079af1
commit b206594eb8
15 changed files with 492 additions and 193 deletions

View File

@ -3,9 +3,10 @@
//
// 1. build a new proxy-group from proxies matching a subscription/keyword
// filter ("proxy-groups"),
// 2. build a new proxy-group from proxies whose name encodes a rate/
// multiplier extracted via regexp, filtered by a comparator
// ("rate-filters"), and
// 2. strip unwanted proxies — matched by subscription/name-pattern and,
// optionally, a rate/multiplier encoded in the name — out of every
// proxy-group's proxies list, subscription-provided or built by a
// proxy-groups rule ("filters"), and
// 3. append/prepend/replace an arbitrary path in the generated config
// ("patches").
//
@ -14,29 +15,36 @@
// writing (see internal/corecfg.Apply).
package automation
// ProxyRef describes a single proxy in the pool merged from every active
// subscription.
// ProxyRef describes a single proxy or proxy-group in the pool merged from
// every active subscription — both are matchable the same way, so a
// `match.name-pattern` can target either a raw proxy or one of a
// subscription's own groups (e.g. its default selector).
type ProxyRef struct {
// OriginalName is the proxy's name as it appeared in its subscription,
// before subscription-name prefixing. Keyword and rate-pattern matching
// both operate on this.
// OriginalName is the proxy/group's name as it appeared in its
// subscription, before subscription-name prefixing. Name-pattern
// matching operates on this.
OriginalName string
// DisplayName is OriginalName prefixed with its source subscription's
// name (e.g. "home-sub | HK 01"), which is what actually appears in the
// generated config's proxies list and must be used when referencing
// this proxy from a built proxy-group.
// generated config's proxies/proxy-groups and must be used when
// referencing this proxy/group from a built proxy-group.
DisplayName string
// Subscription is the name of the subscription this proxy came from.
// Subscription is the name of the subscription this proxy/group came
// from.
Subscription string
}
// MatchRule narrows which proxies a proxy-groups/rate-filters rule
// considers. Both fields are optional; an empty/omitted field imposes no
// restriction on that dimension. Multiple entries within a field are OR'd
// together.
// MatchRule narrows which proxies a proxy-groups/filters rule considers.
// All fields are optional; an empty/omitted field imposes no
// restriction on that dimension. Within Subscriptions, entries are OR'd
// together; the dimensions themselves (subscriptions, name-pattern) are
// AND'd together.
type MatchRule struct {
Subscriptions []string `yaml:"subscriptions"`
NameContains []string `yaml:"name-contains"`
// NamePattern is a regexp matched against the proxy's original
// (pre-prefix) name. Use "|" for OR-ing alternatives and the "(?i)"
// flag for case-insensitive matching, e.g. "(?i)HK|Hong Kong".
NamePattern string `yaml:"name-pattern"`
}
// ProxyGroupRule defines a new proxy-group built from every proxy matching
@ -49,20 +57,27 @@ type ProxyGroupRule struct {
Extra map[string]interface{} `yaml:",inline"`
}
// RateFilterRule defines a new proxy-group built from proxies whose
// (pre-prefix) name matches Pattern — a regexp whose first capture group is
// parsed as a float rate — and whose rate satisfies "rate Operator Value".
// Proxies whose name doesn't match Pattern at all are excluded. Any YAML
// fields beyond the recognized ones are passed through onto the generated
// proxy-group, same as ProxyGroupRule.
type RateFilterRule struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Pattern string `yaml:"pattern"`
Operator string `yaml:"operator"`
Value float64 `yaml:"value"`
Match MatchRule `yaml:"match"`
Extra map[string]interface{} `yaml:",inline"`
// CompareRule extracts a float from a proxy's (pre-prefix) name via
// Pattern's first capture group and matches proxies whose extracted value
// satisfies "value Operator Value". Proxies whose name doesn't match
// Pattern at all don't match this rule.
type CompareRule struct {
Pattern string `yaml:"pattern"`
Operator string `yaml:"operator"`
Value float64 `yaml:"value"`
}
// FilterRule removes unwanted proxies from every proxy-group's `proxies`
// list in the generated config. Match scopes which proxies this rule
// considers; Compares is optional and adds a second narrowing step. A
// scoped proxy is unwanted — and gets stripped out wherever it's
// referenced — if Compares is empty (Match alone acts as a denylist), or
// if it satisfies every entry in Compares (AND'd) when Compares is
// non-empty.
type FilterRule struct {
Name string `yaml:"name"`
Compares []CompareRule `yaml:"compares"`
Match MatchRule `yaml:"match"`
}
// PatchRule appends/prepends/replaces Value at Path (a dot/bracket path
@ -77,6 +92,6 @@ type PatchRule struct {
// Config is the full `ssm:` automation block parsed from core-config.yaml.
type Config struct {
ProxyGroups []ProxyGroupRule `yaml:"proxy-groups"`
RateFilters []RateFilterRule `yaml:"rate-filters"`
Filters []FilterRule `yaml:"filters"`
Patches []PatchRule `yaml:"patches"`
}