-
Notifications
You must be signed in to change notification settings - Fork 9
/
probe.go
174 lines (154 loc) · 4.01 KB
/
probe.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
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
logging "github.com/ipfs/go-log"
"github.com/ipfs/interface-go-ipfs-core/path"
// This package is needed so that all the preloaded plugins are loaded automatically
// bsnet "github.com/ipfs/go-bitswap/network"
)
func helpcmd() {
fmt.Println(`[!] Commands available:
* addFile_<file_fir>
* pin_<path>
* get_<path>
* connect_<peer_multiaddr>
* graphsync_<peer_multiaddr>_<cid>
* exit`)
}
// Process commands received from prompt
func processInput(ctx context.Context, ipfs *IPFSNode, text string, done chan bool) error {
text = strings.ReplaceAll(text, "\n", "")
text = strings.ReplaceAll(text, " ", "")
words := strings.Split(text, "_")
// Defer notifying the that processing is finished.
defer func() {
done <- true
}()
if words[0] == "exit" {
os.Exit(0)
}
if words[0] == "help" {
helpcmd()
return nil
}
if len(words) < 2 {
fmt.Println("Wrong number of arguments")
return fmt.Errorf("Wrong number of arguments")
}
// If we use add we can add random content to the network.
if words[0] == "add" {
size, err := strconv.Atoi(words[1])
if err != nil {
fmt.Println("Not a valid size for random add")
return err
}
addRandomContent(ctx, ipfs, size)
} else if words[0] == "connect" {
connectPeer(ctx, ipfs, words[1])
} else if words[0] == "addFile" {
addFile(ctx, ipfs, words[1])
} else if words[0] == "get" {
fPath := path.New(words[1])
err := getContent(ctx, ipfs, fPath, false)
if err != nil {
fmt.Println("Couldn't find content", err)
return err
}
} else if words[0] == "pin" {
fPath := path.New(words[1])
err := getContent(ctx, ipfs, fPath, true)
if err != nil {
fmt.Println("Couldn't find content", err)
return err
}
} else if words[0] == "graphsync" {
p := words[1]
c := words[2]
fmt.Println("Looking graphsync", p, c)
err := getGraphsync(ctx, ipfs, p, c)
if err != nil {
fmt.Println("Couldn't find content with graphsync:", err)
return err
}
} else {
fmt.Println("[!] Wrong command")
helpcmd()
}
// We could show metrics after each command in certain cases.
// fmt.Println("=== METRICS ===")
// bw := ipfs1.Node.Reporter.GetBandwidthTotals()
// printStats(&bw)
return nil
}
func main() {
addDirectory := flag.String("addDirectory", "", "Add a directory to the probe")
debug := flag.Bool("debug", false, "Set debug logging")
flag.Parse()
if *debug {
logging.SetLogLevel("bitswap", "DEBUG")
logging.SetLogLevel("bitswap_network", "DEBUG")
}
reader := bufio.NewReader(os.Stdin)
fmt.Println("-- Getting an IPFS node running -- ")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := setupPlugins(""); err != nil {
panic(fmt.Errorf("Failed setting up plugins: %s", err))
}
// Spawn a node using a temporary path, creating a temporary repo for the run
fmt.Println("Spawning node on a temporary repo")
// ipfs, err := CreateIPFSNode(ctx)
// if err != nil {
// panic(fmt.Errorf("failed to spawn ephemeral node: %s", err))
// }
nConfig, err := GenerateAddrInfo("127.0.0.1")
if err != nil {
panic(err)
}
// Create IPFS node
ipfs, err := CreateIPFSNodeWithConfig(ctx, nConfig, false)
if err != nil {
panic(err)
}
// Adding random content for testing.
addRandomContent(ctx, ipfs, 11111)
if *addDirectory != "" {
// Adding directory,
fmt.Println("Adding inputData directory")
err := addFile(ctx, ipfs, *addDirectory)
if err != nil {
panic("Wrong directory")
}
}
ch := make(chan string)
chSignal := make(chan os.Signal)
done := make(chan bool)
signal.Notify(chSignal, os.Interrupt, syscall.SIGTERM)
// Prompt routine
go func(ch chan string, done chan bool) {
for {
fmt.Print(">> Enter command: ")
text, _ := reader.ReadString('\n')
ch <- text
<-done
}
}(ch, done)
// Processing loop.
for {
select {
case text := <-ch:
processInput(ctx, ipfs, text, done)
case <-chSignal:
fmt.Printf("\nUse exit to close the tool\n")
fmt.Printf(">> Enter command: ")
}
}
}