-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.go
177 lines (130 loc) · 3.6 KB
/
chain.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
//go:generate stringer -type=instantDecision
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/vasyahuyasa/botassasin/log"
"gopkg.in/yaml.v2"
)
const (
decisionNone instantDecision = iota
decisionBan
decisionWhitelist
checkerField = "checker"
scoreField = "score"
scoreCheckerName = "score"
)
type instantDecision int
type harmScore int
type checker interface {
Check(*logLine) (harm harmScore, descision instantDecision)
}
type checkerWithKind struct {
checker
kind string
}
type chain struct {
reportFn reportCheckerWorkTime
checkers []*checkerWithKind
}
type reportCheckerWorkTime func(name string, seconds float64)
func newChainFromConfig(cfg config, reportFn reportCheckerWorkTime) (*chain, error) {
var checkers []*checkerWithKind
for _, checkerCfg := range cfg.Checkers {
c, err := checkerFromConfig(checkerCfg)
if err != nil {
return nil, fmt.Errorf("cannot create checker: %w", err)
}
checkers = append(checkers, c)
}
return &chain{
reportFn: reportFn,
checkers: checkers,
}, nil
}
func (c *chain) NeedBan(l *logLine) bool {
score := harmScore(0)
for _, chk := range c.checkers {
startedAt := time.Now()
s, decision := chk.Check(l)
c.reportFn(chk.kind, time.Since(startedAt).Seconds())
log.Debugf("%s %s score: %d decision: %s", l.IP(), chk.kind, s, decision)
if decision == decisionNone {
score += s
continue
}
l.Set(checkerField, chk.kind)
l.Set(scoreField, strconv.Itoa(int(score)))
return decision == decisionBan
}
log.Debugf("%s total score: %d", l.IP(), score)
l.Set(checkerField, scoreCheckerName)
l.Set(scoreField, strconv.Itoa(int(score)))
return score > 0
}
func checkerFromConfig(cfg checkerConfig) (*checkerWithKind, error) {
var kindOnly struct {
Kind string
}
err := unmarshalConfig(cfg, &kindOnly)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal checker config: %w", err)
}
switch strings.ToLower(kindOnly.Kind) {
case "geoip":
c := geoIPConfig{}
err = unmarshalConfig(cfg, &c)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal GeoIP checker config: %w", err)
}
gi, err := newGeoIPChecker(c)
if err != nil {
return nil, fmt.Errorf("cannot create GeoIP checker: %w", err)
}
return &checkerWithKind{checker: gi, kind: "geoip"}, nil
case "list":
c := listCheckerConfig{}
err = unmarshalConfig(cfg, &c)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal list checker config: %w", err)
}
list, err := newListChecker(c)
if err != nil {
return nil, fmt.Errorf("cannot create list checker: %w", err)
}
return &checkerWithKind{checker: list, kind: "list"}, nil
case "field":
c := fieldCheckerConfig{}
err = unmarshalConfig(cfg, &c)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal field checker config: %w", err)
}
field, err := newFieldChecker(c)
if err != nil {
return nil, fmt.Errorf("cannot create field checker: %w", err)
}
return &checkerWithKind{checker: field, kind: "field"}, nil
case "reverse_dns":
c := reverseDNSCheckerConfig{}
err = unmarshalConfig(cfg, &c)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal reverse DNS checker config: %w", err)
}
rdns, err := newReverseDNSChecker(c)
if err != nil {
return nil, fmt.Errorf("cannot create reverse DNS checker: %w", err)
}
return &checkerWithKind{checker: rdns, kind: "reverse_dns"}, nil
default:
return nil, fmt.Errorf("unknown checker %q", kindOnly.Kind)
}
}
func unmarshalConfig(basic, specified interface{}) error {
b, err := yaml.Marshal(basic)
if err != nil {
return err
}
return yaml.Unmarshal(b, specified)
}