-
Notifications
You must be signed in to change notification settings - Fork 0
/
peers.go
713 lines (630 loc) · 17.5 KB
/
peers.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package peers
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"time"
bsclient "github.com/ipfs/boxo/bitswap/client"
bsnet "github.com/ipfs/boxo/bitswap/network"
bsserver "github.com/ipfs/boxo/bitswap/server"
"github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
chunker "github.com/ipfs/boxo/chunker"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/ipld/merkledag"
unixfile "github.com/ipfs/boxo/ipld/unixfs/file"
"github.com/ipfs/boxo/ipld/unixfs/importer/balanced"
uih "github.com/ipfs/boxo/ipld/unixfs/importer/helpers" // UnixFS 导入
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
dsync "github.com/ipfs/go-datastore/sync"
pebbleDs "github.com/ipfs/go-ds-pebble"
ipld "github.com/ipfs/go-ipld-format"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"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/core/pnet"
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
dutil "github.com/libp2p/go-libp2p/p2p/discovery/util"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multicodec"
)
// NewPeerNode(nick, room, mdns, nodepriv, extAddr, datadir string, event HandleEvents, bootstrappeer []string) (Pm *PeerManager, h host.Host, err error)
type Peer struct {
H host.Host
PM *PeerManager
bitclient *bsclient.Client
bswap *bsserver.Server
ds datastore.Batching
bs blockstore.Blockstore
dsrv ipld.DAGService
dht *dht.IpfsDHT
swapstore *pebbleDs.Datastore
cidstore *pebbleDs.Datastore
privnet pnet.PSK
port string
Nick string
room, mdns string //mdns
nodepriv string //identity
extAddr []string //pub address
datadir string //dir for database
event HandleEvents
bootstrappeer []string //pub bootstrap
relaypeer []string
privatenet string
isBootstrap bool
isRelay bool
ispeermanage bool
enableBitSwapServer bool
enableBitSwapClient bool
storeIdentity bool
useprivnet bool
}
const IDpriv = "peerid.priv"
const IDpub = "peerid.pub"
const IDpnet = "psk.key"
func NewPeer(nick, dir string, opts ...Option) (*Peer, error) {
var p Peer
p.Nick = nick
p.datadir = dir
if err := p.Apply(opts...); err != nil {
return nil, err
}
p.start()
return &p, nil
}
func (p *Peer) start() {
var extMultiAddr []multiaddr.Multiaddr
if len(p.extAddr) != 0 {
for _, v := range p.extAddr {
addr, err := multiaddr.NewMultiaddr(v)
if err != nil {
fmt.Printf("Error creating multiaddress: %v\n", err)
// extMultiAddr = nil
continue
}
extMultiAddr = append(extMultiAddr, addr)
}
}
if p.storeIdentity {
if FileExist(p.datadir + "/" + IDpriv) {
data, err := os.ReadFile(p.datadir + "/" + IDpriv)
if err != nil {
panic(err)
}
p.nodepriv = string(data)
} else {
key := GenprivKey()
p.nodepriv = key
os.WriteFile(p.datadir+"/"+IDpriv, []byte(key), os.ModePerm)
}
}
addressFactory := func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
if extMultiAddr != nil {
addrs = append(addrs, extMultiAddr...)
}
return addrs
}
listen := libp2p.NoListenAddrs
if p.port != "" {
listen = libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/" + p.port)
}
var allconfig []libp2p.Option
allconfig = append(allconfig, listen)
allconfig = append(allconfig, libp2p.Identity(readconfigpriv(p.nodepriv)))
allconfig = append(allconfig, libp2p.Ping(true))
allconfig = append(allconfig, libp2p.EnableRelay())
allconfig = append(allconfig, libp2p.EnableAutoNATv2())
allconfig = append(allconfig, libp2p.EnableHolePunching())
allconfig = append(allconfig, libp2p.AddrsFactory(addressFactory))
if p.useprivnet {
pkey, err := hex.DecodeString(p.privatenet)
if err != nil {
panic(err)
}
allconfig = append(allconfig, libp2p.PrivateNetwork(pkey))
}
// allconfig = append(allconfig, libp2p.PrivateNetwork(p.privnet))
Host, err := libp2p.New(
allconfig...,
)
if err != nil {
panic(err)
}
if p.storeIdentity {
if !FileExist(p.datadir + "/" + IDpub) {
os.WriteFile(p.datadir+"/"+IDpub, []byte(Host.ID().String()), os.ModePerm)
}
}
ctx := context.Background()
var kademliaDHT *dht.IpfsDHT
kademliaDHT = nil
if len(p.bootstrappeer) != 0 && !p.isBootstrap {
for _, s := range p.bootstrappeer {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
panic(err)
}
DefaultBootstrapPeers = append(DefaultBootstrapPeers, ma)
}
bootstrapPeers := make([]peer.AddrInfo, len(p.bootstrappeer))
kademliaDHT, err = dht.New(ctx, Host, dht.BootstrapPeers(bootstrapPeers...))
if err != nil {
panic(err)
}
}
if p.isBootstrap {
kademliaDHT, err = dht.New(ctx, Host, dht.Mode(dht.ModeAutoServer))
if err != nil {
panic(err)
}
}
p.dht = kademliaDHT
if p.isRelay {
_, err = relay.New(Host, relay.WithInfiniteLimits())
// re.
if err != nil {
fmt.Printf("Failed to instantiate the relay: %v\n", err)
panic(err)
}
}
if p.enableBitSwapServer {
//bitswap server
p.startBitswapDataServer(ctx, Host, p.datadir)
}
if p.enableBitSwapClient {
p.bitclient = p.startBitswapClient(ctx, Host)
}
dht_word := "find-dht"
var routingDiscovery *drouting.RoutingDiscovery
if kademliaDHT != nil {
fmt.Println("Bootstrapping the DHT")
if err = kademliaDHT.Bootstrap(ctx); err != nil {
panic(err)
}
errchan := kademliaDHT.RefreshRoutingTable()
<-errchan
// Wait a bit to let bootstrapping finish (really bootstrap should block until it's ready, but that isn't the case yet.)
time.Sleep(1 * time.Second)
fmt.Println("Announcing ourselves...")
routingDiscovery = drouting.NewRoutingDiscovery(kademliaDHT)
dutil.Advertise(ctx, routingDiscovery, dht_word)
}
//using relay
if len(p.relaypeer) > 0 {
for _, v := range p.relaypeer {
//relayv2 Reserve
_, err = client.Reserve(context.Background(), Host, *getrelayinfo(v))
if err != nil {
fmt.Println(" client.Reserve ", err)
}
}
}
if len(p.bootstrappeer) > 0 {
go func() {
for {
fmt.Println("Searching for other peers...")
peerChan, err := routingDiscovery.FindPeers(ctx, dht_word)
if err != nil {
panic(err)
}
connectdhtpeers(Host, peerChan, p.relaypeer)
time.Sleep(time.Minute * 10)
}
}()
}
// mdns
// setup local mDNS discovery
if p.mdns != "" {
if err := setupDiscovery(Host, p.mdns); err != nil {
panic(err)
}
}
//pubsub
// create a new PubSub service using the GossipSub router
ps, err := pubsub.NewGossipSub(ctx, Host)
if err != nil {
panic(err)
}
// join the chat room
if p.ispeermanage {
cr, err := joinChatRoom(ctx, ps, Host.ID(), p.Nick, p.room)
if err != nil {
panic(err)
}
p.PM = newPeerManager(cr, p.event)
if err = p.PM.run(); err != nil {
return
}
}
//conmanager
connectmanager(Host)
p.H = Host
}
func (p *Peer) startBitswapClient(ctx context.Context, h host.Host) *bsclient.Client {
if p.dht != nil {
n := bsnet.NewFromIpfsHost(h, p.dht)
bswap := bsclient.New(ctx, n, blockstore.NewBlockstore(datastore.NewNullDatastore()))
n.Start(bswap)
// defer bswap.Close()
return bswap
} else {
n := bsnet.NewFromIpfsHost(h, routinghelpers.Null{})
bswap := bsclient.New(ctx, n, blockstore.NewBlockstore(datastore.NewNullDatastore()))
n.Start(bswap)
// defer bswap.Close()
return bswap
}
}
// Local Network peer finding
func SetPort(port string) Option {
return func(cfg *Peer) error {
cfg.port = port
return nil
}
}
// Local Network peer finding
func EnableBitSwap(server, client bool) Option {
return func(cfg *Peer) error {
cfg.enableBitSwapServer = server
cfg.enableBitSwapClient = client
return nil
}
}
// use priv network
func EnablePrivNet(en bool, pk string) Option {
return func(cfg *Peer) error {
cfg.useprivnet = en
cfg.privatenet = pk
return nil
}
}
// Local Network peer finding
func EnablePeerManage(event HandleEvents) Option {
return func(cfg *Peer) error {
cfg.event = event
cfg.ispeermanage = true
return nil
}
}
// Local Network peer finding
func EnableMDNS(room, mdns string) Option {
return func(cfg *Peer) error {
if cfg.mdns != "" || cfg.room != "" {
return fmt.Errorf("cannot specify multiple mdns")
}
cfg.mdns = mdns
cfg.room = room
return nil
}
}
// Pubaddr
func Pubaddr(pubaddr ...string) Option {
return func(cfg *Peer) error {
cfg.extAddr = append(cfg.extAddr, pubaddr...)
return nil
}
}
// DHT bootstrap
func EnableDHT(isboot bool, bootstrap ...string) Option {
return func(cfg *Peer) error {
cfg.isBootstrap = isboot
if !isboot {
cfg.bootstrappeer = append(cfg.bootstrappeer, bootstrap...)
}
return nil
}
}
// DHT bootstrap
func EnableRelay(isrelay bool) Option {
return func(cfg *Peer) error {
cfg.isRelay = isrelay
return nil
}
}
// pub Relay address
func UseRelay(relay ...string) Option {
return func(cfg *Peer) error {
cfg.relaypeer = append(cfg.relaypeer, relay...)
return nil
}
}
// Identity configures libp2p to use the given private key to identify itself.
func IdentityCustom(sk string) Option {
return func(cfg *Peer) error {
if cfg.nodepriv != "" {
return fmt.Errorf("cannot specify multiple identities")
}
cfg.nodepriv = sk
return nil
}
}
// Identity configures libp2p to use the given private key to identify itself.
func IdentityStore() Option {
return func(cfg *Peer) error {
if cfg.nodepriv != "" {
return fmt.Errorf("cannot specify multiple identities")
}
cfg.storeIdentity = true
return nil
}
}
type Option func(p *Peer) error
// Apply applies the given options to the config, returning the first error
// encountered (if any).
func (p *Peer) Apply(opts ...Option) error {
for _, opt := range opts {
if opt == nil {
continue
}
if err := opt(p); err != nil {
return err
}
}
return nil
}
// 启动服务器的函数
func (p *Peer) startBitswapDataServer(ctx context.Context, h host.Host, datadir string) error {
// datastore.New
pebbledb, err := createDB(datadir + "/" + "bistswapdb")
if err != nil {
return err
}
p.swapstore = pebbledb
p.cidstore, err = createDB(datadir + "/" + "ciddb")
if err != nil {
return err
}
p.ds = dsync.MutexWrap(pebbledb) // 初始化datastore
p.bs = blockstore.NewBlockstore(p.ds) // 初始化blockstore
p.bs = blockstore.NewIdStore(p.bs) // 增加对identity multihash支持
bsrv := blockservice.New(p.bs, offline.Exchange(p.bs))
p.dsrv = merkledag.NewDAGService(bsrv) // 将merkledag.DAGService存储为ipld.DAGService
// 启动Bitswap协议
if p.dht != nil {
n := bsnet.NewFromIpfsHost(h, p.dht)
p.bswap = bsserver.New(ctx, n, p.bs)
n.Start(p.bswap) // 启动服务器
} else {
n := bsnet.NewFromIpfsHost(h, routinghelpers.Null{})
p.bswap = bsserver.New(ctx, n, p.bs)
n.Start(p.bswap) // 启动服务器
}
fmt.Println("Bitswap服务器已启动")
if p.dht != nil {
res := p.Restorecid()
for _, v := range res {
cidstring, _ := strings.CutPrefix(v, "/")
cID, err := cid.Decode(cidstring)
fmt.Println(cidstring)
if err != nil {
panic(err)
}
p.dht.Provide(context.Background(), cID, true)
}
}
return nil
}
// 动态添加文件到服务器的方法
func (p *Peer) AddFileToServer(fileReader io.Reader, fname string) (cid.Cid, error) {
// fileReader := bytes.NewReader(fileBytes) // 读取新的文件
ufsImportParams := uih.DagBuilderParams{
Maxlinks: uih.DefaultLinksPerBlock,
RawLeaves: true,
CidBuilder: cid.V1Builder{
Codec: uint64(multicodec.DagPb),
MhType: uint64(multicodec.Sha2_256),
MhLength: -1,
},
Dagserv: p.dsrv, // 使用ipld.DAGService接口
NoCopy: false,
}
ufsBuilder, err := ufsImportParams.New(chunker.NewSizeSplitter(fileReader, chunker.DefaultBlockSize)) // 切分文件
if err != nil {
return cid.Undef, err
}
nd, err := balanced.Layout(ufsBuilder) // 使用平衡布局生成DAG图
if err != nil {
return cid.Undef, err
}
fileCID := nd.Cid() // 获取文件的CID
p.Storecid(fileCID.String(), fname)
p.swapstore.Sync(context.Background(), datastore.NewKey("")) //flush data
if p.dht != nil {
p.dht.Provide(context.Background(), fileCID, true)
}
return fileCID, nil
}
// 动态添加文件夹到服务器的方法
func (p *Peer) Folder(fileReader io.Reader, fname string) {
}
func (p *Peer) RemoveFile(id string) error {
cidstring, _ := strings.CutPrefix(id, "/")
cID, _ := cid.Decode(cidstring)
// 从 DAGService 获取块
node, err := p.dsrv.Get(context.Background(), cID)
if err != nil {
fmt.Println("not found", cidstring)
return errors.New("not found block" + cidstring)
}
// 获取子块的 CIDs
links := node.Links()
i := 0
for _, link := range links {
childCID := link.Cid
// 递归删除子块
if err := p.RemoveFile(childCID.String()); err != nil {
fmt.Println("child err", childCID)
return err
}
i++
// fmt.Println("remove child success", childCID)
}
// p.dsrv.Remove(context.Background(), cID)
// has, err := p.bs.Has(context.Background(), cID)
// fmt.Println("found root", cID, has, err)
// 删除当前块
if err := p.bs.DeleteBlock(context.Background(), cID); err != nil {
return fmt.Errorf("failed to delete block %s: %w", cID.String(), err)
}
fmt.Printf("cid %s removed with %d links", cID, i)
p.removecid(cidstring)
return nil
}
func (p *Peer) Storecid(fid string, filename string) {
p.cidstore.Put(context.TODO(), datastore.NewKey(fid), []byte(filename))
p.cidstore.Sync(context.Background(), datastore.NewKey(""))
}
func (p *Peer) removecid(cid string) {
p.cidstore.Delete(context.TODO(), datastore.NewKey(cid))
p.cidstore.Sync(context.Background(), datastore.NewKey(""))
}
func (p *Peer) Restorecid() (res []string) {
ctx := context.Background()
q := query.Query{
Prefix: "",
Orders: []query.Order{query.OrderByKey{}}, // 按键排序
}
results, err := p.cidstore.Query(ctx, q)
if err != nil {
log.Fatalf("query error: %v", err)
}
defer results.Close()
for result := range results.Next() {
if result.Error != nil {
log.Printf("error: %v", result.Error)
continue
}
fmt.Printf("Key: %s, Value: %s\n", result.Entry.Key, result.Entry.Value)
res = append(res, result.Entry.Key)
}
return res
}
func (p *Peer) DownloadfileViaSwap(ctx context.Context, c cid.Cid, tofile string) error {
if p.dht != nil {
go func() {
time.Sleep(time.Second * 2)
peers, err := p.dht.FindProviders(ctx, c)
if err != nil {
fmt.Println(err)
return
}
cpeers := p.H.Network().Peers()
connections := 0
needrelaypeers := []peer.AddrInfo{}
for _, peer1 := range peers {
find := false
for _, peer2 := range cpeers {
if p.H.Network().Connectedness(peer2) == network.Connected {
if peer2 == peer1.ID {
connections++
find = true
break
}
}
}
if !find {
needrelaypeers = append(needrelaypeers, peer1)
}
}
if connections != 0 {
fmt.Printf("have %d peers ,stop connect more\n", connections)
} else {
for _, peer1 := range needrelaypeers {
err := p.H.Connect(context.Background(), peer1)
if err != nil {
fmt.Println("connect err:", err)
unreachable2relayinfo := buildrelayinfo(peer1.ID, p.relaypeer[0])
if err := p.H.Connect(context.Background(), *unreachable2relayinfo); err != nil {
log.Printf("Unexpected error here. Failed to connect unreachable1 and unreachable2: %v", err)
continue
} else {
fmt.Println("success connect via relay")
}
}
}
}
}()
}
dserv := merkledag.NewReadOnlyDagService(merkledag.NewSession(ctx, merkledag.NewDAGService(blockservice.New(blockstore.NewBlockstore(datastore.NewNullDatastore()), p.bitclient))))
nd, err := dserv.Get(ctx, c)
if err != nil {
return err
}
unixFSNode, err := unixfile.NewUnixfsFile(ctx, dserv, nd)
if err != nil {
return err
}
// 创建新文件(目标文件)
dstFile, err := os.Create(tofile)
if err != nil {
fmt.Println("Error creating destination file:", err)
return err
}
defer dstFile.Close() // 确保在函数返回时关闭目标文件
if f, ok := unixFSNode.(files.File); ok {
fileSize, err := f.Size()
if err != nil {
return err
}
// 包装一个TeeReader来监控读取进度
var totalRead int64
progressReader := io.TeeReader(f, &progressWriter{dstFile, &totalRead, fileSize})
if _, err := io.Copy(dstFile, progressReader); err != nil {
return err
}
}
return nil
}
func (p *Peer) ConnectPeer(id string) (*peer.AddrInfo, error) {
addr, err := multiaddr.NewMultiaddr(id)
if err != nil {
return nil, err
}
peerinfo, err := peer.AddrInfoFromP2pAddr(addr)
if err != nil {
return nil, err
}
return peerinfo, p.H.Connect(context.TODO(), *peerinfo)
}
func FileExist(path string) bool {
_, err := os.Lstat(path)
return !os.IsNotExist(err)
}
func Genpsk() pnet.PSK {
raw, err := GenerateRandomString()
if err != nil {
log.Fatal(err)
}
psk := pnet.PSK(raw)
if err != nil {
log.Fatal(err)
}
return psk
}
// GenerateRandomString 生成一个32字节的随机字符串
func GenerateRandomString() ([]byte, error) {
// 创建一个32字节的切片
b := make([]byte, 32)
// 使用随机源填充字节切片
_, err := rand.Read(b)
if err != nil {
return []byte{}, err
}
// 将字节切片转换为十六进制字符串
return b, nil
}