-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
fetch.go
84 lines (72 loc) · 2.51 KB
/
fetch.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
// Package grpcreflection provides gRPC reflection client.
// Currently, gRPC reflection depends on Protocol Buffers, so we split this package from grpc package.
package reflection
import (
"context"
"errors"
"fmt"
"strings"
"github.com/jhump/protoreflect/desc"
gr "github.com/jhump/protoreflect/grpcreflect"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
"google.golang.org/grpc/status"
)
const reflectionServiceName = "grpc.reflection.v1alpha.ServerReflection"
var ErrTLSHandshakeFailed = errors.New("TLS handshake failed")
// Client defines gRPC reflection client.
type Client interface {
// ListPackages lists file descriptors from the gRPC reflection server.
// ListPackages returns these errors:
// - ErrTLSHandshakeFailed: TLS misconfig.
ListPackages() ([]*desc.FileDescriptor, error)
// Reset clears internal states of Client.
Reset()
}
type client struct {
client *gr.Client
}
// NewClient returns an instance of gRPC reflection client for gRPC protocol.
func NewClient(conn grpc.ClientConnInterface) Client {
return &client{
client: gr.NewClient(context.Background(), grpc_reflection_v1alpha.NewServerReflectionClient(conn)),
}
}
// NewWebClient returns an instance of gRPC reflection client for gRPC-Web protocol.
//func NewWebClient(conn *grpcweb.ClientConn) Client {
// return &client{
// client: gr.NewClient(context.Background(), grpcweb_reflection_v1alpha.NewServerReflectionClient(conn)),
// }
//}
func (c *client) ListPackages() ([]*desc.FileDescriptor, error) {
//c.client.FileContainingExtension()
ssvcs, err := c.client.ListServices()
if err != nil {
msg := status.Convert(err).Message()
// Check whether the error message contains TLS related error.
// If the server didn't enable TLS, the error message contains the first string.
// If Evans didn't enable TLS against to the TLS enabled server, the error message contains
// the second string.
if strings.Contains(msg, "tls: first record does not look like a TLS handshake") ||
strings.Contains(msg, "latest connection error: <nil>") {
return nil, ErrTLSHandshakeFailed
}
return nil, fmt.Errorf("failed to list services from reflecton enabled gRPC server: %w", err)
}
var fds []*desc.FileDescriptor
for _, s := range ssvcs {
if s == reflectionServiceName {
continue
}
svc, err := c.client.ResolveService(s)
if err != nil {
return nil, err
}
fd := svc.GetFile() //.AsFileDescriptorProto()
fds = append(fds, fd)
}
return fds, nil
}
func (c *client) Reset() {
c.client.Reset()
}