-
Notifications
You must be signed in to change notification settings - Fork 6
/
handshake.go
192 lines (153 loc) · 4.1 KB
/
handshake.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
package protocols
import (
"bytes"
"crypto/md5"
"fmt"
"github.com/linkedin/goavro/v2"
"github.com/sirupsen/logrus"
)
func getMD5(str string) []byte {
sum := md5.Sum([]byte(str))
return sum[:]
}
type HandshakeProtocol interface {
PrepareRequest() ([]byte, error)
ProcessResponse(responseBytes []byte) (bool, error)
}
// The Avro Handshake implementation for the Avro RPC protocol.
//
// It is used for establishing a stateful connection between a client and a server.
//
// See http:https://avro.apache.org/docs/1.8.2/spec.html#handshake for details.
type handshakeProtocol struct {
logger *logrus.Entry
proto MessageProtocol
serverHash []byte
clientHash []byte
clientProtocol string
needClientProtocol bool
handshakeRequestCodec *goavro.Codec
handshakeResponseCodec *goavro.Codec
}
func NewHandshake(proto MessageProtocol) (HandshakeProtocol, error) {
m := proto.GetSchema()
p := &handshakeProtocol{
proto: proto,
serverHash: getMD5(m),
clientHash: getMD5(m),
clientProtocol: m,
}
p.logger = logrus.WithFields(logrus.Fields{
"name": "AvroHandshakeProtocol",
})
p.logger.Debug("created")
err := p.init()
if err != nil {
return nil, err
}
return p, nil
}
func (p *handshakeProtocol) init() (err error) {
p.handshakeRequestCodec, err = goavro.NewCodec(handshakeRequestSchema)
if err != nil {
return
}
p.handshakeResponseCodec, err = goavro.NewCodec(handshakeResponseSchema)
if err != nil {
return
}
return
}
func (p *handshakeProtocol) PrepareRequest() ([]byte, error) {
request := make(map[string]interface{})
request["meta"] = nil
request["clientHash"] = p.clientHash
request["serverHash"] = p.serverHash
if !p.needClientProtocol {
request["clientProtocol"] = nil
} else {
request["clientProtocol"] = map[string]interface{}{
"string": p.clientProtocol,
}
}
requestBytes, err := p.handshakeRequestCodec.BinaryFromNative(nil, request)
if err != nil {
return nil, err
}
emptyMessage := []byte{0, 0}
buf := bytes.NewBuffer(requestBytes)
buf.Write(emptyMessage)
return buf.Bytes(), nil
}
func (p *handshakeProtocol) ProcessResponse(responseBytes []byte) (bool, error) {
response, _, err := p.handshakeResponseCodec.NativeFromBinary(responseBytes)
if err != nil {
return false, err
}
responseMap, ok := response.(map[string]interface{})
if !ok {
return false, fmt.Errorf("cannot convert handshake response: %v", responseMap)
}
match := responseMap["match"]
serverHash := responseMap["serverHash"]
serverProtocol := responseMap["serverProtocol"]
switch match {
case "BOTH":
p.logger.Debug("handshake is successful")
if serverHash != nil {
p.logger.Warn("unexpected server's hash")
}
if serverProtocol != nil {
p.logger.Warn("unexpected server's protocol")
}
case "NONE":
p.logger.Debug("unknown client's protocol")
err := p.setServerHash(serverHash)
if err != nil {
return false, err
}
if p.needClientProtocol {
return false, fmt.Errorf("handshake failed: unknown client's protocol")
} else {
p.needClientProtocol = true
}
return true, nil
case "CLIENT":
p.logger.Debug("update server's protocol")
if serverHash == nil {
p.logger.Warn("expected server's hash but got nil")
}
if serverProtocol == nil {
p.logger.Warn("expected server's protocol but got nil")
}
if p.needClientProtocol {
return false, fmt.Errorf("handshake failed: unknown client's protocol")
}
err := p.setServerHash(serverHash)
if err != nil {
return false, err
}
default:
return false, fmt.Errorf("unknown handshake response match field: %v", match)
}
return false, nil
}
func (p *handshakeProtocol) setServerHash(serverHash interface{}) error {
if serverHash == nil {
return nil
}
serverHashMap, ok := serverHash.(map[string]interface{})
if !ok {
return fmt.Errorf("cannot convert hash to map: %v", serverHashMap)
}
md5Int, ok := serverHashMap["org.apache.avro.ipc.MD5"]
if !ok {
return fmt.Errorf("MD5 not found in map: %v", serverHashMap)
}
md5Bytes, ok := md5Int.([]byte)
if !ok {
return fmt.Errorf("cannot convert MD5 to byte array: %v", md5Int)
}
p.serverHash = md5Bytes
return nil
}