Simplify RealIP middleware setup

This commit is contained in:
Deluan 2024-01-20 18:58:12 -05:00
parent b442736a0f
commit 34c29a156f
2 changed files with 11 additions and 7 deletions

View File

@ -157,6 +157,15 @@ func clientUniqueIDMiddleware(next http.Handler) http.Handler {
})
}
// realIPMiddleware wraps the middleware.RealIP middleware function, bypassing it if the
// ReverseProxyWhitelist configuration option is set.
func realIPMiddleware(h http.Handler) http.Handler {
if conf.Server.ReverseProxyWhitelist == "" {
return middleware.RealIP(h)
}
return h
}
// serverAddressMiddleware is a middleware function that modifies the request object
// to reflect the address of the server handling the request, as determined by the
// presence of X-Forwarded-* headers or the scheme and host of the request URL.

View File

@ -164,18 +164,13 @@ func (s *Server) initRoutes() {
secureMiddleware(),
corsHandler(),
middleware.RequestID,
}
if conf.Server.ReverseProxyWhitelist == "" {
middlewares = append(middlewares, middleware.RealIP)
}
middlewares = append(middlewares,
realIPMiddleware,
middleware.Recoverer,
middleware.Heartbeat("/ping"),
robotsTXT(ui.BuildAssets()),
serverAddressMiddleware,
clientUniqueIDMiddleware,
)
}
// Mount the Native API /events endpoint with all middlewares, except the compress and request logger,
// adding the authentication middlewares