Skip to content

Commit

Permalink
remove: holepunch default filter
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Nov 4, 2022
1 parent b9e0bec commit ce34fcb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
17 changes: 2 additions & 15 deletions p2p/protocol/holepunch/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
// It also allows to only consider a subset of received multi addresses
// that remote peers announced to us.
// Theoretically, this API also allows to add multi addresses in both cases.
func WithAddrFilter(maf AddrFilter) Option {
func WithAddrFilter(f AddrFilter) Option {
return func(hps *Service) error {
hps.filter = maf
hps.filter = f
return nil
}
}
Expand All @@ -25,16 +25,3 @@ type AddrFilter interface {
// FilterRemote is a function that filters the multi addresses which we received from the remote peer.
FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr
}

// DefaultAddrFilter is the default address filtering logic. It strips
// all relayed multi addresses from both the locally observed addresses
// and received remote addresses.
type DefaultAddrFilter struct{}

func (d DefaultAddrFilter) FilterLocal(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr {
return removeRelayAddrs(maddrs)
}

func (d DefaultAddrFilter) FilterRemote(remoteID peer.ID, maddrs []ma.Multiaddr) []ma.Multiaddr {
return removeRelayAddrs(maddrs)
}
15 changes: 12 additions & 3 deletions p2p/protocol/holepunch/holepuncher.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ func (hp *holePuncher) initiateHolePunchImpl(str network.Stream) ([]ma.Multiaddr
str.SetDeadline(time.Now().Add(StreamTimeout))

// send a CONNECT and start RTT measurement.
obsAddrs := hp.filter.FilterLocal(str.Conn().RemotePeer(), hp.ids.OwnObservedAddrs())
obsAddrs := removeRelayAddrs(hp.ids.OwnObservedAddrs())
if hp.filter != nil {
obsAddrs = hp.filter.FilterLocal(str.Conn().RemotePeer(), obsAddrs)
}
if len(obsAddrs) == 0 {
return nil, 0, errors.New("aborting hole punch initiation, as we have no public address after filtering")
return nil, 0, errors.New("aborting hole punch initiation as we have no public address")
}

start := time.Now()
if err := w.WriteMsg(&pb.HolePunch{
Type: pb.HolePunch_CONNECT.Enum(),
Expand All @@ -228,7 +232,12 @@ func (hp *holePuncher) initiateHolePunchImpl(str network.Stream) ([]ma.Multiaddr
if t := msg.GetType(); t != pb.HolePunch_CONNECT {
return nil, 0, fmt.Errorf("expect CONNECT message, got %s", t)
}
addrs := hp.filter.FilterRemote(str.Conn().RemotePeer(), addrsFromBytes(msg.ObsAddrs))

addrs := removeRelayAddrs(addrsFromBytes(msg.ObsAddrs))
if hp.filter != nil {
addrs = hp.filter.FilterRemote(str.Conn().RemotePeer(), addrs)
}

if len(addrs) == 0 {
return nil, 0, errors.New("didn't receive any public addresses in CONNECT")
}
Expand Down
14 changes: 11 additions & 3 deletions p2p/protocol/holepunch/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func NewService(h host.Host, ids identify.IDService, opts ...Option) (*Service,
host: h,
ids: ids,
hasPublicAddrsChan: make(chan struct{}),
filter: DefaultAddrFilter{},
}

for _, opt := range opts {
Expand Down Expand Up @@ -170,7 +169,11 @@ func (s *Service) incomingHolePunch(str network.Stream) (rtt time.Duration, addr
if !isRelayAddress(str.Conn().RemoteMultiaddr()) {
return 0, nil, fmt.Errorf("received hole punch stream: %s", str.Conn().RemoteMultiaddr())
}
ownAddrs := s.filter.FilterLocal(str.Conn().RemotePeer(), s.ids.OwnObservedAddrs())
ownAddrs := removeRelayAddrs(s.ids.OwnObservedAddrs())
if s.filter != nil {
ownAddrs = s.filter.FilterLocal(str.Conn().RemotePeer(), ownAddrs)
}

// If we can't tell the peer where to dial us, there's no point in starting the hole punching.
if len(ownAddrs) == 0 {
return 0, nil, errors.New("rejecting hole punch request, as we don't have any public addresses")
Expand All @@ -196,7 +199,12 @@ func (s *Service) incomingHolePunch(str network.Stream) (rtt time.Duration, addr
if t := msg.GetType(); t != pb.HolePunch_CONNECT {
return 0, nil, fmt.Errorf("expected CONNECT message from initiator but got %d", t)
}
obsDial := s.filter.FilterRemote(str.Conn().RemotePeer(), addrsFromBytes(msg.ObsAddrs))

obsDial := removeRelayAddrs(addrsFromBytes(msg.ObsAddrs))
if s.filter != nil {
obsDial = s.filter.FilterRemote(str.Conn().RemotePeer(), obsDial)
}

log.Debugw("received hole punch request", "peer", str.Conn().RemotePeer(), "addrs", obsDial)
if len(obsDial) == 0 {
return 0, nil, errors.New("expected CONNECT message to contain at least one address")
Expand Down

0 comments on commit ce34fcb

Please sign in to comment.