From 3767adfad218438bf26cf401a2353d0862f0ce4d Mon Sep 17 00:00:00 2001 From: Ronni Skansing Date: Thu, 29 Jan 2026 23:07:50 +0100 Subject: [PATCH] extend request deadline for applicatoin update Signed-off-by: Ronni Skansing --- backend/app/administration.go | 2 +- backend/app/middleware.go | 7 +++++++ backend/middleware/timeout.go | 23 +++++++++++++++++++++++ backend/service/update.go | 2 +- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 backend/middleware/timeout.go diff --git a/backend/app/administration.go b/backend/app/administration.go index ca1678f..cd6eb1e 100644 --- a/backend/app/administration.go +++ b/backend/app/administration.go @@ -480,7 +480,7 @@ func setupRoutes( GET(ROUTE_V1_VERSION, middleware.SessionHandler, controllers.Version.Get). // update GET(ROUTE_V1_UPDATE, middleware.SessionHandler, controllers.Update.GetUpdateDetails). - POST(ROUTE_V1_UPDATE, middleware.SessionHandler, controllers.Update.RunUpdate). + POST(ROUTE_V1_UPDATE, middleware.ExtendedTimeout(3*time.Minute), middleware.SessionHandler, controllers.Update.RunUpdate). // backup POST(ROUTE_V1_BACKUP_CREATE, middleware.SessionHandler, controllers.Backup.CreateBackup). GET(ROUTE_V1_BACKUP_LIST, middleware.SessionHandler, controllers.Backup.ListBackups). diff --git a/backend/app/middleware.go b/backend/app/middleware.go index b6add01..c9b7a0f 100644 --- a/backend/app/middleware.go +++ b/backend/app/middleware.go @@ -1,6 +1,8 @@ package app import ( + "time" + "github.com/gin-gonic/gin" "github.com/phishingclub/phishingclub/config" "github.com/phishingclub/phishingclub/middleware" @@ -41,3 +43,8 @@ func NewMiddlewares( SessionHandler: sessionHandler, } } + +// ExtendedTimeout returns a middleware that extends the write deadline for long-running operations. +func (m *Middlewares) ExtendedTimeout(timeout time.Duration) gin.HandlerFunc { + return middleware.ExtendedTimeout(timeout) +} diff --git a/backend/middleware/timeout.go b/backend/middleware/timeout.go new file mode 100644 index 0000000..5cbeac8 --- /dev/null +++ b/backend/middleware/timeout.go @@ -0,0 +1,23 @@ +package middleware + +import ( + "net/http" + "time" + + "github.com/gin-gonic/gin" +) + +// ExtendedTimeout creates a middleware that extends the write deadline for long-running operations. +// this is necessary because the server's default WriteTimeout may be too short for operations +// like downloading updates. +func ExtendedTimeout(timeout time.Duration) gin.HandlerFunc { + return func(c *gin.Context) { + // get the underlying connection and extend the write deadline + rc := http.NewResponseController(c.Writer) + if err := rc.SetWriteDeadline(time.Now().Add(timeout)); err != nil { + c.AbortWithStatus(http.StatusInternalServerError) + return + } + c.Next() + } +} diff --git a/backend/service/update.go b/backend/service/update.go index 780e687..7dd5423 100644 --- a/backend/service/update.go +++ b/backend/service/update.go @@ -306,7 +306,7 @@ func (u *Update) RunUpdate( req.Header.Set("User-Agent", "PhishingClub-Client") client := &http.Client{ - Timeout: 60 * time.Second, // Longer timeout for downloads + Timeout: 3 * time.Minute, // extended timeout for downloads } if !build.Flags.Production {