Skip to content

Commit

Permalink
ratelimit: Use Go 1.18's net/netip (#16)
Browse files Browse the repository at this point in the history
Closes #15.
  • Loading branch information
RussellLuo authored Apr 19, 2023
1 parent 3497b19 commit ab1e18c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
2 changes: 0 additions & 2 deletions ratelimit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ require (
github.com/caddyserver/caddy/v2 v2.4.5
github.com/hashicorp/golang-lru v0.5.1
go.uber.org/zap v1.19.0
go4.org/intern v0.0.0-20220617035311-6925f38cc365 // indirect
inet.af/netaddr v0.0.0-20220811202034-502d2d690317
)
10 changes: 0 additions & 10 deletions ratelimit/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:Htrtb
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac h1:opbrjaN/L8gg6Xh5D04Tem+8xVcz6ajZlGCs49mQgyg=
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
Expand Down Expand Up @@ -1002,14 +1001,7 @@ go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
go.uber.org/zap v1.19.0 h1:mZQZefskPPCMIBCSEH0v2/iUqqLrYtaeqwD6FUGUnFE=
go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go4.org v0.0.0-20180809161055-417644f6feb5 h1:+hE86LblG4AyDgwMCLTE6FOlM9+qjHSYS+rKqxUVdsM=
go4.org v0.0.0-20180809161055-417644f6feb5/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE=
go4.org/intern v0.0.0-20211027215823-ae77deb06f29/go.mod h1:cS2ma+47FKrLPdXFpr7CuxiTW3eyJbWew4qx0qtQWDA=
go4.org/intern v0.0.0-20220617035311-6925f38cc365 h1:t9hFvR102YlOqU0fQn1wgwhNvSbHGBbbJxX9JKfU3l0=
go4.org/intern v0.0.0-20220617035311-6925f38cc365/go.mod h1:WXRv3p7T6gzt0CcJm43AAKdKVZmcQbwwC7EwquU5BZU=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20220617031537-928513b29760 h1:FyBZqvoA/jbNzuAWLQE2kG820zMAkcilx6BMjGbL/E4=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20220617031537-928513b29760/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
gocloud.dev v0.19.0/go.mod h1:SmKwiR8YwIMMJvQBKLsC3fHNyMwXLw3PMDO+VVteJMI=
golang.org/x/build v0.0.0-20190111050920-041ab4dc3f9d/go.mod h1:OWs+y06UdEOHN4y+MfF/py+xQ/tYqIWW03b70/CG9Rw=
golang.org/x/crypto v0.0.0-20180501155221-613d6eafa307/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down Expand Up @@ -1553,8 +1545,6 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
howett.net/plist v0.0.0-20181124034731-591f970eefbb h1:jhnBjNi9UFpfpl8YZhA9CrOqpnJdvzuiHsl/dnxl11M=
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
inet.af/netaddr v0.0.0-20220811202034-502d2d690317 h1:U2fwK6P2EqmopP/hFLTOAjWTki0qgd4GMJn5X8wOleU=
inet.af/netaddr v0.0.0-20220811202034-502d2d690317/go.mod h1:OIezDfdzOgFhuw4HuWapWq2e9l0H9tK4F1j+ETRtF3k=
pack.ag/amqp v0.11.2/go.mod h1:4/cbmt4EJXSKlG6LCfWHoqmN0uFdy5i/+YFz+fTfhV4=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
13 changes: 6 additions & 7 deletions ratelimit/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"net/http"
"net/netip"
"regexp"
"strconv"
"strings"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
"inet.af/netaddr"
)

var (
Expand Down Expand Up @@ -161,7 +161,7 @@ func (rl *RateLimit) ServeHTTP(w http.ResponseWriter, r *http.Request, next cadd
type Var struct {
Raw string
Name string
Bits uint8
Bits int
}

// ParseVar transforms shorthand variables into Caddy-style placeholders.
Expand All @@ -178,7 +178,6 @@ type Var struct {
// - `{remote.ip}`
// - `{remote.host_prefix.<bits>}`
// - `{remote.ip_prefix.<bits>}`
//
func ParseVar(s string) (*Var, error) {
v := &Var{Raw: s}
if regexpFullVar.MatchString(s) {
Expand Down Expand Up @@ -221,11 +220,11 @@ func ParseVar(s string) (*Var, error) {
if r[2] == "" {
return nil, fmt.Errorf("invalid key variable: %q", s)
}
bits, err := strconv.ParseUint(r[2], 10, 8)
bits, err := strconv.Atoi(r[2])
if err != nil {
return nil, err
}
v.Bits = uint8(bits)
v.Bits = bits
default:
return nil, fmt.Errorf("unrecognized key variable: %q", s)
}
Expand Down Expand Up @@ -264,7 +263,7 @@ func (v *Var) evaluatePrefix(r *http.Request, forwarded bool) (value string, err
return prefix.Masked().String(), nil
}

func getClientIP(r *http.Request, forwarded bool) (netaddr.IP, error) {
func getClientIP(r *http.Request, forwarded bool) (netip.Addr, error) {
remote := r.RemoteAddr
if forwarded {
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
Expand All @@ -275,7 +274,7 @@ func getClientIP(r *http.Request, forwarded bool) (netaddr.IP, error) {
if err != nil {
ipStr = remote // OK; probably didn't have a port
}
return netaddr.ParseIP(ipStr)
return netip.ParseAddr(ipStr)
}

func parseRate(rate string) (size time.Duration, limit int, err error) {
Expand Down

0 comments on commit ab1e18c

Please sign in to comment.