Skip to content

Commit 763a127

Browse files
committed
feat: RULE-SET in rules support ,src option
should only be used with `ipcidr` behavior
1 parent a96f72a commit 763a127

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ func parseIPRuleSet(domainSetName string, adapterName string, ruleProviders map[
18021802
default:
18031803
}
18041804
}
1805-
return RP.NewRuleSet(domainSetName, adapterName, true)
1805+
return RP.NewRuleSet(domainSetName, adapterName, false, true)
18061806
}
18071807

18081808
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
18171817
default:
18181818
}
18191819
}
1820-
return RP.NewRuleSet(domainSetName, adapterName, true)
1820+
return RP.NewRuleSet(domainSetName, adapterName, false, true)
18211821
}

constant/metadata.go

+7
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,10 @@ func (m *Metadata) SetRemoteAddress(rawAddress string) error {
302302

303303
return nil
304304
}
305+
306+
func (m *Metadata) SwapSrcDst() {
307+
m.SrcIP, m.DstIP = m.DstIP, m.SrcIP
308+
m.SrcPort, m.DstPort = m.DstPort, m.SrcPort
309+
m.SrcIPASN, m.DstIPASN = m.DstIPASN, m.SrcIPASN
310+
m.SrcGeoIP, m.DstGeoIP = m.DstGeoIP, m.SrcGeoIP
311+
}

rules/common/base.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package common
22

33
import (
44
"errors"
5+
6+
"golang.org/x/exp/slices"
57
)
68

79
var (
810
errPayload = errors.New("payloadRule error")
911
noResolve = "no-resolve"
12+
src = "src"
1013
)
1114

1215
type Base struct {
@@ -23,10 +26,9 @@ func (b *Base) ShouldResolveIP() bool {
2326
func (b *Base) ProviderNames() []string { return nil }
2427

2528
func HasNoResolve(params []string) bool {
26-
for _, p := range params {
27-
if p == noResolve {
28-
return true
29-
}
30-
}
31-
return false
29+
return slices.Contains(params, noResolve)
30+
}
31+
32+
func HasSrc(params []string) bool {
33+
return slices.Contains(params, src)
3234
}

rules/parser.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
7777
parsed, parseErr = logic.NewNOT(payload, target, ParseRule)
7878
case "RULE-SET":
7979
noResolve := RC.HasNoResolve(params)
80-
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
80+
isSrc := RC.HasSrc(params)
81+
if isSrc {
82+
noResolve = true
83+
}
84+
parsed, parseErr = RP.NewRuleSet(payload, target, isSrc, noResolve)
8185
case "MATCH":
8286
parsed = RC.NewMatch(target)
8387
parseErr = nil

rules/provider/rule_set.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type RuleSet struct {
1212
*common.Base
1313
ruleProviderName string
1414
adapter string
15+
isSrc bool
1516
noResolveIP bool
1617
shouldFindProcess bool
1718
}
@@ -32,6 +33,10 @@ func (rs *RuleSet) RuleType() C.RuleType {
3233

3334
func (rs *RuleSet) Match(metadata *C.Metadata) (bool, string) {
3435
if provider, ok := rs.getProvider(); ok {
36+
if rs.isSrc {
37+
metadata.SwapSrcDst()
38+
defer metadata.SwapSrcDst()
39+
}
3540
return provider.Match(metadata), rs.adapter
3641
}
3742
return false, ""
@@ -76,11 +81,12 @@ func (rs *RuleSet) getProvider() (P.RuleProvider, bool) {
7681
return pp, ok
7782
}
7883

79-
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
84+
func NewRuleSet(ruleProviderName string, adapter string, isSrc bool, noResolveIP bool) (*RuleSet, error) {
8085
rs := &RuleSet{
8186
Base: &common.Base{},
8287
ruleProviderName: ruleProviderName,
8388
adapter: adapter,
89+
isSrc: isSrc,
8490
noResolveIP: noResolveIP,
8591
}
8692
return rs, nil

0 commit comments

Comments
 (0)