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 1 commit
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
4 changes: 2 additions & 2 deletions options_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ 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) {
Expand Down
103 changes: 99 additions & 4 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libp2p

import (
"context"
"fmt"
"net"
"testing"
"time"
Expand All @@ -13,16 +14,16 @@ import (
"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,47 @@ 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")
}

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()
var ipNet net.IPNet
for _, addr := range host0.Addrs() {
ipNet, err = ipNetFromMaddr(addr)
if err == nil {
require.NotNil(t, ipNet)
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(),
}
addrs, err := peer.AddrInfoToP2pAddrs(&peerInfo)
ai, err := peer.AddrInfoFromP2pAddr(addrs[0])
require.NoError(err)

err = host0.Connect(ctx, *ai)
require.Error(err)
}

func TestDeprecatedFiltersAndAddressesOptions(t *testing.T) {
require := require.New(t)

Expand Down Expand Up @@ -70,3 +107,61 @@ func TestCannotSetFiltersAndConnGater(t *testing.T) {
require.Error(err)
require.Contains(err.Error(), "cannot configure multiple connection gaters")
}

/// NOTE(jalextowle): This was taken directly from https://github.com/0x-mesh/p2p/banner/banner.go
jalextowle marked this conversation as resolved.
Show resolved Hide resolved
func ipNetFromMaddr(maddr ma.Multiaddr) (ipNet net.IPNet, err error) {
ip, err := ipFromMaddr(maddr)
if err != nil {
return net.IPNet{}, err
}
mask := getAllMaskForIP(ip)
return net.IPNet{
IP: ip,
Mask: mask,
}, nil
}

/// NOTE(jalextowle): This was taken directly from https://github.com/0x-mesh/p2p/banner/banner.go
func ipFromMaddr(maddr ma.Multiaddr) (net.IP, error) {
var (
ip net.IP
found bool
)

ma.ForEach(maddr, func(c ma.Component) bool {
switch c.Protocol().Code {
case ma.P_IP6ZONE:
return true
case ma.P_IP6, ma.P_IP4:
found = true
ip = net.IP(c.RawValue())
return false
default:
return false
}
})

if !found {
return net.IP{}, fmt.Errorf("could not parse IP address from multiaddress: %s", maddr)
}
return ip, nil
}

/// NOTE(jalextowle): This was taken directly from https://github.com/0x-mesh/p2p/banner/banner.go
var (
ipv4AllMask = net.IPMask{255, 255, 255, 255}
ipv6AllMask = net.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
)

/// NOTE(jalextowle): This was taken directly from https://github.com/0x-mesh/p2p/banner/banner.go
// getAllMaskForIP returns an IPMask that will match all IP addresses. The size
// of the mask depends on whether the given IP address is an IPv4 or an IPv6
// address.
func getAllMaskForIP(ip net.IP) net.IPMask {
if ip.To4() != nil {
// This is an ipv4 address. Return 4 byte mask.
return ipv4AllMask
}
// Assume ipv6 address. Return 16 byte mask.
return ipv6AllMask
}