Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert status handler to match existing handlers #175

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
28 changes: 19 additions & 9 deletions fail2ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,32 @@ func New(_ context.Context, next http.Handler, config *Config, _ string) (http.H

f2b := fail2ban.New(rules)

c := chain.New(
next,
denyHandler,
allowHandler,
uDeny.New(rules.URLRegexpBan, f2b),
uAllow.New(rules.URLRegexpAllow),
f2bHandler.New(f2b),
)
var c chain.Chain

if rules.StatusCode != "" {
statusCodeHandler, err := status.New(next, rules.StatusCode, f2b)
if err != nil {
return nil, fmt.Errorf("failed to create status handler: %w", err)
}

c.WithStatus(statusCodeHandler)
c = chain.New(
next,
denyHandler,
allowHandler,
uDeny.New(rules.URLRegexpBan, f2b),
uAllow.New(rules.URLRegexpAllow),
statusCodeHandler,
f2bHandler.New(f2b),
)
} else {
c = chain.New(
next,
denyHandler,
allowHandler,
uDeny.New(rules.URLRegexpBan, f2b),
uAllow.New(rules.URLRegexpAllow),
f2bHandler.New(f2b),
)
}

return c, nil
Expand Down
6 changes: 0 additions & 6 deletions pkg/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,5 @@ func (c *chain) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

if c.status != nil {
(*c.status).ServeHTTP(w, r)

return
}

c.final.ServeHTTP(w, r)
}
14 changes: 6 additions & 8 deletions pkg/response/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package status

import (
"fmt"
"github.com/tomMoulard/fail2ban/pkg/chain"
"net/http"
"strings"

Expand All @@ -29,14 +30,14 @@ func New(next http.Handler, statusCode string, f2b *fail2ban.Fail2Ban) (*status,
}, nil
}

func (s *status) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (s *status) ServeHTTP(w http.ResponseWriter, r *http.Request) (*chain.Status, error) {
fmt.Printf("status handler")

data := data.GetData(r)
if data == nil {
fmt.Print("data is nil")

return
return nil, nil
}

fmt.Printf("data: %+v", data)
Expand All @@ -49,21 +50,18 @@ func (s *status) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !catcher.isFilteredCode() {
w.WriteHeader(catcher.getCode())

return
return nil, nil
}

catcher.allowedRequest = s.f2b.ShouldAllow(data.RemoteIP)
if !catcher.allowedRequest {
fmt.Printf("IP %s is banned", data.RemoteIP)
w.WriteHeader(http.StatusForbidden)

return
return &chain.Status{Return: true}, nil
}

fmt.Printf("IP %s is allowed", data.RemoteIP)
w.WriteHeader(catcher.getCode())

if _, err := w.Write(catcher.bytes); err != nil {
fmt.Printf("failed to write to response: %v", err)
}
return &chain.Status{Break: true}, nil
}