mirror of https://github.com/v2ray/v2ray-core
refactor
parent
5dbfd75542
commit
a0ac334703
|
@ -245,8 +245,14 @@ type UserMatcher struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserMatcher(users []string) *UserMatcher {
|
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{
|
return &UserMatcher{
|
||||||
user: users,
|
user: usersCopy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,8 +274,14 @@ type InboundTagMatcher struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
|
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{
|
return &InboundTagMatcher{
|
||||||
tags: tags,
|
tags: tagsCopy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,16 +12,16 @@ type Rule struct {
|
||||||
Condition Condition
|
Condition Condition
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Rule) Apply(ctx context.Context) bool {
|
func (r *Rule) Apply(ctx context.Context) bool {
|
||||||
return v.Condition.Apply(ctx)
|
return r.Condition.Apply(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *RoutingRule) BuildCondition() (Condition, error) {
|
func (rr *RoutingRule) BuildCondition() (Condition, error) {
|
||||||
conds := NewConditionChan()
|
conds := NewConditionChan()
|
||||||
|
|
||||||
if len(v.Domain) > 0 {
|
if len(rr.Domain) > 0 {
|
||||||
anyCond := NewAnyCondition()
|
anyCond := NewAnyCondition()
|
||||||
for _, domain := range v.Domain {
|
for _, domain := range rr.Domain {
|
||||||
if domain.Type == Domain_Plain {
|
if domain.Type == Domain_Plain {
|
||||||
anyCond.Add(NewPlainDomainMatcher(domain.Value))
|
anyCond.Add(NewPlainDomainMatcher(domain.Value))
|
||||||
} else {
|
} else {
|
||||||
|
@ -35,12 +35,12 @@ func (v *RoutingRule) BuildCondition() (Condition, error) {
|
||||||
conds.Add(anyCond)
|
conds.Add(anyCond)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.Cidr) > 0 {
|
if len(rr.Cidr) > 0 {
|
||||||
ipv4Net := v2net.NewIPNet()
|
ipv4Net := v2net.NewIPNet()
|
||||||
ipv6Cond := NewAnyCondition()
|
ipv6Cond := NewAnyCondition()
|
||||||
hasIpv6 := false
|
hasIpv6 := false
|
||||||
|
|
||||||
for _, ip := range v.Cidr {
|
for _, ip := range rr.Cidr {
|
||||||
switch len(ip.Ip) {
|
switch len(ip.Ip) {
|
||||||
case net.IPv4len:
|
case net.IPv4len:
|
||||||
ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
|
ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
|
||||||
|
@ -68,20 +68,20 @@ func (v *RoutingRule) BuildCondition() (Condition, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.PortRange != nil {
|
if rr.PortRange != nil {
|
||||||
conds.Add(NewPortMatcher(*v.PortRange))
|
conds.Add(NewPortMatcher(*rr.PortRange))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.NetworkList != nil {
|
if rr.NetworkList != nil {
|
||||||
conds.Add(NewNetworkMatcher(v.NetworkList))
|
conds.Add(NewNetworkMatcher(rr.NetworkList))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.SourceCidr) > 0 {
|
if len(rr.SourceCidr) > 0 {
|
||||||
ipv4Net := v2net.NewIPNet()
|
ipv4Net := v2net.NewIPNet()
|
||||||
ipv6Cond := NewAnyCondition()
|
ipv6Cond := NewAnyCondition()
|
||||||
hasIpv6 := false
|
hasIpv6 := false
|
||||||
|
|
||||||
for _, ip := range v.SourceCidr {
|
for _, ip := range rr.SourceCidr {
|
||||||
switch len(ip.Ip) {
|
switch len(ip.Ip) {
|
||||||
case net.IPv4len:
|
case net.IPv4len:
|
||||||
ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
|
ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
|
||||||
|
@ -109,12 +109,12 @@ func (v *RoutingRule) BuildCondition() (Condition, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.UserEmail) > 0 {
|
if len(rr.UserEmail) > 0 {
|
||||||
conds.Add(NewUserMatcher(v.UserEmail))
|
conds.Add(NewUserMatcher(rr.UserEmail))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.InboundTag) > 0 {
|
if len(rr.InboundTag) > 0 {
|
||||||
conds.Add(NewInboundTagMatcher(v.InboundTag))
|
conds.Add(NewInboundTagMatcher(rr.InboundTag))
|
||||||
}
|
}
|
||||||
|
|
||||||
if conds.Len() == 0 {
|
if conds.Len() == 0 {
|
||||||
|
|
Loading…
Reference in New Issue