2018-07-04 10:51:47 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-06-16 20:19:45 +00:00
|
|
|
"crypto/rand"
|
2023-02-22 21:58:17 +00:00
|
|
|
"errors"
|
2018-07-04 10:51:47 +00:00
|
|
|
"fmt"
|
2021-06-16 20:19:45 +00:00
|
|
|
"time"
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/connmgr"
|
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2023-05-19 20:23:55 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/event"
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/metrics"
|
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peerstore"
|
|
|
|
"github.com/libp2p/go-libp2p/core/pnet"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/routing"
|
|
|
|
"github.com/libp2p/go-libp2p/core/sec"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/sec/insecure"
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/transport"
|
2022-04-01 16:16:46 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/autonat"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
|
2018-07-04 10:51:47 +00:00
|
|
|
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
2022-08-19 16:34:07 +00:00
|
|
|
blankhost "github.com/libp2p/go-libp2p/p2p/host/blank"
|
2023-05-19 20:23:55 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
|
2019-06-09 07:24:20 +00:00
|
|
|
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
|
2022-08-19 16:34:07 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/net/swarm"
|
|
|
|
tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader"
|
2022-04-01 16:16:46 +00:00
|
|
|
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
|
|
|
|
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
|
2023-04-07 18:23:07 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2021-10-19 13:43:41 +00:00
|
|
|
madns "github.com/multiformats/go-multiaddr-dns"
|
2023-02-22 21:58:17 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
"go.uber.org/fx/fxevent"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddrsFactory is a function that takes a set of multiaddrs we're listening on and
|
|
|
|
// returns the set of multiaddrs we should advertise to the network.
|
|
|
|
type AddrsFactory = bhost.AddrsFactory
|
|
|
|
|
|
|
|
// NATManagerC is a NATManager constructor.
|
2019-06-09 07:24:20 +00:00
|
|
|
type NATManagerC func(network.Network) bhost.NATManager
|
|
|
|
|
|
|
|
type RoutingC func(host.Host) (routing.PeerRouting, error)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// AutoNATConfig defines the AutoNAT behavior for the libp2p host.
|
|
|
|
type AutoNATConfig struct {
|
|
|
|
ForceReachability *network.Reachability
|
|
|
|
EnableService bool
|
|
|
|
ThrottleGlobalLimit int
|
|
|
|
ThrottlePeerLimit int
|
|
|
|
ThrottleInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
type Security struct {
|
|
|
|
ID protocol.ID
|
|
|
|
Constructor interface{}
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
// Config describes a set of settings for a libp2p node
|
|
|
|
//
|
|
|
|
// This is *not* a stable interface. Use the options defined in the root
|
|
|
|
// package.
|
|
|
|
type Config struct {
|
2019-10-04 15:21:24 +00:00
|
|
|
// UserAgent is the identifier this node will send to other peers when
|
|
|
|
// identifying itself, e.g. via the identify protocol.
|
|
|
|
//
|
|
|
|
// Set it via the UserAgent option function.
|
|
|
|
UserAgent string
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
// ProtocolVersion is the protocol version that identifies the family
|
|
|
|
// of protocols used by the peer in the Identify protocol. It is set
|
|
|
|
// using the [ProtocolVersion] option.
|
|
|
|
ProtocolVersion string
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
PeerKey crypto.PrivKey
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
QUICReuse []fx.Option
|
|
|
|
Transports []fx.Option
|
|
|
|
Muxers []tptu.StreamMuxer
|
|
|
|
SecurityTransports []Security
|
2018-07-04 10:51:47 +00:00
|
|
|
Insecure bool
|
2021-06-16 20:19:45 +00:00
|
|
|
PSK pnet.PSK
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
DialTimeout time.Duration
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
RelayCustom bool
|
2022-04-01 16:16:46 +00:00
|
|
|
Relay bool // should the relay transport be used
|
|
|
|
|
|
|
|
EnableRelayService bool // should we run a circuitv2 relay (if publicly reachable)
|
|
|
|
RelayServiceOpts []relayv2.Option
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
ListenAddrs []ma.Multiaddr
|
|
|
|
AddrsFactory bhost.AddrsFactory
|
|
|
|
ConnectionGater connmgr.ConnectionGater
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
ConnManager connmgr.ConnManager
|
|
|
|
ResourceManager network.ResourceManager
|
|
|
|
|
|
|
|
NATManager NATManagerC
|
|
|
|
Peerstore peerstore.Peerstore
|
|
|
|
Reporter metrics.Reporter
|
2019-06-09 07:24:20 +00:00
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
MultiaddrResolver *madns.Resolver
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
DisablePing bool
|
|
|
|
|
|
|
|
Routing RoutingC
|
|
|
|
|
|
|
|
EnableAutoRelay bool
|
2022-08-19 16:34:07 +00:00
|
|
|
AutoRelayOpts []autorelay.Option
|
2021-06-16 20:19:45 +00:00
|
|
|
AutoNATConfig
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
EnableHolePunching bool
|
|
|
|
HolePunchingOptions []holepunch.Option
|
2023-04-07 18:23:07 +00:00
|
|
|
|
|
|
|
DisableMetrics bool
|
|
|
|
PrometheusRegisterer prometheus.Registerer
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 20:23:55 +00:00
|
|
|
func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swarm, error) {
|
2021-06-16 20:19:45 +00:00
|
|
|
if cfg.Peerstore == nil {
|
|
|
|
return nil, fmt.Errorf("no peerstore specified")
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
// Check this early. Prevents us from even *starting* without verifying this.
|
2021-06-16 20:19:45 +00:00
|
|
|
if pnet.ForcePrivateNetwork && len(cfg.PSK) == 0 {
|
2018-07-04 10:51:47 +00:00
|
|
|
log.Error("tried to create a libp2p node with no Private" +
|
|
|
|
" Network Protector but usage of Private Networks" +
|
|
|
|
" is forced by the enviroment")
|
|
|
|
// Note: This is *also* checked the upgrader itself so it'll be
|
|
|
|
// enforced even *if* you don't use the libp2p constructor.
|
|
|
|
return nil, pnet.ErrNotInPrivateNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.PeerKey == nil {
|
|
|
|
return nil, fmt.Errorf("no peer key specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain Peer ID from public key
|
|
|
|
pid, err := peer.IDFromPublicKey(cfg.PeerKey.GetPublic())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
if err := cfg.Peerstore.AddPrivKey(pid, cfg.PeerKey); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := cfg.Peerstore.AddPubKey(pid, cfg.PeerKey.GetPublic()); err != nil {
|
|
|
|
return nil, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
opts := make([]swarm.Option, 0, 6)
|
2022-04-01 16:16:46 +00:00
|
|
|
if cfg.Reporter != nil {
|
|
|
|
opts = append(opts, swarm.WithMetrics(cfg.Reporter))
|
|
|
|
}
|
|
|
|
if cfg.ConnectionGater != nil {
|
|
|
|
opts = append(opts, swarm.WithConnectionGater(cfg.ConnectionGater))
|
|
|
|
}
|
|
|
|
if cfg.DialTimeout != 0 {
|
|
|
|
opts = append(opts, swarm.WithDialTimeout(cfg.DialTimeout))
|
|
|
|
}
|
|
|
|
if cfg.ResourceManager != nil {
|
|
|
|
opts = append(opts, swarm.WithResourceManager(cfg.ResourceManager))
|
|
|
|
}
|
2022-11-04 13:57:20 +00:00
|
|
|
if cfg.MultiaddrResolver != nil {
|
|
|
|
opts = append(opts, swarm.WithMultiaddrResolver(cfg.MultiaddrResolver))
|
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
if enableMetrics {
|
2023-04-07 18:23:07 +00:00
|
|
|
opts = append(opts,
|
|
|
|
swarm.WithMetricsTracer(swarm.NewMetricsTracer(swarm.WithRegisterer(cfg.PrometheusRegisterer))))
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
// TODO: Make the swarm implementation configurable.
|
2023-05-19 20:23:55 +00:00
|
|
|
return swarm.NewSwarm(pid, cfg.Peerstore, eventBus, opts...)
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
func (cfg *Config) addTransports(h host.Host) error {
|
2021-06-16 20:19:45 +00:00
|
|
|
swrm, ok := h.Network().(transport.TransportNetwork)
|
|
|
|
if !ok {
|
|
|
|
// Should probably skip this if no transports.
|
|
|
|
return fmt.Errorf("swarm does not support transports")
|
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
|
|
|
|
fxopts := []fx.Option{
|
|
|
|
fx.WithLogger(func() fxevent.Logger { return getFXLogger() }),
|
|
|
|
fx.Provide(fx.Annotate(tptu.New, fx.ParamTags(`name:"security"`))),
|
|
|
|
fx.Supply(cfg.Muxers),
|
|
|
|
fx.Supply(h.ID()),
|
|
|
|
fx.Provide(func() host.Host { return h }),
|
|
|
|
fx.Provide(func() crypto.PrivKey { return h.Peerstore().PrivKey(h.ID()) }),
|
|
|
|
fx.Provide(func() connmgr.ConnectionGater { return cfg.ConnectionGater }),
|
|
|
|
fx.Provide(func() pnet.PSK { return cfg.PSK }),
|
|
|
|
fx.Provide(func() network.ResourceManager { return cfg.ResourceManager }),
|
|
|
|
fx.Provide(func() *madns.Resolver { return cfg.MultiaddrResolver }),
|
|
|
|
}
|
|
|
|
fxopts = append(fxopts, cfg.Transports...)
|
2021-06-16 20:19:45 +00:00
|
|
|
if cfg.Insecure {
|
2023-02-22 21:58:17 +00:00
|
|
|
fxopts = append(fxopts,
|
|
|
|
fx.Provide(
|
|
|
|
fx.Annotate(
|
|
|
|
func(id peer.ID, priv crypto.PrivKey) []sec.SecureTransport {
|
|
|
|
return []sec.SecureTransport{insecure.NewWithIdentity(insecure.ID, id, priv)}
|
|
|
|
},
|
|
|
|
fx.ResultTags(`name:"security"`),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2021-06-16 20:19:45 +00:00
|
|
|
} else {
|
2023-02-22 21:58:17 +00:00
|
|
|
// fx groups are unordered, but we need to preserve the order of the security transports
|
|
|
|
// First of all, we construct the security transports that are needed,
|
|
|
|
// and save them to a group call security_unordered.
|
|
|
|
for _, s := range cfg.SecurityTransports {
|
|
|
|
fxName := fmt.Sprintf(`name:"security_%s"`, s.ID)
|
|
|
|
fxopts = append(fxopts, fx.Supply(fx.Annotate(s.ID, fx.ResultTags(fxName))))
|
|
|
|
fxopts = append(fxopts,
|
|
|
|
fx.Provide(fx.Annotate(
|
|
|
|
s.Constructor,
|
|
|
|
fx.ParamTags(fxName),
|
|
|
|
fx.As(new(sec.SecureTransport)),
|
|
|
|
fx.ResultTags(`group:"security_unordered"`),
|
|
|
|
)),
|
|
|
|
)
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
// Then we consume the group security_unordered, and order them by the user's preference.
|
|
|
|
fxopts = append(fxopts, fx.Provide(
|
|
|
|
fx.Annotate(
|
|
|
|
func(secs []sec.SecureTransport) ([]sec.SecureTransport, error) {
|
|
|
|
if len(secs) != len(cfg.SecurityTransports) {
|
|
|
|
return nil, errors.New("inconsistent length for security transports")
|
|
|
|
}
|
|
|
|
t := make([]sec.SecureTransport, 0, len(secs))
|
|
|
|
for _, s := range cfg.SecurityTransports {
|
|
|
|
for _, st := range secs {
|
|
|
|
if s.ID != st.ID() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t = append(t, st)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
},
|
|
|
|
fx.ParamTags(`group:"security_unordered"`),
|
|
|
|
fx.ResultTags(`name:"security"`),
|
|
|
|
)))
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
|
|
|
|
fxopts = append(fxopts, fx.Provide(PrivKeyToStatelessResetKey))
|
|
|
|
if cfg.QUICReuse != nil {
|
|
|
|
fxopts = append(fxopts, cfg.QUICReuse...)
|
|
|
|
} else {
|
|
|
|
fxopts = append(fxopts, fx.Provide(quicreuse.NewConnManager)) // TODO: close the ConnManager when shutting down the node
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
fxopts = append(fxopts, fx.Invoke(
|
|
|
|
fx.Annotate(
|
|
|
|
func(tpts []transport.Transport) error {
|
|
|
|
for _, t := range tpts {
|
|
|
|
if err := swrm.AddTransport(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
fx.ParamTags(`group:"transport"`),
|
|
|
|
)),
|
|
|
|
)
|
2021-06-16 20:19:45 +00:00
|
|
|
if cfg.Relay {
|
2023-02-22 21:58:17 +00:00
|
|
|
fxopts = append(fxopts, fx.Invoke(circuitv2.AddTransport))
|
|
|
|
}
|
|
|
|
app := fx.New(fxopts...)
|
|
|
|
if err := app.Err(); err != nil {
|
|
|
|
h.Close()
|
|
|
|
return err
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNode constructs a new libp2p Host from the Config.
|
|
|
|
//
|
|
|
|
// This function consumes the config. Do not reuse it (really!).
|
2022-04-01 16:16:46 +00:00
|
|
|
func (cfg *Config) NewNode() (host.Host, error) {
|
2023-05-19 20:23:55 +00:00
|
|
|
eventBus := eventbus.NewBus(eventbus.WithMetricsTracer(eventbus.NewMetricsTracer(eventbus.WithRegisterer(cfg.PrometheusRegisterer))))
|
|
|
|
swrm, err := cfg.makeSwarm(eventBus, !cfg.DisableMetrics)
|
2021-06-16 20:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
h, err := bhost.NewHost(swrm, &bhost.HostOpts{
|
2023-05-19 20:23:55 +00:00
|
|
|
EventBus: eventBus,
|
2023-04-07 18:23:07 +00:00
|
|
|
ConnManager: cfg.ConnManager,
|
|
|
|
AddrsFactory: cfg.AddrsFactory,
|
|
|
|
NATManager: cfg.NATManager,
|
|
|
|
EnablePing: !cfg.DisablePing,
|
|
|
|
UserAgent: cfg.UserAgent,
|
|
|
|
ProtocolVersion: cfg.ProtocolVersion,
|
|
|
|
EnableHolePunching: cfg.EnableHolePunching,
|
|
|
|
HolePunchingOptions: cfg.HolePunchingOptions,
|
|
|
|
EnableRelayService: cfg.EnableRelayService,
|
|
|
|
RelayServiceOpts: cfg.RelayServiceOpts,
|
|
|
|
EnableMetrics: !cfg.DisableMetrics,
|
|
|
|
PrometheusRegisterer: cfg.PrometheusRegisterer,
|
2018-07-04 10:51:47 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
swrm.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
if cfg.Relay {
|
|
|
|
// If we've enabled the relay, we should filter out relay
|
|
|
|
// addresses by default.
|
|
|
|
//
|
|
|
|
// TODO: We shouldn't be doing this here.
|
|
|
|
oldFactory := h.AddrsFactory
|
|
|
|
h.AddrsFactory = func(addrs []ma.Multiaddr) []ma.Multiaddr {
|
2022-04-01 16:16:46 +00:00
|
|
|
return oldFactory(autorelay.Filter(addrs))
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
if err := cfg.addTransports(h); err != nil {
|
2018-07-04 10:51:47 +00:00
|
|
|
h.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This method succeeds if listening on one address succeeds. We
|
|
|
|
// should probably fail if listening on *any* addr fails.
|
|
|
|
if err := h.Network().Listen(cfg.ListenAddrs...); err != nil {
|
|
|
|
h.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// Configure routing and autorelay
|
|
|
|
var router routing.PeerRouting
|
|
|
|
if cfg.Routing != nil {
|
|
|
|
router, err = cfg.Routing(h)
|
|
|
|
if err != nil {
|
|
|
|
h.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// Note: h.AddrsFactory may be changed by relayFinder, but non-relay version is
|
2021-06-16 20:19:45 +00:00
|
|
|
// used by AutoNAT below.
|
2022-04-01 16:16:46 +00:00
|
|
|
var ar *autorelay.AutoRelay
|
2021-06-16 20:19:45 +00:00
|
|
|
addrF := h.AddrsFactory
|
2019-06-09 07:24:20 +00:00
|
|
|
if cfg.EnableAutoRelay {
|
|
|
|
if !cfg.Relay {
|
|
|
|
h.Close()
|
|
|
|
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
|
|
|
|
}
|
2023-05-19 20:23:55 +00:00
|
|
|
if !cfg.DisableMetrics {
|
|
|
|
mt := autorelay.WithMetricsTracer(
|
|
|
|
autorelay.NewMetricsTracer(autorelay.WithRegisterer(cfg.PrometheusRegisterer)))
|
|
|
|
mtOpts := []autorelay.Option{mt}
|
|
|
|
cfg.AutoRelayOpts = append(mtOpts, cfg.AutoRelayOpts...)
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
ar, err = autorelay.NewAutoRelay(h, cfg.AutoRelayOpts...)
|
2022-04-01 16:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
autonatOpts := []autonat.Option{
|
|
|
|
autonat.UsingAddresses(func() []ma.Multiaddr {
|
|
|
|
return addrF(h.AllAddrs())
|
|
|
|
}),
|
|
|
|
}
|
2023-04-07 18:23:07 +00:00
|
|
|
if !cfg.DisableMetrics {
|
|
|
|
autonatOpts = append(autonatOpts,
|
|
|
|
autonat.WithMetricsTracer(
|
|
|
|
autonat.NewMetricsTracer(autonat.WithRegisterer(cfg.PrometheusRegisterer))))
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
if cfg.AutoNATConfig.ThrottleInterval != 0 {
|
|
|
|
autonatOpts = append(autonatOpts,
|
|
|
|
autonat.WithThrottling(cfg.AutoNATConfig.ThrottleGlobalLimit, cfg.AutoNATConfig.ThrottleInterval),
|
|
|
|
autonat.WithPeerThrottling(cfg.AutoNATConfig.ThrottlePeerLimit))
|
|
|
|
}
|
|
|
|
if cfg.AutoNATConfig.EnableService {
|
|
|
|
autonatPrivKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
ps, err := pstoremem.NewPeerstore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
// Pull out the pieces of the config that we _actually_ care about.
|
|
|
|
// Specifically, don't setup things like autorelay, listeners,
|
|
|
|
// identify, etc.
|
|
|
|
autoNatCfg := Config{
|
|
|
|
Transports: cfg.Transports,
|
|
|
|
Muxers: cfg.Muxers,
|
|
|
|
SecurityTransports: cfg.SecurityTransports,
|
|
|
|
Insecure: cfg.Insecure,
|
|
|
|
PSK: cfg.PSK,
|
|
|
|
ConnectionGater: cfg.ConnectionGater,
|
|
|
|
Reporter: cfg.Reporter,
|
|
|
|
PeerKey: autonatPrivKey,
|
2022-04-01 16:16:46 +00:00
|
|
|
Peerstore: ps,
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 20:23:55 +00:00
|
|
|
dialer, err := autoNatCfg.makeSwarm(eventbus.NewBus(), false)
|
2021-06-16 20:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
h.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dialerHost := blankhost.NewBlankHost(dialer)
|
2022-04-01 16:16:46 +00:00
|
|
|
if err := autoNatCfg.addTransports(dialerHost); err != nil {
|
2021-06-16 20:19:45 +00:00
|
|
|
dialerHost.Close()
|
|
|
|
h.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// NOTE: We're dropping the blank host here but that's fine. It
|
|
|
|
// doesn't really _do_ anything and doesn't even need to be
|
|
|
|
// closed (as long as we close the underlying network).
|
|
|
|
autonatOpts = append(autonatOpts, autonat.EnableService(dialerHost.Network()))
|
|
|
|
}
|
|
|
|
if cfg.AutoNATConfig.ForceReachability != nil {
|
|
|
|
autonatOpts = append(autonatOpts, autonat.WithReachability(*cfg.AutoNATConfig.ForceReachability))
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
autonat, err := autonat.New(h, autonatOpts...)
|
2021-10-19 13:43:41 +00:00
|
|
|
if err != nil {
|
2021-06-16 20:19:45 +00:00
|
|
|
h.Close()
|
|
|
|
return nil, fmt.Errorf("cannot enable autorelay; autonat failed to start: %v", err)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
h.SetAutoNat(autonat)
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
// start the host background tasks
|
|
|
|
h.Start()
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
var ho host.Host
|
|
|
|
ho = h
|
2019-06-09 07:24:20 +00:00
|
|
|
if router != nil {
|
2022-04-01 16:16:46 +00:00
|
|
|
ho = routed.Wrap(h, router)
|
|
|
|
}
|
|
|
|
if ar != nil {
|
2023-04-07 18:23:07 +00:00
|
|
|
arh := autorelay.NewAutoRelayHost(ho, ar)
|
|
|
|
arh.Start()
|
|
|
|
ho = arh
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
return ho, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option is a libp2p config option that can be given to the libp2p constructor
|
|
|
|
// (`libp2p.New`).
|
|
|
|
type Option func(cfg *Config) error
|
|
|
|
|
|
|
|
// Apply applies the given options to the config, returning the first error
|
|
|
|
// encountered (if any).
|
|
|
|
func (cfg *Config) Apply(opts ...Option) error {
|
|
|
|
for _, opt := range opts {
|
2019-06-09 07:24:20 +00:00
|
|
|
if opt == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
if err := opt(cfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|