diff --git a/options.go b/options.go index 39dc227cff..2f76a4aa7b 100644 --- a/options.go +++ b/options.go @@ -6,7 +6,6 @@ package libp2p import ( "errors" "fmt" - "net" "time" circuit "github.com/libp2p/go-libp2p-circuit" @@ -327,60 +326,6 @@ func AutoNATServiceRateLimit(global, perPeer int, interval time.Duration) Option } } -// FilterAddresses configures libp2p to never dial nor accept connections from -// the given addresses. FilterAddresses should be used for cases where the -// addresses you want to deny are known ahead of time. -// -// Note: Using Filter + FilterAddresses at the same time is fine, but you cannot -// configure a both ConnectionGater and filtered addresses. -// -// Deprecated: Please use ConnectionGater() instead. -func FilterAddresses(addrs ...*net.IPNet) Option { - return func(cfg *Config) error { - var f *filtersConnectionGater - - // preserve backwards compatibility. - // if we have a connection gater, try to cast it to a *filtersConnectionGater. - if cfg.ConnectionGater != nil { - var ok bool - if f, ok = cfg.ConnectionGater.(*filtersConnectionGater); !ok { - return errors.New("cannot configure both Filters and Connection Gater; please consider configuring just a ConnectionGater instead") - } - } - - if f == nil { - f = (*filtersConnectionGater)(ma.NewFilters()) - cfg.ConnectionGater = f - } - - for _, addr := range addrs { - (*ma.Filters)(f).AddFilter(*addr, ma.ActionDeny) - } - - return nil - } -} - -// Filters configures libp2p to use the given filters for accepting/denying -// certain addresses. Filters offers more control and should be used when the -// addresses you want to accept/deny are not known ahead of time and can -// dynamically change. -// -// Note: You cannot configure both a ConnectionGater and a Filter at the same -// time. Under the hood, the Filters object is converted to a ConnectionGater. -// -// Deprecated: use ConnectionGater() instead. -func Filters(filters *ma.Filters) Option { - return func(cfg *Config) error { - if cfg.ConnectionGater != nil { - return errors.New("cannot configure both Filters and Connection Gater; please consider configuring just a ConnectionGater instead") - - } - cfg.ConnectionGater = (*filtersConnectionGater)(filters) - return nil - } -} - // ConnectionGater configures libp2p to use the given ConnectionGater // to actively reject inbound/outbound connections based on the lifecycle stage // of the connection. diff --git a/options_test.go b/options_test.go deleted file mode 100644 index ba8c0cbfba..0000000000 --- a/options_test.go +++ /dev/null @@ -1,116 +0,0 @@ -package libp2p - -import ( - "context" - "net" - "testing" - "time" - - "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p-core/test" - - ma "github.com/multiformats/go-multiaddr" - manet "github.com/multiformats/go-multiaddr/net" - "github.com/stretchr/testify/require" -) - -func TestDeprecatedFiltersOptionsOutbound(t *testing.T) { - require := require.New(t) - - f := ma.NewFilters() - _, ipnet, _ := net.ParseCIDR("127.0.0.0/24") - f.AddFilter(*ipnet, ma.ActionDeny) - - host0, err := New(context.TODO(), Filters(f)) - require.NoError(err) - require.NotNil(host0) - - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - - id, _ := test.RandPeerID() - addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0/p2p/" + id.Pretty()) - ai, _ := peer.AddrInfoFromP2pAddr(addr) - - err = host0.Connect(ctx, *ai) - require.Error(err) - require.Contains(err.Error(), "no good addresses") -} - -var ( - ip4FullMask = net.IPMask{255, 255, 255, 255} - ip6FullMask = net.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} -) - -func TestDeprecatedFiltersOptionsInbound(t *testing.T) { - require := require.New(t) - - host0, err := New(context.TODO()) - require.NoError(err) - require.NotNil(host0) - - f := ma.NewFilters() - for _, addr := range host0.Addrs() { - ip, err := manet.ToIP(addr) - require.NoError(err) - require.NotNil(t, ip) - - var mask net.IPMask - if ip.To4() != nil { - mask = ip4FullMask - } else { - mask = ip6FullMask - } - - ipnet := net.IPNet{IP: ip, Mask: mask} - f.AddFilter(ipnet, ma.ActionDeny) - } - host1, err := New(context.TODO(), Filters(f)) - require.NoError(err) - require.NotNil(host1) - - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - - peerInfo := peer.AddrInfo{ - ID: host1.ID(), - Addrs: host1.Addrs(), - } - err = host0.Connect(ctx, peerInfo) - require.Error(err) -} - -func TestDeprecatedFiltersAndAddressesOptions(t *testing.T) { - require := require.New(t) - - f := ma.NewFilters() - _, ipnet1, _ := net.ParseCIDR("127.0.0.0/24") - _, ipnet2, _ := net.ParseCIDR("128.0.0.0/24") - f.AddFilter(*ipnet1, ma.ActionDeny) - - host, err := New(context.TODO(), Filters(f), FilterAddresses(ipnet2)) - require.NoError(err) - require.NotNil(host) - - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - - id, _ := test.RandPeerID() - addr1, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0/p2p/" + id.Pretty()) - addr2, _ := ma.NewMultiaddr("/ip4/128.0.0.1/tcp/0/p2p/" + id.Pretty()) - ai, _ := peer.AddrInfosFromP2pAddrs(addr1, addr2) - - err = host.Connect(ctx, ai[0]) - require.Error(err) - require.Contains(err.Error(), "no good addresses") -} - -func TestCannotSetFiltersAndConnGater(t *testing.T) { - require := require.New(t) - - f := ma.NewFilters() - - _, err := New(context.TODO(), Filters(f), ConnectionGater(nil)) - require.Error(err) - require.Contains(err.Error(), "cannot configure multiple connection gaters") -}