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 eb275c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
13 changes: 0 additions & 13 deletions p2p/protocol/holepunch/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 12 additions & 2 deletions p2p/protocol/holepunch/holepuncher.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,15 @@ 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")
}

start := time.Now()
if err := w.WriteMsg(&pb.HolePunch{
Type: pb.HolePunch_CONNECT.Enum(),
Expand All @@ -228,7 +233,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 eb275c4

Please sign in to comment.