Darien Raymond 8 tahun lalu
induk
melakukan
a0ac334703
2 mengubah file dengan 31 tambahan dan 19 penghapusan
  1. 14 2
      app/router/condition.go
  2. 17 17
      app/router/config.go

+ 14 - 2
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,
 	}
 }
 

+ 17 - 17
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 {