Browse Source

Merge pull request #983 from sandmanhome/feat/fix-host-lost-AutoNat-PublicAddr

fix host can be dialed by autonat public addr, but lost the public addr to announce
pull/985/head
Will 4 years ago
committed by GitHub
parent
commit
147acc7bf7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      config/config.go
  2. 16
      p2p/host/basic/basic_host.go

2
config/config.go

@ -335,7 +335,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
autonatOpts = append(autonatOpts, autonat.WithReachability(*cfg.AutoNATConfig.ForceReachability))
}
if _, err = autonat.New(ctx, h, autonatOpts...); err != nil {
if h.AutoNat, err = autonat.New(ctx, h, autonatOpts...); err != nil {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; autonat failed to start: %v", err)
}

16
p2p/host/basic/basic_host.go

@ -9,6 +9,7 @@ import (
"sync"
"time"
autonat "github.com/libp2p/go-libp2p-autonat"
"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/event"
@ -110,6 +111,8 @@ type BasicHost struct {
disableSignedPeerRecord bool
signKey crypto.PrivKey
caBook peerstore.CertifiedAddrBook
AutoNat autonat.AutoNAT
}
var _ host.Host = (*BasicHost)(nil)
@ -805,6 +808,19 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
finalAddrs = append(finalAddrs, resolved...)
}
// add autonat PublicAddr Consider the following scenario
// For example, it is deployed on a cloud server,
// it provides an elastic ip accessible to the public network,
// but not have an external network card,
// so net.InterfaceAddrs() not has the public ip
// The host can indeed be dialed !!!
if h.AutoNat != nil {
publicAddr, _ := h.AutoNat.PublicAddr()
if publicAddr != nil {
finalAddrs = append(finalAddrs, publicAddr)
}
}
finalAddrs = dedupAddrs(finalAddrs)
var natMappings []inat.Mapping

Loading…
Cancel
Save