-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.go
366 lines (315 loc) · 10.1 KB
/
connections.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
package peers
import (
"context"
"fmt"
"io"
"log"
"os"
"time"
"github.com/cockroachdb/pebble"
pebbleDs "github.com/ipfs/go-ds-pebble"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
"github.com/mostcute/peers/util"
"github.com/multiformats/go-multiaddr"
)
// DiscoveryServiceTag is used in our mDNS advertisements to discover other chat peers.
//var DiscoveryServiceTag = "objstorage-mdns"
// var ServerdisksheartbeatRoom = "data-servers && data-disks pubsub channel"
var DefaultBootstrapPeers []multiaddr.Multiaddr
var (
// ds datastore.Batching
// bs blockstore.Blockstore
// dsrv ipld.DAGService
// bswap *bsserver.Server
// bswapclient *bsclient.Client
// relaypeer string
)
// func NewPeerNode(nick, room, mdns, nodepriv, extAddr, datadir string, event HandleEvents, bootstrappeer []string) (Pm *PeerManager, h host.Host, err error) {
// ctx := context.Background()
// // create a new libp2p Host that listens on a random TCP port
// ///ip4/%s/tcp/%d
// var extMultiAddr multiaddr.Multiaddr
// if extAddr != "" {
// extMultiAddr, err = multiaddr.NewMultiaddr(extAddr)
// if err != nil {
// fmt.Printf("Error creating multiaddress: %v\n", err)
// extMultiAddr = nil
// }
// } else {
// extMultiAddr = nil
// }
// addressFactory := func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
// if extMultiAddr != nil {
// addrs = append(addrs, extMultiAddr)
// }
// return addrs
// }
// // new host
// Host, err := libp2p.New(
// libp2p.Identity(readconfigpriv(nodepriv)),
// libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/26666"),
// libp2p.Ping(true),
// libp2p.EnableRelay(),
// libp2p.EnableAutoNATv2(),
// libp2p.EnableHolePunching(),
// libp2p.AddrsFactory(addressFactory),
// )
// if err != nil {
// panic(err)
// }
// h = Host
// fmt.Println("listen addrs ", Host.Addrs())
// fmt.Println("mutiaddr: ", GetHostAddress(Host))
// //bitswap server
// startBitswapDataServer(ctx, Host, datadir)
// //bootstrap
// for _, s := range bootstrappeer {
// ma, err := multiaddr.NewMultiaddr(s)
// if err != nil {
// panic(err)
// }
// DefaultBootstrapPeers = append(DefaultBootstrapPeers, ma)
// }
// bootstrapPeers := make([]peer.AddrInfo, len(bootstrappeer))
// kademliaDHT, err := dht.New(ctx, h, dht.BootstrapPeers(bootstrapPeers...))
// if err != nil {
// panic(err)
// }
// bswapclient = startBitswapClient(ctx, Host, kademliaDHT)
// // Bootstrap the DHT. In the default configuration, this spawns a Background
// // thread that will refresh the peer table every five minutes.
// fmt.Println("Bootstrapping the DHT")
// if err = kademliaDHT.Bootstrap(ctx); err != nil {
// panic(err)
// }
// // Wait a bit to let bootstrapping finish (really bootstrap should block until it's ready, but that isn't the case yet.)
// time.Sleep(5 * time.Second)
// // We use a rendezvous point "meet me here" to announce our location.
// // This is like telling your friends to meet you at the Eiffel Tower.
// dht_word := mdns + "-dht"
// fmt.Println("Announcing ourselves...")
// routingDiscovery := drouting.NewRoutingDiscovery(kademliaDHT)
// dutil.Advertise(ctx, routingDiscovery, dht_word)
// //relayv2 Reserve
// _, err = client.Reserve(context.Background(), h, *getrelayinfo(bootstrappeer[0]))
// relaypeer = bootstrappeer[0]
// if err != nil {
// log.Println(" client.Reserve ", err)
// }
// // create a new PubSub service using the GossipSub router
// ps, err := pubsub.NewGossipSub(ctx, Host)
// if err != nil {
// panic(err)
// }
// go func() {
// for {
// fmt.Println("Searching for other peers...")
// peerChan, err := routingDiscovery.FindPeers(ctx, dht_word)
// if err != nil {
// panic(err)
// }
// connectdhtpeers(h, peerChan, bootstrappeer[0])
// time.Sleep(time.Minute * 10)
// }
// }()
// // setup local mDNS discovery
// if mdns == "" {
// mdns = "peer-mdns"
// }
// if err := setupDiscovery(Host, mdns); err != nil {
// return nil, nil, err
// }
// // join the chat room
// cr, err := joinChatRoom(ctx, ps, Host.ID(), nick, room)
// if err != nil {
// return nil, nil, err
// }
// pm := newPeerManager(cr, event)
// if err = pm.run(); err != nil {
// return nil, nil, err
// }
// connectmanager(Host)
// return pm, Host, nil
// //go DiskHeartBeat(PM.inputCh)
// }
func connectmanager(h host.Host) {
//Host.Network().SetConnHandler(test)
h.Network().Notify(&network.NotifyBundle{
//ConnectedF: func(n network.Network, conn network.Conn) {
// fmt.Println("connect ",conn.RemotePeer().String() ,"have con",len(n.ConnsToPeer(conn.RemotePeer())))
//},
//TODO: wait for PeerDisconnected Func
DisconnectedF: func(n network.Network, conn network.Conn) {
remotepeer := conn.RemotePeer()
remoteaddr := conn.RemoteMultiaddr()
remaincons := len(n.ConnsToPeer(remotepeer))
if remaincons == 0 {
// try to reconnect
go func() {
retry := 0
for {
time.Sleep(2 * time.Second)
err := h.Connect(context.Background(), h.Peerstore().PeerInfo(remotepeer))
if err != nil {
//log.Println("reconnect ", remoteaddr.String(), " failed ",err)
} else {
log.Println("reconnect ", remoteaddr.String(), " success")
break
}
retry++
if retry > 30 {
//log.Println("reconnect ", remoteaddr.String(), " cancel failed too many times ")
break
}
}
}()
}
},
})
}
func test(a network.Conn) {
//fmt.Println("some peer connected ",a.RemotePeer().String())
//cons := Host.Network().ConnsToPeer(a.RemotePeer())
//fmt.Println("cons ",cons)
}
// discoveryNotifee gets notified when we find a new peer via mDNS discovery
type discoveryNotifee struct {
h host.Host
}
// HandlePeerFound connects to peers discovered via mDNS. Once they're connected,
// the PubSub system will automatically start interacting with them if they also
// support PubSub.
func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
//fmt.Printf("discovered new peer %s %v\n", pi.ID.Pretty(), pi.Addrs)
err := n.h.Connect(context.Background(), pi)
if err != nil {
if util.ErrorContains(err.Error(), util.ErrDialSelf) {
} else {
fmt.Printf("error connecting to peer %s: ,addrs %s: %s\n", pi.ID.String(), pi.Addrs, err)
}
}
}
// setupDiscovery creates an mDNS discovery service and attaches it to the libp2p Host.
// This lets us automatically discover peers on the same LAN and connect to them.
func setupDiscovery(h host.Host, servicename string) error {
// setup mDNS discovery to find local peers
s := mdns.NewMdnsService(h, servicename, &discoveryNotifee{h: h})
return s.Start()
}
func readconfigpriv(priv string) crypto.PrivKey {
privbyte, _ := crypto.ConfigDecodeKey(priv)
priv2, _ := crypto.UnmarshalPrivateKey(privbyte)
return priv2
}
func GenprivKey() string {
// Set your own keypair
priv, _, _ := crypto.GenerateKeyPair(
crypto.Ed25519, // Select your key type. Ed25519 are nice short
-1, // Select key length when possible (i.e. RSA).
)
privbyte, _ := crypto.MarshalPrivateKey(priv)
privstr := crypto.ConfigEncodeKey(privbyte)
return privstr
}
func getrelayinfo(relaypeer string) *peer.AddrInfo {
relay1info, err := peer.AddrInfoFromString(relaypeer)
if err != nil {
log.Printf("Failed to create relay1addr: %v", err)
return nil
}
return relay1info
}
func buildrelayinfo(targetID peer.ID, relaypeer string) *peer.AddrInfo {
relayaddr, err := multiaddr.NewMultiaddr("/p2p/" + getrelayinfo(relaypeer).ID.String() + "/p2p-circuit/p2p/" + targetID.String())
if err != nil {
log.Println(err)
return nil
}
fmt.Println(relayaddr.String())
// unreachable2relayinfo := peer.AddrInfo{
// ID: peer.ID(unreachable2ID),
// Addrs: []multiaddr.Multiaddr{relayaddr},
// }
unreachable2relayinfo, err := peer.AddrInfoFromString(relayaddr.String())
if err != nil {
log.Println(err)
return nil
}
return unreachable2relayinfo
}
func connectdhtpeers(h host.Host, peerChan <-chan peer.AddrInfo, relaypeer []string) {
ctx := context.Background()
for peer := range peerChan {
if peer.ID == h.ID() {
continue
}
fmt.Println("Found peer from dht:", peer)
fmt.Println("Connecting to:", peer)
err := h.Connect(ctx, peer)
if err != nil {
fmt.Println("Connection failed:", err)
fmt.Println("try with realy connect")
for _, v := range relaypeer {
unreachable2relayinfo := buildrelayinfo(peer.ID, v)
if err := h.Connect(context.Background(), *unreachable2relayinfo); err != nil {
log.Printf("Unexpected error here. Failed to connect unreachable1 and unreachable2: %v", err)
continue
} else {
log.Println("Yep, that worked!")
break
}
}
} else {
}
fmt.Println("Connected to:", peer)
}
}
func GetHostAddress(h host.Host) string {
// Build host multiaddress
hostAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", h.ID().String()))
// Now we can build a full multiaddress to reach this host
// by encapsulating both addresses:
addr := h.Addrs()[0]
return addr.Encapsulate(hostAddr).String()
}
type progressWriter struct {
writer io.Writer
totalRead *int64
fileSize int64
}
func (pw *progressWriter) Write(p []byte) (int, error) {
// n, err := pw.writer.Write(p)
// if err != nil {
// return n, err
// }
n := len(p)
*pw.totalRead += int64(n)
progress := float64(*pw.totalRead) / float64(pw.fileSize) * 100
fmt.Printf("\rDownloading... %.2f%% complete", progress)
return n, nil
}
// createDB creates a PebbleDB DB in the specified directory.
func createDB(dbdir string) (*pebbleDs.Datastore, error) {
// ro := &pebble.IterOptions{}
// wo := &pebble.WriteOptions{Sync: false}
// syncwo := &pebble.WriteOptions{Sync: false}
cache := pebble.NewCache(0)
opts := &pebble.Options{
MaxManifestFileSize: 1024 * 32 * 1024,
MemTableSize: 1024 * 32 * 1024,
Cache: cache,
}
if err := os.MkdirAll(dbdir, 0755); err != nil {
return nil, err
}
ds, err := pebbleDs.NewDatastore(dbdir, opts)
if err != nil {
return nil, err
}
cache.Unref()
return ds, nil
}