2018-03-10 01:56:02 +00:00
|
|
|
package libp2p
|
|
|
|
|
|
|
|
// This file contains all the default configuration options.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
|
2021-07-16 16:41:50 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2019-05-22 02:44:10 +00:00
|
|
|
mplex "github.com/libp2p/go-libp2p-mplex"
|
2020-06-24 17:28:07 +00:00
|
|
|
noise "github.com/libp2p/go-libp2p-noise"
|
2021-07-16 16:41:50 +00:00
|
|
|
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
|
|
|
|
quic "github.com/libp2p/go-libp2p-quic-transport"
|
2022-01-07 12:48:36 +00:00
|
|
|
rcmgr "github.com/libp2p/go-libp2p-resource-manager"
|
2020-04-13 18:15:24 +00:00
|
|
|
tls "github.com/libp2p/go-libp2p-tls"
|
2019-05-22 02:44:10 +00:00
|
|
|
yamux "github.com/libp2p/go-libp2p-yamux"
|
2022-01-18 08:57:07 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
|
2021-07-16 16:41:50 +00:00
|
|
|
"github.com/libp2p/go-tcp-transport"
|
2018-03-10 01:56:02 +00:00
|
|
|
ws "github.com/libp2p/go-ws-transport"
|
2021-07-16 16:41:50 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2018-03-10 01:56:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultSecurity is the default security option.
|
|
|
|
//
|
|
|
|
// Useful when you want to extend, but not replace, the supported transport
|
|
|
|
// security protocols.
|
2020-04-13 18:15:24 +00:00
|
|
|
var DefaultSecurity = ChainOptions(
|
2020-06-24 17:28:07 +00:00
|
|
|
Security(noise.ID, noise.New),
|
2020-04-13 18:15:24 +00:00
|
|
|
Security(tls.ID, tls.New),
|
|
|
|
)
|
2018-03-10 01:56:02 +00:00
|
|
|
|
2018-07-25 16:32:37 +00:00
|
|
|
// DefaultMuxers configures libp2p to use the stream connection multiplexers.
|
2018-03-10 01:56:02 +00:00
|
|
|
//
|
|
|
|
// Use this option when you want to *extend* the set of multiplexers used by
|
|
|
|
// libp2p instead of replacing them.
|
|
|
|
var DefaultMuxers = ChainOptions(
|
|
|
|
Muxer("/yamux/1.0.0", yamux.DefaultTransport),
|
2018-07-30 19:29:16 +00:00
|
|
|
Muxer("/mplex/6.7.0", mplex.DefaultTransport),
|
2018-03-10 01:56:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultTransports are the default libp2p transports.
|
|
|
|
//
|
2018-09-12 17:02:07 +00:00
|
|
|
// Use this option when you want to *extend* the set of transports used by
|
2018-03-10 01:56:02 +00:00
|
|
|
// libp2p instead of replacing them.
|
|
|
|
var DefaultTransports = ChainOptions(
|
|
|
|
Transport(tcp.NewTCPTransport),
|
2021-07-16 16:41:50 +00:00
|
|
|
Transport(quic.NewTransport),
|
2018-03-10 01:56:02 +00:00
|
|
|
Transport(ws.New),
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultPeerstore configures libp2p to use the default peerstore.
|
|
|
|
var DefaultPeerstore Option = func(cfg *Config) error {
|
2021-11-12 18:43:02 +00:00
|
|
|
ps, err := pstoremem.NewPeerstore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cfg.Apply(Peerstore(ps))
|
2018-03-10 01:56:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 14:33:32 +00:00
|
|
|
// RandomIdentity generates a random identity. (default behaviour)
|
2018-03-10 01:56:02 +00:00
|
|
|
var RandomIdentity = func(cfg *Config) error {
|
|
|
|
priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cfg.Apply(Identity(priv))
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:33:32 +00:00
|
|
|
// DefaultListenAddrs configures libp2p to use default listen address.
|
2018-07-25 16:20:54 +00:00
|
|
|
var DefaultListenAddrs = func(cfg *Config) error {
|
2018-07-25 18:12:03 +00:00
|
|
|
defaultIP4ListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
|
2018-07-25 16:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-25 18:12:03 +00:00
|
|
|
|
2018-07-27 21:17:27 +00:00
|
|
|
defaultIP6ListenAddr, err := multiaddr.NewMultiaddr("/ip6/::/tcp/0")
|
2018-07-25 18:12:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cfg.Apply(ListenAddrs(
|
|
|
|
defaultIP4ListenAddr,
|
|
|
|
defaultIP6ListenAddr,
|
|
|
|
))
|
2018-07-25 16:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 14:33:32 +00:00
|
|
|
// DefaultEnableRelay enables relay dialing and listening by default.
|
2018-09-28 08:30:45 +00:00
|
|
|
var DefaultEnableRelay = func(cfg *Config) error {
|
|
|
|
return cfg.Apply(EnableRelay())
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:48:36 +00:00
|
|
|
var DefaultResourceManager = func(cfg *Config) error {
|
|
|
|
// Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB
|
|
|
|
limiter := rcmgr.NewDefaultLimiter()
|
|
|
|
SetDefaultServiceLimits(limiter)
|
|
|
|
|
|
|
|
mgr, err := rcmgr.NewResourceManager(limiter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.Apply(ResourceManager(mgr))
|
|
|
|
}
|
|
|
|
|
2022-01-18 08:57:07 +00:00
|
|
|
// DefaultConnManager creates a default connection manager
|
|
|
|
var DefaultConnectionManager = func(cfg *Config) error {
|
2022-01-18 09:37:47 +00:00
|
|
|
mgr, err := connmgr.NewConnManager(160, 192)
|
2022-01-18 08:57:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.Apply(ConnectionManager(mgr))
|
|
|
|
}
|
|
|
|
|
2018-03-10 01:56:02 +00:00
|
|
|
// Complete list of default options and when to fallback on them.
|
|
|
|
//
|
|
|
|
// Please *DON'T* specify default options any other way. Putting this all here
|
|
|
|
// makes tracking defaults *much* easier.
|
|
|
|
var defaults = []struct {
|
|
|
|
fallback func(cfg *Config) bool
|
|
|
|
opt Option
|
|
|
|
}{
|
2018-07-25 18:12:03 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil },
|
|
|
|
opt: DefaultListenAddrs,
|
|
|
|
},
|
2018-03-10 01:56:02 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Transports == nil },
|
|
|
|
opt: DefaultTransports,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Muxers == nil },
|
|
|
|
opt: DefaultMuxers,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return !cfg.Insecure && cfg.SecurityTransports == nil },
|
|
|
|
opt: DefaultSecurity,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.PeerKey == nil },
|
|
|
|
opt: RandomIdentity,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Peerstore == nil },
|
|
|
|
opt: DefaultPeerstore,
|
|
|
|
},
|
2018-09-28 08:30:45 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return !cfg.RelayCustom },
|
|
|
|
opt: DefaultEnableRelay,
|
|
|
|
},
|
2022-01-07 12:48:36 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.ResourceManager == nil },
|
|
|
|
opt: DefaultResourceManager,
|
|
|
|
},
|
2022-01-18 08:57:07 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.ConnManager == nil },
|
|
|
|
opt: DefaultConnectionManager,
|
|
|
|
},
|
2018-03-10 01:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults configures libp2p to use the default options. Can be combined with
|
|
|
|
// other options to *extend* the default options.
|
|
|
|
var Defaults Option = func(cfg *Config) error {
|
|
|
|
for _, def := range defaults {
|
|
|
|
if err := cfg.Apply(def.opt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FallbackDefaults applies default options to the libp2p node if and only if no
|
2019-07-22 12:44:24 +00:00
|
|
|
// other relevant options have been applied. will be appended to the options
|
2018-03-10 01:56:02 +00:00
|
|
|
// passed into New.
|
|
|
|
var FallbackDefaults Option = func(cfg *Config) error {
|
|
|
|
for _, def := range defaults {
|
|
|
|
if !def.fallback(cfg) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := cfg.Apply(def.opt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|