This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo.go
557 lines (475 loc) · 13.7 KB
/
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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
echopb "istio.io/istio/pilot/test/grpcecho"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// allow all connections by default
return true
},
} //defaults
type pilotTestHandler struct {
port int
version string
}
// Imagine a pie of different flavors.
// The flavors are the HTTP response codes.
// The chance of a particular flavor is ( slices / sum of slices ).
type codeAndSlices struct {
httpResponseCode int
slices int
}
func (h *pilotTestHandler) addResponsePayload(r *http.Request, body *bytes.Buffer) {
body.WriteString("ServiceVersion=" + h.version + "\n")
body.WriteString("ServicePort=" + strconv.Itoa(h.port) + "\n")
body.WriteString("Method=" + r.Method + "\n")
body.WriteString("URL=" + r.URL.String() + "\n")
body.WriteString("Proto=" + r.Proto + "\n")
body.WriteString("RemoteAddr=" + r.RemoteAddr + "\n")
body.WriteString("Host=" + r.Host + "\n")
for name, headers := range r.Header {
for _, h := range headers {
body.WriteString(fmt.Sprintf("%v=%v\n", name, h))
}
}
if hostname, err := os.Hostname(); err == nil {
body.WriteString(fmt.Sprintf("Hostname=%v\n", hostname))
}
}
func (h *pilotTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("testwebsocket") != "" {
h.WebSocketEcho(w, r)
return
}
body := bytes.Buffer{}
if err := r.ParseForm(); err != nil {
body.WriteString("ParseForm() error: " + err.Error() + "\n")
}
if len(r.FormValue("url")) > 0 {
echoClientHandler(w, r)
return
}
// If the request has form ?codes=code[:chance][,code[:chance]]* return those codes, rather than 200
// For example, ?codes=500:1,200:1 returns 500 1/2 times and 200 1/2 times
// For example, ?codes=500:90,200:10 returns 500 90% of times and 200 10% of times
if err := setResponseFromCodes(r, w); err != nil {
body.WriteString("codes error: " + err.Error() + "\n")
}
h.addResponsePayload(r, &body)
w.Header().Set("Content-Type", "application/text")
if _, err := w.Write(body.Bytes()); err != nil {
log.Println(err.Error())
}
}
func (h *pilotTestHandler) Echo(ctx context.Context, req *echopb.EchoRequest) (*echopb.EchoResponse, error) {
body := bytes.Buffer{}
md, ok := metadata.FromIncomingContext(ctx)
if ok {
for key, vals := range md {
body.WriteString(key + "=" + strings.Join(vals, " ") + "\n")
}
}
body.WriteString("ServiceVersion=" + h.version + "\n")
body.WriteString("ServicePort=" + strconv.Itoa(h.port) + "\n")
body.WriteString("Echo=" + req.GetMessage())
return &echopb.EchoResponse{Message: body.String()}, nil
}
func (h *pilotTestHandler) WebSocketEcho(w http.ResponseWriter, r *http.Request) {
body := bytes.Buffer{}
h.addResponsePayload(r, &body) // create resp payload apriori
// adapted from https://github.com/gorilla/websocket/blob/master/examples/echo/server.go
// First send upgrade headers
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("websocket-echo upgrade failed:", err)
return
}
// nolint: errcheck
defer c.Close()
// ping
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("websocket-echo read failed:", err)
return
}
// pong
body.Write(message)
err = c.WriteMessage(mt, body.Bytes())
if err != nil {
log.Println("websocket-echo write failed:", err)
return
}
}
// RunHTTP runs the http echo service.
func RunHTTP(port int, version string) {
fmt.Printf("Listening HTTP1.1 on %v\n", port)
h := &pilotTestHandler{port: port, version: version}
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), h); err != nil {
log.Println(err.Error())
}
}
// RunGRPC runs the GRPC service, with optional TLS
func RunGRPC(port int, version, crt, key string) {
fmt.Printf("Listening GRPC on %v\n", port)
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
h := pilotTestHandler{port: port, version: version}
var grpcServer *grpc.Server
if crt != "" && key != "" {
// Create the TLS credentials
creds, errCreds := credentials.NewServerTLSFromFile(crt, key)
if errCreds != nil {
log.Fatalf("could not load TLS keys: %s", errCreds)
}
grpcServer = grpc.NewServer(grpc.Creds(creds))
} else {
grpcServer = grpc.NewServer()
}
echopb.RegisterEchoTestServiceServer(grpcServer, &h)
if err = grpcServer.Serve(lis); err != nil {
log.Println(err.Error())
}
}
func setResponseFromCodes(request *http.Request, response http.ResponseWriter) error {
responseCodes := request.FormValue("codes")
codes, err := validateCodes(responseCodes)
if err != nil {
return err
}
// Choose a random "slice" from a pie
var totalSlices = 0
for _, flavor := range codes {
totalSlices += flavor.slices
}
slice := rand.Intn(totalSlices)
// What flavor is that slice?
responseCode := codes[len(codes)-1].httpResponseCode // Assume the last slice
position := 0
for n, flavor := range codes {
if position > slice {
responseCode = codes[n-1].httpResponseCode // No, use an earlier slice
break
}
position += flavor.slices
}
response.WriteHeader(responseCode)
return nil
}
// codes must be comma-separated HTTP response code, colon, positive integer
func validateCodes(codestrings string) ([]codeAndSlices, error) {
if codestrings == "" {
// Consider no codes to be "200:1" -- return HTTP 200 100% of the time.
codestrings = strconv.Itoa(http.StatusOK) + ":1"
}
aCodestrings := strings.Split(codestrings, ",")
codes := make([]codeAndSlices, len(aCodestrings))
for i, codestring := range aCodestrings {
codeAndSlice, err := validateCodeAndSlices(codestring)
if err != nil {
return []codeAndSlices{{http.StatusBadRequest, 1}}, err
}
codes[i] = codeAndSlice
}
return codes, nil
}
// code must be HTTP response code
func validateCodeAndSlices(codecount string) (codeAndSlices, error) {
flavor := strings.Split(codecount, ":")
// Demand code or code:number
if len(flavor) == 0 || len(flavor) > 2 {
return codeAndSlices{http.StatusBadRequest, 9999}, fmt.Errorf("invalid %q (want code or code:count)", codecount)
}
n, err := strconv.Atoi(flavor[0])
if err != nil {
return codeAndSlices{http.StatusBadRequest, 9999}, err
}
if n < http.StatusOK || n >= 600 {
return codeAndSlices{http.StatusBadRequest, 9999}, fmt.Errorf("invalid HTTP response code %v", n)
}
count := 1
if len(flavor) > 1 {
count, err = strconv.Atoi(flavor[1])
if err != nil {
return codeAndSlices{http.StatusBadRequest, 9999}, err
}
if count < 0 {
return codeAndSlices{http.StatusBadRequest, 9999}, fmt.Errorf("invalid count %v", count)
}
}
return codeAndSlices{n, count}, nil
}
// Client side
type job func(int, io.Writer) error
const (
hostKey = "Host"
)
// EchoClient controls the client
type EchoClient struct {
count int
timeout time.Duration
qps int
url string
headerKey string
headerVal string
msg string
caFile string
grpcConn *grpc.ClientConn
httpClient *http.Client
wsClient *websocket.Dialer
grpcClient echopb.EchoTestServiceClient
}
func (ec *EchoClient) makeHTTPRequest(i int, w io.Writer) error {
req, err := http.NewRequest("GET", ec.url, nil)
if err != nil {
return err
}
//log.Printf("[%d] Url=%s\n", i, ec.url)
if ec.headerKey == hostKey {
req.Host = ec.headerVal
//log.Printf("[%d] Host=%s\n", i, ec.headerVal)
} else if ec.headerKey != "" {
req.Header.Add(ec.headerKey, ec.headerVal)
//log.Printf("[%d] Header=%s:%s\n", i, ec.headerKey, ec.headerVal)
}
resp, err := ec.httpClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 && w != nil {
fmt.Fprintf(w, "[%d] StatusCode=%d\n", i, resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return err
}
for _, line := range strings.Split(string(data), "\n") {
if line != "" {
if w != nil {
fmt.Fprintf(w, "[%d body] %s\n", i, line)
}
}
}
return nil
}
func (ec *EchoClient) makeWebSocketRequest(i int, w io.Writer) error {
req := make(http.Header)
//log.Printf("[%d] Url=%s\n", i, ec.url)
if ec.headerKey == hostKey {
req.Add("Host", ec.headerVal)
//log.Printf("[%d] Host=%s\n", i, ec.headerVal)
} else if ec.headerKey != "" {
req.Add(ec.headerKey, ec.headerVal)
//log.Printf("[%d] Header=%s:%s\n", i, ec.headerKey, ec.headerVal)
}
//if ec.msg != "" {
// log.Printf("[%d] Body=%s\n", i, ec.msg)
//}
conn, _, err := ec.wsClient.Dial(ec.url, req)
if err != nil {
// timeout or bad handshake
return err
}
// nolint: errcheck
defer conn.Close()
err = conn.WriteMessage(websocket.TextMessage, []byte(ec.msg))
if err != nil {
return err
}
_, resp, err := conn.ReadMessage()
if err != nil {
return err
}
for _, line := range strings.Split(string(resp), "\n") {
if line != "" {
if w != nil {
fmt.Fprintf(w, "[%d body] %s\n", i, line)
}
}
}
return nil
}
func (ec *EchoClient) makeGRPCRequest(i int, w io.Writer) error {
req := &echopb.EchoRequest{Message: fmt.Sprintf("request #%d", i)}
//log.Printf("[%d] grpcecho.Echo(%v)\n", i, req)
resp, err := ec.grpcClient.Echo(context.Background(), req)
if err != nil {
return err
}
// when the underlying HTTP2 request returns status 404, GRPC
// request does not return an error in grpc-go.
// instead it just returns an empty response
for _, line := range strings.Split(resp.GetMessage(), "\n") {
if line != "" {
if w != nil {
fmt.Fprintf(w, "[%d body] %s\n", i, line)
}
}
}
return nil
}
// Initialize the http/ws/grpc client and return the function to call.
func (ec *EchoClient) setupDefaultTest() (job, error) {
if strings.HasPrefix(ec.url, "http://") || strings.HasPrefix(ec.url, "https://") {
/* #nosec */
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Timeout: ec.timeout,
}
ec.httpClient = client
return ec.makeHTTPRequest, nil
} else if strings.HasPrefix(ec.url, "grpc://") || strings.HasPrefix(ec.url, "grpcs://") {
secure := strings.HasPrefix(ec.url, "grpcs://")
var address string
if secure {
address = ec.url[len("grpcs://"):]
} else {
address = ec.url[len("grpc://"):]
}
// grpc-go sets incorrect authority header
authority := address
if ec.headerKey == hostKey {
authority = ec.headerVal
}
// transport security
security := grpc.WithInsecure()
if secure {
creds, err := credentials.NewClientTLSFromFile(ec.caFile, authority)
if err != nil {
return nil, err
}
security = grpc.WithTransportCredentials(creds)
}
var err error
ec.grpcConn, err = grpc.Dial(address,
security,
grpc.WithAuthority(authority),
grpc.WithBlock(),
grpc.WithTimeout(ec.timeout))
if err != nil {
return nil, err
}
client := echopb.NewEchoTestServiceClient(ec.grpcConn)
ec.grpcClient = client
return ec.makeGRPCRequest, nil
} else if strings.HasPrefix(ec.url, "ws://") || strings.HasPrefix(ec.url, "wss://") {
/* #nosec */
client := &websocket.Dialer{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
HandshakeTimeout: ec.timeout,
}
ec.wsClient = client
return ec.makeWebSocketRequest, nil
}
return nil, fmt.Errorf("invalid URL %s", ec.url)
}
func echoClientHandler(w http.ResponseWriter, r *http.Request) {
//flag.IntVar(&count, "count", 1, "Number of times to make the request")
//flag.IntVar(&qps, "qps", 0, "Queries per second")
//flag.DurationVar(&timeout, "timeout", 15*time.Second, "Request timeout")
//flag.StringVar(&url, "url", "", "Specify URL")
//flag.StringVar(&headerKey, "key", "", "Header key (use Host for authority)")
//flag.StrinproxyUrlgVar(&headerVal, "val", "", "Header value")
//flag.StringVar(&caFile, "ca", "/cert.crt", "CA root cert file")
//flag.StringVar(&msg, "msg", "HelloWorld", "message to send (for websockets)")
r.ParseForm()
// ca not yet supported
ec := &EchoClient{
url: r.FormValue("url"),
headerKey: r.FormValue("key"),
headerVal: r.FormValue("val"),
msg: r.FormValue("msg"),
timeout: 15 * time.Second,
}
qpsS := r.FormValue("qps")
if len(qpsS) > 0 {
ec.qps, _ = strconv.Atoi(qpsS)
}
countS := r.FormValue("count")
if len(qpsS) > 0 {
ec.count, _ = strconv.Atoi(countS)
}
j, err := ec.setupDefaultTest()
if j == nil {
fmt.Fprintf(w, "Error: %v", err)
w.WriteHeader(501)
return
}
defer func() {
if ec.grpcConn != nil {
if err1 := ec.grpcConn.Close(); err1 != nil {
log.Println(err1)
}
}
}()
if ec.qps == 0 {
// one request
err = j(0, w)
} else {
sleepTime := time.Second / time.Duration(ec.qps)
//log.Printf("Sleeping %v between requests\n", sleepTime)
throttle := time.Tick(sleepTime)
var wg sync.WaitGroup
var m sync.Mutex
for i := 0; i < ec.count; i++ {
<-throttle
wg.Add(1)
go func() {
defer wg.Done()
e := j(i, w)
if e != nil {
m.Lock()
err = e
m.Unlock()
}
}()
}
wg.Wait()
}
if err != nil {
fmt.Fprintf(w, "Error %s\n", err)
w.WriteHeader(500)
return
}
w.WriteHeader(200)
}