forked from agence-gaya/traefik-plugin-blockuseragent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockuseragent.go
More file actions
166 lines (140 loc) · 3.97 KB
/
Copy pathblockuseragent.go
File metadata and controls
166 lines (140 loc) · 3.97 KB
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
// Package traefik_plugin_blockuseragent a plugin to block User-Agent.
package useragent_block_traefik
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
)
// Config holds the plugin configuration.
type Config struct {
RegexAllow []string `json:"regexAllow,omitempty"`
Regex []string `json:"regex,omitempty"`
PathRegex []string `json:"pathRegex,omitempty"`
StatusCode int `json:"statusCode,omitempty"`
ResponseMessage string `json:"responseMessage,omitempty"`
}
// CreateConfig creates and initializes the plugin configuration.
func CreateConfig() *Config {
return &Config{
RegexAllow: make([]string, 0),
Regex: make([]string, 0),
PathRegex: make([]string, 0),
StatusCode: http.StatusForbidden,
ResponseMessage: "",
}
}
// BlockUserAgent struct.
type BlockUserAgent struct {
name string
next http.Handler
regexpsAllow []*regexp.Regexp
regexpsDeny []*regexp.Regexp
pathRegexps []*regexp.Regexp
statusCode int
responseMessage string
}
// BlockUserAgentMessage struct.
type BlockUserAgentMessage struct {
Regex int `json:"regex"`
UserAgent string `json:"user-agent"`
RemoteAddr string `json:"ip"`
Host string `json:"host"`
RequestURI string `json:"uri"`
}
// New creates and returns a plugin instance.
func New(_ context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
regexpsAllow := make([]*regexp.Regexp, len(config.RegexAllow))
regexpsDeny := make([]*regexp.Regexp, len(config.Regex))
pathRegexps := make([]*regexp.Regexp, len(config.PathRegex))
for index, regex := range config.RegexAllow {
re, err := regexp.Compile(regex)
if err != nil {
return nil, fmt.Errorf("error compiling regexAllow %q: %w", regex, err)
}
regexpsAllow[index] = re
}
for index, regex := range config.Regex {
re, err := regexp.Compile(regex)
if err != nil {
return nil, fmt.Errorf("error compiling regex %q: %w", regex, err)
}
regexpsDeny[index] = re
}
for index, regex := range config.PathRegex {
re, err := regexp.Compile(regex)
if err != nil {
return nil, fmt.Errorf("error compiling pathRegex %q: %w", regex, err)
}
pathRegexps[index] = re
}
statusCode := config.StatusCode
if statusCode == 0 {
statusCode = http.StatusForbidden
}
return &BlockUserAgent{
name: name,
next: next,
regexpsAllow: regexpsAllow,
regexpsDeny: regexpsDeny,
pathRegexps: pathRegexps,
statusCode: statusCode,
responseMessage: config.ResponseMessage,
}, nil
}
func (b *BlockUserAgent) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if req != nil {
// Check path-based blocking first
for _, re := range b.pathRegexps {
if re.MatchString(req.URL.Path) {
message := &BlockUserAgentMessage{
Regex: -1,
UserAgent: req.UserAgent(),
RemoteAddr: req.RemoteAddr,
Host: req.Host,
RequestURI: req.RequestURI,
}
jsonMessage, err := json.Marshal(message)
if err == nil {
log.Printf("%s: blocked path: %s", b.name, jsonMessage)
}
res.WriteHeader(b.statusCode)
if b.responseMessage != "" {
_, _ = res.Write([]byte(b.responseMessage))
}
return
}
}
// Check User-Agent based blocking
userAgent := req.UserAgent()
for _, re := range b.regexpsAllow {
if re.MatchString(userAgent) {
b.next.ServeHTTP(res, req)
return
}
}
for index, re := range b.regexpsDeny {
if re.MatchString(userAgent) {
message := &BlockUserAgentMessage{
Regex: index,
UserAgent: userAgent,
RemoteAddr: req.RemoteAddr,
Host: req.Host,
RequestURI: req.RequestURI,
}
jsonMessage, err := json.Marshal(message)
if err == nil {
log.Printf("%s: %s", b.name, jsonMessage)
}
res.WriteHeader(b.statusCode)
if b.responseMessage != "" {
_, _ = res.Write([]byte(b.responseMessage))
}
return
}
}
}
b.next.ServeHTTP(res, req)
}