-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.go
More file actions
456 lines (430 loc) · 15.7 KB
/
main.go
File metadata and controls
456 lines (430 loc) · 15.7 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package main
import (
"crypto/sha256"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/ipfs/boxo/autoconf"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: name,
Usage: "A Delegated Routing V1 server and proxy for all your routing needs.",
Version: version,
Commands: []*cli.Command{
{
Name: "start",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen-address",
Value: "127.0.0.1:8190",
EnvVars: []string{"SOMEGUY_LISTEN_ADDRESS"},
Usage: "listen address",
},
&cli.StringFlag{
Name: "dht",
Value: "accelerated",
EnvVars: []string{"SOMEGUY_DHT"},
Usage: "mode with which to run the Amino DHT client. Options are 'accelerated' or 'standard' or 'disabled'.",
},
&cli.BoolFlag{
Name: "cached-addr-book",
Value: true,
EnvVars: []string{"SOMEGUY_CACHED_ADDR_BOOK"},
Usage: "use a cached address book to improve provider lookup responses",
},
&cli.BoolFlag{
Name: "cached-addr-book-active-probing",
Value: true,
EnvVars: []string{"SOMEGUY_CACHED_ADDR_BOOK_ACTIVE_PROBING"},
Usage: "actively probe peers in cache to keep their multiaddrs up to date",
},
&cli.DurationFlag{
Name: "cached-addr-book-recent-ttl",
DefaultText: DefaultRecentlyConnectedAddrTTL.String(),
Value: DefaultRecentlyConnectedAddrTTL,
EnvVars: []string{"SOMEGUY_CACHED_ADDR_BOOK_RECENT_TTL"},
Usage: "TTL for recently connected peers' multiaddrs in the cached address book",
},
&cli.StringSliceFlag{
Name: "provider-endpoints",
Value: cli.NewStringSlice(autoconf.AutoPlaceholder),
EnvVars: []string{"SOMEGUY_PROVIDER_ENDPOINTS"},
Usage: "other Delegated Routing V1 endpoints to proxy provider requests to",
},
&cli.StringSliceFlag{
Name: "http-block-provider-endpoints",
Value: nil,
EnvVars: []string{"SOMEGUY_HTTP_BLOCK_PROVIDER_ENDPOINTS"},
Usage: "list of HTTP trustless gateway endpoints to proxy provider requests to",
},
&cli.StringSliceFlag{
Name: "http-block-provider-peerids",
Value: nil,
EnvVars: []string{"SOMEGUY_HTTP_BLOCK_PROVIDER_PEERIDS"},
Usage: "list of peerIDs for the HTTP trustless gateway endpoints to proxy provider requests to",
},
&cli.StringSliceFlag{
Name: "peer-endpoints",
Value: cli.NewStringSlice(autoconf.AutoPlaceholder),
EnvVars: []string{"SOMEGUY_PEER_ENDPOINTS"},
Usage: "other Delegated Routing V1 endpoints to proxy peer requests to",
},
&cli.StringSliceFlag{
Name: "ipns-endpoints",
Value: cli.NewStringSlice(autoconf.AutoPlaceholder),
EnvVars: []string{"SOMEGUY_IPNS_ENDPOINTS"},
Usage: "other Delegated Routing V1 endpoints to proxy IPNS requests to",
},
&cli.StringSliceFlag{
Name: "libp2p-listen-addrs",
Value: cli.NewStringSlice(
"/ip4/0.0.0.0/tcp/4004",
"/ip4/0.0.0.0/udp/4004/quic-v1",
"/ip4/0.0.0.0/udp/4004/webrtc-direct",
"/ip4/0.0.0.0/udp/4004/quic-v1/webtransport",
"/ip6/::/tcp/4004",
"/ip6/::/udp/4004/quic-v1",
"/ip6/::/udp/4004/webrtc-direct",
"/ip6/::/udp/4004/quic-v1/webtransport"),
EnvVars: []string{"SOMEGUY_LIBP2P_LISTEN_ADDRS"},
Usage: "Multiaddresses for libp2p host to listen on (comma-separated)",
},
&cli.IntFlag{
Name: "libp2p-connmgr-low",
Value: 100,
EnvVars: []string{"SOMEGUY_LIBP2P_CONNMGR_LOW"},
Usage: "minimum number of libp2p connections to keep",
},
&cli.IntFlag{
Name: "libp2p-connmgr-high",
Value: 3000,
EnvVars: []string{"SOMEGUY_LIBP2P_CONNMGR_HIGH"},
Usage: "maximum number of libp2p connections to keep",
},
&cli.DurationFlag{
Name: "libp2p-connmgr-grace",
Value: time.Minute,
EnvVars: []string{"SOMEGUY_LIBP2P_CONNMGR_GRACE_PERIOD"},
Usage: "minimum libp2p connection TTL",
},
&cli.Uint64Flag{
Name: "libp2p-max-memory",
Value: 0,
EnvVars: []string{"SOMEGUY_LIBP2P_MAX_MEMORY"},
Usage: "maximum memory to use for libp2p. Defaults to 85% of the system's available RAM",
},
&cli.Uint64Flag{
Name: "libp2p-max-fd",
Value: 0,
EnvVars: []string{"SOMEGUY_LIBP2P_MAX_FD"},
Usage: "maximum number of file descriptors used by libp2p node. Defaults to 50% of the process' limit",
},
&cli.StringFlag{
Name: "tracing-auth",
Value: "",
EnvVars: []string{"SOMEGUY_TRACING_AUTH"},
Usage: "If set the key gates use of the Traceparent header by requiring the key to be passed in the Authorization header",
},
&cli.Float64Flag{
Name: "sampling-fraction",
Value: 0,
EnvVars: []string{"SOMEGUY_SAMPLING_FRACTION"},
Usage: "Rate at which to sample gateway requests. Does not include requests with traceheaders which will always sample",
},
&cli.StringFlag{
Name: "datadir",
Value: "",
EnvVars: []string{"SOMEGUY_DATADIR"},
Usage: "Directory for persistent data (autoconf cache)",
},
&cli.BoolFlag{
Name: "autoconf",
Value: true,
EnvVars: []string{"SOMEGUY_AUTOCONF"},
Usage: "Enable autoconf for bootstrap, DNS resolvers, and HTTP routers",
},
&cli.StringFlag{
Name: "autoconf-url",
Value: "https://conf.ipfs-mainnet.org/autoconf.json",
EnvVars: []string{"SOMEGUY_AUTOCONF_URL"},
Usage: "URL to fetch autoconf data from",
},
&cli.DurationFlag{
Name: "autoconf-refresh",
Value: 24 * time.Hour,
EnvVars: []string{"SOMEGUY_AUTOCONF_REFRESH"},
Usage: "How often to refresh autoconf data",
},
},
Action: func(ctx *cli.Context) error {
cfg := &config{
listenAddress: ctx.String("listen-address"),
dhtType: ctx.String("dht"),
cachedAddrBook: ctx.Bool("cached-addr-book"),
cachedAddrBookActiveProbing: ctx.Bool("cached-addr-book-active-probing"),
cachedAddrBookRecentTTL: ctx.Duration("cached-addr-book-recent-ttl"),
contentEndpoints: ctx.StringSlice("provider-endpoints"),
peerEndpoints: ctx.StringSlice("peer-endpoints"),
ipnsEndpoints: ctx.StringSlice("ipns-endpoints"),
blockProviderEndpoints: ctx.StringSlice("http-block-provider-endpoints"),
blockProviderPeerIDs: ctx.StringSlice("http-block-provider-peerids"),
libp2pListenAddress: ctx.StringSlice("libp2p-listen-addrs"),
connMgrLow: ctx.Int("libp2p-connmgr-low"),
connMgrHi: ctx.Int("libp2p-connmgr-high"),
connMgrGrace: ctx.Duration("libp2p-connmgr-grace"),
maxMemory: ctx.Uint64("libp2p-max-memory"),
maxFD: ctx.Int("libp2p-max-fd"),
tracingAuth: ctx.String("tracing-auth"),
samplingFraction: ctx.Float64("sampling-fraction"),
autoConf: autoConfConfig{
enabled: ctx.Bool("autoconf"),
url: ctx.String("autoconf-url"),
refreshInterval: ctx.Duration("autoconf-refresh"),
cacheDir: filepath.Join(ctx.String("datadir"), ".autoconf-cache"),
},
}
fmt.Printf("Starting %s %s\n", name, version)
fmt.Printf("SOMEGUY_DHT = %s\n", cfg.dhtType)
printIfListConfigured("SOMEGUY_PROVIDER_ENDPOINTS = ", cfg.contentEndpoints)
printIfListConfigured("SOMEGUY_PEER_ENDPOINTS = ", cfg.peerEndpoints)
printIfListConfigured("SOMEGUY_IPNS_ENDPOINTS = ", cfg.ipnsEndpoints)
if len(cfg.blockProviderEndpoints) > 0 && len(cfg.blockProviderPeerIDs) == 0 {
fmt.Printf("SOMEGUY_HTTP_BLOCK_PROVIDER_ENDPOINTS is set but SOMEGUY_HTTP_BLOCK_PROVIDER_PEERIDS were not. PeerIDs will be autogenerated.\n")
// Generate synthetic PeerIDs for HTTP block providers. These are deterministic
// identifiers based on endpoint URLs, used solely for routing system compatibility.
// Since HTTP providers use trustless gateway protocol, these PeerIDs are never
// used for cryptographic operations or libp2p authentication.
for i := 0; i < len(cfg.blockProviderEndpoints); i++ {
digest := sha256.Sum256([]byte(cfg.blockProviderEndpoints[i]))
mh, err := multihash.Encode((digest[:]), multihash.SHA2_256)
if err != nil {
return err
}
p, err := peer.IDFromBytes(mh)
if err != nil {
return err
}
cfg.blockProviderPeerIDs = append(cfg.blockProviderPeerIDs, p.String())
}
}
printIfListConfigured("SOMEGUY_HTTP_BLOCK_PROVIDER_ENDPOINTS = ", cfg.blockProviderEndpoints)
printIfListConfigured("SOMEGUY_HTTP_BLOCK_PROVIDER_PEERIDS = ", cfg.blockProviderPeerIDs)
return start(ctx.Context, cfg)
},
},
{
Name: "ask",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "endpoint",
Value: autoconf.AutoPlaceholder,
Usage: "the Delegated Routing V1 endpoint to ask",
},
&cli.BoolFlag{
Name: "pretty",
Value: false,
Usage: "output data in a prettier format that may convey less information",
},
&cli.StringFlag{
Name: "datadir",
Value: "",
EnvVars: []string{"SOMEGUY_DATADIR"},
Usage: "Directory for persistent data (autoconf cache)",
},
&cli.BoolFlag{
Name: "autoconf",
Value: true,
EnvVars: []string{"SOMEGUY_AUTOCONF"},
Usage: "Enable autoconf for bootstrap, DNS resolvers, and HTTP routers",
},
&cli.StringFlag{
Name: "autoconf-url",
Value: "https://conf.ipfs-mainnet.org/autoconf.json",
EnvVars: []string{"SOMEGUY_AUTOCONF_URL"},
Usage: "URL to fetch autoconf data from",
},
&cli.DurationFlag{
Name: "autoconf-refresh",
Value: 24 * time.Hour,
EnvVars: []string{"SOMEGUY_AUTOCONF_REFRESH"},
Usage: "How often to refresh autoconf data",
},
},
Subcommands: []*cli.Command{
{
Name: "findprovs",
Usage: "findprovs <cid>",
UsageText: "Find providers of a given CID",
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.New("invalid command, see help")
}
cidStr := ctx.Args().Get(0)
c, err := cid.Parse(cidStr)
if err != nil {
return err
}
cfg := &config{
dhtType: "none",
contentEndpoints: []string{ctx.String("endpoint")},
autoConf: autoConfConfig{
enabled: ctx.Bool("autoconf"),
url: ctx.String("autoconf-url"),
refreshInterval: ctx.Duration("autoconf-refresh"),
cacheDir: filepath.Join(ctx.String("datadir"), ".autoconf-cache"),
},
}
autoConf, err := startAutoConf(ctx.Context, cfg)
if err != nil {
logger.Error(err.Error())
}
if err = expandDelegatedRoutingEndpoints(cfg, autoConf); err != nil {
return err
}
if len(cfg.contentEndpoints) == 0 {
return errors.New("no delegated routing endpoint configured, use --endpoint to specify")
}
endPoint := cfg.contentEndpoints[0]
logger.Debugf("delegated routing endpoint: %s", endPoint)
return findProviders(ctx.Context, c, endPoint, ctx.Bool("pretty"))
},
},
{
Name: "findpeers",
Usage: "findpeers <pid>",
UsageText: "Find a peer of a given PID",
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.New("invalid command, see help")
}
pidStr := ctx.Args().Get(0)
pid, err := peer.Decode(pidStr)
if err != nil {
return err
}
cfg := &config{
dhtType: "none",
contentEndpoints: []string{ctx.String("endpoint")},
autoConf: autoConfConfig{
enabled: ctx.Bool("autoconf"),
url: ctx.String("autoconf-url"),
refreshInterval: ctx.Duration("autoconf-refresh"),
cacheDir: filepath.Join(ctx.String("datadir"), ".autoconf-cache"),
},
}
autoConf, err := startAutoConf(ctx.Context, cfg)
if err != nil {
logger.Error(err.Error())
}
if err = expandDelegatedRoutingEndpoints(cfg, autoConf); err != nil {
return err
}
if len(cfg.contentEndpoints) == 0 {
return errors.New("no delegated routing endpoint configured, use --endpoint to specify")
}
endPoint := cfg.contentEndpoints[0]
logger.Debugf("delegated routing endpoint: %s", endPoint)
return findPeers(ctx.Context, pid, endPoint, ctx.Bool("pretty"))
},
},
{
Name: "getclosestpeers",
Usage: "getclosestpeers <key>",
UsageText: "Find DHT-closest peers to a key (CID or peer ID)",
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.New("invalid command, see help")
}
keyStr := ctx.Args().Get(0)
c, err := parseKey(keyStr)
if err != nil {
return err
}
return getClosestPeers(ctx.Context, c, ctx.String("endpoint"), ctx.Bool("pretty"))
},
},
{
Name: "getipns",
Usage: "getipns <ipns-id>",
UsageText: "Get the value of an IPNS ID",
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.New("invalid command, see help")
}
nameStr := ctx.Args().Get(0)
name, err := ipns.NameFromString(nameStr)
if err != nil {
return err
}
return getIPNS(ctx.Context, name, ctx.String("endpoint"), ctx.Bool("pretty"))
},
},
{
Name: "putipns",
Usage: "putipns <ipns-id> <multibase-encoded-record>",
UsageText: "Put an IPNS record",
Flags: []cli.Flag{},
Action: func(ctx *cli.Context) error {
if ctx.NArg() != 2 {
return errors.New("invalid command, see help")
}
nameStr := ctx.Args().Get(0)
name, err := ipns.NameFromString(nameStr)
if err != nil {
return err
}
recordStr := ctx.Args().Get(1)
_, recBytes, err := multibase.Decode(recordStr)
if err != nil {
return err
}
return putIPNS(ctx.Context, name, recBytes, ctx.String("endpoint"))
},
},
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func printIfListConfigured(message string, list []string) {
if len(list) > 0 {
fmt.Printf(message+"%v\n", strings.Join(list, ", "))
}
}
// parseKey parses a string that can be either a CID or a PeerID.
// It accepts the following formats:
// - Arbitrary CIDs (e.g., bafkreidcd7frenco2m6ch7mny63wztgztv3q6fctaffgowkro6kljre5ei)
// - CIDv1 with libp2p-key codec (e.g., bafzaajaiaejca...)
// - Base58-encoded PeerIDs (e.g., 12D3KooW... or QmYyQ...)
//
// Returns the key as a CID. PeerIDs are converted to CIDv1 with libp2p-key codec.
func parseKey(keyStr string) (cid.Cid, error) {
// Try parsing as PeerID first using peer.Decode
// This handles legacy PeerID formats per:
// https://github.com/libp2p/specs/blob/master/peer-ids/peer-ids.md#string-representation
pid, pidErr := peer.Decode(keyStr)
if pidErr == nil {
return peer.ToCid(pid), nil
}
// Fall back to parsing as CID (handles arbitrary CIDs and CIDv1 libp2p-key format)
c, cidErr := cid.Parse(keyStr)
if cidErr == nil {
return c, nil
}
return cid.Cid{}, fmt.Errorf("unable to parse as CID or PeerID: %w", errors.Join(cidErr, pidErr))
}