Skip to content

Commit 991ad53

Browse files
authored
middleware.RealIP: also check True-Client-IP request header (#678)
1 parent 66d6a4d commit 991ad53

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

middleware/realip.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"strings"
99
)
1010

11+
var trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
1112
var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
1213
var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
1314

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

43-
if xrip := r.Header.Get(xRealIP); xrip != "" {
44+
if tcip := r.Header.Get(trueClientIP); tcip != "" {
45+
ip = tcip
46+
} else if xrip := r.Header.Get(xRealIP); xrip != "" {
4447
ip = xrip
4548
} else if xff := r.Header.Get(xForwardedFor); xff != "" {
4649
i := strings.Index(xff, ", ")

0 commit comments

Comments
 (0)