diff options
| author | Henri Vasserman <henv@hot.ee> | 2025-03-28 19:38:34 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-28 13:38:34 -0400 |
| commit | 57c3e9f1b2c1d685472670b8ba2660144d2ae316 (patch) | |
| tree | b3e31374ca5b41ce9e1e16ee2b13752cc1ac7018 /internal/headers.go | |
| parent | e9a6ebffbb5cdd3d3f452610bda4371e51288058 (diff) | |
| download | anubis-57c3e9f1b2c1d685472670b8ba2660144d2ae316.tar.xz anubis-57c3e9f1b2c1d685472670b8ba2660144d2ae316.zip | |
Change how to make Anubis work without a reverse proxy (#86)
* Change how to make Anubis work without a reverse proxy
* Apply suggestions from code review
Co-authored-by: Xe Iaso <me@xeiaso.net>
Signed-off-by: Henri Vasserman <henv@hot.ee>
* add support for unix sockets.
* add env var docs
* lib: fix tests
Signed-off-by: Xe Iaso <me@xeiaso.net>
---------
Signed-off-by: Henri Vasserman <henv@hot.ee>
Signed-off-by: Xe Iaso <me@xeiaso.net>
Co-authored-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'internal/headers.go')
| -rw-r--r-- | internal/headers.go | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/internal/headers.go b/internal/headers.go index 681d076..9d6ba76 100644 --- a/internal/headers.go +++ b/internal/headers.go @@ -2,6 +2,7 @@ package internal import ( "log/slog" + "net" "net/http" "github.com/TecharoHQ/anubis" @@ -21,16 +22,29 @@ func UnchangingCache(next http.Handler) http.Handler { }) } -// DefaultXRealIP sets the X-Real-Ip header to the given value if and only if -// it is not an empty string. -func DefaultXRealIP(defaultIP string, next http.Handler) http.Handler { - if defaultIP == "" { - slog.Debug("skipping middleware, defaultIP is empty") +// RemoteXRealIP sets the X-Real-Ip header to the request's real IP if +// the setting is enabled by the user. +func RemoteXRealIP(useRemoteAddress bool, bindNetwork string, next http.Handler) http.Handler { + if useRemoteAddress == false { + slog.Debug("skipping middleware, useRemoteAddress is empty") return next } + if bindNetwork == "unix" { + // For local sockets there is no real remote address but the localhost + // address should be sensible. + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.Header.Set("X-Real-Ip", "127.0.0.1") + next.ServeHTTP(w, r) + }) + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - r.Header.Set("X-Real-Ip", defaultIP) + host, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + panic(err) // this should never happen + } + r.Header.Set("X-Real-Ip", host) next.ServeHTTP(w, r) }) } |
