Skip to content

Commit

Permalink
Merge pull request #33 from delta10/fix/parse-x-forwarded-for
Browse files Browse the repository at this point in the history
fix: correctly parse X-Forwarded-For when there are multiple IPs
  • Loading branch information
bartjkdp authored Feb 20, 2024
2 parents 436d046 + 179ecff commit c024395
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ func EnvSubst(input string) string {
}

func ReadUserIP(r *http.Request) string {
IPAddress := r.Header.Get("X-Forwarded-For")
if IPAddress == "" {
host, _, _ := net.SplitHostPort(r.RemoteAddr)
IPAddress = host
forwardedFor := r.Header.Get("X-Forwarded-For")
if forwardedFor != "" {
ips := strings.Split(forwardedFor, ",")
return strings.TrimSpace(ips[0])
}
return IPAddress

host, _, _ := net.SplitHostPort(r.RemoteAddr)
return host
}

func StringInSlice(a string, list []string) bool {
Expand Down

0 comments on commit c024395

Please sign in to comment.