diff --git a/app/router/condition.go b/app/router/condition.go index 79225b72c..bf9121459 100644 --- a/app/router/condition.go +++ b/app/router/condition.go @@ -245,8 +245,14 @@ type UserMatcher struct { } func NewUserMatcher(users []string) *UserMatcher { + usersCopy := make([]string, 0, len(users)) + for _, user := range users { + if len(user) > 0 { + usersCopy = append(usersCopy, user) + } + } return &UserMatcher{ - user: users, + user: usersCopy, } } @@ -268,8 +274,14 @@ type InboundTagMatcher struct { } func NewInboundTagMatcher(tags []string) *InboundTagMatcher { + tagsCopy := make([]string, 0, len(tags)) + for _, tag := range tags { + if len(tag) > 0 { + tagsCopy = append(tagsCopy, tag) + } + } return &InboundTagMatcher{ - tags: tags, + tags: tagsCopy, } } diff --git a/app/router/config.go b/app/router/config.go index 4b2a8f3fe..bcb6c2dd2 100644 --- a/app/router/config.go +++ b/app/router/config.go @@ -12,16 +12,16 @@ type Rule struct { Condition Condition } -func (v *Rule) Apply(ctx context.Context) bool { - return v.Condition.Apply(ctx) +func (r *Rule) Apply(ctx context.Context) bool { + return r.Condition.Apply(ctx) } -func (v *RoutingRule) BuildCondition() (Condition, error) { +func (rr *RoutingRule) BuildCondition() (Condition, error) { conds := NewConditionChan() - if len(v.Domain) > 0 { + if len(rr.Domain) > 0 { anyCond := NewAnyCondition() - for _, domain := range v.Domain { + for _, domain := range rr.Domain { if domain.Type == Domain_Plain { anyCond.Add(NewPlainDomainMatcher(domain.Value)) } else { @@ -35,12 +35,12 @@ func (v *RoutingRule) BuildCondition() (Condition, error) { conds.Add(anyCond) } - if len(v.Cidr) > 0 { + if len(rr.Cidr) > 0 { ipv4Net := v2net.NewIPNet() ipv6Cond := NewAnyCondition() hasIpv6 := false - for _, ip := range v.Cidr { + for _, ip := range rr.Cidr { switch len(ip.Ip) { case net.IPv4len: ipv4Net.AddIP(ip.Ip, byte(ip.Prefix)) @@ -68,20 +68,20 @@ func (v *RoutingRule) BuildCondition() (Condition, error) { } } - if v.PortRange != nil { - conds.Add(NewPortMatcher(*v.PortRange)) + if rr.PortRange != nil { + conds.Add(NewPortMatcher(*rr.PortRange)) } - if v.NetworkList != nil { - conds.Add(NewNetworkMatcher(v.NetworkList)) + if rr.NetworkList != nil { + conds.Add(NewNetworkMatcher(rr.NetworkList)) } - if len(v.SourceCidr) > 0 { + if len(rr.SourceCidr) > 0 { ipv4Net := v2net.NewIPNet() ipv6Cond := NewAnyCondition() hasIpv6 := false - for _, ip := range v.SourceCidr { + for _, ip := range rr.SourceCidr { switch len(ip.Ip) { case net.IPv4len: ipv4Net.AddIP(ip.Ip, byte(ip.Prefix)) @@ -109,12 +109,12 @@ func (v *RoutingRule) BuildCondition() (Condition, error) { } } - if len(v.UserEmail) > 0 { - conds.Add(NewUserMatcher(v.UserEmail)) + if len(rr.UserEmail) > 0 { + conds.Add(NewUserMatcher(rr.UserEmail)) } - if len(v.InboundTag) > 0 { - conds.Add(NewInboundTagMatcher(v.InboundTag)) + if len(rr.InboundTag) > 0 { + conds.Add(NewInboundTagMatcher(rr.InboundTag)) } if conds.Len() == 0 {