Add Custom header usage to find the user IP

This commit is contained in:
MathieuHa
2022-10-15 18:49:41 +02:00
parent 552b30a9ef
commit 8eec1c5656
3 changed files with 14 additions and 11 deletions
+3 -10
View File
@@ -80,16 +80,8 @@ func parseIP(addr string) (net.IP, error) {
return userIP, nil
}
// STRATEGY
const (
xForwardedFor = "X-Forwarded-For"
)
// PoolStrategy is a strategy based on an IP Checker.
// It allows to check whether addresses are in a given pool of IPs.
type PoolStrategy struct {
@@ -99,12 +91,13 @@ type PoolStrategy struct {
// GetIP checks the list of Forwarded IPs (most recent first) against the
// Checker pool of IPs. It returns the first IP that is not in the pool, or the
// empty string otherwise.
func (s *PoolStrategy) GetIP(req *http.Request) string {
func (s *PoolStrategy) GetIP(req *http.Request, customHeader string) string {
if s.Checker == nil {
return ""
}
xff := req.Header.Get(xForwardedFor)
xff := req.Header.Get(customHeader)
xffs := strings.Split(xff, ",")
for i := len(xffs) - 1; i >= 0; i-- {