-
Notifications
You must be signed in to change notification settings - Fork 5
/
libp2p.go
218 lines (187 loc) · 5.26 KB
/
libp2p.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
package p2p
import (
"context"
"fmt"
"sync"
"github.com/diandianl/p2p-proxy/config"
"github.com/diandianl/p2p-proxy/log"
"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
// "github.com/kingwel-xie/xcli"
"github.com/libp2p/go-libp2p"
autonat "github.com/libp2p/go-libp2p-autonat-svc"
"github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/routing"
discovery2 "github.com/libp2p/go-libp2p-discovery"
dht "github.com/libp2p/go-libp2p-kad-dht"
dhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
secio "github.com/libp2p/go-libp2p-secio"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
maddr "github.com/multiformats/go-multiaddr"
mh "github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
)
func NewHostAndDiscovererAndBootstrap(ctx context.Context, cfg *config.Config) (h host.Host, dis discovery.Discovery, err error) {
logger := log.NewSubLogger("p2p")
hostOpts, err := hostOptions(ctx, cfg)
if err != nil {
return
}
dhtOpts, err := dhtOptions(cfg)
if err != nil {
return
}
var bootPeers []maddr.Multiaddr // dht.DefaultBootstrapPeers
if len(cfg.P2P.BootstrapPeers) > 0 {
bootPeers, err = convertToMAddr(cfg.P2P.BootstrapPeers)
if err != nil {
return
}
} else if len(config.Default.P2P.BootstrapPeers) > 0 {
bootPeers, err = convertToMAddr(config.Default.P2P.BootstrapPeers)
if err != nil {
return
}
}
dhtDs := dssync.MutexWrap(ds.NewMapDatastore())
dhtOpts = append(dhtOpts, func(options *dhtopts.Options) error {
options.Datastore = dhtDs
return nil
})
if cfg.P2P.Identity.ObservedAddrActivationThresh > 0 {
identify.ActivationThresh = cfg.P2P.Identity.ObservedAddrActivationThresh
logger.Debugf("Override LibP2P observed address activation thresh with %d", identify.ActivationThresh)
}
dhtCreater := newDHTConstructor(ctx, dhtOpts)
if cfg.P2P.EnableAutoRelay {
logger.Debugf("Enable auto relay")
hostOpts = append(hostOpts,
libp2p.Routing(func(h host.Host) (routing routing.PeerRouting, err error) {
return dhtCreater(h)
}),
libp2p.EnableAutoRelay(),
)
}
h, err = libp2p.New(ctx, hostOpts...)
if err != nil {
return nil, nil, err
}
if len(h.Addrs()) > 0 {
logger.Infof("P2P [/ipfs/%s] working addrs: %s", h.ID().Pretty(), h.Addrs())
}
if cfg.P2P.AutoNATService {
logger.Debugf("Enable auto NAT service")
_, err = autonat.NewAutoNATService(ctx, h,
libp2p.Security(secio.ID, secio.New),
libp2p.DefaultTransports,
)
if err != nil {
h.Close()
return nil, nil, err
}
}
router, err := dhtCreater(h)
if err != nil {
h.Close()
return nil, nil, err
}
err = router.Bootstrap(ctx)
if err != nil {
h.Close()
return nil, nil, err
}
var connected []peer.AddrInfo
var wg sync.WaitGroup
for _, peerAddr := range bootPeers {
peerinfo, _ := peer.AddrInfoFromP2pAddr(peerAddr)
wg.Add(1)
go func(pi peer.AddrInfo) {
defer wg.Done()
if err := h.Connect(ctx, pi); err != nil {
logger.Warn(err)
} else {
connected = append(connected, pi)
logger.Debug("Connection established with bootstrap node:", pi)
_, err = permanentAddAddrToPeerstore(h, peerAddr)
if err != nil {
logger.Warnf("Permanent add addr [%s] to peerstore: ", peerAddr, err)
}
}
}(*peerinfo)
}
wg.Wait()
dis = discovery2.NewRoutingDiscovery(router)
return h, dis, nil
}
func extCmds() cli.Commands {
return cli.Commands{
{
Name: "a2cid",
Category: "p2p",
Usage: "a2cid <str>",
Description: "calc str cid",
Action: func(c *cli.Context) error {
cid, err := a2cid(c.Args().First())
if err != nil {
return err
}
fmt.Fprintln(c.App.Writer, cid.Hash().B58String())
return nil
},
},
}
}
func a2cid(str string) (cid.Cid, error) {
h, err := mh.Sum([]byte(str), mh.SHA2_256, -1)
if err != nil {
return cid.Undef, err
}
return cid.NewCidV1(cid.Raw, h), nil
}
func permanentAddAddrToPeerstore(h host.Host, ma maddr.Multiaddr) (peer.ID, error) {
pid, err := ma.ValueForProtocol(maddr.P_IPFS)
if err != nil {
return "", err
}
peerid, err := peer.Decode(pid)
if err != nil {
return "", err
}
// Decapsulate the /ipfs/<peerID> part from the target
// /ip4/<a.b.c.d>/ipfs/<peer> becomes /ip4/<a.b.c.d>
targetPeerAddr, _ := maddr.NewMultiaddr(
fmt.Sprintf("/ipfs/%s", peer.Encode(peerid)))
targetAddr := ma.Decapsulate(targetPeerAddr)
// We have a peer ID and a targetAddr so we add
// it to the peerstore so LibP2P knows how to contact it
h.Peerstore().AddAddr(peerid, targetAddr, peerstore.PermanentAddrTTL)
return peerid, nil
}
func newDHTConstructor(ctx context.Context, opts []dhtopts.Option) func(host.Host) (routing.Routing, error) {
var (
once sync.Once
router routing.Routing
err error
)
return func(h host.Host) (routing.Routing, error) {
once.Do(func() {
router, err = dht.New(ctx, h, opts...)
})
return router, err
}
}
func convertToMAddr(addrs []string) ([]maddr.Multiaddr, error) {
maddrs := make([]maddr.Multiaddr, 0, len(addrs))
for _, addr := range addrs {
ma, err := maddr.NewMultiaddr(addr)
if err != nil {
return nil, err
}
maddrs = append(maddrs, ma)
}
return maddrs, nil
}