-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery_dht.go
105 lines (92 loc) · 2.4 KB
/
discovery_dht.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
package main
import (
"context"
"fmt"
"sync"
"time"
dht "github.com/libp2p/go-libp2p-kad-dht"
"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/routing"
"github.com/libp2p/go-libp2p/p2p/discovery/util"
"github.com/multiformats/go-multiaddr"
)
type discoveryDHT struct {
host host.Host
dht *dht.IpfsDHT
rendezvous string
}
func (n *discoveryDHT) run(address string) {
ticker := time.NewTicker(time.Second * 10)
defer ticker.Stop()
for range ticker.C {
ctx := context.Background()
rd := routing.NewRoutingDiscovery(n.dht)
util.Advertise(ctx, rd, n.rendezvous)
peerCh, err := rd.FindPeers(ctx, n.rendezvous)
if err != nil {
fmt.Println("DHT FindPeers failed:", err)
continue
}
for p := range peerCh {
if p.ID == n.host.ID() || len(p.Addrs) == 0 {
continue
}
switch n.host.Network().Connectedness(p.ID) {
case network.NotConnected:
if err := n.host.Connect(ctx, p); err != nil {
// fmt.Println("DHT Connection failed:", p.ID, ">>", err)
continue
}
fmt.Printf("An address is now joined to vpn-mesh by DHT: %s\n", p.ID)
default:
if err := discoveryWriter(ctx, n.host, address, p); err != nil {
// fmt.Println("DHT writer failed:", p.ID, ">>", err)
continue
}
}
}
}
}
func newDHT(ctx context.Context, h host.Host, rendezvous string) (*discoveryDHT, error) {
kadDHT, err := dht.New(ctx, h)
if err != nil {
return nil, err
}
maddr, err := multiaddr.NewMultiaddr(
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
)
if err != nil {
return nil, err
}
boots := append(dht.DefaultBootstrapPeers, maddr)
var wg sync.WaitGroup
for _, pa := range boots {
peerinfo, _ := peer.AddrInfoFromP2pAddr(pa)
wg.Add(1)
go func() {
defer wg.Done()
if err := h.Connect(ctx, *peerinfo); err != nil {
fmt.Printf("DHT Bootstrap Connection failed: %v\n", err)
}
}()
}
wg.Wait()
if err = kadDHT.Bootstrap(ctx); err != nil {
return nil, err
}
// cid, err := cid.NewPrefixV1(cid.Raw, mh.IDENTITY).Sum([]byte(rendezvous))
// if err != nil {
// return nil, err
// }
// if err := kadDHT.Provide(ctx, cid, true); err != nil {
// return nil, err
// }
ddht := &discoveryDHT{
host: h,
dht: kadDHT,
rendezvous: rendezvous,
}
return ddht, nil
}