Documentation
¶
Overview ¶
Package abuse provides behavioral analysis middleware for Echo v5. It tracks client reputation via a scoring system to mitigate scrapers and scanners.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = Config{ Threshold: 100, Cooldown: 30 * time.Minute, ErrorHandler: func(c *echo.Context, score int) error { return echo.NewHTTPError(http.StatusForbidden, "Access denied due to suspicious activity") }, }
DefaultConfig provides the standard settings for abuse detection middleware.
Functions ¶
func New ¶
func New(config Config) echo.MiddlewareFunc
New returns an Echo middleware that monitors and limits abusive behavior. It should be placed early in the middleware stack to protect downstream resources.
Example ¶
package main
import (
"github.com/its-ernest/http-keystone/abuse"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
// Instant-ban on sensitive files, warn on API spam
e.Use(abuse.New(abuse.Config{
Threshold: 100,
Rules: []abuse.Rule{
// immediate ban for ANY attempt on .env
{Path: "/.env", Score: 100},
// heavy score only for POST requests to login (brute force protection)
{Path: "/api/v1/login", Method: "POST", Score: 20},
// light score for general API exploration
{Path: "/api/v1/*", Method: "GET", Score: 2},
},
}))
}
Output:
Types ¶
type Config ¶
type Config struct {
Store store.Counter
Threshold int
Rules []Rule
Cooldown time.Duration
// EnableDelay adds latency to suspicious requests instead of a hard block.
EnableDelay bool
ErrorHandler func(c *echo.Context, score int) error
}
Config defines the settings for the abuse detection middleware.
type Rule ¶
type Rule struct {
// Path is the URL path to match (supports wildcards via Echo's path matching)
Path string
// Method is the HTTP method to match (GET, POST, etc.). Empty means match all.
Method string
// Score is the amount of *heat* to add when this rule matches
Score int
}
Rule defines the "heat" points added for specific path patterns. Pattern supports suffix wildcards like "/admin*".