diff --git a/README.md b/README.md index 18a5d2f..d1e9200 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ go run . -config config.yaml `tokenEnv` задаёт имя переменной окружения и имеет приоритет над непустым `token`. - `disabled: true` записывает в правило `enabled: false`; правило остаётся в конфигурации. +- `comment` задаёт отображаемое в 3x-ui имя правила; по умолчанию используется `name`. - Один совпавший `outbound.tag` используется напрямую; несколько создают балансировщик. - `leastPing` и `leastLoad` требуют Xray `observatory`. - Порядок правил в 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`. - `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. - `leastPing` and `leastLoad` require an Xray `observatory`. - YAML rule order is retained; `sync.position` places the managed block at `first` or `last`. diff --git a/config.example.yaml b/config.example.yaml index 5370c99..acda0a8 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -7,6 +7,8 @@ servers: rules: - 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 match: domain: [geosite:google] diff --git a/main.go b/main.go index b74635e..857653a 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,7 @@ type Server struct { type Rule struct { Name string `yaml:"name"` + Comment string `yaml:"comment"` Disabled bool `yaml:"disabled"` Match map[string]any `yaml:"match"` Outbound Outbound `yaml:"outbound"` @@ -335,7 +336,12 @@ func reconcileWithOutbounds(config map[string]any, desired []Rule, position stri delete(item, "outboundTag") delete(item, "balancerTag") delete(item, "ruleTag") + delete(item, "comment") item["ruleTag"] = managedPrefix + rule.Name + item["comment"] = rule.Comment + if rule.Comment == "" { + item["comment"] = rule.Name + } item["enabled"] = !rule.Disabled if len(matches) == 1 { item["outboundTag"] = matches[0] diff --git a/main_test.go b/main_test.go index 9800145..99dff60 100644 --- a/main_test.go +++ b/main_test.go @@ -69,3 +69,18 @@ func TestDisabledRuleWritesEnabledFalse(t *testing.T) { 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) + } +}