-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnobots.go
154 lines (138 loc) · 3.08 KB
/
nobots.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
package nobots
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
// botUA config representation
type botUA struct {
uas []string
bomb string
re []*regexp.Regexp
public []*regexp.Regexp
}
// BotUA plugin struct
type BotUA struct {
Next httpserver.Handler
UA *botUA
}
func init() {
caddy.RegisterPlugin("nobots", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// setup callback for parsing the config
func setup(c *caddy.Controller) error {
ua, err := parseUA(c)
if err != nil {
return err
}
// Verfies whether bomb exist
if _, err := os.Stat(ua.bomb); os.IsNotExist(err) {
return fmt.Errorf("Bomb %s not found.", ua.bomb)
}
// Setup de middleware
cfg := httpserver.GetConfig(c)
mid := func(next httpserver.Handler) httpserver.Handler {
return BotUA{Next: next, UA: ua}
}
cfg.AddMiddleware(mid)
return nil
}
// parseUA propper config parser that generates a botUA object
func parseUA(c *caddy.Controller) (*botUA, error) {
var ua botUA
for c.Next() {
if !c.NextArg() {
return nil, c.ArgErr()
}
ua.bomb = c.Val()
for c.NextBlock() {
switch c.Val() {
case "regexp":
if !c.NextArg() {
return nil, c.ArgErr()
}
re, err := regexp.Compile(c.Val())
if err != nil {
return nil, fmt.Errorf("%s", err)
}
ua.re = append(ua.re, re)
case "public":
if !c.NextArg() {
return nil, c.ArgErr()
}
re, err := regexp.Compile(c.Val())
if err != nil {
return nil, fmt.Errorf("%s", err)
}
ua.public = append(ua.public, re)
default:
ua.uas = append(ua.uas, c.Val())
}
}
}
return &ua, nil
}
func (b BotUA) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// Get request UA
rua := r.UserAgent()
// Avoid ban UA for public URI
if !b.IsPublicURI(r.URL.Path) {
// Check if the UA is a evil one
if b.IsEvil(rua) {
return serveBomb(w, r, b.UA.bomb)
}
}
// Nothing happens carry on with next stuff
return b.Next.ServeHTTP(w, r)
}
// IsEvil check the remote UA against evil UAs
func (b BotUA) IsEvil(rua string) bool {
// In case there are regexp
if len(b.UA.re) > 0 {
for _, re := range b.UA.re {
if re.MatchString(rua) {
return true
}
}
}
// In case there are strings
if len(b.UA.uas) > 0 {
for _, ua := range b.UA.uas {
if ua == rua {
return true
}
}
}
// UA is not evil
return false
}
// IsPublicURI check if the requested URI is defined as public or not
func (b BotUA) IsPublicURI(uri string) bool {
if len(b.UA.public) > 0 {
for _, re := range b.UA.public {
if re.MatchString(uri) {
return true
}
}
}
return false
}
// serveBomb provides the bomb to front-end
func serveBomb(w http.ResponseWriter, r *http.Request, bomb string) (int, error) {
file, err := ioutil.ReadFile(bomb)
if err != nil {
return http.StatusNotFound, nil
}
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(file)))
w.Write(file)
return 200, nil
}