Make X-Forwarded-For handling an optional flag

This commit is contained in:
John Donaldson 2020-03-05 03:22:34 +00:00
parent cf72f8f253
commit d9277ad423
3 changed files with 15 additions and 4 deletions

View File

@ -340,11 +340,15 @@ func (ctx *Instance) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
statusCode: new(int), statusCode: new(int),
} }
clientIP := r.Header.Get("X-Forwarded-For") clientIP := r.RemoteAddr
clientIP = strings.Split(clientIP, ", ")[0]
if clientIP == "" { if (ctx.UseXForwarded) {
clientIP = r.RemoteAddr forwardedIP := r.Header.Get("X-Forwarded-For")
forwardedIP = strings.Split(forwardedIP, ", ")[0]
if forwardedIP != "" {
clientIP = forwardedIP
}
} }
ctx.mux.ServeHTTP(w, r) ctx.mux.ServeHTTP(w, r)

View File

@ -25,6 +25,7 @@ type Instance struct {
StateDir string StateDir string
ThemeDir string ThemeDir string
AttemptInterval time.Duration AttemptInterval time.Duration
UseXForwarded bool
Runtime RuntimeConfig Runtime RuntimeConfig

View File

@ -52,6 +52,12 @@ func main() {
20*time.Second, 20*time.Second,
"Time between maintenance tasks", "Time between maintenance tasks",
) )
flag.BoolVar(
&ctx.UseXForwarded,
"x-forwarded-for",
false,
"Emit IPs from the X-Forwarded-For header in logs, when available, instead of the source IP. Use this when running behind a load-balancer or proxy",
)
listen := flag.String( listen := flag.String(
"listen", "listen",
":8080", ":8080",