forked from quay/clair
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quay#410 from KeyboardNerd/xforward
api: fix remote addr shows reverse proxy addr problem
- Loading branch information
Showing
3 changed files
with
28 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package httputil | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// GetClientAddr returns the first value in X-Forwarded-For if it exists | ||
// otherwise fall back to use RemoteAddr | ||
func GetClientAddr(r *http.Request) string { | ||
addr := r.RemoteAddr | ||
if s := r.Header.Get("X-Forwarded-For"); s != "" { | ||
ips := strings.Split(s, ",") | ||
// assume the first one is the client address | ||
if len(ips) != 0 { | ||
// validate the ip | ||
if realIP := net.ParseIP(ips[0]); realIP != nil { | ||
addr = strings.TrimSpace(ips[0]) | ||
} | ||
} | ||
} | ||
return addr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters