-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
231 lines (220 loc) · 7.02 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
package main
import (
"errors"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/ipfs/boxo/ipns"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multibase"
"github.com/urfave/cli/v2"
)
const cidContactEndpoint = "https://cid.contact"
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.BoolFlag{
Name: "accelerated-dht",
Value: true,
EnvVars: []string{"SOMEGUY_ACCELERATED_DHT"},
Usage: "run the accelerated DHT client",
},
&cli.StringSliceFlag{
Name: "provider-endpoints",
Value: cli.NewStringSlice(cidContactEndpoint),
EnvVars: []string{"SOMEGUY_PROVIDER_ENDPOINTS"},
Usage: "other Delegated Routing V1 endpoints to proxy provider requests to",
},
&cli.StringSliceFlag{
Name: "peer-endpoints",
Value: cli.NewStringSlice(),
EnvVars: []string{"SOMEGUY_PEER_ENDPOINTS"},
Usage: "other Delegated Routing V1 endpoints to proxy peer requests to",
},
&cli.StringSliceFlag{
Name: "ipns-endpoints",
Value: cli.NewStringSlice(),
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",
},
},
Action: func(ctx *cli.Context) error {
cfg := &config{
listenAddress: ctx.String("listen-address"),
acceleratedDHTClient: ctx.Bool("accelerated-dht"),
contentEndpoints: ctx.StringSlice("provider-endpoints"),
peerEndpoints: ctx.StringSlice("peer-endpoints"),
ipnsEndpoints: ctx.StringSlice("ipns-endpoints"),
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"),
}
fmt.Printf("Starting %s %s\n", name, version)
fmt.Printf("SOMEGUY_ACCELERATED_DHT = %t\n", cfg.acceleratedDHTClient)
printIfListConfigured("SOMEGUY_PROVIDER_ENDPOINTS = ", cfg.contentEndpoints)
printIfListConfigured("SOMEGUY_PEER_ENDPOINTS = ", cfg.peerEndpoints)
printIfListConfigured("SOMEGUY_IPNS_ENDPOINTS = ", cfg.ipnsEndpoints)
return start(ctx.Context, cfg)
},
},
{
Name: "ask",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "endpoint",
Value: cidContactEndpoint,
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",
},
},
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
}
return findProviders(ctx.Context, c, ctx.String("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
}
return findPeers(ctx.Context, pid, 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, ", "))
}
}