-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
436 lines (395 loc) · 11.3 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
madns "github.com/multiformats/go-multiaddr-dns"
vole "github.com/ipfs-shipyard/vole/lib"
"github.com/urfave/cli/v2"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multibase"
)
func init() {
// Lets us discover our own public address with a single observation
identify.ActivationThresh = 1
}
func main() {
app := &cli.App{
Name: "vole",
Usage: "a collection of tools for digging around IPFS nodes",
Authors: []*cli.Author{
{
Name: "Adin Schmahmann",
Email: "adin.schmahmann@gmail.com",
},
},
Commands: []*cli.Command{
{
Name: "bitswap",
Usage: "tools for working with bitswap",
Subcommands: []*cli.Command{
bitswapGetCmd,
bitswapCheckCmd,
},
},
{
Name: "dht",
Usage: "tools for working with libp2p kademlia DHTs",
Subcommands: []*cli.Command{
{
Name: "put",
ArgsUsage: "<multibase-bytes-key> <multibase-bytes-value> <multiaddr>",
Usage: "put a record to a DHT node",
Description: "creates a libp2p peer and sends a DHT put request to the target",
Action: func(c *cli.Context) error {
if c.NArg() != 3 {
return fmt.Errorf("invalid number of arguments")
}
keyStr := c.Args().Get(0)
valStr := c.Args().Get(1)
maStr := c.Args().Get(2)
protoID := c.String("protocolID")
_, keyBytes, err := multibase.Decode(keyStr)
if err != nil {
return err
}
_, valBytes, err := multibase.Decode(valStr)
if err != nil {
return err
}
ma, err := multiaddr.NewMultiaddr(maStr)
if err != nil {
return err
}
return vole.DhtPut(c.Context, keyBytes, valBytes, protocol.ID(protoID), ma)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "protocolID",
Usage: "the protocol ID",
DefaultText: "/ipfs/kad/1.0.0",
Value: "/ipfs/kad/1.0.0",
},
},
},
{
Name: "get",
ArgsUsage: "<multibase-bytes-key> <multiaddr>",
Usage: "get a record from a DHT node",
Description: "creates a libp2p peer and sends a DHT get request to the target",
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
return fmt.Errorf("invalid number of arguments")
}
keyStr := c.Args().Get(0)
maStr := c.Args().Get(1)
protoID := c.String("protocolID")
base := c.String("base")
_, keyBytes, err := multibase.Decode(keyStr)
if err != nil {
return err
}
ma, err := multiaddr.NewMultiaddr(maStr)
if err != nil {
return err
}
enc, err := multibase.EncoderByName(base)
if err != nil {
return err
}
rec, err := vole.DhtGet(c.Context, keyBytes, protocol.ID(protoID), ma)
if err != nil {
return err
}
fmt.Println(enc.Encode(rec.GetValue()))
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "protocolID",
Usage: "the protocol ID",
DefaultText: "/ipfs/kad/1.0.0",
Value: "/ipfs/kad/1.0.0",
},
&cli.StringFlag{
Name: "base",
Aliases: []string{"b"},
Usage: "multibase to encode the result in (e.g. b or base32 for base32 encoding)",
DefaultText: "base32",
Value: "base32",
},
},
},
{
Name: "getprovs",
ArgsUsage: "<cid> <multiaddr>",
Usage: "gets provider records from a DHT node",
Description: "creates a libp2p peer and sends a DHT get providers request to the target",
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
return fmt.Errorf("invalid number of arguments")
}
cidStr := c.Args().Get(0)
maStr := c.Args().Get(1)
protoID := c.String("protocolID")
showAddrs := c.Bool("show-addrs")
dataCID, err := cid.Decode(cidStr)
if err != nil {
return err
}
ma, err := multiaddr.NewMultiaddr(maStr)
if err != nil {
return err
}
provs, err := vole.DhtGetProvs(c.Context, dataCID.Hash(), protocol.ID(protoID), ma)
if err != nil {
return err
}
return printPeerIDs(provs, showAddrs)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "protocolID",
Usage: "the protocol ID",
DefaultText: "/ipfs/kad/1.0.0",
Value: "/ipfs/kad/1.0.0",
},
&cli.BoolFlag{
Name: "show-addrs",
Aliases: []string{"a"},
Usage: "show the peer address or just the IDs",
DefaultText: "false",
Value: false,
},
},
},
{
Name: "gcp",
ArgsUsage: "<multibase-bytes-key> <multiaddr>",
Usage: "gets the closest peers to the target from a DHT node",
Description: "creates a libp2p peer and sends a DHT get closest peers request to the target - prints the peers and their addresses",
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
return fmt.Errorf("invalid number of arguments")
}
keyStr := c.Args().Get(0)
maStr := c.Args().Get(1)
protoID := c.String("protocolID")
showAddrs := c.Bool("show-addrs")
_, keyBytes, err := multibase.Decode(keyStr)
if err != nil {
return err
}
ma, err := multiaddr.NewMultiaddr(maStr)
if err != nil {
return err
}
ais, err := vole.DhtGetClosestPeers(c.Context, keyBytes, protocol.ID(protoID), ma)
if err != nil {
return err
}
return printPeerIDs(ais, showAddrs)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "protocolID",
Usage: "the protocol ID",
DefaultText: "/ipfs/kad/1.0.0",
Value: "/ipfs/kad/1.0.0",
},
&cli.BoolFlag{
Name: "show-addrs",
Aliases: []string{"a"},
Usage: "show the peer address or just the IDs",
DefaultText: "false",
Value: false,
},
},
},
},
},
{
Name: "dnsaddr",
ArgsUsage: "<domain>",
Usage: "get the multiaddrs from a domain name with a dnsaddr",
Description: "creates a DNSAddr lookup and returns all the multiaddrs",
Action: func(c *cli.Context) error {
if c.NArg() != 1 {
return fmt.Errorf("invalid number of arguments")
}
maStr := fmt.Sprintf("/dnsaddr/%s", c.Args().Get(0))
addr, err := multiaddr.NewMultiaddr(maStr)
if err != nil {
return err
}
addrs, err := madns.DefaultResolver.Resolve(c.Context, addr)
if err != nil {
return err
}
for _, a := range addrs {
fmt.Println(a)
}
return nil
},
},
{
Name: "libp2p",
Usage: "tools for working with libp2p",
Subcommands: []*cli.Command{
{
Name: "identify",
ArgsUsage: "<multiaddr>",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "allow-unknown-peer",
Usage: `if the multiaddr does not end with /p2p/PeerID allow trying to determine the peerID at the destination.
Note: connecting to a peer without knowing its peerID is generally insecure, however it is situationally useful.
Note: may not work with some transports such as p2p-circuit (not applicable) and webtransport (requires certificate hashes).
`,
DefaultText: "false",
Value: false,
},
},
Usage: "learn about the peer with the given multiaddr",
Description: "connects to the target address and runs identify against the peer",
Action: func(c *cli.Context) error {
if c.NArg() != 1 {
return fmt.Errorf("invalid number of arguments")
}
allowUnknownPeer := c.Bool("allow-unknown-peer")
resp, err := vole.IdentifyRequest(c.Context, c.Args().First(), allowUnknownPeer)
if err != nil {
return err
}
fmt.Printf("PeerID: %q\n", resp.PeerId)
fmt.Printf("Protocol version: %q\n", resp.ProtocolVersion)
fmt.Printf("Agent version: %q\n", resp.AgentVersion)
fmt.Println("Listen addresses:")
for _, a := range resp.Addresses {
fmt.Printf("\t- %q\n", a)
}
fmt.Println("Protocols:")
for _, p := range resp.Protocols {
fmt.Printf("\t- %q\n", p)
}
return nil
},
}, {
Name: "ping",
ArgsUsage: "<multiaddr>",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force-relay",
Usage: `Ping the peer over a relay instead of a direct connection`,
DefaultText: "false",
Value: false,
},
},
Usage: "ping a peer",
Description: "connects to the target address and pings",
Action: func(c *cli.Context) error {
if c.NArg() != 1 {
return fmt.Errorf("invalid number of arguments")
}
maStr := c.Args().First()
ai, err := peer.AddrInfoFromString(maStr)
if err != nil {
return err
}
return vole.Ping(c.Context, c.Bool("force-relay"), ai)
},
},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
func printPeerIDs(ais []*peer.AddrInfo, showAddrs bool) error {
for _, a := range ais {
if showAddrs {
b, err := a.MarshalJSON()
if err != nil {
return err
}
var pretty bytes.Buffer
err = json.Indent(&pretty, b, "", "\t")
if err != nil {
return err
}
fmt.Println(pretty.String())
} else {
fmt.Println(a.ID)
}
}
return nil
}
var bitswapGetCmd = &cli.Command{
Name: "get",
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() < 2 {
return fmt.Errorf("must pass cid and multiaddr of peer to fetch from")
}
root, err := cid.Decode(cctx.Args().Get(0))
if err != nil {
return err
}
maddr, err := multiaddr.NewMultiaddr(cctx.Args().Get(1))
if err != nil {
return err
}
ai, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
return err
}
return vole.GetBitswapCID(root, ai)
},
}
var bitswapCheckCmd = &cli.Command{
Name: "check",
ArgsUsage: "<cid> <multiaddr>",
Usage: "check if a peer has a CID",
Description: "creates a libp2p peer and sends a bitswap request to the target - prints true if the peer reports it has the CID and false otherwise",
Action: func(c *cli.Context) error {
if c.NArg() != 2 {
return fmt.Errorf("invalid number of arguments")
}
cidStr := c.Args().Get(0)
maStr := c.Args().Get(1)
getBlock := c.Bool("get-block")
bsCid, err := cid.Decode(cidStr)
if err != nil {
return err
}
ma, err := multiaddr.NewMultiaddr(maStr)
if err != nil {
return err
}
output, err := vole.CheckBitswapCID(c.Context, bsCid, ma, getBlock)
if err != nil {
return err
}
jsOut, err := json.Marshal(output)
if err != nil {
return err
}
fmt.Printf("%s\n", jsOut)
return nil
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "get-block",
Usage: "get the block",
Value: true,
DefaultText: "true",
},
},
}