Skip to content

Commit

Permalink
add certhashes to addresses provided by AddrsFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jul 21, 2024
1 parent ddbfee0 commit b8d0297
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 52 deletions.
9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,9 @@ func (cfg *Config) NewNode() (host.Host, error) {
return addrs
}

// enable autorelay
fxopts = append(fxopts,
fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) (*autorelay.AutoRelay, error) {
fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) error {
oldAddrFactory = h.AddrsFactory
if cfg.EnableAutoRelay {
if !cfg.DisableMetrics {
Expand All @@ -505,12 +506,12 @@ func (cfg *Config) NewNode() (host.Host, error) {

ar, err := autorelay.NewAutoRelay(h, cfg.AutoRelayOpts...)
if err != nil {
return nil, err
return err
}
lifecycle.Append(fx.StartStopHook(ar.Start, ar.Close))
return ar, nil
return nil
}
return nil, nil
return nil
}),
)

Expand Down
28 changes: 24 additions & 4 deletions libp2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,7 @@ func TestRoutedHost(t *testing.T) {
}

func TestAutoNATService(t *testing.T) {
// h, err := New(EnableNATService())
// require.NoError(t, err)
// h.Close()
h, err := New(EnableNATService(), EnableAutoRelayWithStaticRelays([]peer.AddrInfo{{ID: peer.ID("hello")}}))
h, err := New(EnableNATService())
require.NoError(t, err)
h.Close()
}
Expand Down Expand Up @@ -468,3 +465,26 @@ func TestDialCircuitAddrWithWrappedResourceManager(t *testing.T) {
require.NoError(t, res.Error)
defer cancel()
}

func TestHostAddrsFactoryAddsCerthashes(t *testing.T) {
addr := ma.StringCast("/ip4/1.2.3.4/udp/1/quic-v1/webtransport")
h, err := New(
AddrsFactory(func(m []ma.Multiaddr) []ma.Multiaddr {
return []ma.Multiaddr{addr}
}),
)
require.NoError(t, err)
require.Eventually(t, func() bool {
addrs := h.Addrs()
for _, a := range addrs {
first, last := ma.SplitFunc(a, func(c ma.Component) bool {
return c.Protocol().Code == ma.P_CERTHASH
})
if addr.Equal(first) && last != nil {
return true
}
}
return false
}, 5*time.Second, 50*time.Millisecond)
h.Close()
}
105 changes: 61 additions & 44 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
if opts.AddrsFactory != nil {
h.AddrsFactory = opts.AddrsFactory
}
// This is a terrible hack.
// We want to use this AddrsFactory for autonat. Wrapping AddrsFactory here ensures
// that autonat receives addresses with the correct certhashes.
//
// This logic cannot be in Addrs method as the autorelay package updates AddrsFactory
// to only provide p2p-circuit addresses when reachability is Private. For the same reason,
// autonat cannot use Addrs method.
//
// Wrapping it here allows us to provide the wrapped AddrsFactory to autonat before
// autorelay updates it.
addrFactory := h.AddrsFactory
h.AddrsFactory = func(addrs []ma.Multiaddr) []ma.Multiaddr {
return h.addCertHashes(addrFactory(addrs))
}

if opts.NATManager != nil {
h.natmgr = opts.NATManager(n)
Expand Down Expand Up @@ -804,47 +818,9 @@ func (h *BasicHost) ConnManager() connmgr.ConnManager {
// Addrs returns listening addresses that are safe to announce to the network.
// The output is the same as AllAddrs, but processed by AddrsFactory.
func (h *BasicHost) Addrs() []ma.Multiaddr {
// This is a temporary workaround/hack that fixes #2233. Once we have a
// proper address pipeline, rework this. See the issue for more context.
type transportForListeninger interface {
TransportForListening(a ma.Multiaddr) transport.Transport
}

type addCertHasher interface {
AddCertHashes(m ma.Multiaddr) (ma.Multiaddr, bool)
}

addrs := h.AddrsFactory(h.AllAddrs())

s, ok := h.Network().(transportForListeninger)
if !ok {
return addrs
}

// Copy addrs slice since we'll be modifying it.
addrsOld := addrs
addrs = make([]ma.Multiaddr, len(addrsOld))
copy(addrs, addrsOld)

for i, addr := range addrs {
wtOK, wtN := libp2pwebtransport.IsWebtransportMultiaddr(addr)
webrtcOK, webrtcN := libp2pwebrtc.IsWebRTCDirectMultiaddr(addr)
if (wtOK && wtN == 0) || (webrtcOK && webrtcN == 0) {
t := s.TransportForListening(addr)
tpt, ok := t.(addCertHasher)
if !ok {
continue
}
addrWithCerthash, added := tpt.AddCertHashes(addr)
if !added {
log.Debugf("Couldn't add certhashes to multiaddr: %s", addr)
continue
}
addrs[i] = addrWithCerthash
}
}

return addrs
// We don't need to append certhashes here, the user provided addrsFactory was
// wrapped with addCertHashes in the constructor.
return h.AddrsFactory(h.AllAddrs())
}

// NormalizeMultiaddr returns a multiaddr suitable for equality checks.
Expand All @@ -864,8 +840,9 @@ func (h *BasicHost) NormalizeMultiaddr(addr ma.Multiaddr) ma.Multiaddr {
return addr
}

// AllAddrs returns all the addresses of BasicHost at this moment in time.
// It's ok to not include addresses if they're not available to be used now.
// AllAddrs returns all the addresses the host is listening on except circuit addresses.
// The output has webtransport addresses inferred from quic addresses.
// All the addresses have the correct
func (h *BasicHost) AllAddrs() []ma.Multiaddr {
listenAddrs := h.Network().ListenAddresses()
if len(listenAddrs) == 0 {
Expand Down Expand Up @@ -959,10 +936,50 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
}
finalAddrs = ma.Unique(finalAddrs)
finalAddrs = inferWebtransportAddrsFromQuic(finalAddrs)

return finalAddrs
}

func (h *BasicHost) addCertHashes(addrs []ma.Multiaddr) []ma.Multiaddr {
// This is a temporary workaround/hack that fixes #2233. Once we have a
// proper address pipeline, rework this. See the issue for more context.
type transportForListeninger interface {
TransportForListening(a ma.Multiaddr) transport.Transport
}

type addCertHasher interface {
AddCertHashes(m ma.Multiaddr) (ma.Multiaddr, bool)
}

s, ok := h.Network().(transportForListeninger)
if !ok {
return addrs
}

// Copy addrs slice since we'll be modifying it.
addrsOld := addrs
addrs = make([]ma.Multiaddr, len(addrsOld))
copy(addrs, addrsOld)

for i, addr := range addrs {
wtOK, wtN := libp2pwebtransport.IsWebtransportMultiaddr(addr)
webrtcOK, webrtcN := libp2pwebrtc.IsWebRTCDirectMultiaddr(addr)
if (wtOK && wtN == 0) || (webrtcOK && webrtcN == 0) {
t := s.TransportForListening(addr)
tpt, ok := t.(addCertHasher)
if !ok {
continue
}
addrWithCerthash, added := tpt.AddCertHashes(addr)
if !added {
log.Debugf("Couldn't add certhashes to multiaddr: %s", addr)
continue
}
addrs[i] = addrWithCerthash
}
}
return addrs
}

var wtComponent = ma.StringCast("/webtransport")

// inferWebtransportAddrsFromQuic infers more webtransport addresses from QUIC addresses.
Expand Down

0 comments on commit b8d0297

Please sign in to comment.