-
Notifications
You must be signed in to change notification settings - Fork 34
/
rule_group.go
147 lines (132 loc) · 2.86 KB
/
rule_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package rules
import (
"github.com/TeaWeb/code/teawaf/requests"
)
// rule group
type RuleGroup struct {
Id string `yaml:"id" json:"id"`
On bool `yaml:"on" json:"on"`
Name string `yaml:"name" json:"name"` // such as SQL Injection
Description string `yaml:"description" json:"description"`
Code string `yaml:"code" json:"code"` // identify the group
RuleSets []*RuleSet `yaml:"ruleSets" json:"ruleSets"`
IsInbound bool `yaml:"isInbound" json:"isInbound"`
hasRuleSets bool
}
func NewRuleGroup() *RuleGroup {
return &RuleGroup{
On: true,
}
}
func (this *RuleGroup) Init() error {
this.hasRuleSets = len(this.RuleSets) > 0
if this.hasRuleSets {
for _, set := range this.RuleSets {
err := set.Init()
if err != nil {
return err
}
}
}
return nil
}
func (this *RuleGroup) AddRuleSet(ruleSet *RuleSet) {
this.RuleSets = append(this.RuleSets, ruleSet)
}
func (this *RuleGroup) FindRuleSet(id string) *RuleSet {
if len(id) == 0 {
return nil
}
for _, ruleSet := range this.RuleSets {
if ruleSet.Id == id {
return ruleSet
}
}
return nil
}
func (this *RuleGroup) FindRuleSetWithCode(code string) *RuleSet {
if len(code) == 0 {
return nil
}
for _, ruleSet := range this.RuleSets {
if ruleSet.Code == code {
return ruleSet
}
}
return nil
}
func (this *RuleGroup) RemoveRuleSet(id string) {
if len(id) == 0 {
return
}
result := []*RuleSet{}
for _, ruleSet := range this.RuleSets {
if ruleSet.Id == id {
continue
}
result = append(result, ruleSet)
}
this.RuleSets = result
}
func (this *RuleGroup) MatchRequest(req *requests.Request) (b bool, set *RuleSet, err error) {
if !this.hasRuleSets {
return
}
for _, set := range this.RuleSets {
if !set.On {
continue
}
b, err = set.MatchRequest(req)
if err != nil {
return false, nil, err
}
if b {
return true, set, nil
}
}
return
}
func (this *RuleGroup) MatchResponse(req *requests.Request, resp *requests.Response) (b bool, set *RuleSet, err error) {
if !this.hasRuleSets {
return
}
for _, set := range this.RuleSets {
if !set.On {
continue
}
b, err = set.MatchResponse(req, resp)
if err != nil {
return false, nil, err
}
if b {
return true, set, nil
}
}
return
}
func (this *RuleGroup) MoveRuleSet(fromIndex int, toIndex int) {
if fromIndex < 0 || fromIndex >= len(this.RuleSets) {
return
}
if toIndex < 0 || toIndex >= len(this.RuleSets) {
return
}
if fromIndex == toIndex {
return
}
location := this.RuleSets[fromIndex]
result := []*RuleSet{}
for i := 0; i < len(this.RuleSets); i ++ {
if i == fromIndex {
continue
}
if fromIndex > toIndex && i == toIndex {
result = append(result, location)
}
result = append(result, this.RuleSets[i])
if fromIndex < toIndex && i == toIndex {
result = append(result, location)
}
}
this.RuleSets = result
}