This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhello.go
166 lines (134 loc) · 4.05 KB
/
hello.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
package spoe
import (
"fmt"
"strconv"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const (
helloKeyMaxFrameSize = "max-frame-size"
helloKeySupportedVersions = "supported-versions"
helloKeyVersion = "version"
helloKeyCapabilities = "capabilities"
helloKeyHealthcheck = "healthcheck"
helloKeyEngineID = "engine-id"
capabilityAsync = "async"
capabilityPipelining = "pipelining"
)
func (c *conn) handleHello(frame Frame) (Frame, map[string]bool, bool, error) {
data, _, err := decodeKVs(frame.data, -1)
if err != nil {
return frame, nil, false, errors.Wrap(err, "hello")
}
log.Debugf("spoe: hello from %s: %+v", c.Conn.RemoteAddr(), data)
remoteFrameSize, ok := data[helloKeyMaxFrameSize].(uint)
if !ok {
return frame, nil, false, fmt.Errorf("hello: expected %s", helloKeyMaxFrameSize)
}
connFrameSize := remoteFrameSize
if connFrameSize > maxFrameSize {
connFrameSize = maxFrameSize
}
c.frameSize = int(connFrameSize)
remoteSupportedVersions, ok := data[helloKeySupportedVersions].(string)
if !ok {
return frame, nil, false, fmt.Errorf("hello: expected %s", helloKeyVersion)
}
versionOK := false
for _, supportedVersion := range strings.Split(remoteSupportedVersions, ",") {
remoteVersion, err := parseVersion(supportedVersion)
if err != nil {
return frame, nil, false, errors.Wrap(err, "hello")
}
if remoteVersion[0] == 2 {
versionOK = true
}
}
if !versionOK {
return frame, nil, false, fmt.Errorf("hello: incompatible version %s, need %s", remoteSupportedVersions, version)
}
healthcheck, _ := data[helloKeyHealthcheck].(bool)
remoteCapabilitiesStr, ok := data[helloKeyCapabilities].(string)
if !ok {
return frame, nil, false, fmt.Errorf("hello: expected %s", helloKeyCapabilities)
}
remoteCapabilities := parseCapabilities(remoteCapabilitiesStr)
if !remoteCapabilities[capabilityPipelining] && !healthcheck {
// HAProxy never sends pipelining capability on healthcheck hellos
return frame, nil, false, fmt.Errorf("hello: expected pipelining capability")
}
c.engineID, _ = data[helloKeyEngineID].(string)
if len(c.engineID) == 0 && !healthcheck {
// HAProxy never sends engine-id on healthcheck hellos
return frame, nil, false, fmt.Errorf("hello: engine-id not found")
}
frame.ftype = frameTypeAgentHello
frame.flags = frameFlagFin
frame.data = frame.originalData
off := 0
n, err := encodeKV(frame.data[off:], helloKeyVersion, version)
if err != nil {
return frame, nil, false, errors.Wrap(err, "hello")
}
off += n
n, err = encodeKV(frame.data[off:], helloKeyMaxFrameSize, connFrameSize)
if err != nil {
return frame, nil, false, errors.Wrap(err, "hello")
}
off += n
localCapabilities := []string{capabilityPipelining}
if remoteCapabilities[capabilityAsync] {
localCapabilities = append(localCapabilities, capabilityAsync)
}
n, err = encodeKV(frame.data[off:], helloKeyCapabilities, strings.Join(localCapabilities, ","))
if err != nil {
return frame, nil, false, errors.Wrap(err, "hello")
}
off += n
frame.data = frame.data[:off]
return frame, remoteCapabilities, healthcheck, nil
}
func parseCapabilities(capas string) map[string]bool {
caps := map[string]bool{}
for _, s := range strings.Split(capas, ",") {
caps[s] = true
}
return caps
}
func checkCapabilities(capas string) bool {
hasPipelining := false
for _, s := range strings.Split(capas, ",") {
switch s {
case capabilityPipelining:
hasPipelining = true
}
}
return hasPipelining
}
func parseVersion(v string) ([]int, error) {
res := []int{}
v = strings.TrimSpace(v)
s, e := 0, 0
for i := 0; i <= len(v); i++ {
if i == len(v) || v[i] == '.' {
n, err := strconv.Atoi(v[s:e])
if err != nil {
return nil, errors.Wrap(err, "version parse")
}
res = append(res, n)
s = i + 1
e = s
continue
}
if v[i] >= '0' && v[i] <= '9' {
e++
continue
}
return nil, fmt.Errorf("version parse: unexpected char %s", string(v[i]))
}
if len(res) == 0 {
return nil, fmt.Errorf("version parse: expected at least one digit")
}
return res, nil
}