-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrules.go
263 lines (234 loc) · 5.9 KB
/
rules.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package main
import (
"net/mail"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/tidwall/match"
)
type MatchType string
type MatchField string
type ActionType string
const (
MATCH_ALL MatchType = "all"
MATCH_LITERAL MatchType = "literal"
MATCH_REGEX MatchType = "regex"
MATCH_TIME_AFTER MatchType = "timeAfter"
FIELD_TO MatchField = "to"
FIELD_FROM MatchField = "from"
FIELD_SUBJECT MatchField = "subject"
ACTION_DROP ActionType = "drop"
ACTION_FORWARD ActionType = "forward"
ACTION_WEBHOOK ActionType = "webhook"
)
type Match struct {
Type MatchType `json:"type" yaml:"type"`
Field MatchField `json:"field" yaml:"field"`
Value string `json:"value" yaml:"value"`
}
// For Webhook the Action value is: [endpoint, secret token]
type Action struct {
Type ActionType `json:"type" yaml:"type"`
Value []string `json:"value" yaml:"value"`
}
type RuleId string
type Rule struct {
Id RuleId `json:"id" yaml:"id"`
Type int `json:"type" yaml:"type"`
Match []Match `json:"match" yaml:"match"`
Action []Action `json:"action" yaml:"action"`
}
type DomainRules struct {
Rules []Rule `json:"rules" yaml:"rules"`
}
type ActionDrop struct {
DroppedRule bool
}
type ActionSend struct {
Email Email
To string
}
type ActionWebhook struct {
Email Email
Endpoint string
SecretToken string
}
type ActionChans struct {
send chan ActionSend
drop chan ActionDrop
webhook chan ActionWebhook
error chan error
}
func MakeActionChans() ActionChans {
return ActionChans{
send: make(chan ActionSend),
drop: make(chan ActionDrop),
webhook: make(chan ActionWebhook),
error: make(chan error),
}
}
func (chans *ActionChans) Close() {
close(chans.send)
close(chans.drop)
close(chans.webhook)
close(chans.error)
}
func (chans *ActionChans) Error(e error) {
chans.error <- e
chans.Close()
}
func parseAddresses(v string) ([]string, error) {
e, err := mail.ParseAddressList(v)
if err != nil {
return []string{}, errors.Wrapf(err, "failed to parse %s", v)
}
out := make([]string, len(e))
for i, addr := range e {
out[i] = addr.Address
}
return out, nil
}
func getFieldRaw(field MatchField, email Email) ([]string, error) {
switch field {
case FIELD_TO:
if to := email.Data.Header.Get("To"); to != "" {
e, err := parseAddresses(to)
if err != nil || len(e) == 0 {
log.Warnf("failed to parse header `to` %s", to)
} else {
return e, nil
}
}
// FIXME: support multiple to?
to := email.Envelope.To[0]
e, err := parseAddresses(to)
if err != nil {
return []string{},
errors.Wrapf(err, "failed to envelope `to` %s", to)
}
return e, nil
case FIELD_FROM:
if from := email.Data.Header.Get("From"); from != "" {
e, err := parseAddresses(from)
if err != nil || len(e) == 0 {
log.Warnf("failed to parse header `from` %s", from)
} else {
return e, nil
}
}
e, err := parseAddresses(email.Envelope.From)
if err != nil {
return []string{},
errors.Wrapf(err, "failed to envelope `from` %s", email.Envelope.From)
}
return e, nil
case FIELD_SUBJECT:
subject := email.Data.Header.Get("Subject")
e := []string{subject}
return e, nil
}
return []string{}, errors.Errorf("field %s not supported\n", field)
}
func getField(field MatchField, email Email) ([]string, error) {
values, err := getFieldRaw(field, email)
if err != nil {
return nil, err
}
for i, value := range values {
values[i] = strings.ToLower(value)
}
return values, nil
}
func HasMatch(predicates []Match, email Email) (bool, error) {
for _, predicate := range predicates {
switch predicate.Type {
case MATCH_ALL:
// ok
case MATCH_TIME_AFTER:
now := time.Now().UnixNano() / 1e6
end, err := strconv.ParseInt(predicate.Value, 10, 64)
if err != nil {
return false, errors.Wrap(err, "could not parse int")
}
if end > now {
log.Debugf("%d > %d", end, now)
return false, nil
} else {
log.Debugf("%d < %d", end, now)
}
case MATCH_REGEX:
vs, err := getField(predicate.Field, email)
if err != nil {
return false, errors.Wrap(err, "failed to match regex")
}
for _, v := range vs {
if !match.Match(v, predicate.Value) {
log.Debugf("%s != %s", v, predicate.Value)
return false, nil
} else {
log.Debugf("%s ~= %s", v, predicate.Value)
// once matched, exit the loop now
break
}
}
case MATCH_LITERAL:
vs, err := getField(predicate.Field, email)
if err != nil {
return false, errors.Wrap(err, "failed to match literal")
}
for _, v := range vs {
if v != predicate.Value {
log.Debugf("%s != %s", v, predicate.Value)
return false, nil
} else {
log.Debugf("%s == %s", v, predicate.Value)
// once matched, exit the loop now
break
}
}
default:
return false, errors.Errorf("action %s isn't supported\n", predicate)
}
}
return true, nil
}
func ApplyRules(rules []Rule, email Email, chans ActionChans) (*RuleId, error) {
for _, rule := range rules {
match, err := HasMatch(rule.Match, email)
if err != nil {
chans.Error(err)
return nil, err
}
if match {
for _, action := range rule.Action {
switch action.Type {
case ACTION_DROP:
chans.drop <- ActionDrop{DroppedRule: true}
case ACTION_WEBHOOK:
if len(action.Value) != 2 {
return nil, errors.Errorf(
"invalid webhook configuration, expected 2 params got %d", len(action.Value))
}
chans.webhook <- ActionWebhook{
Email: email,
Endpoint: action.Value[0],
SecretToken: action.Value[1],
}
case ACTION_FORWARD:
for _, to := range action.Value {
chans.send <- ActionSend{Email: email, To: to}
}
default:
e := errors.Errorf("action %s isn't supported\n", action)
chans.Error(e)
return nil, e
}
}
return &rule.Id, nil
}
}
chans.drop <- ActionDrop{DroppedRule: false}
return nil, nil
}