Skip to content

Commit

Permalink
chore: remove pretty logs for now (#146)
Browse files Browse the repository at this point in the history
* chore: remove pretty logs for now

* chore: lint

* chore: go test

---------

Co-authored-by: Tom Moulard <tom.moulard@perfectstay.com>
  • Loading branch information
tomMoulard and Tom Moulard committed Sep 6, 2024
1 parent 1b09b8b commit 6b3824f
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 100 deletions.
13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ linters:
- nilnil # Not relevant
- testpackage # Too strict
- cyclop # duplicate of gocyclo
- exportloopref # deprecated

linters-settings:
misspell:
Expand Down Expand Up @@ -80,3 +81,15 @@ issues:
text: 'G304: Potential file inclusion via variable'
linters:
- gosec
- text: 'use of `fmt.Printf` forbidden' # FIXME: add revert this change ASAP
linters:
- forbidigo
- text: 'use of `fmt.Print` forbidden' # FIXME: add revert this change ASAP
linters:
- forbidigo
- text: 'use of `fmt.Println` forbidden' # FIXME: add revert this change ASAP
linters:
- forbidigo

output:
show-stats: true
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 2

before:
hooks:
- go mod download
Expand Down
2 changes: 1 addition & 1 deletion pkg/chain/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ func Example() {
fmt.Println(rec.Body.String())

// Output:
// data: &{RemoteIP:192.0.2.1}
// data: &{RemoteIP:192.0.2.1}data: &{RemoteIP:192.0.2.1}
// pong
}
7 changes: 1 addition & 6 deletions pkg/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ import (
"fmt"
"net"
"net/http"

logger "github.com/tomMoulard/fail2ban/pkg/log"
)

// l debug logger. noop by default.
var l = logger.New("data")

type key string

const contextDataKey key = "data"
Expand All @@ -32,7 +27,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request) (*http.Request, error) {
RemoteIP: remoteIP,
}

l.Printf("data: %+v", data)
fmt.Printf("data: %+v", data)

return r.WithContext(context.WithValue(r.Context(), contextDataKey, data)), nil
}
Expand Down
17 changes: 7 additions & 10 deletions pkg/fail2ban/fail2ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
package fail2ban

import (
"fmt"
"sync"
"time"

"github.com/tomMoulard/fail2ban/pkg/ipchecking"
logger "github.com/tomMoulard/fail2ban/pkg/log"
"github.com/tomMoulard/fail2ban/pkg/rules"
utime "github.com/tomMoulard/fail2ban/pkg/utils/time"
)

// l debug logger. noop by default.
var l = logger.New("fail2ban")

// Fail2Ban is a fail2ban implementation.
type Fail2Ban struct {
rules rules.RulesTransformed
Expand Down Expand Up @@ -44,7 +41,7 @@ func (u *Fail2Ban) ShouldAllow(remoteIP string) bool {
Count: 1,
}

l.Printf("welcome %q", remoteIP)
fmt.Printf("welcome %q", remoteIP)

return true
}
Expand All @@ -57,7 +54,7 @@ func (u *Fail2Ban) ShouldAllow(remoteIP string) bool {
Denied: true,
}

l.Printf("%q is still banned since %q, %d request",
fmt.Printf("%q is still banned since %q, %d request",
remoteIP, ip.Viewed.Format(time.RFC3339), ip.Count+1)

return false
Expand All @@ -69,7 +66,7 @@ func (u *Fail2Ban) ShouldAllow(remoteIP string) bool {
Denied: false,
}

l.Println(remoteIP + " is no longer banned")
fmt.Println(remoteIP + " is no longer banned")

return true
}
Expand All @@ -82,7 +79,7 @@ func (u *Fail2Ban) ShouldAllow(remoteIP string) bool {
Denied: true,
}

l.Printf("%q is banned for %d>=%d request",
fmt.Printf("%q is banned for %d>=%d request",
remoteIP, ip.Count+1, u.rules.MaxRetry)

return false
Expand All @@ -94,7 +91,7 @@ func (u *Fail2Ban) ShouldAllow(remoteIP string) bool {
Denied: false,
}

l.Printf("welcome back %q for the %d time", remoteIP, ip.Count+1)
fmt.Printf("welcome back %q for the %d time", remoteIP, ip.Count+1)

return true
}
Expand All @@ -105,7 +102,7 @@ func (u *Fail2Ban) ShouldAllow(remoteIP string) bool {
Denied: false,
}

l.Printf("welcome back %q", remoteIP)
fmt.Printf("welcome back %q", remoteIP)

return true
}
10 changes: 3 additions & 7 deletions pkg/list/allow/allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import (
"github.com/tomMoulard/fail2ban/pkg/chain"
"github.com/tomMoulard/fail2ban/pkg/data"
"github.com/tomMoulard/fail2ban/pkg/ipchecking"
logger "github.com/tomMoulard/fail2ban/pkg/log"
)

// l debug logger. noop by default.
var l = logger.New("list allow")

type allow struct {
list ipchecking.NetIPs
}
Expand All @@ -34,15 +30,15 @@ func (a *allow) ServeHTTP(w http.ResponseWriter, r *http.Request) (*chain.Status
return nil, errors.New("failed to get data from request context")
}

