-
Notifications
You must be signed in to change notification settings - Fork 11
/
v2_test.go
280 lines (267 loc) · 11.6 KB
/
v2_test.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
package proxyproto_test
import (
"bytes"
"net"
"testing"
"github.com/mailgun/influx/proxyproto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseV2Header(t *testing.T) {
tests := []struct {
name string
header []byte
src net.Addr
dest net.Addr
rawTLVs []byte
isLocal bool
err string
}{
{
name: "TCP4 127.0.0.1",
// VER IP/TCP LENGTH
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x11, 0x00, 0x0C,
// IPV4 -------------| IPV4 ----------------| SRC PORT DEST PORT
0x7F, 0x00, 0x00, 0x01, 0x7F, 0x00, 0x00, 0x01, 0xCA, 0x2B, 0x04, 0x01},
src: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51755},
dest: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 1025},
},
{
name: "UDP4 127.0.0.1",
// IP/UDP
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x12, 0x00, 0x0C,
0x7F, 0x00, 0x00, 0x01, 0x7F, 0x00, 0x00, 0x01, 0xCA, 0x2B, 0x04, 0x01},
src: &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51755},
dest: &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 1025},
},
{
name: "TCP6 Proxy for TCP4 127.0.0.1",
// VER IP/TCP LENGTH
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x00, 0x24,
// IPV6 -------------------------------------------------------------------------------------|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01,
// IPV6 -------------------------------------------------------------------------------------| SRC PORT DEST PORT
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01, 0xCC, 0x4C, 0x04, 0x01},
src: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 52300},
dest: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 1025},
},
{
name: "TCP6 Maximal",
// VER IP/TCP LENGTH
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x00, 0x24,
// IPV6 -------------------------------------------------------------------------------------|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
// IPV6 -------------------------------------------------------------------------------------| SRC PORT DEST PORT
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
src: &net.TCPAddr{IP: net.ParseIP("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"), Port: 65535},
dest: &net.TCPAddr{IP: net.ParseIP("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"), Port: 65535},
},
{
name: "TCP6 Proxy for TCP6 ::1",
// VER IP/TCP LENGTH
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x00, 0x2B,
// IPV6 -------------------------------------------------------------------------------------|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
// IPV6 -------------------------------------------------------------------------------------| SRC PORT DEST PORT
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xCF, 0x8F, 0x04, 0x01,
//TLVs
0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60},
src: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 53135},
dest: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 1025},
rawTLVs: []byte{0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60},
},
{
name: "UDP6 Proxy for UDP6 ::1",
// VER IP/TCP LENGTH
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x22, 0x00, 0x2B,
// IPV6 -------------------------------------------------------------------------------------|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
// IPV6 -------------------------------------------------------------------------------------| SRC PORT DEST PORT
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xCF, 0x8F, 0x04, 0x01,
//TLVs
0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60},
src: &net.UDPAddr{IP: net.ParseIP("::1"), Port: 53135},
dest: &net.UDPAddr{IP: net.ParseIP("::1"), Port: 1025},
rawTLVs: []byte{0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60},
},
{
name: "Missing Proto/Family/Length",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21},
err: "while parsing proxy proto v2 header: while reading proto, family and length bytes: EOF",
},
{
name: "Invalid version",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x01, 0x21, 0x00, 0x2B},
err: "while parsing proxy proto v2 header: unexpected version number '0' at pos '13'",
},
{
name: "Invalid length too long",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x08, 0x01},
err: "while parsing proxy proto v2 header: header lengh of '2049' is greater than the allowed 2048 bytes",
},
{
name: "Not enough bytes",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x08, 0x00},
err: "while parsing proxy proto v2 header: while reading proto and length bytes: EOF",
},
{
name: "Local with no trailing bytes",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x20, 0x00, 0x00, 0x00},
isLocal: true,
},
{
name: "Local with trailing bytes (TLVs)",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x20, 0xFF, 0x00, 0x07,
0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60},
rawTLVs: []byte{0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60},
isLocal: true,
},
{
name: "Proxy with zero byte header",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x00, 0x00, 0x00},
err: "while parsing proxy proto v2 header: expected address but got zero length header",
},
{
name: "Invalid length for IPV4",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x11, 0x00, 0x01, 0xFF},
err: "while parsing proxy proto v2 header: expected 12 bytes for IPV4 address",
},
{
name: "Invalid length for IPV6",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x00, 0x01, 0xFF},
err: "while parsing proxy proto v2 header: expected 36 bytes for IPV6 address",
},
{
name: "Unix Socket Not Implemented",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x31, 0x00, 0x01, 0xFF},
err: "while parsing proxy proto v2 header: Received UNIX socket proxy command, Currently not supported",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := bytes.NewReader(tt.header)
h, err := proxyproto.ReadHeader(r)
if err != nil {
require.Equal(t, err.Error(), tt.err)
return
}
require.NotNil(t, h)
assert.Equal(t, tt.dest, h.Destination)
assert.Equal(t, tt.src, h.Source)
assert.Equal(t, tt.rawTLVs, h.RawTLVs)
assert.Equal(t, 2, h.Version)
assert.Equal(t, tt.isLocal, h.IsLocal)
})
}
}
func TestReadV2Header(t *testing.T) {
// VER IP/TCP LENGTH
header := []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x11, 0x00, 0x0C,
// IPV4 -------------| IPV4 ----------------| SRC PORT DEST PORT
0x7F, 0x00, 0x00, 0x01, 0x7F, 0x00, 0x00, 0x01, 0xCA, 0x2B, 0x04, 0x01}
r := bytes.NewReader(header)
h, err := proxyproto.ReadV2Header(r)
require.NoError(t, err)
require.NotNil(t, h)
assert.Equal(t, &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 1025}, h.Destination)
assert.Equal(t, &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 51755}, h.Source)
assert.Equal(t, 2, h.Version)
assert.Equal(t, false, h.IsLocal)
}
func TestHeader_ParseTLVs(t *testing.T) {
tests := []struct {
header proxyproto.Header
m map[byte][]byte
name string
err string
}{
{
name: "CRC",
header: proxyproto.Header{RawTLVs: []byte{0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60}},
m: map[byte][]byte{
0x03: {0xFD, 0x16, 0xEE, 0x60},
},
},
{
name: "CRC and NoOp",
header: proxyproto.Header{RawTLVs: []byte{
0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60,
0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
}},
m: map[byte][]byte{
0x03: {0xFD, 0x16, 0xEE, 0x60},
0x04: {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
},
},
{
name: "Length to long",
header: proxyproto.Header{RawTLVs: []byte{
0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
}},
err: "TLV '0x4' length '6' is larger than trailing header",
},
{
name: "Zero Length",
header: proxyproto.Header{RawTLVs: []byte{
0x04, 0x00, 0x00,
0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60,
}},
m: map[byte][]byte{
0x03: {0xFD, 0x16, 0xEE, 0x60},
0x04: {},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m, err := tt.header.ParseTLVs()
if err != nil {
require.Equal(t, err.Error(), tt.err)
return
}
assert.Equal(t, tt.m, m)
})
}
}
func BenchmarkReadHeaderV2(b *testing.B) {
tests := []struct {
name string
header []byte
}{
{
name: "TCP6-With-TLVs",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x00, 0x2B,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xCF, 0x8F, 0x04, 0x01,
0x03, 0x00, 0x04, 0xFD, 0x16, 0xEE, 0x60},
},
{
name: "TCP6-Minimal",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x00, 0x24,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x01, 0xCC, 0x4C, 0x04, 0x01},
},
{
name: "TCP6-Maximal",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x21, 0x00, 0x24,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
},
{
name: "TCP4-Minimal",
header: []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A, 0x21, 0x11, 0x00, 0x0C,
0x7F, 0x00, 0x00, 0x01, 0x7F, 0x00, 0x00, 0x01, 0xCA, 0x2B, 0x04, 0x01},
},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
r := bytes.NewReader(tt.header)
_, err := proxyproto.ReadHeader(r)
if err != nil {
b.Errorf("ReadHeader err: %s", err)
}
}
})
}
}