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),
}
clientIP := r.Header.Get("X-Forwarded-For")
clientIP = strings.Split(clientIP, ", ")[0]
clientIP := r.RemoteAddr
if clientIP == "" {
clientIP = r.RemoteAddr
if (ctx.UseXForwarded) {
forwardedIP := r.Header.Get("X-Forwarded-For")
forwardedIP = strings.Split(forwardedIP, ", ")[0]
if forwardedIP != "" {
clientIP = forwardedIP
}
}
ctx.mux.ServeHTTP(w, r)

View File

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

View File

@ -52,6 +52,12 @@ func main() {
20*time.Second,
"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",
":8080",