defaults.go 3.98 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
1
package p2p
Steven Allen's avatar
Steven Allen committed
2 3 4 5 6 7

// This file contains all the default configuration options.

import (
	"crypto/rand"

8
	multiaddr "gitlab.dms3.io/mf/go-multiaddr"
tavit ohanian's avatar
tavit ohanian committed
9 10 11 12 13 14 15 16
	crypto "gitlab.dms3.io/p2p/go-p2p-core/crypto"
	mplex "gitlab.dms3.io/p2p/go-p2p-mplex"
	noise "gitlab.dms3.io/p2p/go-p2p-noise"
	pstoremem "gitlab.dms3.io/p2p/go-p2p-peerstore/pstoremem"
	tls "gitlab.dms3.io/p2p/go-p2p-tls"
	yamux "gitlab.dms3.io/p2p/go-p2p-yamux"
	tcp "gitlab.dms3.io/p2p/go-tcp-transport"
	ws "gitlab.dms3.io/p2p/go-ws-transport"
Steven Allen's avatar
Steven Allen committed
17 18 19 20 21 22
)

// DefaultSecurity is the default security option.
//
// Useful when you want to extend, but not replace, the supported transport
// security protocols.
Steven Allen's avatar
Steven Allen committed
23
var DefaultSecurity = ChainOptions(
24
	Security(noise.ID, noise.New),
Steven Allen's avatar
Steven Allen committed
25 26
	Security(tls.ID, tls.New),
)
Steven Allen's avatar
Steven Allen committed
27

tavit ohanian's avatar
tavit ohanian committed
28
// DefaultMuxers configures p2p to use the stream connection multiplexers.
Steven Allen's avatar
Steven Allen committed
29 30
//
// Use this option when you want to *extend* the set of multiplexers used by
tavit ohanian's avatar
tavit ohanian committed
31
// p2p instead of replacing them.
Steven Allen's avatar
Steven Allen committed
32 33
var DefaultMuxers = ChainOptions(
	Muxer("/yamux/1.0.0", yamux.DefaultTransport),
34
	Muxer("/mplex/6.7.0", mplex.DefaultTransport),
Steven Allen's avatar
Steven Allen committed
35 36
)

tavit ohanian's avatar
tavit ohanian committed
37
// DefaultTransports are the default p2p transports.
Steven Allen's avatar
Steven Allen committed
38
//
gukq's avatar
gukq committed
39
// Use this option when you want to *extend* the set of transports used by
tavit ohanian's avatar
tavit ohanian committed
40
// p2p instead of replacing them.
Steven Allen's avatar
Steven Allen committed
41 42 43 44 45
var DefaultTransports = ChainOptions(
	Transport(tcp.NewTCPTransport),
	Transport(ws.New),
)

tavit ohanian's avatar
tavit ohanian committed
46
// DefaultPeerstore configures p2p to use the default peerstore.
Steven Allen's avatar
Steven Allen committed
47
var DefaultPeerstore Option = func(cfg *Config) error {
Steven Allen's avatar
Steven Allen committed
48
	return cfg.Apply(Peerstore(pstoremem.NewPeerstore()))
Steven Allen's avatar
Steven Allen committed
49 50
}

Vasco Santos's avatar
Vasco Santos committed
51
// RandomIdentity generates a random identity. (default behaviour)
Steven Allen's avatar
Steven Allen committed
52 53 54 55 56 57 58 59
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))
}

tavit ohanian's avatar
tavit ohanian committed
60
// DefaultListenAddrs configures p2p to use default listen address.
Abhishek Upperwal's avatar
Abhishek Upperwal committed
61
var DefaultListenAddrs = func(cfg *Config) error {
62
	defaultIP4ListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
Abhishek Upperwal's avatar
Abhishek Upperwal committed
63 64 65
	if err != nil {
		return err
	}
66

Abhishek Upperwal's avatar
Abhishek Upperwal committed
67
	defaultIP6ListenAddr, err := multiaddr.NewMultiaddr("/ip6/::/tcp/0")
68 69 70 71 72 73 74
	if err != nil {
		return err
	}
	return cfg.Apply(ListenAddrs(
		defaultIP4ListenAddr,
		defaultIP6ListenAddr,
	))
Abhishek Upperwal's avatar
Abhishek Upperwal committed
75 76
}

Vasco Santos's avatar
Vasco Santos committed
77
// DefaultEnableRelay enables relay dialing and listening by default.
vyzo's avatar
vyzo committed
78 79 80 81
var DefaultEnableRelay = func(cfg *Config) error {
	return cfg.Apply(EnableRelay())
}

Steven Allen's avatar
Steven Allen committed
82 83 84 85 86 87 88 89
// 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
}{
90 91 92 93
	{
		fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil },
		opt:      DefaultListenAddrs,
	},
Steven Allen's avatar
Steven Allen committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	{
		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,
	},
vyzo's avatar
vyzo committed
114 115 116 117
	{
		fallback: func(cfg *Config) bool { return !cfg.RelayCustom },
		opt:      DefaultEnableRelay,
	},
Steven Allen's avatar
Steven Allen committed
118 119
}

tavit ohanian's avatar
tavit ohanian committed
120
// Defaults configures p2p to use the default options. Can be combined with
Steven Allen's avatar
Steven Allen committed
121 122 123 124 125 126 127 128 129 130
// 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
}

tavit ohanian's avatar
tavit ohanian committed
131
// FallbackDefaults applies default options to the p2p node if and only if no
Christian Muehlhaeuser's avatar
Christian Muehlhaeuser committed
132
// other relevant options have been applied. will be appended to the options
Steven Allen's avatar
Steven Allen committed
133 134 135 136 137 138 139 140 141 142 143 144
// 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
}