/
main.go
155 lines (150 loc) · 4.37 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
package main
import (
"errors"
"log"
"os"
"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",
},
},
Action: func(ctx *cli.Context) error {
return start(ctx.Context, ctx.String("listen-address"), ctx.Bool("accelerated-dht"), ctx.StringSlice("provider-endpoints"), ctx.StringSlice("peer-endpoints"), ctx.StringSlice("ipns-endpoints"))
},
},
{
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)
}
}