Fix rule-set matching logic

This commit is contained in:
世界 2023-11-30 21:28:00 +08:00
parent efdb34de91
commit 392c4be55b
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
2 changed files with 52 additions and 44 deletions

View File

@ -49,6 +49,13 @@ type InboundContext struct {
FakeIP bool FakeIP bool
IPCIDRMatchSource bool IPCIDRMatchSource bool
// rule merge
SourceAddressMatch bool
SourcePortMatch bool
DestinationAddressMatch bool
DestinationPortMatch bool
// dns cache // dns cache
QueryType uint16 QueryType uint16

View File

@ -17,6 +17,7 @@ type abstractDefaultRule struct {
destinationAddressItems []RuleItem destinationAddressItems []RuleItem
destinationPortItems []RuleItem destinationPortItems []RuleItem
allItems []RuleItem allItems []RuleItem
ruleSetItem RuleItem
invert bool invert bool
outbound string outbound string
} }
@ -62,62 +63,62 @@ func (r *abstractDefaultRule) Match(metadata *adapter.InboundContext) bool {
return true return true
} }
if len(r.sourceAddressItems) > 0 && !metadata.SourceAddressMatch {
for _, item := range r.sourceAddressItems {
if item.Match(metadata) {
metadata.SourceAddressMatch = true
break
}
}
}
if len(r.sourcePortItems) > 0 && !metadata.SourceAddressMatch {
for _, item := range r.sourcePortItems {
if item.Match(metadata) {
metadata.SourcePortMatch = true
break
}
}
}
if len(r.destinationAddressItems) > 0 && !metadata.SourceAddressMatch {
for _, item := range r.destinationAddressItems {
if item.Match(metadata) {
metadata.DestinationAddressMatch = true
break
}
}
}
if len(r.destinationPortItems) > 0 && !metadata.SourceAddressMatch {
for _, item := range r.destinationPortItems {
if item.Match(metadata) {
metadata.DestinationPortMatch = true
break
}
}
}
for _, item := range r.items { for _, item := range r.items {
if !item.Match(metadata) { if !item.Match(metadata) {
return r.invert return r.invert
} }
} }
if len(r.sourceAddressItems) > 0 { if len(r.sourceAddressItems) > 0 && !metadata.SourceAddressMatch {
var sourceAddressMatch bool return r.invert
for _, item := range r.sourceAddressItems {
if item.Match(metadata) {
sourceAddressMatch = true
break
}
}
if !sourceAddressMatch {
return r.invert
}
} }
if len(r.sourcePortItems) > 0 { if len(r.sourcePortItems) > 0 && !metadata.SourcePortMatch {
var sourcePortMatch bool return r.invert
for _, item := range r.sourcePortItems {
if item.Match(metadata) {
sourcePortMatch = true
break
}
}
if !sourcePortMatch {
return r.invert
}
} }
if len(r.destinationAddressItems) > 0 { if len(r.destinationAddressItems) > 0 && !metadata.DestinationAddressMatch {
var destinationAddressMatch bool return r.invert
for _, item := range r.destinationAddressItems {
if item.Match(metadata) {
destinationAddressMatch = true
break
}
}
if !destinationAddressMatch {
return r.invert
}
} }
if len(r.destinationPortItems) > 0 { if len(r.destinationPortItems) > 0 && !metadata.DestinationPortMatch {
var destinationPortMatch bool return r.invert
for _, item := range r.destinationPortItems {
if item.Match(metadata) {
destinationPortMatch = true
break
}
}
if !destinationPortMatch {
return r.invert
}
} }
return !r.invert return !r.invert