limits: sync streams, listings per author, inbound connections per IP
A peer may hold 4 sync streams and open 1500 a minute; banned peers get none and cannot redial. A node keeps at most 100 unexpired listings per author (--max-per-author); updates, renewals and deletions of held ones still work. At most 16 inbound connections per public IP or IPv6 /64 (--max-conns-per-ip); local and private addresses are exempt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/libp2p/go-libp2p/core/network"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
|
||||
type fakeConnAddrs struct{ remote ma.Multiaddr }
|
||||
|
||||
func (f fakeConnAddrs) LocalMultiaddr() ma.Multiaddr { return nil }
|
||||
func (f fakeConnAddrs) RemoteMultiaddr() ma.Multiaddr { return f.remote }
|
||||
|
||||
func mustAddr(t *testing.T, s string) ma.Multiaddr {
|
||||
t.Helper()
|
||||
a, err := ma.NewMultiaddr(s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func TestIPKeysGroupASourceAndExemptLocalAddresses(t *testing.T) {
|
||||
for addr, want := range map[string]string{
|
||||
"/ip4/203.0.113.7/tcp/4001": "203.0.113.7",
|
||||
"/ip4/203.0.113.7/udp/9/quic-v1": "203.0.113.7",
|
||||
"/ip6/2001:db8:1:2:aaaa::1/tcp/4001": "2001:db8:1:2::/64",
|
||||
"/ip6/2001:db8:1:2:bbbb::9/tcp/4001": "2001:db8:1:2::/64",
|
||||
"/ip4/127.0.0.1/tcp/4001": "",
|
||||
"/ip4/192.168.1.20/tcp/4001": "",
|
||||
"/ip4/10.1.2.3/tcp/4001": "",
|
||||
"/ip6/::1/tcp/4001": "",
|
||||
"/ip6/fe80::1/tcp/4001": "",
|
||||
"/ip6/fd00::5/tcp/4001": "",
|
||||
"/dns4/example.org/tcp/4001": "",
|
||||
} {
|
||||
if got := ipKey(mustAddr(t, addr)); got != want {
|
||||
t.Errorf("ipKey(%s) = %q, want %q", addr, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOneAddressCannotHoldMoreConnectionsThanTheLimit(t *testing.T) {
|
||||
g := newIPGate(2)
|
||||
a := fakeConnAddrs{mustAddr(t, "/ip4/203.0.113.7/tcp/1000")}
|
||||
other := fakeConnAddrs{mustAddr(t, "/ip4/203.0.113.8/tcp/1000")}
|
||||
if !g.InterceptAccept(a) {
|
||||
t.Fatal("first connection refused")
|
||||
}
|
||||
g.counts["203.0.113.7"] = 1
|
||||
if !g.InterceptAccept(a) {
|
||||
t.Fatal("second connection refused")
|
||||
}
|
||||
g.counts["203.0.113.7"] = 2
|
||||
if g.InterceptAccept(a) {
|
||||
t.Fatal("a third connection from one address was accepted")
|
||||
}
|
||||
if !g.InterceptAccept(other) {
|
||||
t.Fatal("another address must not be affected")
|
||||
}
|
||||
g.counts["203.0.113.7"] = 1 // one closed
|
||||
if !g.InterceptAccept(a) {
|
||||
t.Fatal("a closed connection must free its place")
|
||||
}
|
||||
local := fakeConnAddrs{mustAddr(t, "/ip4/127.0.0.1/tcp/1000")}
|
||||
for i := 0; i < 50; i++ {
|
||||
if !g.InterceptAccept(local) {
|
||||
t.Fatal("local addresses are exempt")
|
||||
}
|
||||
}
|
||||
if newIPGate(0).full(a.remote) {
|
||||
t.Fatal("a limit of zero means no limit")
|
||||
}
|
||||
}
|
||||
|
||||
// fakeConn answers only what the gate asks of a connection.
|
||||
type fakeConn struct {
|
||||
network.Conn
|
||||
remote ma.Multiaddr
|
||||
dir network.Direction
|
||||
}
|
||||
|
||||
func (c fakeConn) RemoteMultiaddr() ma.Multiaddr { return c.remote }
|
||||
func (c fakeConn) Stat() network.ConnStats {
|
||||
return network.ConnStats{Stats: network.Stats{Direction: c.dir}}
|
||||
}
|
||||
|
||||
func TestEstablishedInboundConnectionsAreCountedUntilTheyClose(t *testing.T) {
|
||||
g := newIPGate(2)
|
||||
n := g.notifee()
|
||||
in := func() fakeConn {
|
||||
return fakeConn{remote: mustAddr(t, "/ip4/203.0.113.7/tcp/1000"), dir: network.DirInbound}
|
||||
}
|
||||
out := fakeConn{remote: mustAddr(t, "/ip4/203.0.113.7/tcp/1000"), dir: network.DirOutbound}
|
||||
a, b := in(), in()
|
||||
n.Connected(nil, a)
|
||||
n.Connected(nil, out) // ones we dialed do not count
|
||||
if g.full(a.remote) {
|
||||
t.Fatal("one inbound connection fills a limit of two")
|
||||
}
|
||||
n.Connected(nil, b)
|
||||
if !g.full(a.remote) {
|
||||
t.Fatal("two inbound connections must fill a limit of two")
|
||||
}
|
||||
n.Disconnected(nil, out)
|
||||
if !g.full(a.remote) {
|
||||
t.Fatal("closing an outbound connection must not free an inbound place")
|
||||
}
|
||||
n.Disconnected(nil, a)
|
||||
if g.full(a.remote) {
|
||||
t.Fatal("a closed connection must free its place")
|
||||
}
|
||||
n.Disconnected(nil, b)
|
||||
if len(g.counts) != 0 {
|
||||
t.Fatalf("%d addresses still counted after everything closed", len(g.counts))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBannedPeersCannotComeBack(t *testing.T) {
|
||||
g := newIPGate(2)
|
||||
cm := fakeConnAddrs{mustAddr(t, "/ip4/203.0.113.7/tcp/1000")}
|
||||
if !g.InterceptSecured(network.DirInbound, "mallory", cm) {
|
||||
t.Fatal("nobody is banned before the guard says so")
|
||||
}
|
||||
banned := func(p peer.ID) bool { return p == "mallory" }
|
||||
g.banned.Store(&banned)
|
||||
if g.InterceptSecured(network.DirInbound, "mallory", cm) {
|
||||
t.Fatal("a banned peer got back in")
|
||||
}
|
||||
if !g.InterceptSecured(network.DirInbound, "alice", cm) {
|
||||
t.Fatal("an innocent peer was refused")
|
||||
}
|
||||
if !g.InterceptSecured(network.DirOutbound, "mallory", cm) {
|
||||
t.Fatal("we may still dial out; only inbound is gated")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user