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

View File

@ -77,7 +77,11 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
parsed, parseErr = logic.NewNOT(payload, target, ParseRule) parsed, parseErr = logic.NewNOT(payload, target, ParseRule)
case "RULE-SET": case "RULE-SET":
noResolve := RC.HasNoResolve(params) 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": case "MATCH":
parsed = RC.NewMatch(target) parsed = RC.NewMatch(target)
parseErr = nil parseErr = nil

View File

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