Skip to content

Commit e3c4f8d

Browse files
authored
feat: special case CloudFlare client IP addrs (#148)
1 parent 8cd8f9c commit e3c4f8d

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

httpbin/helpers.go

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func getClientIP(r *http.Request) string {
4949
if clientIP := r.Header.Get("Fly-Client-IP"); clientIP != "" {
5050
return clientIP
5151
}
52+
if clientIP := r.Header.Get("CF-Connecting-IP"); clientIP != "" {
53+
return clientIP
54+
}
5255

5356
// Try to pull a reasonable value from the X-Forwarded-For header, if
5457
// present, by taking the first entry in a comma-separated list of IPs.

httpbin/helpers_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ func TestGetClientIP(t *testing.T) {
235235
},
236236
want: "9.9.9.9",
237237
},
238+
"custom cloudflare header take precedence": {
239+
given: &http.Request{
240+
Header: makeHeaders(map[string]string{
241+
"CF-Connecting-IP": "9.9.9.9",
242+
"X-Forwarded-For": "1.1.1.1,2.2.2.2,3.3.3.3",
243+
}),
244+
RemoteAddr: "0.0.0.0",
245+
},
246+
want: "9.9.9.9",
247+
},
238248
"x-forwarded-for is parsed": {
239249
given: &http.Request{
240250
Header: makeHeaders(map[string]string{

0 commit comments

Comments
 (0)