dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 923aed5e17e207cf64cd70a506c471023bc767c7
parent 666fe2df4de727ca2e9aee07b4556c469eda8ca8
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sun, 18 Dec 2022 14:08:26 -0800

simplify code

Diffstat:
Mpkg/web/handlers/handlers.go | 10++--------
Mpkg/web/handlers/utils/utils.go | 7+++++++
Mpkg/web/middlewares/middlewares.go | 12+++---------
3 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -18,7 +18,6 @@ import ( "io" "io/ioutil" "math/rand" - "net" "net/http" "net/url" "os" @@ -265,9 +264,7 @@ func LoginAttackHandler(c echo.Context) error { // This way, the captcha can be used multiple times by different users until it's time has expired. if err := captcha.VerifyStringDangerous(tempLoginStore, loginLink.ID, key); err != nil { // If the captcha was invalid, kill the circuit. - if conn, ok := c.Request().Context().Value("conn").(net.Conn); ok { - config.ConnMap.Close(conn) - } + hutils.KillCircuit(c) time.Sleep(utils.RandSec(3, 5)) return c.NoContent(http.StatusNotFound) } @@ -803,10 +800,7 @@ func waitPageWrapper(c echo.Context, clb echo.HandlerFunc, cookieName string) er if c.Request().Method == http.MethodGet { // If you reload the page before the wait time is over, we kill the circuit. if now.Unix() < payload.Unix { - // Kill circuit - if conn, ok := c.Request().Context().Value("conn").(net.Conn); ok { - config.ConnMap.Close(conn) - } + hutils.KillCircuit(c) return c.String(http.StatusFound, "DDoS filter killed your path") } diff --git a/pkg/web/handlers/utils/utils.go b/pkg/web/handlers/utils/utils.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "net" "net/http" "strconv" "time" @@ -155,3 +156,9 @@ func CaptchaVerifyString(c echo.Context, id, digits string) error { } return nil } + +func KillCircuit(c echo.Context) { + if conn, ok := c.Request().Context().Value("conn").(net.Conn); ok { + config.ConnMap.Close(conn) + } +} diff --git a/pkg/web/middlewares/middlewares.go b/pkg/web/middlewares/middlewares.go @@ -394,9 +394,7 @@ func DdosMiddleware(next echo.HandlerFunc) echo.HandlerFunc { config.RpsCounter.Incr() if authCookie, err := c.Cookie(hutils.AuthCookieName); err == nil { if len(authCookie.Value) > 64 { - if conn, ok := c.Request().Context().Value("conn").(net.Conn); ok { - config.ConnMap.Close(conn) - } + hutils.KillCircuit(c) config.RejectedReqCounter.Incr() time.Sleep(utils.RandSec(5, 20)) return c.NoContent(http.StatusOK) @@ -404,18 +402,14 @@ func DdosMiddleware(next echo.HandlerFunc) echo.HandlerFunc { } if csrfCookie, err := c.Cookie("_csrf"); err == nil { if len(csrfCookie.Value) > 32 { - if conn, ok := c.Request().Context().Value("conn").(net.Conn); ok { - config.ConnMap.Close(conn) - } + hutils.KillCircuit(c) config.RejectedReqCounter.Incr() time.Sleep(utils.RandSec(5, 20)) return c.NoContent(http.StatusOK) } } if len(c.QueryParam("captcha")) > 6 { - if conn, ok := c.Request().Context().Value("conn").(net.Conn); ok { - config.ConnMap.Close(conn) - } + hutils.KillCircuit(c) config.RejectedReqCounter.Incr() time.Sleep(utils.RandSec(5, 20)) return c.NoContent(http.StatusOK)