Skip to content

Commit 00a70e7

Browse files
authored
feat: support Fastly and Akamai headers for client IP addr (#167)
Add support for Fastly and Akamai custom headres to get a reasonable value of the client ip address. Co-authored-by: haccht <[email protected]>
1 parent f6ce865 commit 00a70e7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

httpbin/helpers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ func getClientIP(r *http.Request) string {
4949
if clientIP := r.Header.Get("CF-Connecting-IP"); clientIP != "" {
5050
return clientIP
5151
}
52+
if clientIP := r.Header.Get("Fastly-Client-IP"); clientIP != "" {
53+
return clientIP
54+
}
55+
if clientIP := r.Header.Get("True-Client-IP"); clientIP != "" {
56+
return clientIP
57+
}
5258

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

httpbin/helpers_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,26 @@ func TestGetClientIP(t *testing.T) {
247247
},
248248
want: "9.9.9.9",
249249
},
250+
"custom fastly header take precedence": {
251+
given: &http.Request{
252+
Header: makeHeaders(map[string]string{
253+
"Fastly-Client-IP": "9.9.9.9",
254+
"X-Forwarded-For": "1.1.1.1,2.2.2.2,3.3.3.3",
255+
}),
256+
RemoteAddr: "0.0.0.0",
257+
},
258+
want: "9.9.9.9",
259+
},
260+
"custom akamai header take precedence": {
261+
given: &http.Request{
262+
Header: makeHeaders(map[string]string{
263+
"True-Client-IP": "9.9.9.9",
264+
"X-Forwarded-For": "1.1.1.1,2.2.2.2,3.3.3.3",
265+
}),
266+
RemoteAddr: "0.0.0.0",
267+
},
268+
want: "9.9.9.9",
269+
},
250270
"x-forwarded-for is parsed": {
251271
given: &http.Request{
252272
Header: makeHeaders(map[string]string{

0 commit comments

Comments
 (0)