/
interop.go
66 lines (57 loc) · 1.17 KB
/
interop.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
package protocol
import (
"encoding/hex"
"fmt"
"io"
"github.com/kevwan/tproxy/display"
)
const (
ServerSide = "SERVER"
ClientSide = "CLIENT"
bufferSize = 1 << 20
grpcProtocol = "grpc"
http2Protocol = "http2"
redisProtocol = "redis"
mongoProtocol = "mongo"
mqttProtocol = "mqtt"
)
var interop defaultInterop
type Interop interface {
Dump(r io.Reader, source string, id int, quiet bool)
}
func CreateInterop(protocol string) Interop {
switch protocol {
case grpcProtocol:
return &http2Interop{
explainer: new(grpcExplainer),
}
case http2Protocol:
return new(http2Interop)
case redisProtocol:
return new(redisInterop)
case mongoProtocol:
return new(mongoInterop)
case mqttProtocol:
return new(mqttInterop)
default:
return interop
}
}
type defaultInterop struct{}
func (d defaultInterop) Dump(r io.Reader, source string, id int, quiet bool) {
data := make([]byte, bufferSize)
for {
n, err := r.Read(data)
if n > 0 && !quiet {
display.PrintfWithTime("from %s [%d]:\n", source, id)
fmt.Println(hex.Dump(data[:n]))
}
if err != nil && err != io.EOF {
fmt.Printf("unable to read data %v", err)
break
}
if n == 0 {
break
}
}
}