feat: RULE-SET in rules support ,src option

should only be used with `ipcidr` behavior
This commit is contained in:
wwqgtxx 2024-08-29 23:49:16 +08:00
parent a96f72ade4
commit 763a127287
5 changed files with 29 additions and 10 deletions

View File

@ -1802,7 +1802,7 @@ func parseIPRuleSet(domainSetName string, adapterName string, ruleProviders map[
default:
}
}
return RP.NewRuleSet(domainSetName, adapterName, true)
return RP.NewRuleSet(domainSetName, adapterName, false, true)
}
func parseDomainRuleSet(domainSetName string, adapterName string, ruleProviders map[string]providerTypes.RuleProvider) (C.DomainMatcher, error) {
@ -1817,5 +1817,5 @@ func parseDomainRuleSet(domainSetName string, adapterName string, ruleProviders
default:
}
}
return RP.NewRuleSet(domainSetName, adapterName, true)
return RP.NewRuleSet(domainSetName, adapterName, false, true)
}

View File

@ -302,3 +302,10 @@ func (m *Metadata) SetRemoteAddress(rawAddress string) error {
return nil
}
func (m *Metadata) SwapSrcDst() {
m.SrcIP, m.DstIP = m.DstIP, m.SrcIP
m.SrcPort, m.DstPort = m.DstPort, m.SrcPort
m.SrcIPASN, m.DstIPASN = m.DstIPASN, m.SrcIPASN
m.SrcGeoIP, m.DstGeoIP = m.DstGeoIP, m.SrcGeoIP
}

View File

@ -2,11 +2,14 @@ package common
import (
"errors"
"golang.org/x/exp/slices"
)
var (
errPayload = errors.New("payloadRule error")
noResolve = "no-resolve"
src = "src"
)
type Base struct {
@ -23,10 +26,9 @@ func (b *Base) ShouldResolveIP() bool {
func (b *Base) ProviderNames() []string { return nil }
func HasNoResolve(params []string) bool {
for _, p := range params {
if p == noResolve {
return true
}
}
return false
return slices.Contains(params, noResolve)
}
func HasSrc(params []string) bool {
return slices.Contains(params, src)
}

View File

@ -77,7 +77,11 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
parsed, parseErr = logic.NewNOT(payload, target, ParseRule)
case "RULE-SET":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
isSrc := RC.HasSrc(params)
if isSrc {
noResolve = true
}
parsed, parseErr = RP.NewRuleSet(payload, target, isSrc, noResolve)
case "MATCH":
parsed = RC.NewMatch(target)
parseErr = nil

View File

@ -12,6 +12,7 @@ type RuleSet struct {
*common.Base
ruleProviderName string
adapter string
isSrc bool
noResolveIP bool
shouldFindProcess bool
}
@ -32,6 +33,10 @@ func (rs *RuleSet) RuleType() C.RuleType {
func (rs *RuleSet) Match(metadata *C.Metadata) (bool, string) {
if provider, ok := rs.getProvider(); ok {
if rs.isSrc {
metadata.SwapSrcDst()
defer metadata.SwapSrcDst()
}
return provider.Match(metadata), rs.adapter
}
return false, ""
@ -76,11 +81,12 @@ func (rs *RuleSet) getProvider() (P.RuleProvider, bool) {
return pp, ok
}
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
func NewRuleSet(ruleProviderName string, adapter string, isSrc bool, noResolveIP bool) (*RuleSet, error) {
rs := &RuleSet{
Base: &common.Base{},
ruleProviderName: ruleProviderName,
adapter: adapter,
isSrc: isSrc,
noResolveIP: noResolveIP,
}
return rs, nil