Fix duplicate tag detection for empty tags

Closes https://github.com/SagerNet/sing-box/issues/3665
This commit is contained in:
世界
2026-01-01 18:35:44 +08:00
parent d78828fd81
commit 568612fc70

View File

@@ -5,6 +5,7 @@ import (
"context" "context"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/json" "github.com/sagernet/sing/common/json"
) )
@@ -60,37 +61,40 @@ func checkOptions(options *Options) error {
func checkInbounds(inbounds []Inbound) error { func checkInbounds(inbounds []Inbound) error {
seen := make(map[string]bool) seen := make(map[string]bool)
for _, inbound := range inbounds { for i, inbound := range inbounds {
if inbound.Tag == "" { tag := inbound.Tag
continue if tag == "" {
tag = F.ToString(i)
} }
if seen[inbound.Tag] { if seen[tag] {
return E.New("duplicate inbound tag: ", inbound.Tag) return E.New("duplicate inbound tag: ", tag)
} }
seen[inbound.Tag] = true seen[tag] = true
} }
return nil return nil
} }
func checkOutbounds(outbounds []Outbound, endpoints []Endpoint) error { func checkOutbounds(outbounds []Outbound, endpoints []Endpoint) error {
seen := make(map[string]bool) seen := make(map[string]bool)
for _, outbound := range outbounds { for i, outbound := range outbounds {
if outbound.Tag == "" { tag := outbound.Tag
continue if tag == "" {
tag = F.ToString(i)
} }
if seen[outbound.Tag] { if seen[tag] {
return E.New("duplicate outbound/endpoint tag: ", outbound.Tag) return E.New("duplicate outbound/endpoint tag: ", tag)
} }
seen[outbound.Tag] = true seen[tag] = true
} }
for _, endpoint := range endpoints { for i, endpoint := range endpoints {
if endpoint.Tag == "" { tag := endpoint.Tag
continue if tag == "" {
tag = F.ToString(i)
} }
if seen[endpoint.Tag] { if seen[tag] {
return E.New("duplicate outbound/endpoint tag: ", endpoint.Tag) return E.New("duplicate outbound/endpoint tag: ", tag)
} }
seen[endpoint.Tag] = true seen[tag] = true
} }
return nil return nil
} }