diff options
| author | 2026-05-02 14:29:55 +0700 | |
|---|---|---|
| committer | 2026-05-02 14:29:55 +0700 | |
| commit | ca1f36376865cb2a20923aa62fdff3ee217abde3 (patch) | |
| tree | 2e3676489327505dd2b4d53023ca0da6fc2dc5a2 | |
| parent | 3898baa47faf268864ecac3699f7e98c6cac01a4 (diff) | |
| download | web-ca1f36376865cb2a20923aa62fdff3ee217abde3.tar.gz web-ca1f36376865cb2a20923aa62fdff3ee217abde3.zip | |
feat: implement rate limiting
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | internal/router/basic-handler.go | 4 | ||||
| -rw-r--r-- | internal/router/handlers/api-v1-blog-search.go | 4 | ||||
| -rw-r--r-- | internal/router/handlers/api-v1-email-send-verification-code.go | 4 | ||||
| -rw-r--r-- | internal/router/handlers/api-v1-email-verify.go | 4 | ||||
| -rw-r--r-- | internal/router/handlers/api-v1-like-put.go | 4 | ||||
| -rw-r--r-- | internal/router/handlers/api-v1-subs-put.go | 4 | ||||
| -rw-r--r-- | internal/router/rate-limiters.go | 37 | ||||
| -rw-r--r-- | internal/router/router.go | 5 |
10 files changed, 72 insertions, 0 deletions
@@ -24,6 +24,8 @@ require ( github.com/aws/aws-sdk-go-v2/service/sso v1.30.15 // indirect github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.19 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.41.10 // indirect + github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect + github.com/tinylib/msgp v1.2.5 // indirect ) require ( @@ -91,6 +91,8 @@ github.com/mattn/go-runewidth v0.0.22 h1:76lXsPn6FyHtTY+jt2fTTvsMUCZq1k0qwRsAMux github.com/mattn/go-runewidth v0.0.22/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= github.com/mattn/go-sqlite3 v1.14.41 h1:8p7Pwz5NHkEbWSqc/ygU4CBGubhFFkpgP9KwcdkAHNA= github.com/mattn/go-sqlite3 v1.14.41/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ= +github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c h1:dAMKvw0MlJT1GshSTtih8C2gDs04w8dReiOGXrGLNoY= +github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -100,6 +102,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/tinylib/msgp v1.2.5 h1:WeQg1whrXRFiZusidTQqzETkRpGjFjcIhW6uqWH09po= +github.com/tinylib/msgp v1.2.5/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.70.0 h1:LAhMGcWk13QZWm85+eg8ZBNbrq5mnkWFGbHMUJHIdXA= diff --git a/internal/router/basic-handler.go b/internal/router/basic-handler.go index b973d01..68d305c 100644 --- a/internal/router/basic-handler.go +++ b/internal/router/basic-handler.go @@ -42,6 +42,10 @@ func (r *BasicHandler) ContentType() string { return fiber.MIMETextHTMLCharsetUTF8 } +func (r *BasicHandler) RateLimiter() *fiber.Handler { + return nil +} + func (r *BasicHandler) Render(c *fiber.Ctx, supplements *Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) { if !r.IsTemplated() { panic("handler did not implement Render method (while being non-templated)") diff --git a/internal/router/handlers/api-v1-blog-search.go b/internal/router/handlers/api-v1-blog-search.go index c9a91ad..38004a1 100644 --- a/internal/router/handlers/api-v1-blog-search.go +++ b/internal/router/handlers/api-v1-blog-search.go @@ -44,6 +44,10 @@ func (r *BlogSearchHandler) ToValidateLang() router.LangSetting { return router.InForm } +func (r *BlogSearchHandler) RateLimiter() *fiber.Handler { + return &router.RateLimiterLoose +} + func (r *BlogSearchHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) { sort := c.Query("sort") tz := c.Query("tz") diff --git a/internal/router/handlers/api-v1-email-send-verification-code.go b/internal/router/handlers/api-v1-email-send-verification-code.go index 20c5fb6..0b53efa 100644 --- a/internal/router/handlers/api-v1-email-send-verification-code.go +++ b/internal/router/handlers/api-v1-email-send-verification-code.go @@ -38,6 +38,10 @@ func (r *SendVerificationCodeHandler) ToValidateLang() router.LangSetting { return router.InReferer } +func (r *SendVerificationCodeHandler) RateLimiter() *fiber.Handler { + return &router.RateLimiterStrict +} + func (r *SendVerificationCodeHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) { id := c.IP() templateMap["StatusId"] = "email-message" diff --git a/internal/router/handlers/api-v1-email-verify.go b/internal/router/handlers/api-v1-email-verify.go index c9ec969..ace1870 100644 --- a/internal/router/handlers/api-v1-email-verify.go +++ b/internal/router/handlers/api-v1-email-verify.go @@ -36,6 +36,10 @@ func (r *VerifyCodeHandler) ToValidateLang() router.LangSetting { return router.InReferer } +func (r *VerifyCodeHandler) RateLimiter() *fiber.Handler { + return &router.RateLimiterStrict +} + func (r *VerifyCodeHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) { verificationCode := c.FormValue("email_code") templateMap["StatusId"] = "verification-message" diff --git a/internal/router/handlers/api-v1-like-put.go b/internal/router/handlers/api-v1-like-put.go index 434f15a..8305d2b 100644 --- a/internal/router/handlers/api-v1-like-put.go +++ b/internal/router/handlers/api-v1-like-put.go @@ -37,6 +37,10 @@ func (r *PutLikeHandler) ToValidateLang() router.LangSetting { return router.InReferer } +func (r *PutLikeHandler) RateLimiter() *fiber.Handler { + return &router.RateLimiterMedium +} + func (r *PutLikeHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) { path, pathParts, _, err := router.GetPathFromReferer(c) if err != nil { diff --git a/internal/router/handlers/api-v1-subs-put.go b/internal/router/handlers/api-v1-subs-put.go index 144abe5..32fba7e 100644 --- a/internal/router/handlers/api-v1-subs-put.go +++ b/internal/router/handlers/api-v1-subs-put.go @@ -34,6 +34,10 @@ func (r *PutSubsHandler) ToValidateLang() router.LangSetting { return router.InReferer } +func (r *PutSubsHandler) RateLimiter() *fiber.Handler { + return &router.RateLimiterMedium +} + func (r *PutSubsHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) { templateMap["StatusId"] = "subs-message" diff --git a/internal/router/rate-limiters.go b/internal/router/rate-limiters.go new file mode 100644 index 0000000..b43581d --- /dev/null +++ b/internal/router/rate-limiters.go @@ -0,0 +1,37 @@ +package router + +import ( + "time" + + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/limiter" +) + +var ( + RateLimiterStrict = limiter.New(limiter.Config{ + Max: 5, + Expiration: time.Minute, + KeyGenerator: func(c *fiber.Ctx) string { return c.IP() }, + LimitReached: func(c *fiber.Ctx) error { + return c.Status(fiber.StatusTooManyRequests).SendString("rate limit") + }, + }) + + RateLimiterMedium = limiter.New(limiter.Config{ + Max: 30, + Expiration: time.Minute, + KeyGenerator: func(c *fiber.Ctx) string { return c.IP() }, + LimitReached: func(c *fiber.Ctx) error { + return c.Status(fiber.StatusTooManyRequests).SendString("rate limit") + }, + }) + + RateLimiterLoose = limiter.New(limiter.Config{ + Max: 60, + Expiration: time.Minute, + KeyGenerator: func(c *fiber.Ctx) string { return c.IP() }, + LimitReached: func(c *fiber.Ctx) error { + return c.Status(fiber.StatusTooManyRequests).SendString("rate limit") + }, + }) +) diff --git a/internal/router/router.go b/internal/router/router.go index 926d21a..5230e74 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -78,6 +78,7 @@ type Route interface { TemplatesToInject() []string SitemapInfo(supplements *Supplements) []SitemapInfo ContentType() string + RateLimiter() *fiber.Handler Render(c *fiber.Ctx, supplements *Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) AddMeta(c *fiber.Ctx, supplements *Supplements, lang string, templateMap fiber.Map) (meta []MetaField, err error) AddLinkedData(c *fiber.Ctx, supplements *Supplements, lang string, templateMap fiber.Map) (ld map[string]any, err error) @@ -267,6 +268,10 @@ func (r *Router) InitRoutes() (err error) { return fmt.Errorf("failed to add '%s %s' route into template manager: %w", method, match, err) } + if rateLimiter := route.RateLimiter(); rateLimiter != nil { + r.app.Use(match, *rateLimiter) + } + if route.IsTemplated() { if _, ok := r.templatedRoutes[method]; !ok { r.templatedRoutes[method] = make(map[string]Route) |