-
Notifications
You must be signed in to change notification settings - Fork 680
/
grpc-echo.go
180 lines (150 loc) · 3.99 KB
/
grpc-echo.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
package services
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net"
"time"
// "os"
"strconv"
"strings"
pb "github.com/datawire/ambassador/pkg/api/kat"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
metadata "google.golang.org/grpc/metadata"
status "google.golang.org/grpc/status"
)
// GRPC server object (all fields are required).
type GRPC struct {
Port int16
Backend string
SecurePort int16
SecureBackend string
Cert string
Key string
}
// DefaultOpts sets gRPC service options.
func DefaultOpts() []grpc.ServerOption {
return []grpc.ServerOption{
grpc.MaxRecvMsgSize(1024 * 1024 * 5),
grpc.MaxSendMsgSize(1024 * 1024 * 5),
}
}
// Start initializes the gRPC server.
func (g *GRPC) Start() <-chan bool {
log.Printf("GRPC: %s listening on %d/%d", g.Backend, g.Port, g.SecurePort)
exited := make(chan bool)
proto := "tcp"
go func() {
port := fmt.Sprintf(":%d", g.Port)
ln, err := net.Listen(proto, port)
if err != nil {
log.Fatal()
}
s := grpc.NewServer(DefaultOpts()...)
pb.RegisterEchoServiceServer(s, g)
s.Serve(ln)
defer ln.Close()
close(exited)
}()
go func() {
cer, err := tls.LoadX509KeyPair(g.Cert, g.Key)
if err != nil {
log.Fatal(err)
return
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
port := fmt.Sprintf(":%d", g.SecurePort)
ln, err := tls.Listen(proto, port, config)
if err != nil {
log.Fatal(err)
return
}
s := grpc.NewServer(DefaultOpts()...)
pb.RegisterEchoServiceServer(s, g)
s.Serve(ln)
defer ln.Close()
close(exited)
}()
log.Print("starting gRPC echo service")
return exited
}
// Echo returns the an object with the HTTP context of the request.
func (g *GRPC) Echo(ctx context.Context, r *pb.EchoRequest) (*pb.EchoResponse, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Error(codes.Code(13), "request has not valid context metadata")
}
buf := bytes.Buffer{}
buf.WriteString("metadata received: \n")
for k, v := range md {
buf.WriteString(fmt.Sprintf("%v : %s\n", k, strings.Join(v, ",")))
}
log.Println(buf.String())
request := &pb.Request{
Headers: make(map[string]string),
}
response := &pb.Response{
Headers: make(map[string]string),
}
// Sets request headers.
for k, v := range md {
request.Headers[k] = strings.Join(v, ",")
response.Headers[k] = strings.Join(v, ",")
}
// Set default backend and assume we're the clear side of the world.
backend := g.Backend
// Checks scheme and set TLS info.
if len(md["x-forwarded-proto"]) > 0 && md["x-forwarded-proto"][0] == "https" {
// We're the secure side of the world, I guess.
backend = g.SecureBackend
request.Tls = &pb.TLS{
Enabled: true,
}
}
// Check header and delay response.
if h, ok := md["Requested-Backend-Delay"]; ok {
if v, err := strconv.Atoi(h[0]); err == nil {
log.Printf("Delaying response by %v ms", v)
time.Sleep(time.Duration(v) * time.Millisecond)
}
}
// Set response date header.
response.Headers["date"] = time.Now().Format(time.RFC1123)
// Sets client requested metadata.
if len(md["requested-headers"]) > 0 {
for _, v := range md["requested-headers"] {
if len(md[v]) > 0 {
s := strings.Join(md[v], ",")
response.Headers[v] = s
p := metadata.Pairs(v, s)
grpc.SendHeader(ctx, p)
}
}
}
// Sets grpc response.
echoRES := &pb.EchoResponse{
Backend: backend,
Request: request,
Response: response,
}
// Set a log message.
if data, err := json.MarshalIndent(echoRES, "", " "); err == nil {
log.Printf("setting response: %s\n", string(data))
}
// Checks if requested-status is a valid and not OK gRPC status.
if len(md["requested-status"]) > 0 {
val, err := strconv.Atoi(md["requested-status"][0])
if err == nil {
if val < 18 || val > 0 {
// Return response and the not OK status.
return echoRES, status.Error(codes.Code(val), "requested-error")
}
}
}
// Returns response and the OK status.
return echoRES, nil
}