Skip to content

Commit 38fd371

Browse files
committed
feat: GEOIP,IP-ASN,IP-CIDR,IP-CIDR6 and IP-SUFFIX in rules support ,src option
keep the same writing style as `RULE-SET`
1 parent 763a127 commit 38fd371

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

rules/common/base.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import (
88

99
var (
1010
errPayload = errors.New("payloadRule error")
11-
noResolve = "no-resolve"
12-
src = "src"
11+
)
12+
13+
// params
14+
var (
15+
NoResolve = "no-resolve"
16+
Src = "src"
1317
)
1418

1519
type Base struct {
@@ -25,10 +29,12 @@ func (b *Base) ShouldResolveIP() bool {
2529

2630
func (b *Base) ProviderNames() []string { return nil }
2731

28-
func HasNoResolve(params []string) bool {
29-
return slices.Contains(params, noResolve)
30-
}
31-
32-
func HasSrc(params []string) bool {
33-
return slices.Contains(params, src)
32+
func ParseParams(params []string) (isSrc bool, noResolve bool) {
33+
isSrc = slices.Contains(params, Src)
34+
if isSrc {
35+
noResolve = true
36+
} else {
37+
noResolve = slices.Contains(params, NoResolve)
38+
}
39+
return
3440
}

rules/parser.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
2222
case "GEOSITE":
2323
parsed, parseErr = RC.NewGEOSITE(payload, target)
2424
case "GEOIP":
25-
noResolve := RC.HasNoResolve(params)
26-
parsed, parseErr = RC.NewGEOIP(payload, target, false, noResolve)
25+
isSrc, noResolve := RC.ParseParams(params)
26+
parsed, parseErr = RC.NewGEOIP(payload, target, isSrc, noResolve)
2727
case "SRC-GEOIP":
2828
parsed, parseErr = RC.NewGEOIP(payload, target, true, true)
2929
case "IP-ASN":
30-
noResolve := RC.HasNoResolve(params)
31-
parsed, parseErr = RC.NewIPASN(payload, target, false, noResolve)
30+
isSrc, noResolve := RC.ParseParams(params)
31+
parsed, parseErr = RC.NewIPASN(payload, target, isSrc, noResolve)
3232
case "SRC-IP-ASN":
3333
parsed, parseErr = RC.NewIPASN(payload, target, true, true)
3434
case "IP-CIDR", "IP-CIDR6":
35-
noResolve := RC.HasNoResolve(params)
36-
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRNoResolve(noResolve))
35+
isSrc, noResolve := RC.ParseParams(params)
36+
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(isSrc), RC.WithIPCIDRNoResolve(noResolve))
3737
case "SRC-IP-CIDR":
3838
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
3939
case "IP-SUFFIX":
40-
noResolve := RC.HasNoResolve(params)
41-
parsed, parseErr = RC.NewIPSuffix(payload, target, false, noResolve)
40+
isSrc, noResolve := RC.ParseParams(params)
41+
parsed, parseErr = RC.NewIPSuffix(payload, target, isSrc, noResolve)
4242
case "SRC-IP-SUFFIX":
4343
parsed, parseErr = RC.NewIPSuffix(payload, target, true, true)
4444
case "SRC-PORT":
@@ -76,11 +76,7 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
7676
case "NOT":
7777
parsed, parseErr = logic.NewNOT(payload, target, ParseRule)
7878
case "RULE-SET":
79-
noResolve := RC.HasNoResolve(params)
80-
isSrc := RC.HasSrc(params)
81-
if isSrc {
82-
noResolve = true
83-
}
79+
isSrc, noResolve := RC.ParseParams(params)
8480
parsed, parseErr = RP.NewRuleSet(payload, target, isSrc, noResolve)
8581
case "MATCH":
8682
parsed = RC.NewMatch(target)

0 commit comments

Comments
 (0)