forked from fango6/proxyproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2.go
245 lines (210 loc) · 6.53 KB
/
v2.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
package proxyproto
import (
"bufio"
"bytes"
"encoding/binary"
"io"
"net"
"github.com/pkg/errors"
)
const (
// addressLengthIPv4 address length is 2*4 + 2*2 = 12 bytes.
addressLengthIPv4 = 12
// addressLengthIPv6 address length is 2*16 + 2*2 = 36 bytes.
addressLengthIPv6 = 36
// addressLengthUnix address length is 2*108 = 216 bytes.
addressLengthUnix = 216
)
var (
ErrUnknownVersionAndCommand = errors.New("pp2 unknown version and command")
ErrUnknownAddrFamilyAndTranProtocol = errors.New("pp2 unknown address family and transport protocol")
ErrPayloadLengthTooShort = errors.New("pp2 payload length is too short")
ErrPayloadBytesTooShort = errors.New("pp2 payload of bytes are too short")
)
// readAndParseV2 read and parse header of version 2.
func readAndParseV2(reader *bufio.Reader) (*Header, error) {
header, err := readV2(reader)
if err != nil {
return nil, err
}
if err := parseV2(header); err != nil {
return nil, err
}
return header, nil
}
// readV2 read header of version 2.
func readV2(reader *bufio.Reader) (*Header, error) {
if reader == nil {
return nil, errors.New("pp2 reader is nil")
}
var raw = make([]byte, len(v2Signature), len(v2Signature)+4)
n, err := reader.Read(raw[:len(v2Signature)])
if err != nil || n < len(v2Signature) || !bytes.Equal(raw, v2Signature) {
return nil, ErrNoProxyProtocol
}
// 13th byte: version and command
verAndCmd, err := reader.ReadByte()
if err != nil {
return nil, err
}
ver, cmd := Version(verAndCmd>>4), Command(verAndCmd&0x0F)
// reject all of unknown versions and commands
if ver != Version2 || cmd.String() == Unknown {
return nil, ErrUnknownVersionAndCommand
}
// 14th byte: address family and transport protocol
afAndTp, err := reader.ReadByte()
if err != nil {
return nil, err
}
af, tp := AddressFamily(afAndTp>>4), TransportProtocol(afAndTp&0x0F)
// reject all of unknown address family, and transport protocols
if af.String() == Unknown || tp.String() == Unknown {
return nil, ErrUnknownAddrFamilyAndTranProtocol
}
// 15~16th bytes: number of following bytes part of the header
var payloadLength uint16
if err := binary.Read(io.LimitReader(reader, 2), binary.BigEndian, &payloadLength); err != nil {
return nil, err
}
raw = append(raw, verAndCmd, afAndTp, byte(payloadLength>>8), byte(payloadLength))
header := &Header{Version: Version2, Command: cmd, AddressFamily: af, TransportProtocol: tp, Raw: raw}
// command Local
if payloadLength == 0 || header.Command == CMD_LOCAL {
header.Command = CMD_LOCAL
return header, nil
}
if err := validatePayloadLength(payloadLength, af); err != nil {
return nil, err
}
var payload = make([]byte, payloadLength)
n, err = reader.Read(payload)
if err != nil {
return nil, err
}
if n != int(payloadLength) {
return nil, ErrPayloadBytesTooShort
}
header.Raw = make([]byte, 0, len(v2Signature)+4+int(payloadLength))
header.Raw = append(header.Raw, raw...)
header.Raw = append(header.Raw, payload...)
return header, nil
}
// parseV2 parse header with Header
func parseV2(header *Header) error {
if header == nil {
return errors.New("pp2 header is nil")
}
if header.Command == CMD_LOCAL {
return nil
}
if len(header.Raw) <= len(v2Signature)+4 {
return errors.New("pp2 payload is empty")
}
var payload = header.Raw[len(v2Signature)+4:]
var err error
var srcAddr, dstAddr net.Addr
var rawTLVs []byte
switch header.AddressFamily {
case AF_INET: // IPv4
srcAddr, dstAddr, err = parseV2IPv4(payload, header.TransportProtocol)
if err != nil {
return err
}
rawTLVs = payload[addressLengthIPv4:]
case AF_INET6: // IPv6
srcAddr, dstAddr, err = parseV2IPv6(payload, header.TransportProtocol)
if err != nil {
return err
}
rawTLVs = payload[addressLengthIPv6:]
case AF_UNIX: // Unix
srcAddr, dstAddr, err = parseV2Unix(payload, header.TransportProtocol)
if err != nil {
return err
}
rawTLVs = payload[addressLengthUnix:]
default:
return ErrUnknownAddrFamilyAndTranProtocol
}
header.TLVs, err = parseTLVs(rawTLVs)
if err != nil {
return err
}
header.SrcAddr = srcAddr
header.DstAddr = dstAddr
return nil
}
func parseV2IPv4(payload []byte, tp TransportProtocol) (src, dst net.Addr, err error) {
if len(payload) < addressLengthIPv4 {
err = ErrPayloadBytesTooShort
return
}
srcIP := net.IPv4(payload[0], payload[1], payload[2], payload[3])
if err = validateIP(srcIP, AF_INET); err != nil {
return nil, nil, errors.Wrap(err, "source")
}
dstIP := net.IPv4(payload[4], payload[5], payload[6], payload[7])
if err = validateIP(dstIP, AF_INET); err != nil {
return nil, nil, errors.Wrap(err, "destination")
}
srcPort := int(binary.BigEndian.Uint16(payload[8:10]))
if err = validatePort(srcPort); err != nil {
return nil, nil, errors.Wrap(err, "source")
}
dstPort := int(binary.BigEndian.Uint16(payload[10:addressLengthIPv4]))
if err = validatePort(dstPort); err != nil {
return nil, nil, errors.Wrap(err, "destination")
}
if tp == SOCK_DGRAM {
src = &net.UDPAddr{IP: srcIP, Port: srcPort}
dst = &net.UDPAddr{IP: dstIP, Port: dstPort}
return
}
src = &net.TCPAddr{IP: srcIP, Port: srcPort}
dst = &net.TCPAddr{IP: dstIP, Port: dstPort}
return
}
func parseV2IPv6(payload []byte, tp TransportProtocol) (src, dst net.Addr, err error) {
if len(payload) < addressLengthIPv6 {
err = ErrPayloadBytesTooShort
return
}
srcIP := net.IP(payload[:16])
if err = validateIP(srcIP, AF_INET6); err != nil {
return nil, nil, errors.Wrap(err, "source")
}
dstIP := net.IP(payload[16:32])
if err = validateIP(dstIP, AF_INET6); err != nil {
return nil, nil, errors.Wrap(err, "destination")
}
srcPort := int(binary.BigEndian.Uint16(payload[32:34]))
if err = validatePort(srcPort); err != nil {
return nil, nil, errors.Wrap(err, "source")
}
dstPort := int(binary.BigEndian.Uint16(payload[34:addressLengthIPv6]))
if err = validatePort(dstPort); err != nil {
return nil, nil, errors.Wrap(err, "destination")
}
if tp == SOCK_DGRAM {
src = &net.UDPAddr{IP: srcIP, Port: srcPort}
dst = &net.UDPAddr{IP: dstIP, Port: dstPort}
return
}
src = &net.TCPAddr{IP: srcIP, Port: srcPort}
dst = &net.TCPAddr{IP: dstIP, Port: dstPort}
return
}
func parseV2Unix(payload []byte, tp TransportProtocol) (src, dst net.Addr, err error) {
if len(payload) < addressLengthUnix {
err = ErrPayloadBytesTooShort
return
}
var network = "unix"
if tp == SOCK_DGRAM {
network = "unixgram"
}
src = &net.UnixAddr{Net: network, Name: parseUnixName(payload[:108])}
dst = &net.UnixAddr{Net: network, Name: parseUnixName(payload[108:addressLengthUnix])}
return
}