2018-03-10 01:56:02 +00:00
|
|
|
package libp2p
|
|
|
|
|
|
|
|
// This file contains all the default configuration options.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
|
2022-08-17 07:38:18 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2022-08-15 09:08:56 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
|
2022-08-15 10:01:02 +00:00
|
|
|
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
|
2022-04-25 19:03:16 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
|
2022-04-22 13:43:07 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
|
2022-04-26 17:59:10 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/security/noise"
|
2022-04-27 10:00:11 +00:00
|
|
|
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
|
2022-04-22 15:34:30 +00:00
|
|
|
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
|
2022-04-22 13:43:07 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
2022-04-22 14:49:01 +00:00
|
|
|
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
|
2022-04-22 13:43:07 +00:00
|
|
|
|
2021-07-16 16:41:50 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2022-08-22 11:04:56 +00:00
|
|
|
madns "github.com/multiformats/go-multiaddr-dns"
|
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.
|
2022-03-30 16:51:36 +00:00
|
|
|
var DefaultMuxers = Muxer("/yamux/1.0.0", yamux.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),
|
|
|
|
)
|
|
|
|
|
2022-12-02 07:18:26 +00:00
|
|
|
// DefaultPrivateTransports are the default libp2p transports when a PSK is supplied.
|
|
|
|
//
|
|
|
|
// Use this option when you want to *extend* the set of transports used by
|
|
|
|
// libp2p instead of replacing them.
|
|
|
|
var DefaultPrivateTransports = ChainOptions(
|
|
|
|
Transport(tcp.NewTCPTransport),
|
|
|
|
Transport(ws.New),
|
|
|
|
)
|
|
|
|
|
2018-03-10 01:56:02 +00:00
|
|
|
// 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 {
|
2022-08-09 15:03:55 +00:00
|
|
|
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
2018-03-10 01:56:02 +00:00
|
|
|
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 {
|
2022-11-22 21:53:39 +00:00
|
|
|
addrs := []string{
|
|
|
|
"/ip4/0.0.0.0/tcp/0",
|
|
|
|
"/ip4/0.0.0.0/udp/0/quic",
|
|
|
|
"/ip4/0.0.0.0/udp/0/quic-v1",
|
|
|
|
"/ip6/::/tcp/0",
|
|
|
|
"/ip6/::/udp/0/quic",
|
|
|
|
"/ip6/::/udp/0/quic-v1",
|
2018-07-25 16:20:54 +00:00
|
|
|
}
|
2022-11-22 21:53:39 +00:00
|
|
|
listenAddrs := make([]multiaddr.Multiaddr, 0, len(addrs))
|
|
|
|
for _, s := range addrs {
|
|
|
|
addr, err := multiaddr.NewMultiaddr(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
listenAddrs = append(listenAddrs, addr)
|
2018-07-25 18:12:03 +00:00
|
|
|
}
|
2022-11-22 21:53:39 +00:00
|
|
|
return cfg.Apply(ListenAddrs(listenAddrs...))
|
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
|
2022-07-03 17:04:29 +00:00
|
|
|
limits := rcmgr.DefaultLimits
|
|
|
|
SetDefaultServiceLimits(&limits)
|
|
|
|
mgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(limits.AutoScale()))
|
2022-01-07 12:48:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.Apply(ResourceManager(mgr))
|
|
|
|
}
|
|
|
|
|
2022-07-03 17:04:29 +00:00
|
|
|
// DefaultConnectionManager creates a default connection manager
|
2022-01-18 08:57:07 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-08-22 11:04:56 +00:00
|
|
|
// DefaultMultiaddrResolver creates a default connection manager
|
|
|
|
var DefaultMultiaddrResolver = func(cfg *Config) error {
|
|
|
|
return cfg.Apply(MultiaddrResolver(madns.DefaultResolver))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-12-02 07:18:26 +00:00
|
|
|
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.PSK == nil },
|
2018-03-10 01:56:02 +00:00
|
|
|
opt: DefaultTransports,
|
|
|
|
},
|
2022-12-02 07:18:26 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.PSK != nil },
|
|
|
|
opt: DefaultPrivateTransports,
|
|
|
|
},
|
2018-03-10 01:56:02 +00:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
2022-08-22 11:04:56 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.MultiaddrResolver == nil },
|
|
|
|
opt: DefaultMultiaddrResolver,
|
|
|
|
},
|
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
|
|
|
|
}
|