-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker_list.go
263 lines (207 loc) · 5.52 KB
/
checker_list.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 (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/netip"
"strings"
"time"
"github.com/vasyahuyasa/botassasin/log"
"github.com/yl2chen/cidranger"
"go4.org/netipx"
)
const (
listCheckerActionWhitelist listCheckerAction = iota
listCheckerActionBlock
listCheckerSrcTypeTxt = "txt"
listCheckerSrcTypeAWSIpRanges = "aws_ip_ranges"
httpRequestTimeout = time.Second * 5
)
var (
listCheckerActionMap = map[string]listCheckerAction{
"whitelist": listCheckerActionWhitelist,
"block": listCheckerActionBlock,
}
_ checker = &listChecker{}
ipMaskFull = net.IPMask{0xff, 0xff, 0xff, 0xff}
)
type listCheckerAction int
type listCheckerSrcConfig struct {
Src string `yaml:"src"`
Type string `yaml:"type"`
Action string `yaml:"action"`
AwsServiceFilter []string `yaml:"aws_service_filter"`
}
type listCheckerConfig struct {
Sources []listCheckerSrcConfig
}
// ipList naive ip map implementation with stdlib net.IPNet
// not used at the momnet, only benchmark
type ipList struct {
ips []*net.IPNet
action listCheckerAction
}
// ipList2 ip map implementation with cidranger package
// not used at the momnet, only benchmark
type ipList2 struct {
ranger cidranger.Ranger
action listCheckerAction
}
// ipList3 ip map implementation with netipx.IPSet package
// most perfomant at the momnet
type ipList3 struct {
ipset *netipx.IPSet
action listCheckerAction
}
type listChecker struct {
lists []ipList
}
func newListChecker(cfg listCheckerConfig) (*listChecker, error) {
var lists []ipList
for _, srcCfg := range cfg.Sources {
action, ok := listCheckerActionMap[srcCfg.Action]
if !ok {
return nil, fmt.Errorf("unknow action %q (supported: whitelist, block)", srcCfg.Action)
}
data, err := bytesFromSrc(srcCfg.Src)
if err != nil {
return nil, fmt.Errorf("cannot get %s list %q: %w", srcCfg.Type, srcCfg.Src, err)
}
switch srcCfg.Type {
case listCheckerSrcTypeTxt:
ips := parseTxt(data)
lists = append(lists, ipList{
ips: ips,
action: action,
})
log.Printf("list %s (%s) created with %d rules action = %s", srcCfg.Type, srcCfg.Src, len(ips), srcCfg.Action)
case listCheckerSrcTypeAWSIpRanges:
ips, err := parseAWSIpRanges(data, srcCfg.AwsServiceFilter)
if err != nil {
return nil, fmt.Errorf("cannot parse aws_ip_ranges: %w", err)
}
lists = append(lists, ipList{
ips: ips,
action: action,
})
log.Printf("list %s (%s) created with %d rules action = %s filter = %v", srcCfg.Type, srcCfg.Src, len(ips), srcCfg.Action, srcCfg.AwsServiceFilter)
default:
return nil, fmt.Errorf("unknown source type %q (supported types %v)", srcCfg.Type, []string{listCheckerSrcTypeTxt, listCheckerSrcTypeAWSIpRanges})
}
}
return &listChecker{
lists: lists,
}, nil
}
func (c *listChecker) Check(l *logLine) (score harmScore, descision instantDecision) {
for _, list := range c.lists {
if list.contains(l.IP()) {
if list.action == listCheckerActionWhitelist {
return 0, decisionWhitelist
}
return 0, decisionBan
}
}
return 0, decisionNone
}
func (list *ipList) contains(ip net.IP) bool {
for _, ipnet := range list.ips {
if ipnet.Contains(ip) {
return true
}
}
return false
}
func (list *ipList2) contains(ip net.IP) bool {
ok, _ := list.ranger.Contains(ip)
return ok
}
func (list *ipList3) contains(ip netip.Addr) bool {
return list.ipset.Contains(ip)
}
func bytesFromSrc(src string) ([]byte, error) {
// TODO: file cache
// read file over network
if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
client := http.Client{
Timeout: httpRequestTimeout,
}
res, err := client.Get(src)
if err != nil {
return nil, fmt.Errorf("cannot perform GET query to %q: %w", src, err)
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("cannot read from %q: %w", src, err)
}
return data, nil
}
// read local file
data, err := ioutil.ReadFile(src)
if err != nil {
return nil, fmt.Errorf("cannot read from %q: %w", src, err)
}
return data, nil
}
func parseTxt(data []byte) []*net.IPNet {
scanner := bufio.NewScanner(bytes.NewBuffer(data))
var ips []*net.IPNet
for scanner.Scan() {
// remove comments and trim
str := strings.TrimSpace(strings.Split(scanner.Text(), "#")[0])
ipnet, err := parseIPorCIDR(str)
if err != nil {
log.Printf("cannot parse %q: %v", str, err)
continue
}
ips = append(ips, ipnet)
}
return ips
}
func parseAWSIpRanges(data []byte, filter []string) ([]*net.IPNet, error) {
// some field are ommited
type awsIpRanges struct {
Prefixes []struct {
IpPrefix string `json:"ip_prefix"`
Service string `json:"service"`
} `json:"prefixes"`
}
var ranges awsIpRanges
err := json.Unmarshal(data, &ranges)
if err != nil {
return nil, fmt.Errorf("cannot unmarshal aws ip range data: %w", err)
}
var ips []*net.IPNet
for _, r := range ranges.Prefixes {
if strInSlice(r.Service, filter) {
ipnet, err := parseIPorCIDR(r.IpPrefix)
if err != nil {
log.Printf("cannot parse %q: %v", r.IpPrefix, err)
continue
}
ips = append(ips, ipnet)
}
}
return ips, nil
}
func strInSlice(str string, all []string) bool {
for _, v := range all {
if v == str {
return true
}
}
return false
}
func parseIPorCIDR(ip string) (*net.IPNet, error) {
if strings.IndexByte(ip, '/') != -1 {
_, ipNet, err := net.ParseCIDR(ip)
return ipNet, err
}
ip4 := net.ParseIP(ip)
return &net.IPNet{IP: ip4, Mask: ipMaskFull}, nil
}