Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting multiple XFF entries #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions realip.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func isPrivateAddress(address string) (bool, error) {
func FromRequest(r *http.Request) string {
// Fetch header value
xRealIP := r.Header.Get("X-Real-Ip")
xForwardedFor := r.Header.Get("X-Forwarded-For")
xForwardedFor, _ := r.Header["X-Forwarded-For"]

// If both empty, return IP from remote address
if xRealIP == "" && xForwardedFor == "" {
if xRealIP == "" && len(xForwardedFor) == 0 {
var remoteIP string

// If there are colon in remote address, remove the port number
Expand All @@ -71,11 +71,13 @@ func FromRequest(r *http.Request) string {
}

// Check list of IP in X-Forwarded-For and return the first global address
for _, address := range strings.Split(xForwardedFor, ",") {
address = strings.TrimSpace(address)
isPrivate, err := isPrivateAddress(address)
if !isPrivate && err == nil {
return address
for _, a := range xForwardedFor {
for _, b := range strings.Split(a, ",") {
address := strings.TrimSpace(b)
isPrivate, err := isPrivateAddress(address)
if !isPrivate && err == nil {
return address
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions realip_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package realip

import (
"fmt"
"net/http"
"testing"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestRealIP(t *testing.T) {
h := http.Header{}
h.Set("X-Real-IP", xRealIP)
for _, address := range xForwardedFor {
h.Set("X-Forwarded-For", address)
h.Add("X-Forwarded-For", address)
}

return &http.Request{
Expand All @@ -75,10 +76,18 @@ func TestRealIP(t *testing.T) {
name: "Has X-Forwarded-For",
request: newRequest("", "", publicAddr1),
expected: publicAddr1,
}, {
name: "Has X-Forwarded-For multiple IPs (comma separated)(",
request: newRequest("", "", fmt.Sprintf("%s,%s", localAddr, publicAddr1)),
expected: publicAddr1,
}, {
name: "Has X-Forwarded-For multiple IPs (comma and then space)",
request: newRequest("", "", fmt.Sprintf("%s, %s", localAddr, publicAddr1)),
expected: publicAddr1,
}, {
name: "Has multiple X-Forwarded-For",
request: newRequest("", "", localAddr, publicAddr1, publicAddr2),
expected: publicAddr2,
expected: publicAddr1,
}, {
name: "Has X-Real-IP",
request: newRequest("", publicAddr1),
Expand Down