libp2p.go 4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package libp2p

import (
	"context"
	"crypto/rand"

	crypto "github.com/libp2p/go-libp2p-crypto"
	host "github.com/libp2p/go-libp2p-host"
	pnet "github.com/libp2p/go-libp2p-interface-pnet"
	metrics "github.com/libp2p/go-libp2p-metrics"
	peer "github.com/libp2p/go-libp2p-peer"
	pstore "github.com/libp2p/go-libp2p-peerstore"
	swarm "github.com/libp2p/go-libp2p-swarm"
	transport "github.com/libp2p/go-libp2p-transport"
	bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
	mux "github.com/libp2p/go-stream-muxer"
	ma "github.com/multiformats/go-multiaddr"
	mplex "github.com/whyrusleeping/go-smux-multiplex"
	msmux "github.com/whyrusleeping/go-smux-multistream"
	yamux "github.com/whyrusleeping/go-smux-yamux"
)

Jeromy's avatar
Jeromy committed
23
// Config describes a set of settings for a libp2p node
24
type Config struct {
Jeromy's avatar
Jeromy committed
25
26
27
28
29
30
31
32
	Transports   []transport.Transport
	Muxer        mux.Transport
	ListenAddrs  []ma.Multiaddr
	PeerKey      crypto.PrivKey
	Peerstore    pstore.Peerstore
	Protector    pnet.Protector
	Reporter     metrics.Reporter
	DisableSecio bool
33
34
}

Jeromy's avatar
Jeromy committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
type Option func(cfg *Config) error

func Transports(tpts []transport.Transport) Option {
	return func(cfg *Config) error {
		cfg.Transports = tpts
		return nil
	}
}

func ListenAddrStrings(s ...string) Option {
	return func(cfg *Config) error {
		for _, addrstr := range s {
			a, err := ma.NewMultiaddr(addrstr)
			if err != nil {
				return err
			}
			cfg.ListenAddrs = append(cfg.ListenAddrs, a)
		}
		return nil
	}
}

func ListenAddrs(addrs []ma.Multiaddr) Option {
	return func(cfg *Config) error {
		cfg.ListenAddrs = append(cfg.ListenAddrs, addrs...)
		return nil
	}
}

func NoSecio(cfg *Config) error {
	cfg.DisableSecio = true
	return nil
}

func WithMuxer(m mux.Transport) Option {
	return func(cfg *Config) error {
		cfg.Muxer = m
		return nil
	}
}

func WithPeerstore(ps pstore.Peerstore) Option {
	return func(cfg *Config) error {
		cfg.Peerstore = ps
		return nil
	}
}

func WithNetProtector(prot pnet.Protector) Option {
	return func(cfg *Config) error {
		cfg.Protector = prot
		return nil
	}
}

func WithBandwidthReporter(rep metrics.Reporter) Option {
	return func(cfg *Config) error {
		cfg.Reporter = rep
		return nil
	}
}

func New(ctx context.Context, opts ...Option) (host.Host, error) {
	cfg := DefaultConfig()

	for _, opt := range opts {
		if err := opt(cfg); err != nil {
			return nil, err
		}
	}

	return newWithCfg(ctx, cfg)
}

func WithPeerKey(sk crypto.PrivKey) Option {
	return func(cfg *Config) error {
		cfg.PeerKey = sk
		return nil
	}
Jeromy's avatar
Jeromy committed
114
115
}

Jeromy's avatar
Jeromy committed
116
func newWithCfg(ctx context.Context, cfg *Config) (host.Host, error) {
117
118
119
120
	if cfg == nil {
		cfg = DefaultConfig()
	}

Jeromy's avatar
Jeromy committed
121
	// If no key was given, generate a random 2048 bit RSA key
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	if cfg.PeerKey == nil {
		priv, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, rand.Reader)
		if err != nil {
			return nil, err
		}
		cfg.PeerKey = priv
	}

	// Obtain Peer ID from public key
	pid, err := peer.IDFromPublicKey(cfg.PeerKey.GetPublic())
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
136
	// Create a new blank peerstore if none was passed in
137
138
139
140
141
	ps := cfg.Peerstore
	if ps == nil {
		ps = pstore.NewPeerstore()
	}

Jeromy's avatar
Jeromy committed
142
143
144
145
146
	// If secio is disabled, don't add our private key to the peerstore
	if !cfg.DisableSecio {
		ps.AddPrivKey(pid, cfg.PeerKey)
		ps.AddPubKey(pid, cfg.PeerKey.GetPublic())
	}
147
148
149
150
151
152
153
154
155
156
157
158
159
160

	swrm, err := swarm.NewSwarmWithProtector(ctx, cfg.ListenAddrs, pid, ps, cfg.Protector, cfg.Muxer, cfg.Reporter)
	if err != nil {
		return nil, err
	}

	netw := (*swarm.Network)(swrm)

	return bhost.New(netw), nil
}

func DefaultMuxer() mux.Transport {
	// Set up stream multiplexer
	tpt := msmux.NewBlankTransport()
Jeromy's avatar
Jeromy committed
161
162

	// By default, support yamux and multiplex
163
164
	tpt.AddTransport("/yamux/1.0.0", yamux.DefaultTransport)
	tpt.AddTransport("/mplex/6.3.0", mplex.DefaultTransport)
Jeromy's avatar
Jeromy committed
165

166
167
168
169
	return tpt
}

func DefaultConfig() *Config {
Jeromy's avatar
Jeromy committed
170
171
	// Create a multiaddress that listens on a random port on all interfaces
	addr, err := ma.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
172
173
174
175
176
177
178
179
180
181
	if err != nil {
		panic(err)
	}

	return &Config{
		ListenAddrs: []ma.Multiaddr{addr},
		Peerstore:   pstore.NewPeerstore(),
		Muxer:       DefaultMuxer(),
	}
}