forked from tolsen/mongonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connwrapper.go
336 lines (275 loc) · 6.8 KB
/
connwrapper.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package mongonet
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
)
var (
v1Signature = []byte{0x50, 0x52, 0x4F, 0x58, 0x59}
v2Signature = []byte{0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A}
)
type Conn struct {
wrapped net.Conn
rbuf *bufio.Reader
version byte
proxy net.Addr
remote net.Addr
target net.Addr
}
func NewConn(wrapped net.Conn) (*Conn, error) {
c := &Conn{
wrapped: wrapped,
rbuf: bufio.NewReader(wrapped),
}
if err := c.init(); err != nil {
return nil, err
}
return c, nil
}
// Close implements the io.Closer interface.
func (c *Conn) Close() error {
return c.wrapped.Close()
}
// IsProxied returns true if the PROXY PROTOCOL was used.
func (c *Conn) IsProxied() bool {
return c.proxy != nil
}
// LocalAddr implements the net.Conn interface.
func (c *Conn) LocalAddr() net.Addr {
return c.wrapped.LocalAddr()
}
// ProxyAddr returns the proxy server's addr if IsProxied() is true; otherwise nil.
func (c *Conn) ProxyAddr() net.Addr {
return c.proxy
}
// Read implements the io.Reader interface.
func (c *Conn) Read(p []byte) (int, error) {
return c.rbuf.Read(p)
}
// RemoteAddr implements the net.Conn interface.
func (c *Conn) RemoteAddr() net.Addr {
return c.remote
}
// TargetAddr returns the client's intended target addr if IsProxied() is true; otherwise nil.
func (c *Conn) TargetAddr() net.Addr {
return c.target
}
// SetDeadline implements the net.Conn interface.
func (c *Conn) SetDeadline(t time.Time) error {
return c.wrapped.SetDeadline(t)
}
// SetReadDeadline implements the net.Conn interface.
func (c *Conn) SetReadDeadline(t time.Time) error {
return c.wrapped.SetReadDeadline(t)
}
// SetWriteDeadline implements the net.Conn interface.
func (c *Conn) SetWriteDeadline(t time.Time) error {
return c.wrapped.SetWriteDeadline(t)
}
// Version returns the version of the PROXY PROTOCOL.
func (c *Conn) Version() byte {
return c.version
}
// Write implements the io.Writer interface.
func (c *Conn) Write(p []byte) (int, error) {
return c.wrapped.Write(p)
}
func (c *Conn) init() error {
if c.wrapped == nil {
return fmt.Errorf("init a nil connection")
}
c.remote = c.wrapped.RemoteAddr()
// check if the first byte is one of our recognized signatures
if b1, err := c.rbuf.Peek(1); err == nil && len(b1) > 0 && (b1[0] == v1Signature[0] || b1[0] == v2Signature[0]) {
if sig, err := c.rbuf.Peek(5); err == nil && bytes.Equal(sig, v1Signature) {
return c.initv1()
}
if sig, err := c.rbuf.Peek(12); err == nil && bytes.Equal(sig, v2Signature) {
return c.initv2()
}
}
// not a proxy connection
return nil
}
func (c *Conn) initv1() error {
c.version = 1
line, _ := c.rbuf.ReadString('\n')
if !strings.HasSuffix(line, "\r\n") {
return errors.New("invalid header")
}
parts := strings.Split(line[:len(line)-2], " ")
if len(parts) < 6 {
if len(parts) > 1 && parts[1] == "UNKNOWN" {
return nil
}
return errors.New("invalid header")
}
var srcIP net.IP
var dstIP net.IP
switch parts[1] {
case "TCP4":
if srcIP = net.ParseIP(parts[2]).To4(); srcIP == nil {
return errors.New("invalid ip address")
}
if dstIP = net.ParseIP(parts[3]).To4(); dstIP == nil {
return errors.New("invalid ip address")
}
case "TCP6":
if srcIP = net.ParseIP(parts[2]).To16(); srcIP == nil {
return errors.New("invalid ip address")
}
if dstIP = net.ParseIP(parts[3]).To16(); dstIP == nil {
return errors.New("invalid ip address")
}
case "UNKNOWN":
return nil
default:
return errors.New("invalid protocol and family")
}
srcPort, err := parsePort(parts[4])
if err != nil {
return err
}
dstPort, err := parsePort(parts[5])
if err != nil {
return err
}
c.remote = &net.TCPAddr{
IP: srcIP,
Port: srcPort,
}
c.target = &net.TCPAddr{
IP: dstIP,
Port: dstPort,
}
c.proxy = c.wrapped.RemoteAddr()
return nil
}
func (c *Conn) initv2() error {
header := make([]byte, 16)
_, err := io.ReadFull(c.rbuf, header)
if err != nil {
return fmt.Errorf("failed reading header: %w", err)
}
if !bytes.Equal(header[:12], v2Signature) {
return errors.New("v2 header does not match signature")
}
vac := versionAndCommand(header[12])
if vac.Version() != 2 {
return errors.New("invalid version")
} else if vac.Command() != local && vac.Command() != proxy {
return errors.New("invalid command")
}
c.version = vac.Version()
addrProto := addressFamilyAndProtocol(header[13])
length := int64(binary.BigEndian.Uint16(header[14:16]))
payload := make([]byte, length)
_, err = io.ReadFull(c.rbuf, payload)
if err != nil {
return fmt.Errorf("failed reading payload: %w", err)
}
if vac.Command() == local {
// we can ignore everything else
return nil
}
var srcIP net.IP
var dstIP net.IP
var srcPort uint16
var dstPort uint16
switch addrProto.AddressFamily() {
case inet:
if len(payload) < 12 {
return errors.New("invalid IPv4 payload")
}
srcIP = net.IPv4(payload[0], payload[1], payload[2], payload[3])
dstIP = net.IPv4(payload[4], payload[5], payload[6], payload[7])
srcPort = binary.BigEndian.Uint16(payload[8:10])
dstPort = binary.BigEndian.Uint16(payload[10:12])
case inet6:
if len(payload) < 36 {
return errors.New("invalid IPv6 payload")
}
srcIP = net.IP(payload[:16])
dstIP = net.IP(payload[16:32])
srcPort = binary.BigEndian.Uint16(payload[32:34])
dstPort = binary.BigEndian.Uint16(payload[34:36])
case unix:
return errors.New("unix sockets are not supported")
case unspec:
// ignore
default:
return errors.New("invalid address family")
}
switch addrProto.Protocol() {
case stream:
c.remote = &net.TCPAddr{
IP: srcIP,
Port: int(srcPort),
}
c.target = &net.TCPAddr{
IP: dstIP,
Port: int(dstPort),
}
case datagram:
c.remote = &net.UDPAddr{
IP: srcIP,
Port: int(srcPort),
}
c.target = &net.UDPAddr{
IP: dstIP,
Port: int(dstPort),
}
default:
if addrProto.AddressFamily() != unspec {
return errors.New("invalid protocol")
}
}
c.proxy = c.wrapped.RemoteAddr()
return nil
}
type addressFamilyAndProtocol byte
type addressFamily byte
type protocol byte
const (
unspec addressFamily = iota
inet
inet6
unix
)
const (
stream protocol = iota + 1
datagram
)
func (ap addressFamilyAndProtocol) AddressFamily() addressFamily {
return addressFamily(ap >> 4)
}
func (ap addressFamilyAndProtocol) Protocol() protocol {
return protocol(ap & 0x0F)
}
type versionAndCommand byte
type command byte
// Command constants.
const (
local command = iota
proxy
)
func (vac versionAndCommand) Version() byte {
return byte(vac >> 4)
}
func (vac versionAndCommand) Command() command {
return command(vac & 0x0F)
}
func parsePort(s string) (int, error) {
port, err := strconv.Atoi(s)
if err != nil || port < 0 || port > 65535 {
return 0, errors.New("invalid port number")
}
return port, nil
}