defaults.go 4.18 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1
2
3
4
5
6
7
8
package libp2p

// This file contains all the default configuration options.

import (
	"crypto/rand"

	crypto "github.com/libp2p/go-libp2p-crypto"
Steven Allen's avatar
Steven Allen committed
9
	pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
Steven Allen's avatar
Steven Allen committed
10
11
12
	secio "github.com/libp2p/go-libp2p-secio"
	tcp "github.com/libp2p/go-tcp-transport"
	ws "github.com/libp2p/go-ws-transport"
Abhishek Upperwal's avatar
Abhishek Upperwal committed
13
	multiaddr "github.com/multiformats/go-multiaddr"
Steven Allen's avatar
Steven Allen committed
14
15
16
17
18
19
20
21
22
23
	mplex "github.com/whyrusleeping/go-smux-multiplex"
	yamux "github.com/whyrusleeping/go-smux-yamux"
)

// DefaultSecurity is the default security option.
//
// Useful when you want to extend, but not replace, the supported transport
// security protocols.
var DefaultSecurity = Security(secio.ID, secio.New)

Abhishek Upperwal's avatar
Abhishek Upperwal committed
24
// DefaultMuxers configures libp2p to use the stream connection multiplexers.
Steven Allen's avatar
Steven Allen committed
25
26
27
28
29
//
// 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),
30
	Muxer("/mplex/6.7.0", mplex.DefaultTransport),
Steven Allen's avatar
Steven Allen committed
31
32
33
34
)

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

// DefaultPeerstore configures libp2p to use the default peerstore.
var DefaultPeerstore Option = func(cfg *Config) error {
Steven Allen's avatar
Steven Allen committed
44
	return cfg.Apply(Peerstore(pstoremem.NewPeerstore()))
Steven Allen's avatar
Steven Allen committed
45
46
47
48
49
50
51
52
53
54
55
}

// RandomIdentity generates a random identity (default behaviour)
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))
}

Abhishek Upperwal's avatar
Abhishek Upperwal committed
56
// DefaultListenAddrs configures libp2p to use default listen address
Abhishek Upperwal's avatar
Abhishek Upperwal committed
57
var DefaultListenAddrs = func(cfg *Config) error {
58
	defaultIP4ListenAddr, err := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
Abhishek Upperwal's avatar
Abhishek Upperwal committed
59
60
61
	if err != nil {
		return err
	}
62

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

vyzo's avatar
vyzo committed
73
74
75
76
77
// DefaultEnableRelay enables relay dialing and listening by default
var DefaultEnableRelay = func(cfg *Config) error {
	return cfg.Apply(EnableRelay())
}

78
79
80
81
82
// DefaultEnablePing enables the ping service by default
var DefaultEnablePing = func(cfg *Config) error {
	return cfg.Apply(Ping(true))
}

Steven Allen's avatar
Steven Allen committed
83
84
85
86
87
88
89
90
// 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
}{
91
92
93
94
	{
		fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil },
		opt:      DefaultListenAddrs,
	},
Steven Allen's avatar
Steven Allen committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
	{
		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
115
116
117
118
	{
		fallback: func(cfg *Config) bool { return !cfg.RelayCustom },
		opt:      DefaultEnableRelay,
	},
119
120
121
122
	{
		fallback: func(cfg *Config) bool { return !cfg.PingCustom },
		opt:      DefaultEnablePing,
	},
Steven Allen's avatar
Steven Allen committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
}

// 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
// other relevent options have been applied. will be appended to the options
// 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
}