/
main.go
285 lines (250 loc) · 7.19 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
package main
import (
"context"
"encoding/base64"
"fmt"
"io"
"os"
"github.com/alecthomas/kong"
"github.com/pkg/errors"
"google.golang.org/grpc"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
dpb "google.golang.org/protobuf/types/descriptorpb"
)
var version = "v0.0.0"
type globals struct {
Address string `short:"a" env:"GRPC_ADDRESS" help:"gRPC server address"`
Plaintext bool `short:"p" help:"Use plain-text; no TLS" env:"GRPC_PLAINTEXT"`
Format string `short:"f" help:"output protoset as one of json, base64, bin, text" enum:"json,base64,bin,text" default:"json"`
Out string `short:"o" help:"output file, default: stdout" default:"-"`
out io.Writer
hostAddress string // used in tests to work with localhost:0
}
type config struct {
globals
Version kong.VersionFlag `short:"V" help:"Print version information" group:"Other:"`
Services servicesCmd `cmd:"" help:"Call list_services"`
Symbol symbolCmd `cmd:"" help:"Call file_containing_symbol"`
Filename filenameCmd `cmd:"" help:"Call file_by_filename"`
Extension extensionCmd `cmd:"" help:"Call file_containing_extension"`
Extensions extensionsCmd `cmd:"" help:"Call all_extension_numbers_of_type"`
FDS fdsCmd `cmd:"" help:"Decode base64 encoded FileDescriptorSet"`
FD fdCmd `cmd:"" help:"Decode base64 encoded FileDescriptor"`
FDSF fdsfCmd `cmd:"" help:"Decode proto encoded FileDescriptorSet.pb file"`
}
type servicesCmd struct{}
type symbolCmd struct {
Symbol string `arg:""`
}
type filenameCmd struct {
Filename string `arg:""`
}
type extensionCmd struct {
Type string `arg:""`
Number int32 `arg:""`
}
type extensionsCmd struct {
Type string `arg:""`
}
type fdCmd struct {
FileDescriptor string `arg:""`
}
type fdsCmd struct {
FileDescriptorSet string `arg:""`
}
type fdsfCmd struct {
FileDescriptorSetFile kong.FileContentFlag `arg:""`
}
func main() {
cfg := &config{}
kctx := kong.Parse(cfg,
kong.Vars{"version": version},
kong.Description("gRPC reflection API toolkit"),
)
err := kctx.Run(cfg.globals)
kctx.FatalIfErrorf(err)
}
func (cfg *config) AfterApply() error {
cfg.out = os.Stdout
if cfg.Out != "-" {
var err error
if cfg.out, err = os.Create(cfg.Out); err != nil {
return errors.Wrap(err, "cannot create output file")
}
}
cfg.hostAddress = cfg.Address
return nil
}
func (f *fdCmd) Run(g globals) error {
m := &dpb.FileDescriptorProto{}
return decode(f.FileDescriptor, m, g)
}
func (f *fdsCmd) Run(g globals) error {
m := &dpb.FileDescriptorSet{}
return decode(f.FileDescriptorSet, m, g)
}
func (f *fdsfCmd) Run(g globals) error {
m := &dpb.FileDescriptorSet{}
err := proto.Unmarshal(f.FileDescriptorSetFile, m)
if err != nil {
return errors.Wrap(err, "cannot decode proto message")
}
return printProto(g.out, m, g.Format)
}
func decode(b64 string, m protoreflect.ProtoMessage, g globals) error {
b, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return errors.Wrap(err, "cannot decode b64 string")
}
err = proto.Unmarshal(b, m)
if err != nil {
return errors.Wrap(err, "cannot decode proto message")
}
return printProto(g.out, m, g.Format)
}
func (s *servicesCmd) Run(g globals) error {
req := &rpb.ServerReflectionRequest{
Host: g.hostAddress,
MessageRequest: &rpb.ServerReflectionRequest_ListServices{},
}
return run(req, g)
}
func (s *symbolCmd) Run(g globals) error {
req := &rpb.ServerReflectionRequest{
Host: g.hostAddress,
MessageRequest: &rpb.ServerReflectionRequest_FileContainingSymbol{
FileContainingSymbol: s.Symbol,
},
}
return run(req, g)
}
func (f *filenameCmd) Run(g globals) error {
req := &rpb.ServerReflectionRequest{
Host: g.hostAddress,
MessageRequest: &rpb.ServerReflectionRequest_FileByFilename{
FileByFilename: f.Filename,
},
}
return run(req, g)
}
func (e *extensionCmd) Run(g globals) error {
req := &rpb.ServerReflectionRequest{
Host: g.hostAddress,
MessageRequest: &rpb.ServerReflectionRequest_FileContainingExtension{
FileContainingExtension: &rpb.ExtensionRequest{
ContainingType: e.Type,
ExtensionNumber: e.Number,
},
},
}
return run(req, g)
}
func (e *extensionsCmd) Run(g globals) error {
req := &rpb.ServerReflectionRequest{
Host: g.hostAddress,
MessageRequest: &rpb.ServerReflectionRequest_AllExtensionNumbersOfType{
AllExtensionNumbersOfType: e.Type,
},
}
return run(req, g)
}
func run(req *rpb.ServerReflectionRequest, g globals) error {
stream, err := newStream(context.Background(), g)
if err != nil {
return err
}
defer closeAndDrain(stream)
resp, err := send(stream, req)
if err != nil {
return err
}
return printProto(g.out, resp, g.Format)
}
func closeAndDrain(stream rpb.ServerReflection_ServerReflectionInfoClient) {
_ = stream.CloseSend()
for {
if _, err := stream.Recv(); err != nil {
return
}
}
}
func newStream(ctx context.Context, g globals) (rpb.ServerReflection_ServerReflectionInfoClient, error) {
var opts []grpc.DialOption
if g.Plaintext {
opts = append(opts, grpc.WithInsecure())
}
conn, err := grpc.Dial(g.Address, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot grpc dial %s", g.Address)
}
client := rpb.NewServerReflectionClient(conn)
stream, err := client.ServerReflectionInfo(ctx)
if err != nil {
return nil, errors.Wrap(err, "cannot setup reflection stream")
}
return stream, nil
}
func send(stream rpb.ServerReflection_ServerReflectionInfoClient, req *rpb.ServerReflectionRequest) (*rpb.ServerReflectionResponse, error) {
if err := stream.Send(req); err != nil {
return nil, errors.Wrap(err, "cannot send reflection request")
}
resp, err := stream.Recv()
if err != nil {
return nil, errors.Wrap(err, "cannot receive reflection response")
}
return resp, nil
}
func printProto(w io.Writer, m protoreflect.ProtoMessage, format string) error {
var b []byte
var err error
switch format {
case "json":
b, err = jsonString(m)
case "base64":
b, err = base64String(m)
case "text":
b, err = textString(m)
case "bin":
b, err = binString(m)
default:
err = fmt.Errorf("unknown format %s", format)
}
if err != nil {
return err
}
_, err = w.Write(b)
return errors.Wrap(err, "cannot print proto")
}
func jsonString(m protoreflect.ProtoMessage) ([]byte, error) {
marshaler := protojson.MarshalOptions{Multiline: true}
out, err := marshaler.Marshal(m)
if err != nil {
return nil, errors.Wrap(err, "cannot jsonString")
}
return out, nil
}
func base64String(m protoreflect.ProtoMessage) ([]byte, error) {
b, err := proto.Marshal(m)
if err != nil {
return nil, errors.Wrap(err, "cannot base64String")
}
return []byte(base64.StdEncoding.EncodeToString(b)), nil
}
func binString(m protoreflect.ProtoMessage) ([]byte, error) {
out, err := proto.Marshal(m)
if err != nil {
return nil, errors.Wrap(err, "cannot binString")
}
return out, nil
}
func textString(m protoreflect.ProtoMessage) ([]byte, error) {
out, err := prototext.Marshal(m)
if err != nil {
return nil, errors.Wrap(err, "cannot textString")
}
return out, nil
}