Skip to content

Commit

Permalink
Update to go 1.23
Browse files Browse the repository at this point in the history
  • Loading branch information
jliebert committed Oct 4, 2024
1 parent 4366b1d commit 0480dc5
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-cross.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
go-version: [ 1.18, 1.x ]
go-version: [ 1.23, 1.x ]
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
name: Main Process
runs-on: ubuntu-latest
env:
GO_VERSION: 1.18
GO_VERSION: 1.23
GOLANGCI_LINT_VERSION: v1.33.0
YAEGI_VERSION: v0.9.8
YAEGI_VERSION: v0.16.1
CGO_ENABLED: 0
defaults:
run:
Expand Down
5 changes: 3 additions & 2 deletions .golangci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
enable-all = true
disable = [
"bodyclose",
"goerr113",
"err113",
"nlreturn",
"testpackage",
"paralleltest",
Expand All @@ -45,7 +45,8 @@
max-same-issues = 0
exclude = [
"don't use an underscore in package name",
"ST1003: should not use underscores in package names"
"ST1003: should not use underscores in package names",
"got 'user-agent' want 'userAgent'"
]

[[issues.exclude-rules]]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: lint test vendor clean

export GO118MODULE=on
export GO123MODULE=on

default: lint test

Expand Down
42 changes: 27 additions & 15 deletions blockuseragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package traefik_plugin_blockuseragent

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"encoding/json"
)

// Config holds the plugin configuration.
Expand All @@ -20,31 +20,33 @@ func CreateConfig() *Config {
return &Config{Regex: make([]string, 0)}
}

// BlockUserAgent struct.
type BlockUserAgent struct {
name string
next http.Handler
regexps []*regexp.Regexp
}

// BlockUserAgentMessage struct.
type BlockUserAgentMessage struct {
rule int `json:"intValue"`
agent string `json:"stringValue"`
ip string `json:"stringValue"`
host string `json:"stringValue"`
uri string `json:"stringValue"`
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) {
regexps := make([]*regexp.Regexp, len(config.Regex))

for i, regex := range config.Regex {
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)
}

regexps[i] = re
regexps[index] = re
}

return &BlockUserAgent{
Expand All @@ -54,21 +56,31 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
}, nil
}

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

for i, re := range b.regexps {
for index, re := range b.regexps {
if re.MatchString(userAgent) {
message := map[string]interface{}{ "regex": i, "user-agent": userAgent, "ip": req.RemoteAddr, "host": req.Host, "uri": req.RequestURI }
jsonMessage, _ := json.Marshal(message)
log.Printf("%s: %s", b.name, jsonMessage)
rw.WriteHeader(http.StatusForbidden)
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)

return
}
}
}

b.next.ServeHTTP(rw, req)
b.next.ServeHTTP(res, req)
}
23 changes: 12 additions & 11 deletions blockuseragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,70 +43,71 @@ func TestServeHTTP(t *testing.T) {
desc string
regexps []string
reqUserAgent string
reqUri string
reqURI string
expNextCall bool
expStatusCode int
}{
{
desc: "should return forbidden status",
regexps: []string{"\\bagent1\\b"},
reqUserAgent: "agent1",
reqUri: "http://localhost/",
reqURI: "http://localhost/",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
{
desc: "should return forbidden status",
regexps: []string{"\\bagent1\\b", "\\bagent2\\b"},
reqUserAgent: "agent2",
reqUri: "http://localhost/test",
reqURI: "http://localhost/test",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
{
desc: "should return ok status",
regexps: []string{"\\bagent1\\b", "\\bagent2\\b"},
reqUserAgent: "agentok",
reqUri: "http://localhost/test",
reqURI: "http://localhost/test",
expNextCall: true,
expStatusCode: http.StatusOK,
},
{
desc: "should return ok status",
regexps: nil,
reqUserAgent: "agentok",
reqUri: "http://localhost/test",
reqURI: "http://localhost/test",
expNextCall: true,
expStatusCode: http.StatusOK,
},
{
desc: "should return forbidden status",
regexps: []string{"\\bagent.*"},
reqUserAgent: "agent1",
reqUri: "http://localhost/test",
reqURI: "http://localhost/test",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
{
desc: "should return forbidden status",
regexps: []string{"^agent.*"},
reqUserAgent: "agent1",
reqUri: "http://localhost/test",
reqURI: "http://localhost/test",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
{
desc: "should return forbidden status",
regexps: []string{"^$"},
reqUserAgent: "",
reqUri: "http://localhost/test",
reqURI: "http://localhost/test",
expNextCall: false,
expStatusCode: http.StatusForbidden,
},
{
desc: "should return ok status",
regexps: []string{"^$"},
reqUserAgent: "agentok",
reqUri: "http://localhost/test",
reqURI: "http://localhost/test",
expNextCall: true,
expStatusCode: http.StatusOK,
},
Expand All @@ -119,7 +120,7 @@ func TestServeHTTP(t *testing.T) {
}

nextCall := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
nextCall = true
})

Expand All @@ -130,7 +131,7 @@ func TestServeHTTP(t *testing.T) {

recorder := httptest.NewRecorder()

req := httptest.NewRequest(http.MethodGet, test.reqUri, nil)
req := httptest.NewRequest(http.MethodGet, test.reqURI, nil)
req.Header.Add("User-Agent", test.reqUserAgent)

handler.ServeHTTP(recorder, req)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/agence-gaya/traefik-plugin-blockuseragent

go 1.18
go 1.23

0 comments on commit 0480dc5

Please sign in to comment.