-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbotblocker.go
227 lines (195 loc) · 5.43 KB
/
botblocker.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
package traefik_ultimate_bad_bot_blocker
import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"net/netip"
"strings"
"time"
log "github.com/discoverygarden/traefik-ultimate-bad-bot-blocker/utils"
)
type Config struct {
IpBlocklistUrls []string `json:"ipblocklisturls,omitempty"`
UserAgentBlocklistUrls []string `json:"useragentblocklisturls,omitempty"`
LogLevel string `json:"loglevel,omitempty"`
}
func CreateConfig() *Config {
return &Config{
IpBlocklistUrls: []string{},
UserAgentBlocklistUrls: []string{},
LogLevel: "INFO",
}
}
type BotBlocker struct {
next http.Handler
name string
prefixBlocklist []netip.Prefix
userAgentBlockList []string
lastUpdated time.Time
Config
}
func (b *BotBlocker) update() error {
startTime := time.Now()
err := b.updateIps()
if err != nil {
return fmt.Errorf("failed to update CIDR blocklists: %w", err)
}
err = b.updateUserAgents()
if err != nil {
return fmt.Errorf("failed to update user agent blocklists: %w", err)
}
b.lastUpdated = time.Now()
duration := time.Since(startTime)
log.Info("Updated block lists. Blocked CIDRs: ", len(b.prefixBlocklist), " Duration: ", duration)
return nil
}
func (b *BotBlocker) updateIps() error {
prefixBlockList := make([]netip.Prefix, 0)
log.Info("Updating CIDR blocklist")
for _, url := range b.IpBlocklistUrls {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("failed fetch CIDR list: %w", err)
}
if resp.StatusCode > 299 {
return fmt.Errorf("failed to fetch CIDR list: received a %v from %v", resp.Status, url)
}
prefixes, err := readPrefixes(resp.Body)
if err != nil {
return fmt.Errorf("failed to update CIDRs: %e", err)
}
prefixBlockList = append(prefixBlockList, prefixes...)
}
b.prefixBlocklist = prefixBlockList
return nil
}
func readPrefixes(prefixReader io.ReadCloser) ([]netip.Prefix, error) {
prefixes := make([]netip.Prefix, 0)
defer prefixReader.Close()
scanner := bufio.NewScanner(prefixReader)
for scanner.Scan() {
entry := strings.TrimSpace(scanner.Text())
var prefix netip.Prefix
if strings.Contains(entry, "/") {
var err error
prefix, err = netip.ParsePrefix(entry)
if err != nil {
return []netip.Prefix{}, err
}
} else {
addr, err := netip.ParseAddr(entry)
if err != nil {
return []netip.Prefix{}, err
}
var bits int
if addr.Is4() {
bits = 32
} else {
bits = 128
}
prefix, err = addr.Prefix(bits)
if err != nil {
return []netip.Prefix{}, err
}
}
prefixes = append(prefixes, prefix)
}
return prefixes, nil
}
func readUserAgents(userAgentReader io.ReadCloser) ([]string, error) {
userAgents := make([]string, 0)
defer userAgentReader.Close()
scanner := bufio.NewScanner(userAgentReader)
for scanner.Scan() {
agent := strings.ToLower(strings.TrimSpace(scanner.Text()))
userAgents = append(userAgents, agent)
}
return userAgents, nil
}
func (b *BotBlocker) updateUserAgents() error {
userAgentBlockList := make([]string, 0)
log.Info("Updating user agent blocklist")
for _, url := range b.UserAgentBlocklistUrls {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("failed fetch useragent list: %w", err)
}
if resp.StatusCode > 299 {
return fmt.Errorf("failed fetch useragent list: received a %v from %v", resp.Status, url)
}
agents, err := readUserAgents(resp.Body)
if err != nil {
return err
}
userAgentBlockList = append(userAgentBlockList, agents...)
}
b.userAgentBlockList = userAgentBlockList
return nil
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
logLevel, err := log.ParseLevel(config.LogLevel)
if err != nil {
return nil, fmt.Errorf("failed to set log level: %w", err)
}
log.Default().Level = logLevel
blocker := BotBlocker{
name: name,
next: next,
Config: *config,
}
err = blocker.update()
if err != nil {
return nil, fmt.Errorf("failed to update blocklists: %s", err)
}
return &blocker, nil
}
func (b *BotBlocker) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if time.Since(b.lastUpdated) > time.Hour {
err := b.update()
if err != nil {
log.Errorf("failed to update blocklist: %v", err)
}
}
startTime := time.Now()
log.Debugf("Checking request: CIDR: \"%v\" user agent: \"%s\"", req.RemoteAddr, req.UserAgent())
timer := func() {
log.Debugf("Checked request in %v", time.Since(startTime))
}
defer timer()
remoteAddrPort, err := netip.ParseAddrPort(req.RemoteAddr)
if err != nil {
http.Error(rw, "internal error", http.StatusInternalServerError)
return
}
if b.shouldBlockIp(remoteAddrPort.Addr()) {
log.Infof("blocked request with from IP %v", remoteAddrPort.Addr())
http.Error(rw, "blocked", http.StatusForbidden)
return
}
agent := strings.ToLower(req.UserAgent())
if b.shouldBlockAgent(agent) {
log.Infof("blocked request with user agent %v because it contained %v", agent, agent)
http.Error(rw, "blocked", http.StatusForbidden)
return
}
b.next.ServeHTTP(rw, req)
}
func (b *BotBlocker) shouldBlockIp(addr netip.Addr) bool {
for _, badPrefix := range b.prefixBlocklist {
if badPrefix.Contains(addr) {
return true
}
}
return false
}
func (b *BotBlocker) shouldBlockAgent(userAgent string) bool {
userAgent = strings.ToLower(strings.TrimSpace(userAgent))
for _, badAgent := range b.userAgentBlockList {
if strings.Contains(userAgent, badAgent) {
return true
}
}
return false
}