Skip to content

Commit

Permalink
feat: RULE-SET in rules support ,src option
Browse files Browse the repository at this point in the history
should only be used with `ipcidr` behavior
  • Loading branch information
wwqgtxx committed Aug 29, 2024
1 parent a96f72a commit 763a127
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
7 changes: 7 additions & 0 deletions constant/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 8 additions & 6 deletions rules/common/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
6 changes: 5 additions & 1 deletion rules/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion rules/provider/rule_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type RuleSet struct {
*common.Base
ruleProviderName string
adapter string
isSrc bool
noResolveIP bool
shouldFindProcess bool
}
Expand All @@ -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, ""
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 763a127

Please sign in to comment.