This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
297 lines (267 loc) · 8.72 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/filecoin-project/lassie/pkg/types"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multicodec"
"github.com/urfave/cli/v2"
)
var logger = log.Logger("cassiopeia")
func main() {
// set up a context that is canceled when a command is interrupted
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// set up a signal handler to cancel the context
go func() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
select {
case <-interrupt:
fmt.Println()
logger.Info("received interrupt signal")
cancel()
case <-ctx.Done():
}
// Allow any further SIGTERM or SIGINT to kill process
signal.Stop(interrupt)
}()
app := &cli.App{
Name: "cassiopeia",
Usage: "Utility for retrieving content from the Filecoin network",
Suggest: true,
Flags: daemonFlags,
Action: serveAction,
}
if err := app.RunContext(ctx, os.Args); err != nil {
logger.Fatal(err)
}
}
var daemonFlags = []cli.Flag{
&cli.StringFlag{
Name: "address",
Aliases: []string{"a"},
Usage: "the address the http server listens on",
Value: "127.0.0.1",
DefaultText: "127.0.0.1",
EnvVars: []string{"LASSIE_ADDRESS"},
},
&cli.UintFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "the port the http server listens on",
Value: 0,
DefaultText: "random",
EnvVars: []string{"LASSIE_PORT"},
},
&cli.Uint64Flag{
Name: "maxblocks",
Aliases: []string{"mb"},
Usage: "maximum number of blocks sent before closing connection",
Value: 0,
DefaultText: "no limit",
EnvVars: []string{"LASSIE_MAX_BLOCKS_PER_REQUEST"},
},
&cli.IntFlag{
Name: "libp2p-conns-lowwater",
Aliases: []string{"lw"},
Usage: "lower limit of libp2p connections",
Value: 0,
DefaultText: "libp2p default",
EnvVars: []string{"LASSIE_LIBP2P_CONNECTIONS_LOWWATER"},
},
&cli.IntFlag{
Name: "libp2p-conns-highwater",
Aliases: []string{"hw"},
Usage: "upper limit of libp2p connections",
Value: 0,
DefaultText: "libp2p default",
EnvVars: []string{"LASSIE_LIBP2P_CONNECTIONS_HIGHWATER"},
},
&cli.UintFlag{
Name: "concurrent-sp-retrievals",
Aliases: []string{"cr"},
Usage: "max number of simultaneous SP retrievals",
Value: 0,
DefaultText: "no limit",
EnvVars: []string{"LASSIE_CONCURRENT_SP_RETRIEVALS"},
},
FlagEventRecorderAuth,
FlagEventRecorderInstanceId,
FlagEventRecorderUrl,
FlagVerbose,
FlagVeryVerbose,
FlagProtocols,
FlagAllowProviders,
FlagExcludeProviders,
FlagTempDir,
FlagBitswapConcurrency,
FlagGlobalTimeout,
FlagProviderTimeout,
}
const (
defaultBitswapConcurrency int = 6 // 6 concurrent requests
defaultProviderTimeout time.Duration = 20 * time.Second // 20 seconds
)
var (
defaultTempDirectory string = os.TempDir() // use the system default temp dir
verboseLoggingSubsystems []string = []string{ // verbose logging is enabled for these subsystems when using the verbose or very-verbose flags
"lassie",
"lassie/retriever",
"lassie/httpserver",
"lassie/indexerlookup",
"lassie/bitswap",
}
)
// FlagVerbose enables verbose mode, which shows info information about
// operations invoked in the CLI.
var FlagVerbose = &cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "enable verbose mode for logging",
Action: setLogLevel("INFO"),
}
// FlagVeryVerbose enables very verbose mode, which shows debug information about
// operations invoked in the CLI.
var FlagVeryVerbose = &cli.BoolFlag{
Name: "very-verbose",
Aliases: []string{"vv"},
Usage: "enable very verbose mode for debugging",
Action: setLogLevel("DEBUG"),
}
// setLogLevel returns a CLI Action function that sets the
// logging level for the given subsystems to the given level.
// It is used as an action for the verbose and very-verbose flags.
func setLogLevel(level string) func(*cli.Context, bool) error {
return func(cctx *cli.Context, _ bool) error {
// don't override logging if set in the environment.
if os.Getenv("GOLOG_LOG_LEVEL") != "" {
return nil
}
// set the logging level for the given subsystems
for _, name := range verboseLoggingSubsystems {
_ = log.SetLogLevel(name, level)
}
return nil
}
}
// FlagEventRecorderAuth asks for and provides the authorization token for
// sending metrics to an event recorder API via a Basic auth Authorization
// HTTP header. Value will formatted as "Basic <value>" if provided.
var FlagEventRecorderAuth = &cli.StringFlag{
Name: "event-recorder-auth",
Usage: "the authorization token for an event recorder API",
DefaultText: "no authorization token will be used",
EnvVars: []string{"LASSIE_EVENT_RECORDER_AUTH"},
}
// FlagEventRecorderUrl asks for and provides the URL for an event recorder API
// to send metrics to.
var FlagEventRecorderInstanceId = &cli.StringFlag{
Name: "event-recorder-instance-id",
Usage: "the instance ID to use for an event recorder API request",
DefaultText: "a random v4 uuid",
EnvVars: []string{"LASSIE_EVENT_RECORDER_INSTANCE_ID"},
}
// FlagEventRecorderUrl asks for and provides the URL for an event recorder API
// to send metrics to.
var FlagEventRecorderUrl = &cli.StringFlag{
Name: "event-recorder-url",
Usage: "the url of an event recorder API",
DefaultText: "no event recorder API will be used",
EnvVars: []string{"LASSIE_EVENT_RECORDER_URL"},
}
var providerBlockList map[peer.ID]bool
var FlagExcludeProviders = &cli.StringFlag{
Name: "exclude-providers",
DefaultText: "All providers allowed",
Usage: "Provider peer IDs, seperated by a comma. Example: 12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4",
EnvVars: []string{"LASSIE_EXCLUDE_PROVIDERS"},
Action: func(cctx *cli.Context, v string) error {
// Do nothing if given an empty string
if v == "" {
return nil
}
providerBlockList = make(map[peer.ID]bool)
vs := strings.Split(v, ",")
for _, v := range vs {
peerID, err := peer.Decode(v)
if err != nil {
return err
}
providerBlockList[peerID] = true
}
return nil
},
}
var fetchProviderAddrInfos []peer.AddrInfo
var FlagAllowProviders = &cli.StringFlag{
Name: "providers",
Aliases: []string{"provider"},
DefaultText: "Providers will be discovered automatically",
Usage: "Addresses of providers, including peer IDs, to use instead of automatic discovery, seperated by a comma. All protocols will be attempted when connecting to these providers. Example: /ip4/1.2.3.4/tcp/1234/p2p/12D3KooWBSTEYMLSu5FnQjshEVah9LFGEZoQt26eacCEVYfedWA4",
EnvVars: []string{"LASSIE_ALLOW_PROVIDERS"},
Action: func(cctx *cli.Context, v string) error {
// Do nothing if given an empty string
if v == "" {
return nil
}
var err error
fetchProviderAddrInfos, err = types.ParseProviderStrings(v)
return err
},
}
var protocols []multicodec.Code
var FlagProtocols = &cli.StringFlag{
Name: "protocols",
DefaultText: "bitswap,graphsync,http",
Usage: "List of retrieval protocols to use, seperated by a comma",
EnvVars: []string{"LASSIE_SUPPORTED_PROTOCOLS"},
Action: func(cctx *cli.Context, v string) error {
// Do nothing if given an empty string
if v == "" {
return nil
}
var err error
protocols, err = types.ParseProtocolsString(v)
return err
},
}
var FlagTempDir = &cli.StringFlag{
Name: "tempdir",
Aliases: []string{"td"},
Usage: "directory to store temporary files while downloading",
Value: defaultTempDirectory,
DefaultText: "os temp directory",
EnvVars: []string{"LASSIE_TEMP_DIRECTORY"},
}
var FlagBitswapConcurrency = &cli.IntFlag{
Name: "bitswap-concurrency",
Usage: "maximum number of concurrent bitswap requests per retrieval",
Value: defaultBitswapConcurrency,
EnvVars: []string{"LASSIE_BITSWAP_CONCURRENCY"},
}
var FlagGlobalTimeout = &cli.DurationFlag{
Name: "global-timeout",
Aliases: []string{"gt"},
Usage: "consider it an error after not completing a retrieval after this amount of time",
EnvVars: []string{"LASSIE_GLOBAL_TIMEOUT"},
}
var FlagProviderTimeout = &cli.DurationFlag{
Name: "provider-timeout",
Aliases: []string{"pt"},
Usage: "consider it an error after not receiving a response from a storage provider after this amount of time",
Value: defaultProviderTimeout,
EnvVars: []string{"LASSIE_PROVIDER_TIMEOUT"},
}
var FlagIPNIEndpoint = &cli.StringFlag{
Name: "ipni-endpoint",
Aliases: []string{"ipni"},
DefaultText: "Defaults to https://cid.contact",
Usage: "HTTP endpoint of the IPNI instance used to discover providers.",
}