feat: support sub-rule, eg.

rules:
  - SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
  - SUB-RULE,(GEOIP,!CN),TEST1
  - MATCH,DIRECT

sub-rules:
  TEST2:
    - MATCH,Proxy
  TEST1:
    - RULE-SET,Local,DIRECT,no-resolve
    - GEOSITE,CN,Domestic
    - GEOIP,CN,Domestic
    - MATCH,Proxy
This commit is contained in:
adlyq
2022-09-06 17:30:35 +08:00
parent a9694fcdc0
commit 9b89ff9f2d
28 changed files with 325 additions and 105 deletions

View File

@ -5,7 +5,7 @@ import (
)
var (
errPayload = errors.New("payload error")
errPayload = errors.New("payloadRule error")
initFlag bool
noResolve = "no-resolve"
)

View File

@ -18,11 +18,11 @@ func (d *Domain) RuleType() C.RuleType {
return C.Domain
}
func (d *Domain) Match(metadata *C.Metadata) bool {
func (d *Domain) Match(metadata *C.Metadata) (bool, string) {
if metadata.AddrType != C.AtypDomainName {
return false
return false, ""
}
return metadata.Host == d.domain
return metadata.Host == d.domain, d.adapter
}
func (d *Domain) Adapter() string {
@ -47,4 +47,4 @@ func NewDomain(domain string, adapter string) *Domain {
}
}
var _ C.Rule = (*Domain)(nil)
//var _ C.Rule = (*Domain)(nil)

View File

@ -18,12 +18,12 @@ func (dk *DomainKeyword) RuleType() C.RuleType {
return C.DomainKeyword
}
func (dk *DomainKeyword) Match(metadata *C.Metadata) bool {
func (dk *DomainKeyword) Match(metadata *C.Metadata) (bool, string) {
if metadata.AddrType != C.AtypDomainName {
return false
return false, ""
}
domain := metadata.Host
return strings.Contains(domain, dk.keyword)
return strings.Contains(domain, dk.keyword), dk.adapter
}
func (dk *DomainKeyword) Adapter() string {
@ -48,4 +48,4 @@ func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
}
}
var _ C.Rule = (*DomainKeyword)(nil)
//var _ C.Rule = (*DomainKeyword)(nil)

View File

@ -18,12 +18,12 @@ func (ds *DomainSuffix) RuleType() C.RuleType {
return C.DomainSuffix
}
func (ds *DomainSuffix) Match(metadata *C.Metadata) bool {
func (ds *DomainSuffix) Match(metadata *C.Metadata) (bool, string) {
if metadata.AddrType != C.AtypDomainName {
return false
return false, ""
}
domain := metadata.Host
return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix
return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix, ds.adapter
}
func (ds *DomainSuffix) Adapter() string {
@ -48,4 +48,4 @@ func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
}
}
var _ C.Rule = (*DomainSuffix)(nil)
//var _ C.Rule = (*DomainSuffix)(nil)

View File

@ -13,8 +13,8 @@ func (f *Match) RuleType() C.RuleType {
return C.MATCH
}
func (f *Match) Match(metadata *C.Metadata) bool {
return true
func (f *Match) Match(metadata *C.Metadata) (bool, string) {
return true, f.adapter
}
func (f *Match) Adapter() string {
@ -32,4 +32,4 @@ func NewMatch(adapter string) *Match {
}
}
var _ C.Rule = (*Match)(nil)
//var _ C.Rule = (*Match)(nil)

View File

@ -25,10 +25,10 @@ func (g *GEOIP) RuleType() C.RuleType {
return C.GEOIP
}
func (g *GEOIP) Match(metadata *C.Metadata) bool {
func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
ip := metadata.DstIP
if !ip.IsValid() {
return false
return false, ""
}
if strings.EqualFold(g.country, "LAN") {
@ -37,13 +37,13 @@ func (g *GEOIP) Match(metadata *C.Metadata) bool {
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
resolver.IsFakeBroadcastIP(ip)
resolver.IsFakeBroadcastIP(ip), g.adapter
}
if !C.GeodataMode {
record, _ := mmdb.Instance().Country(ip.AsSlice())
return strings.EqualFold(record.Country.IsoCode, g.country)
return strings.EqualFold(record.Country.IsoCode, g.country), g.adapter
}
return g.geoIPMatcher.Match(ip.AsSlice())
return g.geoIPMatcher.Match(ip.AsSlice()), g.adapter
}
func (g *GEOIP) Adapter() string {
@ -98,4 +98,4 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
return geoip, nil
}
var _ C.Rule = (*GEOIP)(nil)
//var _ C.Rule = (*GEOIP)(nil)

View File

@ -23,13 +23,13 @@ func (gs *GEOSITE) RuleType() C.RuleType {
return C.GEOSITE
}
func (gs *GEOSITE) Match(metadata *C.Metadata) bool {
func (gs *GEOSITE) Match(metadata *C.Metadata) (bool, string) {
if metadata.AddrType != C.AtypDomainName {
return false
return false, ""
}
domain := metadata.Host
return gs.matcher.ApplyDomain(domain)
return gs.matcher.ApplyDomain(domain), gs.adapter
}
func (gs *GEOSITE) Adapter() string {
@ -75,4 +75,4 @@ func NewGEOSITE(country string, adapter string) (*GEOSITE, error) {
return geoSite, nil
}
var _ C.Rule = (*GEOSITE)(nil)
//var _ C.Rule = (*GEOSITE)(nil)

View File

@ -13,13 +13,13 @@ type InType struct {
payload string
}
func (u *InType) Match(metadata *C.Metadata) bool {
func (u *InType) Match(metadata *C.Metadata) (bool, string) {
for _, tp := range u.types {
if metadata.Type == tp {
return true
return true, u.adapter
}
}
return false
return false, ""
}
func (u *InType) RuleType() C.RuleType {

View File

@ -35,12 +35,12 @@ func (i *IPCIDR) RuleType() C.RuleType {
return C.IPCIDR
}
func (i *IPCIDR) Match(metadata *C.Metadata) bool {
func (i *IPCIDR) Match(metadata *C.Metadata) (bool, string) {
ip := metadata.DstIP
if i.isSourceIP {
ip = metadata.SrcIP
}
return ip.IsValid() && i.ipnet.Contains(ip)
return ip.IsValid() && i.ipnet.Contains(ip), i.adapter
}
func (i *IPCIDR) Adapter() string {
@ -74,4 +74,4 @@ func NewIPCIDR(s string, adapter string, opts ...IPCIDROption) (*IPCIDR, error)
return ipcidr, nil
}
var _ C.Rule = (*IPCIDR)(nil)
//var _ C.Rule = (*IPCIDR)(nil)

View File

@ -22,7 +22,7 @@ func (is *IPSuffix) RuleType() C.RuleType {
return C.IPSuffix
}
func (is *IPSuffix) Match(metadata *C.Metadata) bool {
func (is *IPSuffix) Match(metadata *C.Metadata) (bool, string) {
ip := metadata.DstIP
if is.isSourceIP {
ip = metadata.SrcIP
@ -30,7 +30,7 @@ func (is *IPSuffix) Match(metadata *C.Metadata) bool {
mIPBytes := ip.AsSlice()
if len(is.ipBytes) != len(mIPBytes) {
return false
return false, ""
}
size := len(mIPBytes)
@ -38,15 +38,15 @@ func (is *IPSuffix) Match(metadata *C.Metadata) bool {
for i := bits / 8; i > 0; i-- {
if is.ipBytes[size-i] != mIPBytes[size-i] {
return false
return false, ""
}
}
if (is.ipBytes[size-bits/8-1] << (8 - bits%8)) != (mIPBytes[size-bits/8-1] << (8 - bits%8)) {
return false
return false, ""
}
return true
return true, is.adapter
}
func (is *IPSuffix) Adapter() string {

View File

@ -36,8 +36,8 @@ func (n *NetworkType) RuleType() C.RuleType {
return C.Network
}
func (n *NetworkType) Match(metadata *C.Metadata) bool {
return n.network == metadata.NetWork
func (n *NetworkType) Match(metadata *C.Metadata) (bool, string) {
return n.network == metadata.NetWork, n.adapter
}
func (n *NetworkType) Adapter() string {

View File

@ -24,11 +24,11 @@ func (p *Port) RuleType() C.RuleType {
return C.DstPort
}
func (p *Port) Match(metadata *C.Metadata) bool {
func (p *Port) Match(metadata *C.Metadata) (bool, string) {
if p.isSource {
return p.matchPortReal(metadata.SrcPort)
return p.matchPortReal(metadata.SrcPort), p.adapter
}
return p.matchPortReal(metadata.DstPort)
return p.matchPortReal(metadata.DstPort), p.adapter
}
func (p *Port) Adapter() string {

View File

@ -17,12 +17,12 @@ func (ps *Process) RuleType() C.RuleType {
return C.Process
}
func (ps *Process) Match(metadata *C.Metadata) bool {
func (ps *Process) Match(metadata *C.Metadata) (bool, string) {
if ps.nameOnly {
return strings.EqualFold(metadata.Process, ps.process)
return strings.EqualFold(metadata.Process, ps.process), ps.adapter
}
return strings.EqualFold(metadata.ProcessPath, ps.process)
return strings.EqualFold(metadata.ProcessPath, ps.process), ps.adapter
}
func (ps *Process) Adapter() string {

View File

@ -71,10 +71,10 @@ func (u *Uid) RuleType() C.RuleType {
return C.Uid
}
func (u *Uid) Match(metadata *C.Metadata) bool {
func (u *Uid) Match(metadata *C.Metadata) (bool, string) {
srcPort, err := strconv.ParseUint(metadata.SrcPort, 10, 16)
if err != nil {
return false
return false, ""
}
var uid int32
if metadata.Uid != nil {
@ -83,15 +83,15 @@ func (u *Uid) Match(metadata *C.Metadata) bool {
metadata.Uid = &uid
} else {
log.Warnln("[UID] could not get uid from %s", metadata.String())
return false
return false, ""
}
for _, _uid := range u.uids {
if _uid.Contains(uid) {
return true
return true, u.adapter
}
}
return false
return false, ""
}
func (u *Uid) Adapter() string {

View File

@ -20,9 +20,9 @@ func (A *AND) ShouldFindProcess() bool {
}
func NewAND(payload string, adapter string,
parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) (*AND, error) {
parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) (*AND, error) {
and := &AND{Base: &common.Base{}, payload: payload, adapter: adapter}
rules, err := parseRuleByPayload(payload, parse)
rules, err := ParseRuleByPayload(payload, parse)
if err != nil {
return nil, err
}
@ -45,14 +45,14 @@ func (A *AND) RuleType() C.RuleType {
return C.AND
}
func (A *AND) Match(metadata *C.Metadata) bool {
func (A *AND) Match(metadata *C.Metadata) (bool, string) {
for _, rule := range A.rules {
if !rule.Match(metadata) {
return false
if m, _ := rule.Match(metadata); !m {
return false, ""
}
}
return true
return true, A.adapter
}
func (A *AND) Adapter() string {

View File

@ -9,7 +9,7 @@ import (
_ "unsafe"
)
func parseRuleByPayload(payload string, parseRule func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) ([]C.Rule, error) {
func ParseRuleByPayload(payload string, parseRule func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) ([]C.Rule, error) {
regex, err := regexp.Compile("\\(.*\\)")
if err != nil {
return nil, err
@ -59,13 +59,13 @@ func payloadToRule(subPayload string, parseRule func(tp, payload, target string,
return parseRule(tp, param[0], "", param[1:])
}
func parseLogicSubRule(parseRule func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
func parseLogicSubRule(parseRule func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
return func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
switch tp {
case "MATCH":
return nil, fmt.Errorf("unsupported rule type on logic rule")
case "MATCH", "SUB-RULE":
return nil, fmt.Errorf("unsupported rule type [%s] on logic rule", tp)
default:
return parseRule(tp, payload, target, params)
return parseRule(tp, payload, target, params, nil)
}
}
}

View File

@ -3,13 +3,15 @@ package logic
import (
"fmt"
"github.com/Dreamacro/clash/constant"
C "github.com/Dreamacro/clash/constant"
RC "github.com/Dreamacro/clash/rules/common"
RP "github.com/Dreamacro/clash/rules/provider"
"github.com/Dreamacro/clash/rules/sub_rule"
"github.com/stretchr/testify/assert"
"testing"
)
func ParseRule(tp, payload, target string, params []string) (parsed constant.Rule, parseErr error) {
func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed constant.Rule, parseErr error) {
switch tp {
case "DOMAIN":
parsed = RC.NewDomain(payload, target)
@ -46,6 +48,8 @@ func ParseRule(tp, payload, target string, params []string) (parsed constant.Rul
parsed, parseErr = RC.NewUid(payload, target)
case "IN-TYPE":
parsed, parseErr = RC.NewInType(payload, target)
case "SUB-RULE":
parsed, parseErr = sub_rule.NewSubRule(payload, target, subRules, ParseRule)
case "AND":
parsed, parseErr = NewAND(payload, target, ParseRule)
case "OR":
@ -54,7 +58,7 @@ func ParseRule(tp, payload, target string, params []string) (parsed constant.Rul
parsed, parseErr = NewNOT(payload, target, ParseRule)
case "RULE-SET":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve, ParseRule)
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
case "MATCH":
parsed = RC.NewMatch(target)
parseErr = nil
@ -70,12 +74,13 @@ func TestAND(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, "DIRECT", and.adapter)
assert.Equal(t, false, and.ShouldResolveIP())
assert.Equal(t, true, and.Match(&constant.Metadata{
m, _ := and.Match(&constant.Metadata{
Host: "baidu.com",
AddrType: constant.AtypDomainName,
NetWork: constant.TCP,
DstPort: "20000",
}))
})
assert.Equal(t, true, m)
and, err = NewAND("(DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule)
assert.NotEqual(t, nil, err)
@ -87,9 +92,10 @@ func TestAND(t *testing.T) {
func TestNOT(t *testing.T) {
not, err := NewNOT("((DST-PORT,6000-6500))", "REJECT", ParseRule)
assert.Equal(t, nil, err)
assert.Equal(t, false, not.Match(&constant.Metadata{
m, _ := not.Match(&constant.Metadata{
DstPort: "6100",
}))
})
assert.Equal(t, false, m)
_, err = NewNOT("((DST-PORT,5600-6666),(DOMAIN,baidu.com))", "DIRECT", ParseRule)
assert.NotEqual(t, nil, err)
@ -101,8 +107,9 @@ func TestNOT(t *testing.T) {
func TestOR(t *testing.T) {
or, err := NewOR("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule)
assert.Equal(t, nil, err)
assert.Equal(t, true, or.Match(&constant.Metadata{
m, _ := or.Match(&constant.Metadata{
NetWork: constant.TCP,
}))
})
assert.Equal(t, true, m)
assert.Equal(t, false, or.ShouldResolveIP())
}

View File

@ -17,9 +17,9 @@ func (not *NOT) ShouldFindProcess() bool {
return false
}
func NewNOT(payload string, adapter string, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) (*NOT, error) {
func NewNOT(payload string, adapter string, parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) (*NOT, error) {
not := &NOT{Base: &common.Base{}, adapter: adapter}
rule, err := parseRuleByPayload(payload, parse)
rule, err := ParseRuleByPayload(payload, parse)
if err != nil {
return nil, err
}
@ -38,8 +38,16 @@ func (not *NOT) RuleType() C.RuleType {
return C.NOT
}
func (not *NOT) Match(metadata *C.Metadata) bool {
return not.rule == nil || !not.rule.Match(metadata)
func (not *NOT) Match(metadata *C.Metadata) (bool, string) {
if not.rule == nil {
return true, not.adapter
}
if m, _ := not.rule.Match(metadata); m {
return true, not.adapter
}
return false, ""
}
func (not *NOT) Adapter() string {

View File

@ -23,14 +23,14 @@ func (or *OR) RuleType() C.RuleType {
return C.OR
}
func (or *OR) Match(metadata *C.Metadata) bool {
func (or *OR) Match(metadata *C.Metadata) (bool, string) {
for _, rule := range or.rules {
if rule.Match(metadata) {
return true
if m, _ := rule.Match(metadata); m {
return true, or.adapter
}
}
return false
return false, ""
}
func (or *OR) Adapter() string {
@ -45,9 +45,9 @@ func (or *OR) ShouldResolveIP() bool {
return or.needIP
}
func NewOR(payload string, adapter string, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) (*OR, error) {
func NewOR(payload string, adapter string, parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) (*OR, error) {
or := &OR{Base: &common.Base{}, payload: payload, adapter: adapter}
rules, err := parseRuleByPayload(payload, parse)
rules, err := ParseRuleByPayload(payload, parse)
if err != nil {
return nil, err
}

View File

@ -6,9 +6,10 @@ import (
RC "github.com/Dreamacro/clash/rules/common"
"github.com/Dreamacro/clash/rules/logic"
RP "github.com/Dreamacro/clash/rules/provider"
"github.com/Dreamacro/clash/rules/sub_rule"
)
func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error) {
switch tp {
case "DOMAIN":
parsed = RC.NewDomain(payload, target)
@ -45,6 +46,8 @@ func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, pars
parsed, parseErr = RC.NewUid(payload, target)
case "IN-TYPE":
parsed, parseErr = RC.NewInType(payload, target)
case "SUB-RULE":
parsed, parseErr = sub_rule.NewSubRule(payload, target, subRules, ParseRule)
case "AND":
parsed, parseErr = logic.NewAND(payload, target, ParseRule)
case "OR":
@ -53,7 +56,7 @@ func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, pars
parsed, parseErr = logic.NewNOT(payload, target, ParseRule)
case "RULE-SET":
noResolve := RC.HasNoResolve(params)
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve, ParseRule)
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
case "MATCH":
parsed = RC.NewMatch(target)
parseErr = nil

View File

@ -16,7 +16,7 @@ type classicalStrategy struct {
func (c *classicalStrategy) Match(metadata *C.Metadata) bool {
for _, rule := range c.rules {
if rule.Match(metadata) {
if m, _ := rule.Match(metadata); m {
return true
}
}
@ -66,13 +66,13 @@ func ruleParse(ruleRaw string) (string, string, []string) {
return "", "", nil
}
func NewClassicalStrategy(parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) *classicalStrategy {
func NewClassicalStrategy(parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) *classicalStrategy {
return &classicalStrategy{rules: []C.Rule{}, parse: func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
switch tp {
case "MATCH":
case "MATCH", "SUB-RULE":
return nil, fmt.Errorf("unsupported rule type on rule-set")
default:
return parse(tp, payload, target, params)
return parse(tp, payload, target, params, nil)
}
}}
}

View File

@ -17,7 +17,7 @@ type ruleProviderSchema struct {
Interval int `provider:"interval,omitempty"`
}
func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) (P.RuleProvider, error) {
func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) (P.RuleProvider, error) {
schema := &ruleProviderSchema{}
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
if err := decoder.Decode(mapping, schema); err != nil {

View File

@ -103,7 +103,7 @@ func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
}
func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration, vehicle P.Vehicle,
parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) P.RuleProvider {
parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) P.RuleProvider {
rp := &ruleSetProvider{
behavior: behavior,
}
@ -126,7 +126,7 @@ func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration
return wrapper
}
func newStrategy(behavior P.RuleType, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) ruleStrategy {
func newStrategy(behavior P.RuleType, parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) ruleStrategy {
switch behavior {
case P.Domain:
strategy := NewDomainStrategy()

View File

@ -23,8 +23,8 @@ func (rs *RuleSet) RuleType() C.RuleType {
return C.RuleSet
}
func (rs *RuleSet) Match(metadata *C.Metadata) bool {
return rs.getProviders().Match(metadata)
func (rs *RuleSet) Match(metadata *C.Metadata) (bool, string) {
return rs.getProviders().Match(metadata), rs.adapter
}
func (rs *RuleSet) Adapter() string {
@ -47,7 +47,7 @@ func (rs *RuleSet) getProviders() P.RuleProvider {
return rs.ruleProvider
}
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool, parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)) (*RuleSet, error) {
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
rp, ok := RuleProviders()[ruleProviderName]
if !ok {
return nil, fmt.Errorf("rule set %s not found", ruleProviderName)

View File

@ -0,0 +1,91 @@
package sub_rule
import (
"fmt"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/rules/common"
"github.com/Dreamacro/clash/rules/logic"
)
type SubRule struct {
*common.Base
payload string
payloadRule C.Rule
subName string
subRules *map[string][]C.Rule
shouldFindProcess *bool
shouldResolveIP *bool
}
func NewSubRule(payload, subName string, sub *map[string][]C.Rule,
parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) (*SubRule, error) {
payloadRule, err := logic.ParseRuleByPayload(fmt.Sprintf("(%s)", payload), parse)
if err != nil {
return nil, err
}
if len(payloadRule) != 1 {
return nil, fmt.Errorf("Sub-Rule rule must contain one rule")
}
return &SubRule{
Base: &common.Base{},
payload: payload,
payloadRule: payloadRule[0],
subName: subName,
subRules: sub,
}, nil
}
func (r *SubRule) RuleType() C.RuleType {
return C.SubRules
}
func (r *SubRule) Match(metadata *C.Metadata) (bool, string) {
return match(metadata, r.subName, r.subRules)
}
func match(metadata *C.Metadata, name string, subRules *map[string][]C.Rule) (bool, string) {
for _, rule := range (*subRules)[name] {
if m, a := rule.Match(metadata); m {
if rule.RuleType() == C.SubRules {
match(metadata, rule.Adapter(), subRules)
} else {
return m, a
}
}
}
return false, ""
}
func (r *SubRule) ShouldResolveIP() bool {
if r.shouldResolveIP == nil {
s := false
for _, rule := range (*r.subRules)[r.subName] {
s = s || rule.ShouldResolveIP()
}
r.shouldResolveIP = &s
}
return *r.shouldResolveIP
}
func (r *SubRule) ShouldFindProcess() bool {
if r.shouldFindProcess == nil {
s := false
for _, rule := range (*r.subRules)[r.subName] {
s = s || rule.ShouldFindProcess()
}
r.shouldFindProcess = &s
}
return *r.shouldFindProcess
}
func (r *SubRule) Adapter() string {
return r.subName
}
func (r *SubRule) Payload() string {
return r.payload
}