-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add banhammer and user status that defaults to pending (#35)
- Loading branch information
Showing
14 changed files
with
159 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"piccolo/api/service/banhammerservice" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func BanHammer(banHammerService *banhammerservice.BanHammerService) echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
ip := c.RealIP() | ||
|
||
banned, ttl := banHammerService.IsBanned(ctx, ip) | ||
if banned { | ||
return echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("You are banned for %s", ttl)) | ||
} | ||
|
||
return next(c) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package banhammerservice | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
func (svc *BanHammerService) IsBanned(ctx context.Context, ip string) (bool, time.Duration) { | ||
key := banKeyPrefix + ip | ||
|
||
ttl, err := svc.rdb.TTL(ctx, key).Result() | ||
if err != nil && err != redis.Nil { | ||
slog.Error("Error checking ban status:", "error", err) | ||
} | ||
|
||
return ttl > 0, ttl | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package banhammerservice | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
) | ||
|
||
func (svc *BanHammerService) RecordFailedAttempt(ctx context.Context, ip string) error { | ||
attemptsKey := attemptKeyPrefix + ip | ||
banKey := banKeyPrefix + ip | ||
|
||
// Increment the failed attempts count atomically | ||
attempts, err := svc.rdb.Incr(ctx, attemptsKey).Result() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Set expiration for the tracking key if it’s the first attempt | ||
if attempts == 1 { | ||
svc.rdb.Expire(ctx, attemptsKey, trackingTime) | ||
} | ||
|
||
// Check if the IP should be banned | ||
if attempts >= maxAttempts { | ||
err := svc.rdb.Set(ctx, banKey, "1", banDuration).Err() // Ban the IP | ||
if err != nil { | ||
return err | ||
} | ||
// Cleanup the attempts key | ||
svc.rdb.Del(ctx, attemptsKey) | ||
slog.Info("IP %s has been banned for %s\n", ip, banDuration) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package banhammerservice | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type BanHammerService struct { | ||
rdb *redis.Client | ||
} | ||
|
||
const ( | ||
maxAttempts = 10 | ||
banDuration = 1 * time.Hour | ||
trackingTime = 10 * time.Minute | ||
banKeyPrefix = "ban-hammer:ban:" | ||
attemptKeyPrefix = "ban-hammer:attempt:" | ||
) | ||
|
||
func New() *BanHammerService { | ||
opts, err := redis.ParseURL(os.Getenv("REDIS_URL")) | ||
if err != nil { | ||
slog.Error("cannot create redis connection", "error", err) | ||
} | ||
|
||
rdb := redis.NewClient(opts) | ||
|
||
return &BanHammerService{rdb} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
alter table users | ||
drop column if exists status; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
create type user_status_enum as enum ('pending', 'active', 'suspended'); | ||
|
||
alter table users | ||
add column status user_status_enum default 'pending'; |