Skip to content

Commit

Permalink
feat: support Fastly and Akamai headers for client IP addr (#167)
Browse files Browse the repository at this point in the history
Add support for Fastly and Akamai custom headres to get a reasonable
value of the client ip address.

Co-authored-by: haccht <haccht@users.noreply.github.com>
  • Loading branch information
haccht and haccht authored Feb 18, 2024
1 parent f6ce865 commit 00a70e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions httpbin/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func getClientIP(r *http.Request) string {
if clientIP := r.Header.Get("CF-Connecting-IP"); clientIP != "" {
return clientIP
}
if clientIP := r.Header.Get("Fastly-Client-IP"); clientIP != "" {
return clientIP
}
if clientIP := r.Header.Get("True-Client-IP"); clientIP != "" {
return clientIP
}

// Try to pull a reasonable value from the X-Forwarded-For header, if
// present, by taking the first entry in a comma-separated list of IPs.
Expand Down
20 changes: 20 additions & 0 deletions httpbin/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@ func TestGetClientIP(t *testing.T) {
},
want: "9.9.9.9",
},
"custom fastly header take precedence": {
given: &http.Request{
Header: makeHeaders(map[string]string{
"Fastly-Client-IP": "9.9.9.9",
"X-Forwarded-For": "1.1.1.1,2.2.2.2,3.3.3.3",
}),
RemoteAddr: "0.0.0.0",
},
want: "9.9.9.9",
},
"custom akamai header take precedence": {
given: &http.Request{
Header: makeHeaders(map[string]string{
"True-Client-IP": "9.9.9.9",
"X-Forwarded-For": "1.1.1.1,2.2.2.2,3.3.3.3",
}),
RemoteAddr: "0.0.0.0",
},
want: "9.9.9.9",
},
"x-forwarded-for is parsed": {
given: &http.Request{
Header: makeHeaders(map[string]string{
Expand Down

0 comments on commit 00a70e7

Please sign in to comment.