feat: add readable routing rule comments

This commit is contained in:
Zeusina committed 2026-09-28 17:33:07 +03:00
1 parent 2206d03b39
commit 2fcb4ae171
4 files changed
+25

No files matched your search

+2
View File
@@ -18,6 +18,7 @@ go run . -config config.yaml
`tokenEnv` задаёт имя переменной окружения и имеет приоритет над непустым `token`. `tokenEnv` задаёт имя переменной окружения и имеет приоритет над непустым `token`.
- `disabled: true` записывает в правило `enabled: false`; правило остаётся в конфигурации. - `disabled: true` записывает в правило `enabled: false`; правило остаётся в конфигурации.
- `comment` задаёт отображаемое в 3x-ui имя правила; по умолчанию используется `name`.
- Один совпавший `outbound.tag` используется напрямую; несколько создают балансировщик. - Один совпавший `outbound.tag` используется напрямую; несколько создают балансировщик.
- `leastPing` и `leastLoad` требуют Xray `observatory`. - `leastPing` и `leastLoad` требуют Xray `observatory`.
- Порядок правил в YAML сохраняется; `sync.position` выбирает размещение блока: `first` или `last`. - Порядок правил в YAML сохраняется; `sync.position` выбирает размещение блока: `first` или `last`.
@@ -40,6 +41,7 @@ go run . -config config.yaml
`tokenEnv` names an environment variable and overrides a non-empty `token`. `tokenEnv` names an environment variable and overrides a non-empty `token`.
- `disabled: true` writes `enabled: false` to the rule while retaining it in configuration. - `disabled: true` writes `enabled: false` to the rule while retaining it in configuration.
- `comment` sets the rule name displayed in 3x-ui; it defaults to `name`.
- One matching `outbound.tag` is used directly; several matches create a balancer. - One matching `outbound.tag` is used directly; several matches create a balancer.
- `leastPing` and `leastLoad` require an Xray `observatory`. - `leastPing` and `leastLoad` require an Xray `observatory`.
- YAML rule order is retained; `sync.position` places the managed block at `first` or `last`. - YAML rule order is retained; `sync.position` places the managed block at `first` or `last`.
+2
View File
@@ -7,6 +7,8 @@ servers:
rules: rules:
- name: google - name: google
# Displayed as the rule name in 3x-ui. Defaults to `name` when omitted.
comment: Google
# disabled: true # writes enabled: false in the Xray rule # disabled: true # writes enabled: false in the Xray rule
match: match:
domain: [geosite:google] domain: [geosite:google]
+6
View File
@@ -38,6 +38,7 @@ type Server struct {
type Rule struct { type Rule struct {
Name string `yaml:"name"` Name string `yaml:"name"`
Comment string `yaml:"comment"`
Disabled bool `yaml:"disabled"` Disabled bool `yaml:"disabled"`
Match map[string]any `yaml:"match"` Match map[string]any `yaml:"match"`
Outbound Outbound `yaml:"outbound"` Outbound Outbound `yaml:"outbound"`
@@ -335,7 +336,12 @@ func reconcileWithOutbounds(config map[string]any, desired []Rule, position stri
delete(item, "outboundTag") delete(item, "outboundTag")
delete(item, "balancerTag") delete(item, "balancerTag")
delete(item, "ruleTag") delete(item, "ruleTag")
delete(item, "comment")
item["ruleTag"] = managedPrefix + rule.Name item["ruleTag"] = managedPrefix + rule.Name
item["comment"] = rule.Comment
if rule.Comment == "" {
item["comment"] = rule.Name
}
item["enabled"] = !rule.Disabled item["enabled"] = !rule.Disabled
if len(matches) == 1 { if len(matches) == 1 {
item["outboundTag"] = matches[0] item["outboundTag"] = matches[0]
+15
View File
@@ -69,3 +69,18 @@ func TestDisabledRuleWritesEnabledFalse(t *testing.T) {
t.Fatalf("disabled rule: %#v", rules) t.Fatalf("disabled rule: %#v", rules)
} }
} }
func TestReconcileWritesRuleComment(t *testing.T) {
config := map[string]any{"outbounds": []any{map[string]any{"tag": "proxy-a"}}}
updated, err := reconcile(config, []Rule{
{Name: "clash-royale", Comment: "Clash Royale", Match: map[string]any{"comment": "ignored"}, Outbound: Outbound{TagRegex: "proxy-a"}},
{Name: "ip-rules", Outbound: Outbound{TagRegex: "proxy-a"}},
}, "last")
if err != nil {
t.Fatal(err)
}
rules, _ := objects(updated["routing"].(map[string]any)["rules"])
if rules[0]["comment"] != "Clash Royale" || rules[1]["comment"] != "ip-rules" {
t.Fatalf("unexpected comments: %#v", rules)
}
}