-
Notifications
You must be signed in to change notification settings - Fork 3
/
debug.go
71 lines (59 loc) · 1.58 KB
/
debug.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
package main
import (
"bytes"
"context"
"flag"
"fmt"
"log"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-smart-record/protocol"
"github.com/multiformats/go-multiaddr"
)
func main() {
ctx := context.Background()
room := flag.String("room", "", "Select the chat room you want to connect to")
dest := flag.String("d", "", "Destination multiaddr string")
flag.Parse()
if *dest == "" || *room == "" {
fmt.Println("Specify a destination with -d and a -room to debug")
return
}
fmt.Println("[*] Starting hosts")
// Instantiating hosts
h2, err := libp2p.New(ctx)
if err != nil {
panic(err)
}
defer h2.Close()
smClient, _ := protocol.NewSmartRecordClient(ctx, h2)
// Turn the destination into a multiaddr.
maddr, err := multiaddr.NewMultiaddr(*dest)
if err != nil {
log.Fatalln(err)
}
// Extract the peer ID from the multiaddr.
info, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
log.Fatalln(err)
}
// Add the destination's peer multiaddress in the peerstore.
// This will be used during connection and stream creation by libp2p.
h2.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.PermanentAddrTTL)
// Get Record stored
fmt.Println("[*] Getting updated record from peer")
out, err := smClient.Get(ctx, *room, info.ID)
if err != nil {
panic(err)
}
fmt.Println("[*] Record:")
for k, v := range *out {
fmt.Println("Value for peer: ", k.String())
// Print result for convenience
var w bytes.Buffer
v.WritePretty(&w)
fmt.Println(w.String())
w.Reset()
}
}