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

Trigger Autorelay on NAT events #807

Merged
merged 12 commits into from
Mar 20, 2020
65 changes: 52 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import (
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/pnet"
"github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"

bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
relay "github.com/libp2p/go-libp2p/p2p/host/relay"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"

autonat "github.com/libp2p/go-libp2p-autonat"
circuit "github.com/libp2p/go-libp2p-circuit"
discovery "github.com/libp2p/go-libp2p-discovery"
swarm "github.com/libp2p/go-libp2p-swarm"
Expand All @@ -39,6 +41,8 @@ type NATManagerC func(network.Network) bhost.NATManager

type RoutingC func(host.Host) (routing.PeerRouting, error)

// AutoNATMode defines the AutoNAT behavior for the libp2p host.

// Config describes a set of settings for a libp2p node
//
// This is *not* a stable interface. Use the options defined in the root
Expand Down Expand Up @@ -76,13 +80,12 @@ type Config struct {
Routing RoutingC

EnableAutoRelay bool
Reachability network.Reachability
AutoNATService bool
StaticRelays []peer.AddrInfo
}

// NewNode constructs a new libp2p Host from the Config.
//
// This function consumes the config. Do not reuse it (really!).
func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
func (cfg *Config) newHost(ctx context.Context, store peerstore.Peerstore) (*bhost.BasicHost, error) {
// Check this early. Prevents us from even *starting* without verifying this.
if pnet.ForcePrivateNetwork && len(cfg.PSK) == 0 {
log.Error("tried to create a libp2p node with no Private" +
Expand All @@ -103,19 +106,15 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
return nil, err
}

if cfg.Peerstore == nil {
return nil, fmt.Errorf("no peerstore specified")
}

if err := cfg.Peerstore.AddPrivKey(pid, cfg.PeerKey); err != nil {
if err := store.AddPrivKey(pid, cfg.PeerKey); err != nil {
return nil, err
}
if err := cfg.Peerstore.AddPubKey(pid, cfg.PeerKey.GetPublic()); err != nil {
if err := store.AddPubKey(pid, cfg.PeerKey.GetPublic()); err != nil {
return nil, err
}

// TODO: Make the swarm implementation configurable.
swrm := swarm.NewSwarm(ctx, pid, cfg.Peerstore, cfg.Reporter)
swrm := swarm.NewSwarm(ctx, pid, store, cfg.Reporter)
if cfg.Filters != nil {
swrm.Filters = cfg.Filters
}
Expand Down Expand Up @@ -183,6 +182,21 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
return nil, err
}
}
return h, nil
}

// NewNode constructs a new libp2p Host from the Config.
//
// This function consumes the config. Do not reuse it (really!).
func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
if cfg.Peerstore == nil {
return nil, fmt.Errorf("no peerstore specified")
}

h, err := cfg.newHost(ctx, cfg.Peerstore)
if err != nil {
return nil, err
}

// TODO: This method succeeds if listening on one address succeeds. We
// should probably fail if listening on *any* addr fails.
Expand All @@ -201,6 +215,9 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
}
}

// Note: h.AddrsFactory may be changed by AutoRelay, but non-relay version is
// used by AutoNAT below.
addrF := h.AddrsFactory
if cfg.EnableAutoRelay {
if !cfg.Relay {
h.Close()
Expand All @@ -216,7 +233,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
}

if !hop && len(cfg.StaticRelays) > 0 {
_ = relay.NewAutoRelay(swrm.Context(), h, nil, router, cfg.StaticRelays)
_ = relay.NewAutoRelay(ctx, h, nil, router, cfg.StaticRelays)
} else {
if router == nil {
h.Close()
Expand All @@ -235,11 +252,33 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
// advertise ourselves
relay.Advertise(ctx, discovery)
} else {
_ = relay.NewAutoRelay(swrm.Context(), h, discovery, router, cfg.StaticRelays)
_ = relay.NewAutoRelay(ctx, h, discovery, router, cfg.StaticRelays)
}
}
}

autonatOpts := []autonat.Option{autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
})}
if cfg.AutoNATService {
dialerStore := pstoremem.NewPeerstore()
dialerHost, err := cfg.newHost(ctx, dialerStore)
if err != nil {
h.Close()
return nil, err
}

autonatOpts = append(autonatOpts, autonat.EnableService(dialerHost.Network()))
}
if cfg.Reachability != network.ReachabilityUnknown {
autonatOpts = append(autonatOpts, autonat.WithReachability(cfg.Reachability))
}

if _, err = autonat.New(ctx, h, autonatOpts...); err != nil {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; autonat failed to start: %v", err)
}

// start the host background tasks
h.Start()

Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/jbenet/goprocess v0.1.3
github.com/libp2p/go-conn-security-multistream v0.1.0
github.com/libp2p/go-eventbus v0.1.0
github.com/libp2p/go-libp2p-autonat v0.1.1
github.com/libp2p/go-libp2p-autonat v0.2.0
github.com/libp2p/go-libp2p-blankhost v0.1.4
github.com/libp2p/go-libp2p-circuit v0.1.4
github.com/libp2p/go-libp2p-core v0.5.0
Expand All @@ -29,15 +29,12 @@ require (
github.com/libp2p/go-stream-muxer-multistream v0.2.0
github.com/libp2p/go-tcp-transport v0.1.1
github.com/libp2p/go-ws-transport v0.2.0
github.com/miekg/dns v1.1.12 // indirect
github.com/miekg/dns v1.1.28 // indirect
github.com/multiformats/go-multiaddr v0.2.1
github.com/multiformats/go-multiaddr-dns v0.2.0
github.com/multiformats/go-multiaddr-net v0.1.3
github.com/multiformats/go-multistream v0.1.1
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
)

go 1.12
Loading