Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug for inbound connections gated by the deprecated filter option #1004

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions options_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func (f *filtersConnectionGater) InterceptPeerDial(p peer.ID) (allow bool) {
return true
}

func (f *filtersConnectionGater) InterceptAccept(_ network.ConnMultiaddrs) (allow bool) {
return true
func (f *filtersConnectionGater) InterceptAccept(connAddr network.ConnMultiaddrs) (allow bool) {
return !(*ma.Filters)(f).AddrBlocked(connAddr.RemoteMultiaddr())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarshkshah1992 is this the only place to do this? Don't we need something for at least InterceptSecured if not InterceptUpgraded also?

Copy link
Contributor

@aarshkshah1992 aarshkshah1992 Sep 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aschmahmann

An InterceptUpgraded wont do match for us here as all those rejections will happen in InterceptAccepted also(an incoming connection is first accepted and then secured). We didn't have the notion of rejecting connections at different "lifecycle states" when we had this old filter.

However, there is no harm in doing so and I am okay either ways.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually @aschmahmann , we might need this for QUIC. Thanks for catching this.

@jalextowle Please can you add a similar behaviour to InterceptSecured as well ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aarshkshah1992 Yep, I added that behavior.

}

func (f *filtersConnectionGater) InterceptSecured(_ network.Direction, _ peer.ID, _ network.ConnMultiaddrs) (allow bool) {
return true
func (f *filtersConnectionGater) InterceptSecured(_ network.Direction, _ peer.ID, connAddr network.ConnMultiaddrs) (allow bool) {
return !(*ma.Filters)(f).AddrBlocked(connAddr.RemoteMultiaddr())
}

func (f *filtersConnectionGater) InterceptUpgraded(_ network.Conn) (allow bool, reason control.DisconnectReason) {
Expand Down
52 changes: 48 additions & 4 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import (
"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 TestDeprecatedFiltersOptions(t *testing.T) {
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)

host, err := New(context.TODO(), Filters(f))
host0, err := New(context.TODO(), Filters(f))
require.NoError(err)
require.NotNil(host)
require.NotNil(host0)

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
Expand All @@ -31,11 +32,54 @@ func TestDeprecatedFiltersOptions(t *testing.T) {
addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/0/p2p/" + id.Pretty())
ai, _ := peer.AddrInfoFromP2pAddr(addr)

err = host.Connect(ctx, *ai)
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) {
jalextowle marked this conversation as resolved.
Show resolved Hide resolved
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)

Expand Down