Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,25 @@ You can use regexAllow to make exception on blocking regex.
[[http.services.my-service.loadBalancer.servers]]
url = "http://127.0.0.1"
```

## Omit logs for blocked requests

By default, this plugin logs each blocked request.
To prevent this, you can set the `quiet` configuration parameter:

```toml
[http.middlewares]
[http.middlewares.block-foo.plugin.blockuseragent]
quiet = true
```

## Manual testing

To manually test the current state of the plugin, you can start traefik using the included [compose file](./compose.yaml).
*Beware that this mounts the docker daemon socket* and uses it to configure traefik via docker labels.
The current configuration blocks requests whose useragent includes the string `blockthis`.
To test this, use e.g. curl:

```bash
curl --header "User-Agent: blockthis" -i http://localhost/
```
42 changes: 29 additions & 13 deletions blockuseragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ import (
type Config struct {
RegexAllow []string `json:"regexAllow,omitempty"`
Regex []string `json:"regex,omitempty"`
Quiet *bool `json:"quiet,omitempty"`
}

// CreateConfig creates and initializes the plugin configuration.
func CreateConfig() *Config {
return &Config{RegexAllow: make([]string, 0), Regex: make([]string, 0)}
quiet := false

return &Config{
RegexAllow: make([]string, 0),
Regex: make([]string, 0),
Quiet: &quiet,
}
}

// BlockUserAgent struct.
Expand All @@ -27,6 +34,7 @@ type BlockUserAgent struct {
next http.Handler
regexpsAllow []*regexp.Regexp
regexpsDeny []*regexp.Regexp
quiet bool
}

// BlockUserAgentMessage struct.
Expand Down Expand Up @@ -61,16 +69,22 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
regexpsDeny[index] = re
}

quiet := false
if config.Quiet != nil {
quiet = *config.Quiet
}

return &BlockUserAgent{
name: name,
next: next,
regexpsAllow: regexpsAllow,
regexpsDeny: regexpsDeny,
quiet: quiet,
}, nil
}

func (b *BlockUserAgent) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if req != nil {
if req != nil { //nolint:nestif
userAgent := req.UserAgent()

for _, re := range b.regexpsAllow {
Expand All @@ -83,17 +97,19 @@ func (b *BlockUserAgent) ServeHTTP(res http.ResponseWriter, req *http.Request) {

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)
if !b.quiet {
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(http.StatusForbidden)
Expand Down
6 changes: 6 additions & 0 deletions blockuseragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ func TestNew(t *testing.T) {
},
}

quiet := false

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := &Config{
RegexAllow: test.regexAllow,
Regex: test.regexDeny,
Quiet: &quiet,
}

if _, err := New(context.Background(), nil, cfg, "name"); test.expErr && err == nil {
Expand Down Expand Up @@ -186,11 +189,14 @@ func TestServeHTTP(t *testing.T) {
},
}

quiet := false

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := &Config{
RegexAllow: test.regexAllow,
Regex: test.regexDeny,
Quiet: &quiet,
}

nextCall := false
Expand Down
23 changes: 23 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
services:
traefik:
image: "traefik:latest"
command:
- "--api.dashboard=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--experimental.localplugins.blockuseragent.modulename=github.com/agence-gaya/traefik-plugin-blockuseragent"
ports:
- "80:80"
volumes:
- ".:/plugins-local/src/github.com/agence-gaya/traefik-plugin-blockuseragent"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
labels:
- "traefik.enable=true"
- "traefik.http.routers.dashboard.rule=Host(`localhost`)"
- "traefik.http.routers.dashboard.entryPoints=web"
- "traefik.http.routers.dashboard.service=api@internal"
- "traefik.http.routers.dashboard.middlewares=blockuseragent"
- "traefik.http.middlewares.blockuseragent.plugin.blockuseragent.regex=(blockthis)"
- "traefik.http.middlewares.blockuseragent.plugin.blockuseragent.quiet=true"