-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2pnet.go
208 lines (162 loc) · 4.64 KB
/
p2pnet.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
package p2pnet
import (
"context"
"crypto/rand"
"fmt"
"io/ioutil"
"log"
"time"
libp2p "github.com/libp2p/go-libp2p"
circuit "github.com/libp2p/go-libp2p-circuit"
crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
pstore "github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p/p2p/discovery"
multiaddr "github.com/multiformats/go-multiaddr"
)
//Network represents libp2p network layer.
type Network struct {
ctx context.Context
cfg *Config
addresses []multiaddr.Multiaddr
bootstrapDone chan struct{}
//ID of this Host
UUID string
//Stream manager
StreamMgr *StreamMgr
//RPC object of network
RPC *RPC
//Router object of network
Router *Router
//libp2p host handle. We dont need this mostly
Host host.Host
}
func readKey(keyfile string) (crypto.PrivKey, error) {
raw, err := ioutil.ReadFile(keyfile)
if err != nil {
return nil, err
}
return crypto.UnmarshalPrivateKey(raw)
}
func writeKey(key crypto.PrivKey, keyfile string) {
raw, err := crypto.MarshalPrivateKey(key)
if err != nil {
log.Printf("Could not write key to file (%s) ", err)
}
ioutil.WriteFile(keyfile, raw, 0640)
}
//NewNetwork creates a network handle
func NewNetwork(cfg *Config) (*Network, error) {
n := &Network{cfg: cfg}
n.ctx = context.Background()
r := rand.Reader
var prvKey crypto.PrivKey
// Check if we can retrieve key from file
prvKey, err := readKey(cfg.KeyFile)
if err != nil {
// Creates a new RSA key pair for this host.
prvKey, _, err = crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
if err != nil {
return nil, err
}
writeKey(prvKey, cfg.KeyFile)
}
sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", cfg.ListenHost, cfg.ListenPort))
// Other options can be added here.
opts := []libp2p.Option{libp2p.ListenAddrs(sourceMultiAddr), libp2p.Identity(prvKey)}
if cfg.EnableRelay {
opts = append(opts, libp2p.EnableRelay(circuit.OptActive, circuit.OptHop, circuit.OptDiscovery))
}
// libp2p.New constructs a new libp2p Host.
n.Host, err = libp2p.New(n.ctx, opts...)
n.StreamMgr = &StreamMgr{host: n.Host}
n.bootstrapDone = make(chan struct{})
if err != nil {
return nil, err
}
n.Router, err = initRouter(n.ctx, n.Host)
if err != nil {
log.Println("Discovery object initialization failed")
return nil, err
}
for _, addr := range cfg.BootstrapPeers {
peerinfo, _ := pstore.InfoFromP2pAddr(addr)
n.HandlePeerFound(*peerinfo)
}
n.UUID = n.Host.ID().Pretty()
log.Printf("Host ID is %s\n", n.UUID)
//Save the our addresses
addrs := n.Host.Addrs()
n.addresses = make([]multiaddr.Multiaddr, len(addrs))
for i, addr := range addrs {
ipfsAddr, err := multiaddr.NewMultiaddr("/p2p/" + n.Host.ID().Pretty())
if err != nil {
panic(err)
}
peerAddr := addr.Encapsulate(ipfsAddr)
n.addresses[i] = peerAddr
}
n.RPC = initRPC(n.Host, cfg.RendezvousString)
go func() {
for {
peerIDs := n.Host.Peerstore().PeersWithAddrs()
if len(peerIDs) > 0 {
connected := 0
for _, id := range peerIDs {
peer := n.Host.Peerstore().PeerInfo(id)
err := n.Connect(n.ctx, peer)
if err == nil {
log.Println(n.Host.ID(), " Connected to ", peer.ID)
connected++
}
}
if connected > 0 {
log.Println("Advertising")
n.Router.Advertise(n.ctx, n.cfg.RendezvousString)
close(n.bootstrapDone)
break
}
}
time.Sleep(time.Second * 3)
}
}()
return n, nil
}
//InitMDNS initializes the MDNS discovery in the network
func (net *Network) InitMDNS() error {
ser, err := discovery.NewMdnsService(net.ctx, net.Host, time.Hour, net.cfg.RendezvousString)
if err != nil {
return err
}
ser.RegisterNotifee(net)
return nil
}
//HandlePeerFound is the Notifee interface for mdns discovery.
//It can be also called to update the peer info found via other ways
func (net *Network) HandlePeerFound(peer pstore.PeerInfo) {
net.Host.Peerstore().AddAddrs(peer.ID, peer.Addrs, pstore.ProviderAddrTTL)
log.Println("found", peer.ID)
}
//Addrs returns list of multi addresses we listen on
func (net *Network) Addrs() []multiaddr.Multiaddr {
return net.addresses
}
//Context returns parent context of Network
func (net *Network) Context() context.Context {
return net.ctx
}
//Connect ensures a connection to remote peer
func (net *Network) Connect(ctx context.Context, peer pstore.PeerInfo) error {
return net.Host.Connect(ctx, peer)
}
//Close shut down the network and services
func (net *Network) Close() error {
return net.Host.Close()
}
//WaitForBootstrap blocks untill local peer is bootstrapped
func (net *Network) WaitForBootstrap() {
select {
case <-net.bootstrapDone:
return
}
}