Bumps the minor-and-patch group with 7 updates in the / directory: | Package | From | To | | --- | --- | --- | | [github.com/go-chi/httprate](https://github.com/go-chi/httprate) | `0.16.0` | `0.16.1` | | [github.com/go-ldap/ldap/v3](https://github.com/go-ldap/ldap) | `3.4.14` | `3.4.15` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.15.0` | `2.15.1` | | [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) | `1.24.1` | `1.25.0` | | [github.com/tidwall/gjson](https://github.com/tidwall/gjson) | `1.19.1` | `1.20.0` | | [go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp](https://github.com/open-telemetry/opentelemetry-go-contrib) | `0.71.0` | `0.72.0` | | [golang.org/x/sync](https://github.com/golang/sync) | `0.23.0` | `0.24.0` | Updates `github.com/go-chi/httprate` from 0.16.0 to 0.16.1 - [Release notes](https://github.com/go-chi/httprate/releases) - [Commits](https://github.com/go-chi/httprate/compare/v0.16.0...v0.16.1) Updates `github.com/go-ldap/ldap/v3` from 3.4.14 to 3.4.15 - [Release notes](https://github.com/go-ldap/ldap/releases) - [Commits](https://github.com/go-ldap/ldap/compare/v3.4.14...v3.4.15) Updates `github.com/nats-io/nats-server/v2` from 2.15.0 to 2.15.1 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.15.0...v2.15.1) Updates `github.com/prometheus/client_golang` from 1.24.1 to 1.25.0 - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/v1.25.0/CHANGELOG.md) - [Commits](https://github.com/prometheus/client_golang/compare/v1.24.1...v1.25.0) Updates `github.com/tidwall/gjson` from 1.19.1 to 1.20.0 - [Commits](https://github.com/tidwall/gjson/compare/v1.19.1...v1.20.0) Updates `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` from 0.71.0 to 0.72.0 - [Release notes](https://github.com/open-telemetry/opentelemetry-go-contrib/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-telemetry/opentelemetry-go-contrib/compare/zpages/v0.71.0...zpages/v0.72.0) Updates `golang.org/x/sync` from 0.23.0 to 0.24.0 - [Commits](https://github.com/golang/sync/compare/v0.23.0...v0.24.0) --- updated-dependencies: - dependency-name: github.com/go-chi/httprate dependency-version: 0.16.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/go-ldap/ldap/v3 dependency-version: 3.4.15 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.15.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/prometheus/client_golang dependency-version: 1.25.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/tidwall/gjson dependency-version: 1.20.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp dependency-version: 0.72.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: golang.org/x/sync dependency-version: 0.24.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
169 lines
5.2 KiB
Go
169 lines
5.2 KiB
Go
package httprate
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// LimitBy is the canonical entry point for rate-limiting by an explicit key.
|
|
//
|
|
// It is shorthand for Limit with keyFn installed as the rate-limit key. The
|
|
// key is a required positional argument, so every call site has to state, on
|
|
// purpose, what it rate-limits by.
|
|
//
|
|
// To rate-limit by a trusted client IP behind a proxy, resolve the IP with one
|
|
// of chi's middleware.ClientIPFrom* middlewares (chi v5.3.0+) and read it back
|
|
// in the KeyFunc; CanonicalizeIP buckets IPv6 clients by their /64:
|
|
//
|
|
// r.Use(middleware.ClientIPFromXFF("10.0.0.0/8"))
|
|
// r.Use(httprate.LimitBy(100, time.Minute, func(r *http.Request) (string, error) {
|
|
// return httprate.CanonicalizeIP(middleware.GetClientIP(r.Context())), nil
|
|
// }))
|
|
//
|
|
// Use JoinKeys to rate-limit by more than one dimension at once:
|
|
//
|
|
// r.Use(httprate.LimitBy(100, time.Minute,
|
|
// httprate.JoinKeys(clientIPKey, httprate.KeyByEndpoint)))
|
|
func LimitBy(requestLimit int, windowLength time.Duration, keyFn KeyFunc, options ...Option) func(next http.Handler) http.Handler {
|
|
return NewRateLimiter(requestLimit, windowLength, append([]Option{WithKeyFuncs(keyFn)}, options...)...).Handler
|
|
}
|
|
|
|
type KeyFunc func(r *http.Request) (string, error)
|
|
type Option func(rl *RateLimiter)
|
|
|
|
// Set custom response headers. If empty, the header is omitted.
|
|
type ResponseHeaders struct {
|
|
Limit string // Default: X-RateLimit-Limit
|
|
Remaining string // Default: X-RateLimit-Remaining
|
|
Increment string // Default: X-RateLimit-Increment
|
|
Reset string // Default: X-RateLimit-Reset
|
|
RetryAfter string // Default: Retry-After
|
|
}
|
|
|
|
func Key(key string) func(r *http.Request) (string, error) {
|
|
return func(r *http.Request) (string, error) {
|
|
return key, nil
|
|
}
|
|
}
|
|
|
|
// CanonicalizeIP normalizes a client IP string for use as a rate-limit key:
|
|
//
|
|
// - IPv4 addresses are returned unchanged. IPv4 mapped IPv6 addresses use
|
|
// their dotted IPv4 form.
|
|
// - IPv6 addresses are reduced to their /64 prefix. An IPv6 client typically
|
|
// controls a whole /64 (2^64 addresses via SLAAC), so keying on the full
|
|
// address would let it rotate within its own /64 to win a fresh bucket per
|
|
// request and bypass a per-IP limit. Widen/narrow the prefix yourself if your
|
|
// clients are delegated a larger block (e.g. a /56 or /48).
|
|
// - Any other string, including "", is returned unchanged.
|
|
//
|
|
// httprate stays router-agnostic, so it does not resolve the client IP for you —
|
|
// pair CanonicalizeIP with whatever does. With chi's middleware.ClientIPFrom*
|
|
// (chi v5.3.0+) and middleware.GetClientIP:
|
|
//
|
|
// r.Use(middleware.ClientIPFromXFF("10.0.0.0/8"))
|
|
// r.Use(httprate.LimitBy(100, time.Minute, func(r *http.Request) (string, error) {
|
|
// return httprate.CanonicalizeIP(middleware.GetClientIP(r.Context())), nil
|
|
// }))
|
|
//
|
|
// WARNING: if the resolver returns "" (e.g. no ClientIPFrom* middleware is
|
|
// installed upstream), CanonicalizeIP returns "" and every request shares a
|
|
// single global rate-limit bucket. Strictly more restrictive, but a footgun —
|
|
// make sure the client IP is actually resolved upstream.
|
|
func CanonicalizeIP(ip string) string {
|
|
isIPv6 := false
|
|
// This is how net.ParseIP decides if an address is IPv6
|
|
// https://cs.opensource.google/go/go/+/refs/tags/go1.17.7:src/net/ip.go;l=704
|
|
for i := 0; !isIPv6 && i < len(ip); i++ {
|
|
switch ip[i] {
|
|
case '.':
|
|
// IPv4
|
|
return ip
|
|
case ':':
|
|
// IPv6
|
|
isIPv6 = true
|
|
}
|
|
}
|
|
if !isIPv6 {
|
|
// Not an IP address at all
|
|
return ip
|
|
}
|
|
|
|
ipv6 := net.ParseIP(ip)
|
|
if ipv6 == nil {
|
|
return ip
|
|
}
|
|
if ipv4 := ipv6.To4(); ipv4 != nil {
|
|
return ipv4.String()
|
|
}
|
|
|
|
return ipv6.Mask(net.CIDRMask(64, 128)).String()
|
|
}
|
|
|
|
func KeyByEndpoint(r *http.Request) (string, error) {
|
|
return r.URL.Path, nil
|
|
}
|
|
|
|
func WithKeyFuncs(keyFuncs ...KeyFunc) Option {
|
|
return func(rl *RateLimiter) {
|
|
if len(keyFuncs) > 0 {
|
|
rl.keyFn = JoinKeys(keyFuncs...)
|
|
}
|
|
}
|
|
}
|
|
|
|
func WithLimitHandler(h http.HandlerFunc) Option {
|
|
return func(rl *RateLimiter) {
|
|
rl.onRateLimited = h
|
|
}
|
|
}
|
|
|
|
func WithErrorHandler(h func(http.ResponseWriter, *http.Request, error)) Option {
|
|
return func(rl *RateLimiter) {
|
|
rl.onError = h
|
|
}
|
|
}
|
|
|
|
func WithLimitCounter(c LimitCounter) Option {
|
|
return func(rl *RateLimiter) {
|
|
rl.limitCounter = c
|
|
}
|
|
}
|
|
|
|
func WithResponseHeaders(headers ResponseHeaders) Option {
|
|
return func(rl *RateLimiter) {
|
|
rl.headers = headers
|
|
}
|
|
}
|
|
|
|
func WithNoop() Option {
|
|
return func(rl *RateLimiter) {}
|
|
}
|
|
|
|
// JoinKeys joins the results of several KeyFuncs into a single key with ":"
|
|
// separators, so they can be passed to LimitBy's positional key slot for
|
|
// multi-dimensional rate-limiting:
|
|
//
|
|
// r.Use(httprate.LimitBy(100, time.Minute,
|
|
// httprate.JoinKeys(clientIPKey, httprate.KeyByEndpoint)))
|
|
//
|
|
// where clientIPKey is your own client-IP KeyFunc (see LimitBy and
|
|
// CanonicalizeIP). It is the positional-argument equivalent of WithKeyFuncs. If any component
|
|
// KeyFunc returns an error, the joined key returns that error.
|
|
func JoinKeys(fns ...KeyFunc) KeyFunc {
|
|
return func(r *http.Request) (string, error) {
|
|
var key strings.Builder
|
|
for i := 0; i < len(fns); i++ {
|
|
k, err := fns[i](r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
key.WriteString(k)
|
|
key.WriteRune(':')
|
|
}
|
|
return key.String(), nil
|
|
}
|
|
}
|