removes rate limiter
This commit is contained in:
@@ -1,54 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
rateLimit = 20 // times per rateLimitInterval
|
|
||||||
rateLimitInterval = 1 * time.Hour
|
|
||||||
)
|
|
||||||
|
|
||||||
var accesses = &sync.Map{}
|
|
||||||
|
|
||||||
type access struct {
|
|
||||||
count int
|
|
||||||
timestamp time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func legitAccess(c echo.Context) bool {
|
|
||||||
ip := c.Request().RemoteAddr
|
|
||||||
aRaw, found := accesses.Load(ip)
|
|
||||||
var a *access
|
|
||||||
if found {
|
|
||||||
a, _ = aRaw.(*access)
|
|
||||||
} else {
|
|
||||||
a = &access{}
|
|
||||||
}
|
|
||||||
a.count++
|
|
||||||
a.timestamp = time.Now()
|
|
||||||
accesses.Store(ip, a)
|
|
||||||
return a.count < rateLimit
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanAccessRegistry(logger echo.Logger) {
|
|
||||||
for {
|
|
||||||
time.Sleep(rateLimitInterval)
|
|
||||||
t, e := 0, 0
|
|
||||||
accesses.Range(func(ip, aRaw interface{}) bool {
|
|
||||||
t++
|
|
||||||
a, _ := aRaw.(*access)
|
|
||||||
if a.timestamp.Add(rateLimitInterval).Before(time.Now()) {
|
|
||||||
accesses.Delete(ip)
|
|
||||||
e++
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
if e > 0 {
|
|
||||||
logger.Infof("cleaned up %d/%d outdated accesses", e, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -20,7 +20,6 @@ var (
|
|||||||
403: "Forbidden",
|
403: "Forbidden",
|
||||||
404: "Not found",
|
404: "Not found",
|
||||||
412: "Precondition failed",
|
412: "Precondition failed",
|
||||||
429: "Too many requests",
|
|
||||||
503: "Service unavailable",
|
503: "Service unavailable",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go persistStats(e.Logger, db)
|
go persistStats(e.Logger, db)
|
||||||
go cleanAccessRegistry(e.Logger)
|
|
||||||
|
|
||||||
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
|
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
|
||||||
|
|
||||||
@@ -110,11 +109,6 @@ func main() {
|
|||||||
code := http.StatusForbidden
|
code := http.StatusForbidden
|
||||||
return c.Render(code, "Note", responsePage(code))
|
return c.Render(code, "Note", responsePage(code))
|
||||||
}
|
}
|
||||||
if !legitAccess(c) {
|
|
||||||
code := http.StatusTooManyRequests
|
|
||||||
c.Logger().Errorf("rate limit exceeded for %s", c.Request().RemoteAddr)
|
|
||||||
return c.Render(code, "Note", responsePage(code))
|
|
||||||
}
|
|
||||||
if c.FormValue("tos") != "on" {
|
if c.FormValue("tos") != "on" {
|
||||||
code := http.StatusPreconditionFailed
|
code := http.StatusPreconditionFailed
|
||||||
c.Logger().Errorf("POST /note error: %d", code)
|
c.Logger().Errorf("POST /note error: %d", code)
|
||||||
@@ -152,7 +146,7 @@ func main() {
|
|||||||
|
|
||||||
e.POST("/:id/report", func(c echo.Context) error {
|
e.POST("/:id/report", func(c echo.Context) error {
|
||||||
report := c.FormValue("report")
|
report := c.FormValue("report")
|
||||||
if legitAccess(c) && report != "" {
|
if report != "" {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
if err := email(id, report); err != nil {
|
if err := email(id, report); err != nil {
|
||||||
c.Logger().Errorf("couldn't send email: %v", err)
|
c.Logger().Errorf("couldn't send email: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user