| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-11-06 23:37:41 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-11-06 23:57:34 UTC |
| parent | 90e8cd50a6f38d9ce59ca1e3a6e90a9248836b2c |
| ipratelimit/ipratelimit.go | +23 | -21 |
| ipratelimit/ipratelimit_test.go | +217 | -69 |
| server/http.go | +4 | -16 |
| server/raw.go | +1 | -1 |
| server/server_test.go | +81 | -0 |
diff --git a/ipratelimit/ipratelimit.go b/ipratelimit/ipratelimit.go index a7642e6..5198c83 100644 --- a/ipratelimit/ipratelimit.go +++ b/ipratelimit/ipratelimit.go @@ -28,8 +28,7 @@ package ipratelimit // blitiri.com.ar/go/gofer/ipratelimit import ( "encoding/binary" "fmt" - "math/big" - "net" + "net/netip" "strings" "sync" "time" @@ -69,16 +68,17 @@ import ( // some requests differently). // Convert an IPv4 to our uint64 representation. -// ip4 MUST be an ipv4 address. It is not checked for performance reasons -// (this improves ipv4 performance by 1.5% to 3.5%). -func ipv4ToUint64(ip4 net.IP) uint64 { - return uint64(binary.BigEndian.Uint32(ip4)) +// ip4 MUST be an ipv4 address, otherwise this will panic. +func ipv4ToUint64(ip4 netip.Addr) uint64 { + b := ip4.As4() + return uint64(binary.BigEndian.Uint32(b[:])) } // Convert an IPv6 to a set of uint64 representations, one for /48, /56, and // /64, as described above. -func ipv6ExtractMasks(ip net.IP) (ip48, ip56, ip64 uint64) { - ip64 = binary.BigEndian.Uint64(ip[0:8]) +func ipv6ExtractMasks(ip netip.Addr) (ip48, ip56, ip64 uint64) { + b := ip.As16() + ip64 = binary.BigEndian.Uint64(b[0:8]) ip56 = ip64 & 0xffff_ffff_ffff_ff00 ip48 = ip64 & 0xffff_ffff_ffff_0000 return @@ -315,10 +315,11 @@ func (l *Limiter) SetIPv6s48Rate(req uint64, per time.Duration) { } // Allow checks if the given IP address is allowed to make a request. -func (l *Limiter) Allow(ip net.IP) bool { - if ip4 := ip.To4(); ip4 != nil { +func (l *Limiter) Allow(ip netip.Addr) bool { + ip = ip.Unmap() + if ip.Is4() { // Convert the IPv4 address to a 64-bit integer, and use that as key. - return l.ipv4.allow(ipv4ToUint64(ip4)) + return l.ipv4.allow(ipv4ToUint64(ip)) } // Check if the three masks for ipv6. All must be allowed for the request @@ -327,7 +328,7 @@ func (l *Limiter) Allow(ip net.IP) bool { return a48 && a56 && a64 } -func (l *Limiter) allowV6(ip net.IP) (a48, a56, a64 bool) { +func (l *Limiter) allowV6(ip netip.Addr) (a48, a56, a64 bool) { ip48, ip56, ip64 := ipv6ExtractMasks(ip) return l.ip48.allow(ip48), l.ip56.allow(ip56), l.ip64.allow(ip64) } @@ -352,7 +353,7 @@ func (l *Limiter) DebugString() string { return s } -func (l *limiter) debugString(kToIP func(uint64) net.IP) string { +func (l *limiter) debugString(kToIP func(uint64) netip.Addr) string { l.mu.Lock() defer l.mu.Unlock() @@ -390,7 +391,7 @@ func (l *Limiter) DebugHTML() string { return s } -func (l *limiter) debugHTML(kToIP func(uint64) net.IP) string { +func (l *limiter) debugHTML(kToIP func(uint64) netip.Addr) string { l.mu.Lock() defer l.mu.Unlock() @@ -420,15 +421,16 @@ func (l *limiter) debugHTML(kToIP func(uint64) net.IP) string { return s } -func kToIPv4(k uint64) net.IP { - return net.IPv4(byte(k>>24), byte(k>>16), byte(k>>8), byte(k)) +func kToIPv4(k uint64) netip.Addr { + var b [4]byte + binary.BigEndian.PutUint32(b[:], uint32(k)) + return netip.AddrFrom4(b) } -func kToIPv6(k uint64) net.IP { - buf := make([]byte, 16) - b := big.NewInt(0).SetUint64(k) - b = b.Lsh(b, 64) - return net.IP(b.FillBytes(buf[:])) +func kToIPv6(k uint64) netip.Addr { + var b [16]byte + binary.BigEndian.PutUint64(b[:8], k) + return netip.AddrFrom16(b) } // miniTime is a small representation of time, as the number of nanoseconds diff --git a/ipratelimit/ipratelimit_test.go b/ipratelimit/ipratelimit_test.go index d7084a0..7c76106 100644 --- a/ipratelimit/ipratelimit_test.go +++ b/ipratelimit/ipratelimit_test.go @@ -1,23 +1,28 @@ package ipratelimit import ( - "net" + "net/netip" + "sync/atomic" "testing" "time" "github.com/google/go-cmp/cmp" ) +func ipv4(a, b, c, d byte) netip.Addr { + return netip.AddrFrom4([4]byte{a, b, c, d}) +} + func TestIPv4ToUint32(t *testing.T) { cases := []struct { - ip net.IP + ip netip.Addr expected uint64 }{ - {net.IPv4(0, 0, 0, 0), 0}, - {net.IPv4(1, 2, 3, 4), 0x01020304}, + {ipv4(0, 0, 0, 0), 0}, + {ipv4(1, 2, 3, 4), 0x01020304}, } for _, c := range cases { - v := ipv4ToUint64(c.ip.To4()) + v := ipv4ToUint64(c.ip) if v != c.expected { t.Errorf("IPv4ToUint32(%v) -> %v, expected %v", c.ip, v, c.expected) @@ -42,7 +47,7 @@ func TestIPv6ExtractMasks(t *testing.T) { } for _, c := range cases { - ip := net.ParseIP(c.ip) + ip := netip.MustParseAddr(c.ip) ip48, ip56, ip64 := ipv6ExtractMasks(ip) if ip48 != c.eip48 || ip56 != c.eip56 || ip64 != c.eip64 { t.Errorf("IP %q (%v)", c.ip, ip) @@ -81,7 +86,7 @@ func TestBasic(t *testing.T) { } for _, c := range cases { l := NewLimiter(c.reqs, c.period, 256) - ip := net.IPv4(1, 2, 3, 4) + ip := ipv4(1, 2, 3, 4) as := []bool{} for i := uint64(0); i < c.pkts; i++ { as = append(as, l.Allow(ip)) @@ -109,7 +114,7 @@ func TestBasicIPv6(t *testing.T) { l := NewLimiter(1, time.Second, 256) for i, op := range operations { - ip := net.ParseIP(op.ip) + ip := netip.MustParseAddr(op.ip) allowed := l.Allow(ip) if allowed != op.allowed { t.Errorf("operation %d: Allow(%v) -> %v, expected %v", @@ -121,21 +126,21 @@ func TestBasicIPv6(t *testing.T) { func TestIPv6Subnetting(t *testing.T) { // These two are equal in the first 64 bits, and differ at the end. // So they should be counted as the same at all levels. - ip64a := net.ParseIP("1111:1111:1111:1111:aaaa::a") - ip64b := net.ParseIP("1111:1111:1111:1111:bbbb::b") + ip64a := netip.MustParseAddr("1111:1111:1111:1111:aaaa::a") + ip64b := netip.MustParseAddr("1111:1111:1111:1111:bbbb::b") // These two are equal in the first 56 bits, and differ at the end. // So they should be counted as the same for /48 and /56, but not /64. - ip56a := net.ParseIP("2222:2222:2222:22aa::a") - ip56b := net.ParseIP("2222:2222:2222:22bb::b") + ip56a := netip.MustParseAddr("2222:2222:2222:22aa::a") + ip56b := netip.MustParseAddr("2222:2222:2222:22bb::b") // These two are equal in the first 48 bits, and differ at the end. // So they should be counted as the same for /48, but not /56 or /64. - ip48a := net.ParseIP("3333:3333:3333:aaaa::a") - ip48b := net.ParseIP("3333:3333:3333:bbbb::b") + ip48a := netip.MustParseAddr("3333:3333:3333:aaaa::a") + ip48b := netip.MustParseAddr("3333:3333:3333:bbbb::b") operations := []struct { - ip net.IP + ip netip.Addr a48, a56, a64 bool }{ {ip64a, true, true, true}, @@ -175,8 +180,8 @@ func TestSize(t *testing.T) { // First, run up to size to fill in the map. for ; i < size+1; i++ { - ip := net.IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)) - if !l.allow(ipv4ToUint64(ip.To4())) { + ip := ipv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)) + if !l.allow(ipv4ToUint64(ip)) { t.Errorf("size %d, IP %v, i %d: not allowed", size, ip, i) } @@ -189,8 +194,8 @@ func TestSize(t *testing.T) { // Now do another size iterations, checking that the size of the maps // stays constant. for ; i < (size+1)*2; i++ { - ip := net.IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)) - if !l.allow(ipv4ToUint64(ip.To4())) { + ip := ipv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)) + if !l.allow(ipv4ToUint64(ip)) { t.Errorf("size %d, IP %v, i %d: not allowed", size, ip, i) } @@ -203,46 +208,46 @@ func TestSize(t *testing.T) { } func TestLRU(t *testing.T) { - ip1 := net.IPv4(1, 1, 1, 1) - ip2 := net.IPv4(2, 2, 2, 2) - ip3 := net.IPv4(3, 3, 3, 3) - ip4 := net.IPv4(4, 4, 4, 4) + ip1 := ipv4(1, 1, 1, 1) + ip2 := ipv4(2, 2, 2, 2) + ip3 := ipv4(3, 3, 3, 3) + ip4 := ipv4(4, 4, 4, 4) // We're going to do a sequence of allow() calls, and check that the LRU // list is as we expect after each one. operations := []struct { - ip net.IP - lru []net.IP + ip netip.Addr + lru []netip.Addr }{ - {ip1, []net.IP{ip1}}, + {ip1, []netip.Addr{ip1}}, // Bump ip1 (it is a special case when there's only one element). - {ip1, []net.IP{ip1}}, + {ip1, []netip.Addr{ip1}}, // Add ip2 and ip3, all straightforward. - {ip2, []net.IP{ip2, ip1}}, - {ip3, []net.IP{ip3, ip2, ip1}}, + {ip2, []netip.Addr{ip2, ip1}}, + {ip3, []netip.Addr{ip3, ip2, ip1}}, // Add ip4, evict ip1 which is the oldest. - {ip4, []net.IP{ip4, ip3, ip2}}, + {ip4, []netip.Addr{ip4, ip3, ip2}}, // Add ip1, evict ip2 which is the oldest. - {ip1, []net.IP{ip1, ip4, ip3}}, + {ip1, []netip.Addr{ip1, ip4, ip3}}, // Bump ip3 (last one), twice in a row. - {ip3, []net.IP{ip3, ip1, ip4}}, - {ip3, []net.IP{ip3, ip1, ip4}}, + {ip3, []netip.Addr{ip3, ip1, ip4}}, + {ip3, []netip.Addr{ip3, ip1, ip4}}, // Bump ip1 (middle one). - {ip1, []net.IP{ip1, ip3, ip4}}, + {ip1, []netip.Addr{ip1, ip3, ip4}}, } l := newlimiter(1, 0, 3) for i, op := range operations { - l.allow(ipv4ToUint64(op.ip.To4())) + l.allow(ipv4ToUint64(op.ip)) lru := getLRU(l) - if diff := cmp.Diff(op.lru, lru); diff != "" { + if diff := cmp.Diff(op.lru, lru, cmpAddr); diff != "" { t.Errorf("operation %d: allow(%v)", i, op.ip) t.Errorf(" expected LRU %v, got %v", op.lru, lru) t.Errorf(" diff (-want +got):\n%s", diff) @@ -250,12 +255,14 @@ func TestLRU(t *testing.T) { } } -func getLRU(l *limiter) []net.IP { - r := []net.IP{} +// cmpAddr lets cmp compare netip.Addr, which has unexported fields. +var cmpAddr = cmp.Comparer(func(a, b netip.Addr) bool { return a == b }) + +func getLRU(l *limiter) []netip.Addr { + r := []netip.Addr{} k := l.lruFirst for k != 0 { - ip := net.IPv4(byte(k>>24), byte(k>>16), byte(k>>8), byte(k)) - r = append(r, ip) + r = append(r, kToIPv4(k)) k = l.m[k].lruNext } return r @@ -267,8 +274,8 @@ func TestZeroKey(t *testing.T) { t.Errorf("allow(0) = false, want true") } lru := getLRU(l) - expected := []net.IP{net.IPv4(0, 0, 0, 1)} - if diff := cmp.Diff(expected, lru); diff != "" { + expected := []netip.Addr{ipv4(0, 0, 0, 1)} + if diff := cmp.Diff(expected, lru, cmpAddr); diff != "" { t.Errorf("allow(0):") t.Errorf(" expected LRU %v, got %v", expected, lru) t.Errorf(" diff (-want +got):\n%s", diff) @@ -349,37 +356,106 @@ func TestSetIPv6Rates(t *testing.T) { check(l.ip48, 48, time.Second/48) } +// TestAddrForms checks that different forms of an address are all treated as +// the same address. +func TestAddrForms(t *testing.T) { + cases := []struct{ a, b string }{ + // IPv4 and IPv4-mapped IPv6. + {"1.2.3.4", "::ffff:1.2.3.4"}, + {"::ffff:1.2.3.4", "1.2.3.4"}, + + // Zones are ignored. + {"fe80::1%eth0", "fe80::1"}, + {"fe80::1%eth0", "fe80::1%eth1"}, + } + for _, c := range cases { + l := NewLimiter(1, time.Hour, 3) + if !l.Allow(netip.MustParseAddr(c.a)) { + t.Errorf("Allow(%q) = false, want true", c.a) + } + if l.Allow(netip.MustParseAddr(c.b)) { + t.Errorf("Allow(%q) after %q = true, want false", c.b, c.a) + } + } + + // An IPv4 address embedded in IPv6 without the mapping is not IPv4. + l := NewLimiter(1, time.Hour, 3) + l.Allow(netip.MustParseAddr("1.2.3.4")) + if !l.Allow(netip.MustParseAddr("::1.2.3.4")) { + t.Errorf("Allow(::1.2.3.4) after 1.2.3.4 = false, want true") + } +} + +// The zero netip.Addr is not expected, but it must not panic. +func TestInvalidAddr(t *testing.T) { + l := NewLimiter(1, time.Hour, 3) + if !l.Allow(netip.Addr{}) { + t.Errorf("Allow(Addr{}) = false, want true") + } +} + +func TestKToIP(t *testing.T) { + v4 := []struct { + k uint64 + want string + }{ + {1, "0.0.0.1"}, + {0x01020304, "1.2.3.4"}, + {0xffffffff, "255.255.255.255"}, + } + for _, c := range v4 { + if got := kToIPv4(c.k).String(); got != c.want { + t.Errorf("kToIPv4(%x) = %q, want %q", c.k, got, c.want) + } + } + + v6 := []struct { + k uint64 + want string + }{ + {1, "0:0:0:1::"}, + {0x1111222233334444, "1111:2222:3333:4444::"}, + {0x1111222233330000, "1111:2222:3333::"}, + {0xffffffffffffffff, "ffff:ffff:ffff:ffff::"}, + } + for _, c := range v6 { + if got := kToIPv6(c.k).String(); got != c.want { + t.Errorf("kToIPv6(%x) = %q, want %q", c.k, got, c.want) + } + } +} + func TestDebugString(t *testing.T) { l := NewLimiter(1, time.Second, 3) - l.Allow(net.IPv4(1, 1, 1, 1)) - l.Allow(net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888")) + l.Allow(ipv4(1, 1, 1, 1)) + l.Allow(netip.MustParseAddr("1111:2222:3333:4444:5555:6666:7777:8888")) t.Log(l.DebugString()) } func TestDebugHTML(t *testing.T) { l := NewLimiter(1, time.Second, 3) - l.Allow(net.IPv4(1, 1, 1, 1)) - l.Allow(net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888")) + l.Allow(ipv4(1, 1, 1, 1)) + l.Allow(netip.MustParseAddr("1111:2222:3333:4444:5555:6666:7777:8888")) t.Log(l.DebugHTML()) } func BenchmarkDifferentIPv4_256(b *testing.B) { l := NewLimiter(1, time.Second, 256) for i := 0; b.Loop(); i++ { - l.Allow(net.IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i))) + l.Allow(ipv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i))) } } func BenchmarkDifferentIPv4_10000(b *testing.B) { l := NewLimiter(1, time.Second, 10000) for i := 0; b.Loop(); i++ { - l.Allow(net.IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i))) + l.Allow(ipv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i))) } } func BenchmarkSameIPv4_Strict(b *testing.B) { l := NewLimiter(1, time.Second, 256) - ip := net.IPv4(1, 2, 3, 4) + ip := ipv4(1, 2, 3, 4) for b.Loop() { l.Allow(ip) } @@ -387,7 +463,7 @@ func BenchmarkSameIPv4_Strict(b *testing.B) { func BenchmarkSameIPv4_Bursty(b *testing.B) { l := NewLimiter(100, time.Second, 256) - ip := net.IPv4(1, 2, 3, 4) + ip := ipv4(1, 2, 3, 4) for b.Loop() { l.Allow(ip) } @@ -395,7 +471,7 @@ func BenchmarkSameIPv4_Bursty(b *testing.B) { func BenchmarkSameIPv4_Strict_Parallel(b *testing.B) { l := NewLimiter(1, time.Second, 256) - ip := net.IPv4(1, 2, 3, 4) + ip := ipv4(1, 2, 3, 4) b.RunParallel(func(pb *testing.PB) { for pb.Next() { l.Allow(ip) @@ -405,33 +481,21 @@ func BenchmarkSameIPv4_Strict_Parallel(b *testing.B) { func BenchmarkDifferentIPv6_256(b *testing.B) { l := NewLimiter(1, time.Second, 256) - ip := net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888") for i := 0; b.Loop(); i++ { - // Change the IP at different levels, so they don't all fall under the - // same /64, /56, /48. - ip[7] = byte(i) - ip[6] = byte(i >> 8) - ip[5] = byte(i) - ip[4] = byte(i >> 8) - l.Allow(ip) + l.Allow(benchIPv6(i)) } } func BenchmarkDifferentIPv6_10000(b *testing.B) { l := NewLimiter(1, time.Second, 10000) - ip := net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888") for i := 0; b.Loop(); i++ { - ip[7] = byte(i) - ip[6] = byte(i >> 8) - ip[5] = byte(i) - ip[4] = byte(i >> 8) - l.Allow(ip) + l.Allow(benchIPv6(i)) } } func BenchmarkSameIPv6_Strict(b *testing.B) { l := NewLimiter(1, time.Second, 256) - ip := net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888") + ip := netip.MustParseAddr("1111:2222:3333:4444:5555:6666:7777:8888") for b.Loop() { l.Allow(ip) } @@ -439,18 +503,102 @@ func BenchmarkSameIPv6_Strict(b *testing.B) { func BenchmarkSameIPv6_Bursty(b *testing.B) { l := NewLimiter(100, time.Second, 256) - ip := net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888") + ip := netip.MustParseAddr("1111:2222:3333:4444:5555:6666:7777:8888") for b.Loop() { l.Allow(ip) } } func BenchmarkSameIPv6_Strict_Parallel(b *testing.B) { - l := NewLimiter(100, time.Second, 256) - ip := net.ParseIP("1111:2222:3333:4444:5555:6666:7777:8888") + l := NewLimiter(1, time.Second, 256) + ip := netip.MustParseAddr("1111:2222:3333:4444:5555:6666:7777:8888") b.RunParallel(func(pb *testing.PB) { for pb.Next() { l.Allow(ip) } }) } + +func benchIPv4(i int) netip.Addr { + return ipv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)) +} + +// benchIPv6 returns an IPv6 address which changes at different levels, so they +// don't all fall under the same /64, /56, /48. +func benchIPv6(i int) netip.Addr { + return netip.AddrFrom16([16]byte{ + 0x11, 0x11, 0x22, 0x22, + byte(i >> 8), byte(i), byte(i >> 8), byte(i), + 0x55, 0x55, 0x66, 0x66, 0x77, 0x77, 0x88, 0x88, + }) +} + +// Benchmarks on prebuilt addresses, so they only measure the limiter and not +// the cost of building the input. + +func benchIPs(n int, f func(int) netip.Addr) []netip.Addr { + ips := make([]netip.Addr, n) + for i := range ips { + ips[i] = f(i + 1) + } + return ips +} + +// benchHit has a limiter with enough room for all the addresses, so each call +// finds an existing entry and moves it from the end of the LRU to the front. +func benchHit(b *testing.B, size int, f func(int) netip.Addr) { + l := NewLimiter(1, time.Second, size) + ips := benchIPs(size, f) + for _, ip := range ips { + l.Allow(ip) + } + for i := 0; b.Loop(); i++ { + l.Allow(ips[i%len(ips)]) + } +} + +// benchMiss has twice as many addresses as the limiter has room for, so each +// call misses, and evicts the oldest entry. +func benchMiss(b *testing.B, size int, f func(int) netip.Addr) { + l := NewLimiter(1, time.Second, size) + ips := benchIPs(size*2, f) + for i := 0; b.Loop(); i++ { + l.Allow(ips[i%len(ips)]) + } +} + +func benchHitParallel(b *testing.B, size int, f func(int) netip.Addr) { + l := NewLimiter(1, time.Second, size) + ips := benchIPs(size, f) + for _, ip := range ips { + l.Allow(ip) + } + var seed atomic.Int64 + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + // Start each goroutine on a different part of the list. + i := int(seed.Add(1)) * 7919 + for pb.Next() { + l.Allow(ips[i%len(ips)]) + i++ + } + }) +} + +func BenchmarkHitIPv4_256(b *testing.B) { benchHit(b, 256, benchIPv4) } +func BenchmarkHitIPv4_10000(b *testing.B) { benchHit(b, 10000, benchIPv4) } +func BenchmarkHitIPv6_256(b *testing.B) { benchHit(b, 256, benchIPv6) } +func BenchmarkHitIPv6_10000(b *testing.B) { benchHit(b, 10000, benchIPv6) } +func BenchmarkMissIPv4_10000(b *testing.B) { benchMiss(b, 10000, benchIPv4) } +func BenchmarkMissIPv6_10000(b *testing.B) { benchMiss(b, 10000, benchIPv6) } +func BenchmarkHitIPv4_10000_Parallel(b *testing.B) { benchHitParallel(b, 10000, benchIPv4) } +func BenchmarkHitIPv6_10000_Parallel(b *testing.B) { benchHitParallel(b, 10000, benchIPv6) } + +// IPv4-mapped addresses are what the limiter gets from a dual-stack socket. +func BenchmarkSameIPv4Mapped_Strict(b *testing.B) { + l := NewLimiter(1, time.Second, 256) + ip := netip.MustParseAddr("::ffff:1.2.3.4") + for b.Loop() { + l.Allow(ip) + } +} diff --git a/server/http.go b/server/http.go index fa31f69..9c7c14a 100644 --- a/server/http.go +++ b/server/http.go @@ -7,10 +7,10 @@ import ( "fmt" "io" golog "log" - "net" "net/http" "net/http/cgi" "net/http/httputil" + "net/netip" "net/url" "path/filepath" "strings" @@ -573,30 +573,18 @@ func reqLog(r *http.Request, status int, length int64, latency time.Duration) { func WithRateLimit(parent http.Handler, rl *ipratelimit.Limiter) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var ip net.IP - host, _, err := net.SplitHostPort(r.RemoteAddr) + addrPort, err := netip.ParseAddrPort(r.RemoteAddr) if err != nil { ratelimit.Trace(rl).Errorf( - "[http] failed to split remote address %q: %v", + "[http] failed to parse remote address %q: %v", r.RemoteAddr, err) - goto allow - } - - ip = net.ParseIP(host) - if ip == nil { - ratelimit.Trace(rl).Errorf( - "[http] failed to parse IP address: %q", r.RemoteAddr) - goto allow - } - - if !rl.Allow(ip) { + } else if ip := addrPort.Addr(); !rl.Allow(ip) { ratelimit.Trace(rl).Printf( "[http] rate limit exceeded for %q", ip) http.Error(w, "rate limit exceeded", http.StatusTooManyRequests) return } - allow: parent.ServeHTTP(w, r) }) } diff --git a/server/raw.go b/server/raw.go index 937f536..4a54998 100644 --- a/server/raw.go +++ b/server/raw.go @@ -62,7 +62,7 @@ func allowed(addr net.Addr, lim *ipratelimit.Limiter) bool { return true } - if !lim.Allow(tcpAddr.IP) { + if !lim.Allow(tcpAddr.AddrPort().Addr()) { ratelimit.Trace(lim).Printf( "[raw] rate limit exceeded for %q", tcpAddr.IP) return false diff --git a/server/server_test.go b/server/server_test.go index 62aa273..c635b27 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -13,6 +13,7 @@ import ( "time" "blitiri.com.ar/go/gofer/config" + "blitiri.com.ar/go/gofer/ipratelimit" "blitiri.com.ar/go/gofer/ratelimit" "blitiri.com.ar/go/gofer/reqlog" "blitiri.com.ar/go/log" @@ -277,6 +278,19 @@ func TestRateLimitRequestErrors(t *testing.T) { if rootHCalled != 2 { t.Errorf("expected root handler to be called 2, got %v", rootHCalled) } + + // Test that a request with a link-local address, which includes a zone, is + // rate limited like any other. + r = httptest.NewRequest("GET", "http://[::1]:1234/", nil) + r.RemoteAddr = "[fe80::1%eth0]:1234" + w = httptest.NewRecorder() + h.ServeHTTP(w, r) + if w.Code != http.StatusTooManyRequests { + t.Errorf("expected status 429 (too many requests), got %v", w.Code) + } + if rootHCalled != 2 { + t.Errorf("expected root handler to be called 2, got %v", rootHCalled) + } } // TestLoggingStatus checks that the request log records the status the client @@ -386,3 +400,70 @@ func BenchmarkParallel(b *testing.B) { b.RunParallel(makeP("http://" + rawAddr + "/be")) }) } + +// nopResponseWriter is a minimal http.ResponseWriter for benchmarks. +type nopResponseWriter struct { + hdr http.Header +} + +func (w *nopResponseWriter) Header() http.Header { return w.hdr } +func (w *nopResponseWriter) Write(b []byte) (int, error) { return len(b), nil } +func (w *nopResponseWriter) WriteHeader(int) {} + +func benchLimiter(b *testing.B, requests uint64) *ipratelimit.Limiter { + name := b.Name() + ratelimit.FromConfig(name, config.RateLimit{ + Rate: config.Rate{Requests: requests, Period: time.Hour}, + Size: 1024, + }) + return ratelimit.FromName(name) +} + +var benchRemoteAddrs = []struct{ name, addr string }{ + {"IPv4", "192.0.2.1:1234"}, + {"IPv6", "[2001:db8:1:2:3:4:5:6]:1234"}, +} + +// BenchmarkWithRateLimit measures the rate limiter as seen by HTTP requests, +// including parsing the remote address. The denied path matters because it is +// what a client that is being abusive will exercise. +func BenchmarkWithRateLimit(b *testing.B) { + h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) + for _, c := range []struct { + name string + requests uint64 + }{{"Allowed", 1 << 62}, {"Denied", 1}} { + for _, a := range benchRemoteAddrs { + b.Run(c.name+"/"+a.name, func(b *testing.B) { + wrapped := WithRateLimit(h, benchLimiter(b, c.requests)) + r := httptest.NewRequest("GET", "http://unused/", nil) + r.RemoteAddr = a.addr + w := &nopResponseWriter{hdr: http.Header{}} + for b.Loop() { + wrapped.ServeHTTP(w, r) + } + }) + } + } +} + +// BenchmarkRawAllowed measures the rate limiter as seen by raw proxying. +// Note net.TCPAddr holds IPv4 addresses in their 16-byte form when they come +// from a dual-stack socket. +func BenchmarkRawAllowed(b *testing.B) { + for _, c := range []struct { + name string + ip net.IP + }{ + {"IPv4", net.ParseIP("192.0.2.1")}, + {"IPv6", net.ParseIP("2001:db8:1:2:3:4:5:6")}, + } { + b.Run(c.name, func(b *testing.B) { + lim := benchLimiter(b, 1<<62) + addr := &net.TCPAddr{IP: c.ip, Port: 1234} + for b.Loop() { + allowed(addr, lim) + } + }) + } +}