Skip to content

Commit 7cc3b67

Browse files
authored
fix: make sure that realIP is always set, even when not using CF
1 parent 220d5ab commit 7cc3b67

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

real_ip.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,17 @@ func (r *RealIPOverWriter) ServeHTTP(rw http.ResponseWriter, req *http.Request)
6565
}
6666
}
6767

68-
if realIP == "" {
68+
// Use `Cf-Connecting-Ip` when available
69+
if req.Header.Get(cfConnectingIP) != "" {
6970
realIP = req.Header.Get(cfConnectingIP)
7071
req.Header.Set(xForwardedFor, realIP)
7172
}
7273

74+
// If it is not possible to determine the real IP, the connecting IP is used as a fallback.
75+
if realIP == "" {
76+
realIP = req.RemoteAddr;
77+
}
78+
7379
req.Header.Set(xRealIP, realIP)
7480

7581
r.next.ServeHTTP(rw, req)

0 commit comments

Comments
 (0)