Skip to content

Commit

Permalink
Merge pull request #53 from ulule/dev
Browse files Browse the repository at this point in the history
Limiter v3.1.0
  • Loading branch information
novln committed Jan 17, 2019
2 parents 4499266 + 811a63c commit 4a9155b
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,24 @@ var (

// GetIP returns IP address from request.
func (limiter *Limiter) GetIP(r *http.Request) net.IP {
if limiter.Options.TrustForwardHeader {
return GetIP(r, limiter.Options)
}

// GetIPWithMask returns IP address from request by applying a mask.
func (limiter *Limiter) GetIPWithMask(r *http.Request) net.IP {
return GetIPWithMask(r, limiter.Options)
}

// GetIPKey extracts IP from request and returns hashed IP to use as store key.
func (limiter *Limiter) GetIPKey(r *http.Request) string {
return limiter.GetIPWithMask(r).String()
}

// GetIP returns IP address from request.
// If options is defined and TrustForwardHeader is true, it will lookup IP in
// X-Forwarded-For and X-Real-IP headers.
func GetIP(r *http.Request, options ...Options) net.IP {
if len(options) >= 1 && options[0].TrustForwardHeader {
ip := r.Header.Get("X-Forwarded-For")
if ip != "" {
parts := strings.SplitN(ip, ",", 2)
Expand All @@ -39,18 +56,17 @@ func (limiter *Limiter) GetIP(r *http.Request) net.IP {
}

// GetIPWithMask returns IP address from request by applying a mask.
func (limiter *Limiter) GetIPWithMask(r *http.Request) net.IP {
ip := limiter.GetIP(r)
func GetIPWithMask(r *http.Request, options ...Options) net.IP {
if len(options) == 0 {
return GetIP(r)
}

ip := GetIP(r, options[0])
if ip.To4() != nil {
return ip.Mask(limiter.Options.IPv4Mask)
return ip.Mask(options[0].IPv4Mask)
}
if ip.To16() != nil {
return ip.Mask(limiter.Options.IPv6Mask)
return ip.Mask(options[0].IPv6Mask)
}
return ip
}

// GetIPKey extracts IP from request and returns hashed IP to use as store key.
func (limiter *Limiter) GetIPKey(r *http.Request) string {
return limiter.GetIPWithMask(r).String()
}

0 comments on commit 4a9155b

Please sign in to comment.