forked from fango6/proxyproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2_test.go
236 lines (224 loc) · 7.83 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
package proxyproto
import (
"bufio"
"io"
"net"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
var readAndParseV2Tests = []struct {
name string
raw string
want *Header
}{
{
name: "local-command",
raw: ("\r\n\r\n\x00\r\nQUIT\n" + // version 2 signature
"\x20" + // version 2, local command
"\x11" + // IPv4, TCP
"\x00\x00"), // payload length of zero
want: &Header{
Version: Version2,
Command: CMD_LOCAL,
AddressFamily: AF_INET,
TransportProtocol: SOCK_STREAM,
},
}, {
name: "proxy-command-IPv4",
raw: ("\r\n\r\n\x00\r\nQUIT\n" + // version 2 signature
"\x21\x11\x00\x0C" + // version 2, proxy command, IPv4, TCP, payload length of 12
"\x7F\x00\x00\x01" + // source ip is 127.0.0.1
"\x7F\x00\x00\x01" + // destination ip is 127.0.0.1
"\x30\x39\xDD\xD5"), // source port is 12345, destination port is 56789
want: &Header{
Version: Version2,
Command: CMD_PROXY,
AddressFamily: AF_INET,
TransportProtocol: SOCK_STREAM,
SrcAddr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12345},
DstAddr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 56789},
},
}, {
name: "proxy-command-IPv6",
raw: ("\r\n\r\n\x00\r\nQUIT\n" + // version 2 signature
"\x21\x21\x00\x24" + // version 2, proxy command, IPv6, TCP, payload length of 36
"\x00\x7F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" + // source ip is 127:0:0:0:0:0:0:1
"\x00\x7F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01" + // destination ip is 127:0:0:0:0:0:0:1
"\x30\x39\xDD\xD5"), // source port is 12345, destination port is 56789
want: &Header{
Version: Version2,
Command: CMD_PROXY,
AddressFamily: AF_INET6,
TransportProtocol: SOCK_STREAM,
SrcAddr: &net.TCPAddr{
IP: net.IP([]byte("\x00\x7F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")),
Port: 12345,
},
DstAddr: &net.TCPAddr{
IP: net.IP([]byte("\x00\x7F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")),
Port: 56789,
},
},
}, {
name: "tlv-groups",
raw: ("\r\n\r\n\x00\r\nQUIT\n" + // version 2 signature
"\x21\x11\x00\x3C" + // version 2, proxy command, IPv4, TCP, payload length of 60
"\x7F\x00\x00\x01\x7F\x00\x00\x01" + // source and destination ips are 127.0.0.1
"\x30\x39\xDD\xD5" + // source port is 12345, destination port is 56789
"\xEA\x00\x22vcpe-abcdefg-hijklmn-opqrst-uvwxyz" + // type:234, length:34, value:vcpe-abcdefg-hijklmn-opqrst-uvwxyz
"\x04\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00"), // type:PP2_TYPE_NOOP, length:8, value:"\x00\x00\x00\x00\x00\x00\x00\x00"
want: &Header{
Version: Version2,
Command: CMD_PROXY,
AddressFamily: AF_INET,
TransportProtocol: SOCK_STREAM,
SrcAddr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12345},
DstAddr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 56789},
TLVs: TLVs{
{Type: 234, Length: 34, Value: []byte("vcpe-abcdefg-hijklmn-opqrst-uvwxyz")},
{Type: 4, Length: 8, Value: []byte("\x00\x00\x00\x00\x00\x00\x00\x00")},
},
},
}, {
name: "tlv-crc32c-group",
raw: ("\r\n\r\n\x00\r\nQUIT\n" + // v2 signature
"\x21\x11\x00\x43" + // version 2, proxy command, IPv4, TCP, payload length of 67
"\x7F\x00\x00\x01\x7F\x00\x00\x01" + // source and destination ips are 127.0.0.1
"\x30\x39\xDD\xD5" + // source port is 12345, destination port is 56789
"\xEA\x00\x22vcpe-abcdefg-hijklmn-opqrst-uvwxyz" + // type:234, length:34, value:vcpe-abcdefg-hijklmn-opqrst-uvwxyz
"\x03\x00\x04\x13\x49\xCA\x53" + // type:PP2_TYPE_CRC32C, length:4, value:"\x13\x49\xCA\x53"
"\x04\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00"), // type:PP2_TYPE_NOOP, length:8, value:"\x00\x00\x00\x00\x00\x00\x00\x00"
want: &Header{
Version: Version2,
Command: CMD_PROXY,
AddressFamily: AF_INET,
TransportProtocol: SOCK_STREAM,
SrcAddr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12345},
DstAddr: &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 56789},
TLVs: TLVs{
{Type: 234, Length: 34, Value: []byte("vcpe-abcdefg-hijklmn-opqrst-uvwxyz")},
{Type: 3, Length: 4, Value: []byte("\x13\x49\xCA\x53")},
{Type: 4, Length: 8, Value: []byte("\x00\x00\x00\x00\x00\x00\x00\x00")},
},
},
},
}
// Test_readAndParseV2 want success
func Test_readAndParseV2(t *testing.T) {
for _, tt := range readAndParseV2Tests {
t.Run(tt.name, func(t *testing.T) {
tt.want.Raw = []byte(tt.raw)
reader := bufio.NewReader(strings.NewReader(tt.raw))
got, err := readAndParseV2(reader)
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
t.Run("proxy-command-Unix", func(t *testing.T) {
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(dir)
require.NoError(t, err)
})
var namePrefix = filepath.Join(dir, "sock")
var name = formatUnixName(namePrefix)
var raw = "\r\n\r\n\x00\r\nQUIT\n" + // version signature
"\x21\x31\x00\xD8" + // version 2, proxy, tcp, 216
name + name
var want = &Header{
Version: Version2,
Command: CMD_PROXY,
AddressFamily: AF_UNIX,
TransportProtocol: SOCK_STREAM,
SrcAddr: &net.UnixAddr{Name: namePrefix, Net: "unix"},
DstAddr: &net.UnixAddr{Name: namePrefix, Net: "unix"},
Raw: []byte(raw),
}
gotHeader, err := readAndParseV2(bufio.NewReader(strings.NewReader(raw)))
require.NoError(t, err)
require.Equal(t, want, gotHeader)
})
}
var readV2Tests = []struct {
name string
raw string
wantErr error
}{
{
name: "not pp2 header",
raw: "\t\n\r\n\x00\r\nQUIT\t",
wantErr: ErrNoProxyProtocol,
}, {
name: "invalid version",
raw: "\r\n\r\n\x00\r\nQUIT\n" + "\x51",
wantErr: ErrUnknownVersionAndCommand,
}, {
name: "unknown command",
raw: "\r\n\r\n\x00\r\nQUIT\n" + "\x25",
wantErr: ErrUnknownVersionAndCommand,
}, {
name: "unknown address family",
raw: "\r\n\r\n\x00\r\nQUIT\n" + "\x21\x51",
wantErr: ErrUnknownAddrFamilyAndTranProtocol,
}, {
name: "unknown transport protocol",
raw: "\r\n\r\n\x00\r\nQUIT\n" + "\x21\x15",
wantErr: ErrUnknownAddrFamilyAndTranProtocol,
}, {
name: "payload length is unexpected",
raw: "\r\n\r\n\x00\r\nQUIT\n" + "\x21\x11\x01",
wantErr: io.ErrUnexpectedEOF,
}, {
name: "payload is missing",
raw: "\r\n\r\n\x00\r\nQUIT\n" + "\x21\x11\x00\x0F",
wantErr: io.EOF,
}, {
name: "payload length invalid",
raw: "\r\n\r\n\x00\r\nQUIT\n" + "\x21\x11\x00\x0A",
wantErr: ErrPayloadLengthTooShort,
}, {
name: "payload too short",
raw: ("\r\n\r\n\x00\r\nQUIT\n" +
"\x21\x11\x00\x0C" +
"\x7F\x00\x00\x01\x7F\x00\x00\x01\x04\xD2"),
wantErr: ErrPayloadBytesTooShort,
},
}
func Test_readV2(t *testing.T) {
for _, tt := range readV2Tests {
t.Run(tt.name, func(t *testing.T) {
reader := bufio.NewReader(strings.NewReader(tt.raw))
_, err := readV2(reader)
require.EqualError(t, err, tt.wantErr.Error())
})
}
}
func Test_parseV2(t *testing.T) {
t.Run("payload is empty", func(t *testing.T) {
header := &Header{
Version: Version2,
Command: CMD_PROXY,
AddressFamily: AF_INET,
TransportProtocol: SOCK_STREAM,
Raw: []byte("\r\n\r\n\x00\r\nQUIT\n\x21\x11\x00\x0C"),
}
err := parseV2(header)
require.EqualError(t, err, "pp2 payload is empty")
})
t.Run("unknown address family", func(t *testing.T) {
header := &Header{
Version: Version2,
Command: CMD_PROXY,
AddressFamily: AF_UNSPEC,
TransportProtocol: SOCK_STREAM,
Raw: []byte("\r\n\r\n\x00\r\nQUIT\n\x21\x01\x00\x0C" +
"\x7F\x00\x00\x01\x7F\x00\x00\x01\x30\x39\xDD\xD5"),
}
err := parseV2(header)
require.EqualError(t, err, ErrUnknownAddrFamilyAndTranProtocol.Error())
})
}