Skip to content

Commit

Permalink
middleware.RealIP: also check True-Client-IP request header (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Nov 18, 2021
1 parent 66d6a4d commit 991ad53
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions middleware/realip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"strings"
)

var trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
var xRealIP = http.CanonicalHeaderKey("X-Real-IP")

// RealIP is a middleware that sets a http.Request's RemoteAddr to the results
// of parsing either the X-Real-IP header or the X-Forwarded-For header (in that
// order).
// of parsing either the True-Client-IP, X-Real-IP or the X-Forwarded-For headers
// (in that order).
//
// This middleware should be inserted fairly early in the middleware stack to
// ensure that subsequent layers (e.g., request loggers) which examine the
Expand All @@ -40,7 +41,9 @@ func RealIP(h http.Handler) http.Handler {
func realIP(r *http.Request) string {
var ip string

if xrip := r.Header.Get(xRealIP); xrip != "" {
if tcip := r.Header.Get(trueClientIP); tcip != "" {
ip = tcip
} else if xrip := r.Header.Get(xRealIP); xrip != "" {
ip = xrip
} else if xff := r.Header.Get(xForwardedFor); xff != "" {
i := strings.Index(xff, ", ")
Expand Down

0 comments on commit 991ad53

Please sign in to comment.