diff --git a/go.mod b/go.mod index 7bc146f..4f91e3a 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/PeernetOfficial/core v0.0.0-20220601150942-0e3e8dc9885c github.com/gin-gonic/gin v1.8.1 github.com/google/uuid v1.3.0 + github.com/julianshen/gin-limiter v0.0.0-20161123033831-fc39b5e90fe7 ) require ( @@ -20,6 +21,7 @@ require ( github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/juju/ratelimit v1.0.2 // indirect github.com/klauspost/cpuid/v2 v2.0.12 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/mattn/go-isatty v0.0.14 // indirect diff --git a/go.sum b/go.sum index c9bae4e..fff943f 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,10 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI= +github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk= +github.com/julianshen/gin-limiter v0.0.0-20161123033831-fc39b5e90fe7 h1:hlGKdRwZ0XLX3Sattpx6nqkvI0XFpIpvH7JK1u6ODbs= +github.com/julianshen/gin-limiter v0.0.0-20161123033831-fc39b5e90fe7/go.mod h1:rmrkiYPm2yvC0bT9+XTQO9jN1af7kUcdMSGXHLYfptk= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE= github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= diff --git a/main.go b/main.go index a294fbc..bf756de 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "github.com/PeernetOfficial/core/webapi" "github.com/gin-gonic/gin" "github.com/google/uuid" + limiter "github.com/julianshen/gin-limiter" "io" "io/ioutil" "mime/multipart" @@ -228,11 +229,17 @@ func main() { r := gin.Default() r.LoadHTMLGlob("templates/*.html") r.Static("/templates", "./templates") - r.GET("/upload", func(c *gin.Context) { + + // Middleware rate limiter + lm := limiter.NewRateLimiter(time.Minute, 2, func(ctx *gin.Context) (string, error) { + return "", nil + }) + + r.GET("/upload", lm.Middleware(), func(c *gin.Context) { c.HTML(http.StatusOK, "upload.html", nil) }) - r.POST("/uploadFile", func(c *gin.Context) { + r.POST("/uploadFile", lm.Middleware(), func(c *gin.Context) { file, header, err := c.Request.FormFile("file") defer file.Close() @@ -258,7 +265,7 @@ func main() { // Implement CURL script to ensure linux users can upload directly // the Cli like https://bashupload.com // Ex: curl http://localhost:8080/uploadCurl -F add=@ - r.POST("/uploadCurl", func(c *gin.Context) { + r.POST("/uploadCurl", lm.Middleware(), func(c *gin.Context) { file, header, err := c.Request.FormFile("add") defer file.Close()