mirror of https://github.com/dirtbags/moth.git
Make X-Forwarded-For handling an optional flag
This commit is contained in:
parent
cf72f8f253
commit
d9277ad423
|
@ -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)
|
||||
|
|
|
@ -25,6 +25,7 @@ type Instance struct {
|
|||
StateDir string
|
||||
ThemeDir string
|
||||
AttemptInterval time.Duration
|
||||
UseXForwarded bool
|
||||
|
||||
Runtime RuntimeConfig
|
||||
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue