Do not apply RealIP middleware twice

This commit is contained in:
Jeremiah Menétrey 2024-04-21 15:44:20 +02:00
parent 34d1e32bc2
commit 3bc775b5de
3 changed files with 11 additions and 13 deletions

View File

@ -159,9 +159,9 @@ func clientUniqueIDMiddleware(next http.Handler) http.Handler {
})
}
// RealIPMiddleware applies middleware.RealIP, and additionally saves the request's original RemoteAddr to the request's
// realIPMiddleware applies middleware.RealIP, and additionally saves the request's original RemoteAddr to the request's
// context if navidrome is behind a trusted reverse proxy.
func RealIPMiddleware(next http.Handler) http.Handler {
func realIPMiddleware(next http.Handler) http.Handler {
if conf.Server.ReverseProxyWhitelist != "" {
return chi.Chain(
reqToCtx(request.ReverseProxyIp, func(r *http.Request) any { return r.RemoteAddr }),

View File

@ -164,7 +164,7 @@ func (s *Server) initRoutes() {
secureMiddleware(),
corsHandler(),
middleware.RequestID,
RealIPMiddleware,
realIPMiddleware,
middleware.Recoverer,
middleware.Heartbeat("/ping"),
robotsTXT(ui.BuildAssets()),

View File

@ -7,8 +7,6 @@ import (
"fmt"
"net/http"
"github.com/navidrome/navidrome/server"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/navidrome/navidrome/conf"
@ -73,12 +71,12 @@ func (api *Router) routes() http.Handler {
// Subsonic endpoints, grouped by controller
r.Group(func(r chi.Router) {
r.Use(server.RealIPMiddleware, getPlayer(api.players))
r.Use(getPlayer(api.players))
h(r, "ping", api.Ping)
h(r, "getLicense", api.GetLicense)
})
r.Group(func(r chi.Router) {
r.Use(server.RealIPMiddleware, getPlayer(api.players))
r.Use(getPlayer(api.players))
h(r, "getMusicFolders", api.GetMusicFolders)
h(r, "getIndexes", api.GetIndexes)
h(r, "getArtists", api.GetArtists)
@ -96,7 +94,7 @@ func (api *Router) routes() http.Handler {
h(r, "getSimilarSongs2", api.GetSimilarSongs2)
})
r.Group(func(r chi.Router) {
r.Use(server.RealIPMiddleware, getPlayer(api.players))
r.Use(getPlayer(api.players))
hr(r, "getAlbumList", api.GetAlbumList)
hr(r, "getAlbumList2", api.GetAlbumList2)
h(r, "getStarred", api.GetStarred)
@ -106,14 +104,14 @@ func (api *Router) routes() http.Handler {
h(r, "getSongsByGenre", api.GetSongsByGenre)
})
r.Group(func(r chi.Router) {
r.Use(server.RealIPMiddleware, getPlayer(api.players))
r.Use(getPlayer(api.players))
h(r, "setRating", api.SetRating)
h(r, "star", api.Star)
h(r, "unstar", api.Unstar)
h(r, "scrobble", api.Scrobble)
})
r.Group(func(r chi.Router) {
r.Use(server.RealIPMiddleware, getPlayer(api.players))
r.Use(getPlayer(api.players))
h(r, "getPlaylists", api.GetPlaylists)
h(r, "getPlaylist", api.GetPlaylist)
h(r, "createPlaylist", api.CreatePlaylist)
@ -121,7 +119,7 @@ func (api *Router) routes() http.Handler {
h(r, "updatePlaylist", api.UpdatePlaylist)
})
r.Group(func(r chi.Router) {
r.Use(server.RealIPMiddleware, getPlayer(api.players))
r.Use(getPlayer(api.players))
h(r, "getBookmarks", api.GetBookmarks)
h(r, "createBookmark", api.CreateBookmark)
h(r, "deleteBookmark", api.DeleteBookmark)
@ -129,7 +127,7 @@ func (api *Router) routes() http.Handler {
h(r, "savePlayQueue", api.SavePlayQueue)
})
r.Group(func(r chi.Router) {
r.Use(server.RealIPMiddleware, getPlayer(api.players))
r.Use(getPlayer(api.players))
h(r, "search2", api.Search2)
h(r, "search3", api.Search3)
})
@ -158,7 +156,7 @@ func (api *Router) routes() http.Handler {
hr(r, "getCoverArt", api.GetCoverArt)
})
r.Group(func(r chi.Router) {
r.Use(server.RealIPMiddleware, getPlayer(api.players))
r.Use(getPlayer(api.players))
hr(r, "stream", api.Stream)
hr(r, "download", api.Download)
})