-
Notifications
You must be signed in to change notification settings - Fork 2
/
overlay.go
381 lines (326 loc) · 9.96 KB
/
overlay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
114
115
116
117
118
119
120
121
122
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
Package overlay spec outline
- spin up host/peer that will be a part of the overlay network
- spin up host/peer that will connect to public dht to perform rendezvous
- perform rendezvous on the namespace of the overlay dht 'name', i.e. 'cat-pics-0001'
- if there are any peers in the namespace,
- send a 'request' to them for their associated peer address, i.e. thethe peer/host connected to the overlay dht
- hand the responded address to the peer associated with the current peer
- the associated peer will then perform a bootstrapping to the provided address
- future optimizations:
- only require the last n peers to stay in the public dht network providing the rendezvous service
- if the number of peers in the overlay network drop below n, spin the public peer up and start rendezvous again
*/
package overlay
import (
"bufio"
"context"
"fmt"
"strings"
"sync"
"github.com/ipfs/go-log"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
discovery "github.com/libp2p/go-libp2p-discovery"
host "github.com/libp2p/go-libp2p-host"
dht "github.com/libp2p/go-libp2p-kad-dht"
inet "github.com/libp2p/go-libp2p-net"
peerstore "github.com/libp2p/go-libp2p-peerstore"
protocol "github.com/libp2p/go-libp2p-protocol"
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
maddr "github.com/multiformats/go-multiaddr"
logging "github.com/whyrusleeping/go-logging"
)
var logger = log.Logger("overlay-coordinator")
func init() {
log.SetAllLoggers(logging.WARNING)
log.SetLogLevel("overlay-coordinator", "DEBUG")
}
// Coordinator represents a Peer that connects to the Public DHT,
// a Peer that connects to an overlay DHT and the communication
// communication channels in between those peers.
type Coordinator struct {
*Identity
Config
PublicPeer, OverlayPeer host.Host
PublicDHT, OverlayDHT *dht.IpfsDHT
peerInfoCh chan peerstore.PeerInfo
overlayBootMu sync.RWMutex
overlayBootstrapped bool
}
// NewCoordinator is a constructor for the Coordinator type.
func NewCoordinator(ctx context.Context, config Config) (*Coordinator, error) {
p := &Coordinator{Config: config}
iden, err := LoadIdentity("./identity.json")
if err != nil {
logger.Error(err)
}
if iden == nil {
iden = &Identity{}
iden.PublicPrivKey, _, err = crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
return nil, err
}
iden.OverlayPrivKey, _, err = crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
return nil, err
}
}
p.Identity = iden
// libp2p.New constructs a new libp2p Host. Other options can be added
// here.
publicPeer, err := libp2p.New(
ctx,
libp2p.Identity(p.Identity.PublicPrivKey),
libp2p.ListenAddrs([]maddr.Multiaddr(p.ListenAddresses)...),
libp2p.NATPortMap(),
)
if err != nil {
return nil, err
}
overlayPeer, err := libp2p.New(
ctx,
libp2p.Identity(p.Identity.OverlayPrivKey),
libp2p.ListenAddrs([]maddr.Multiaddr(p.OverlayListenAddresses)...),
libp2p.NATPortMap(),
)
if err != nil {
return nil, err
}
// Set a function as stream handler. This function is called when a peer
// initiates a connection and starts a stream with this peer.
publicPeer.SetStreamHandler(protocol.ID(p.ProtocolID), publicPeerStreamHandlerFunc(p))
// Start a DHT, for use in peer discovery. We can't just make a new DHT
// client because we want each peer to maintain its own local copy of the
// DHT, so that the bootstrapping node of the DHT can go down without
// inhibiting future peer discovery.
p.PublicDHT, err = dht.New(ctx, publicPeer)
if err != nil {
return nil, err
}
// Bootstrap the DHT. In the default configuration, this spawns a Background
// thread that will refresh the peer table every five minutes.
// logger.Debug("Bootstrapping the DHT")
// create OverlayDHT
p.OverlayDHT, err = dht.New(ctx, overlayPeer)
if err != nil {
return nil, err
}
// wrap hosts in routedHost
p.PublicPeer = routedhost.Wrap(publicPeer, p.PublicDHT)
p.OverlayPeer = routedhost.Wrap(overlayPeer, p.OverlayDHT)
str := `Coordinator:
PublicPeer.ID: %v
PublicPeer.Addrs: %v
OverlayPeer.ID: %v
OverlayPeer.Addrs: %v`
logger.Debugf(
str,
p.PublicPeer.ID().Pretty(),
p.PublicPeer.Addrs(),
p.OverlayPeer.ID().Pretty(),
p.OverlayPeer.Addrs(),
)
err = p.Identity.Save("./identity.json")
if err != nil {
logger.Error(err)
}
return p, nil
}
// Bootstrap bootstraps the public peer, connects to rendezvous peers, and then
// bootstraps the overlay peer.
func (p *Coordinator) Bootstrap(ctx context.Context) error {
err := p.bootstrapPublicIPFSNetwork(ctx, p.Config.BootstrapPeers)
if err != nil {
logger.Error(err)
}
go p.bootstrapOverlayNetwork(ctx)
err = p.coordinateOverlayPeers(ctx)
if err != nil {
logger.Error(err)
}
return nil
}
// bootstrapPublicIPFSNetwork connects the PublicPeer to the configured BootstrapPeers.
func (p *Coordinator) bootstrapPublicIPFSNetwork(ctx context.Context, peers []maddr.Multiaddr) error {
connected := make(chan struct{})
// bootstrap public peer
var wg sync.WaitGroup
logger.Debugf("bootstrap peers: %d", len(peers))
for _, peerAddr := range peers {
peerinfo, _ := peerstore.InfoFromP2pAddr(peerAddr)
wg.Add(1)
go func() {
defer wg.Done()
if err := p.PublicPeer.Connect(ctx, *peerinfo); err != nil {
logger.Error(err)
return
}
logger.Info("connection established with bootstrap node: ", *peerinfo)
connected <- struct{}{}
}()
}
logger.Debug("waiting for connection go routines to return")
go func() {
wg.Wait()
close(connected)
}()
i := 0
for range connected {
i++
}
if nPeers := len(peers); i < nPeers/2 {
logger.Warningf("only connected to %d bootstrap peers out of %d", i, nPeers)
} else {
logger.Warningf("connected to %d bootstrap peers out of %d", i, nPeers)
}
return p.PublicDHT.Bootstrap(ctx)
}
// bootstrapOverlayNetwork connects the PublicPeer to the configured BootstrapPeers.
func (p *Coordinator) bootstrapOverlayNetwork(ctx context.Context) error {
connected := make(chan struct{})
// bootstrap public peer
var wg sync.WaitGroup
for pi := range p.peerInfoCh {
wg.Add(1)
go func() {
defer wg.Done()
if err := p.OverlayPeer.Connect(ctx, *pi); err != nil {
logger.Error(err)
return
}
logger.Info("connection established with bootstrap node: ", *pi)
connected <- struct{}{}
}()
}
logger.Debug("waiting for connection go routines to return")
go func() {
wg.Wait()
close(connected)
}()
i := 0
for range connected {
i++
}
logger.Warningf("connected to %d bootstrap peers", i)
return p.OverlayDHT.Bootstrap(ctx)
}
// coordinateOverlayPeers coordinates between the public peer and the
// overlay peer to gather overlay peer addresses.
func (p *Coordinator) coordinateOverlayPeers(ctx context.Context) error {
logger.Debug("starting discovery")
// annouce on the public dht that we are a coordinator node
routingDiscovery := discovery.NewRoutingDiscovery(p.PublicDHT)
logger.Debug("advertising peer")
discovery.Advertise(ctx, routingDiscovery, p.Config.RendezvousString)
logger.Debug("finding other peers")
// perform rendezvous to get the addresses of other coordinator public peers
peerChan, err := routingDiscovery.FindPeers(ctx, p.Config.RendezvousString)
if err != nil {
return err
}
gotPeerCh := make(chan struct{})
go func() {
// process peers performing rendezvous protocol
for pr := range peerChan {
if pr.ID == p.PublicPeer.ID() {
continue
}
logger.Info("connecting to peer: ", pr)
stream, err := p.PublicPeer.NewStream(ctx, pr.ID, protocol.ID(p.Config.ProtocolID))
if err != nil {
logger.Error("connection failed: ", err)
continue
}
close(gotPeerCh)
rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
go p.readOverlayPeerInfo(rw)
go p.sendOverlayPeerInfo(rw)
}
}()
<-gotPeerCh
return nil
}
func publicPeerStreamHandlerFunc(p *Coordinator) func(inet.Stream) {
return func(stream inet.Stream) {
logger.Info("Got a new stream!")
// Create a buffer stream for non blocking read and write.
rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))
go p.readOverlayPeerInfo(rw)
go p.sendOverlayPeerInfo(rw)
}
}
func (p *Coordinator) readOverlayPeerInfo(rw *bufio.ReadWriter) {
logger.Info("reading overlay peer info")
str, err := rw.ReadString('\n')
if err != nil {
logger.Error("error reading from buffer")
panic(err)
}
mas, err := textUnmarshalMultiaddrSlice(str)
if err != nil {
logger.Error(str)
panic(err)
}
logger.Info("got multiaddr, putting on addrCh")
for _, ma := range mas {
p.addrsCh <- ma
}
}
func (p *Coordinator) sendOverlayPeerInfo(rw *bufio.ReadWriter) {
logger.Info("sending overlay peer info")
logger.Info(p.OverlayPeer.Addrs())
mas, err := textMarshalMultiaddrSlice(p.OverlayPeer.Addrs())
if err != nil {
logger.Errorf("sendOverlayPeerAddr: %v", err)
return
}
err = writeData(rw, mas)
if err != nil {
logger.Errorf("sendOverlayPeerAddr: %v", err)
return
}
}
func textMarshalMultiaddrSlice(mas []maddr.Multiaddr) (string, error) {
var result string
for _, ma := range mas {
text, err := ma.MarshalText()
if err != nil {
return "", err
}
result += string(text) + ","
}
strings.TrimSuffix(result, ",")
return result, nil
}
func textUnmarshalMultiaddrSlice(text string) ([]maddr.Multiaddr, error) {
mas := make([]maddr.Multiaddr, 1)
mastrs := strings.Split(text, ",")
for _, mastr := range mastrs {
ma, err := maddr.NewMultiaddr("")
if err != nil {
logger.Errorf("NewMultiaddr: %v", err)
return nil, err
}
logger.Debug(mastr)
err = ma.UnmarshalText([]byte(strings.TrimSpace(mastr)))
if err != nil {
logger.Errorf("ma.UnmarshalText: %v", err)
return nil, err
}
mas = append(mas, ma)
}
return mas, nil
}
func writeData(rw *bufio.ReadWriter, data string) error {
logger.Debug("writedata: ", data)
_, err := rw.WriteString(fmt.Sprintf("%s\n", data))
if err != nil {
return fmt.Errorf("error writing to buffer: %v", err)
}
err = rw.Flush()
if err != nil {
return fmt.Errorf("error flushing buffer: %v", err)
}
return nil
}