refactor(routing): 暴露 system_locked_domains + 服务端 follow-up 清理(FK/Validate/dedup/exclude单源/共用Store/Builtin保留)

This commit is contained in:
wangjia
2026-07-29 06:38:52 +08:00
parent 52912268d0
commit 3159dd75c1
11 changed files with 285 additions and 36 deletions
+23 -8
View File
@@ -21,8 +21,17 @@ type Rule struct {
// Builtin toggles the built-in routing behaviors that previously were
// hardcoded into the server's sing-box config rendering.
type Builtin struct {
ChinaDirect bool `json:"china_direct"`
LanDirect bool `json:"lan_direct"`
ChinaDirect bool `json:"china_direct"`
// LanDirect is reserved/always-on: the system layer (route_exclude_address
// + the hardcoded LAN direct rule in clientconfig.go) renders unconditionally
// regardless of this flag's value — the renderer does not read it. Kept in
// the wire contract for a possible future per-user opt-out; today it has no
// effect on rendering.
LanDirect bool `json:"lan_direct"`
// PrivateViaTunnel is reserved/always-on: when PANGOLIN_PRIVATE_SPLIT_DOMAINS
// is configured, those domains are always force-routed through the tunnel —
// the renderer does not read this flag. Kept in the wire contract for a
// possible future per-user opt-out; today it has no effect on rendering.
PrivateViaTunnel bool `json:"private_via_tunnel"`
}
@@ -64,11 +73,11 @@ var geoWhitelist = map[string]bool{"cn": true} // geoip/geosite 仅自托管 cn
// Validate checks the profile against the type/action whitelist, CIDR
// syntax, the geo set whitelist, and the rule count cap. It returns an empty
// (nil) slice when the profile is valid; every violation is reported
// independently (no short-circuiting) so callers can surface all errors at
// once.
// (non-nil) slice when the profile is valid — so JSON encoding produces `[]`
// rather than `null` — and every violation is reported independently (no
// short-circuiting) so callers can surface all errors at once.
func (p *Profile) Validate() []FieldError {
var errs []FieldError
errs := []FieldError{}
if p.Mode != "rule" && p.Mode != "global" && p.Mode != "direct" {
errs = append(errs, FieldError{-1, "mode", "must be rule|global|direct"})
}
@@ -102,14 +111,20 @@ func (p *Profile) Validate() []FieldError {
return errs
}
// dedupKey identifies a rule for Normalize's de-duplication. Using a struct
// (rather than string-concatenating Type+Value+Action with a separator)
// avoids false-collision when a value itself contains the separator
// character.
type dedupKey struct{ Type, Value, Action string }
// Normalize trims rule values and de-duplicates rules by (type, value,
// action), keeping the first occurrence's position (and its Note/Enabled).
func (p *Profile) Normalize() {
seen := map[string]bool{}
seen := map[dedupKey]bool{}
out := p.Rules[:0]
for _, r := range p.Rules {
r.Value = strings.TrimSpace(r.Value)
k := r.Type + "|" + r.Value + "|" + r.Action
k := dedupKey{r.Type, r.Value, r.Action}
if seen[k] {
continue
}