l.Printf("data: %+v", data)
fmt.Printf("data: %+v", data)

if a.list.Contains(data.RemoteIP) {
l.Printf("IP %s is allowed", data.RemoteIP)
fmt.Printf("IP %s is allowed", data.RemoteIP)

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

l.Printf("IP %s not is allowed", data.RemoteIP)
fmt.Printf("IP %s not is allowed", data.RemoteIP)

return nil, nil
}
10 changes: 3 additions & 7 deletions pkg/list/deny/deny.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import (
"github.com/tomMoulard/fail2ban/pkg/chain"
"github.com/tomMoulard/fail2ban/pkg/data"
"github.com/tomMoulard/fail2ban/pkg/ipchecking"
logger "github.com/tomMoulard/fail2ban/pkg/log"
)

// l debug logger. noop by default.
var l = logger.New("list deny")

type deny struct {
list ipchecking.NetIPs
}
Expand All @@ -34,15 +30,15 @@ func (d *deny) ServeHTTP(w http.ResponseWriter, r *http.Request) (*chain.Status,
return nil, errors.New("failed to get data from request context")
}

l.Printf("data: %+v", data)
fmt.Printf("data: %+v", data)

if d.list.Contains(data.RemoteIP) {
l.Printf("IP %s is denied", data.RemoteIP)
fmt.Printf("IP %s is denied", data.RemoteIP)

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

l.Printf("IP %s not is denied", data.RemoteIP)
fmt.Printf("IP %s not is denied", data.RemoteIP)

return nil, nil
}
22 changes: 0 additions & 22 deletions pkg/log/log.go

This file was deleted.

21 changes: 0 additions & 21 deletions pkg/log/log_debug.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/response/status/code_catcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (cc *codeCatcher) Write(buf []byte) (int, error) {
return len(buf), nil
}

l.Printf("Write: buf: %q, code: %d", buf, cc.code)
fmt.Printf("Write: buf: %q, code: %d", buf, cc.code)

i, err := cc.responseWriter.Write(buf)
if err != nil {
Expand All @@ -94,7 +94,7 @@ func (cc *codeCatcher) WriteHeader(code int) {
return
}

l.Printf("Write header: code: %d", code)
fmt.Printf("Write header: code: %d", code)

// Handling informational headers.
if code >= 100 && code <= 199 {
Expand Down Expand Up @@ -149,7 +149,7 @@ func (cc *codeCatcher) Flush() {
// Otherwise, cc.code is actually a 200 here.
cc.WriteHeader(cc.code)

l.Printf("Flush: code: %d, caughtFilteredCode: %t", cc.code, cc.caughtFilteredCode)
fmt.Printf("Flush: code: %d, caughtFilteredCode: %t", cc.code, cc.caughtFilteredCode)

// We don't care about the contents of the response,
// since we want to serve the forbidden page,
Expand Down
18 changes: 7 additions & 11 deletions pkg/response/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import (

"github.com/tomMoulard/fail2ban/pkg/data"
"github.com/tomMoulard/fail2ban/pkg/fail2ban"
logger "github.com/tomMoulard/fail2ban/pkg/log"
)

// l debug logger. noop by default.
var l = logger.New("status")

type status struct {
next http.Handler
codeRanges HTTPCodeRanges
Expand All @@ -34,21 +30,21 @@ func New(next http.Handler, statusCode string, f2b *fail2ban.Fail2Ban) (*status,
}

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

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

return
}

l.Printf("data: %+v", data)
fmt.Printf("data: %+v", data)

catcher := newCodeCatcher(w, s.codeRanges)
s.next.ServeHTTP(catcher, r)

l.Printf("catcher: %+v", *catcher)
fmt.Printf("catcher: %+v", *catcher)

if !catcher.isFilteredCode() {
w.WriteHeader(catcher.getCode())
Expand All @@ -58,16 +54,16 @@ func (s *status) ServeHTTP(w http.ResponseWriter, r *http.Request) {

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

return
}

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

if _, err := w.Write(catcher.bytes); err != nil {
l.Printf("failed to write to response: %v", err)
fmt.Printf("failed to write to response: %v", err)
}
}
9 changes: 3 additions & 6 deletions pkg/url/allow/allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
package allow

import (
"fmt"
"net/http"
"regexp"

"github.com/tomMoulard/fail2ban/pkg/chain"
logger "github.com/tomMoulard/fail2ban/pkg/log"
)

// l debug logger. noop by default.
var l = logger.New("url allow")

type allow struct {
regs []*regexp.Regexp
}
Expand All @@ -23,13 +20,13 @@ func New(regs []*regexp.Regexp) *allow {
func (a *allow) ServeHTTP(w http.ResponseWriter, r *http.Request) (*chain.Status, error) {
for _, reg := range a.regs {
if reg.MatchString(r.URL.String()) {
l.Printf("url %s not allowed", r.URL.String())
fmt.Printf("url %s not allowed", r.URL.String())

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

l.Printf("url %s not is allowed", r.URL.String())
fmt.Printf("url %s not is allowed", r.URL.String())

return nil, nil
}
Loading

0 comments on commit 6b3824f

Please sign in to comment.