-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaproxy.go
More file actions
116 lines (110 loc) · 3.06 KB
/
Copy pathhaproxy.go
File metadata and controls
116 lines (110 loc) · 3.06 KB
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
package proxy
import (
"bytes"
"encoding/binary"
"net"
"strconv"
"strings"
)
var haproxyV2Signature = []byte{0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, 'Q', 'U', 'I', 'T', 0x0a}
type HAProxyInfo struct {
Version int
Command string
Protocol string
SourceIP net.IP
DestIP net.IP
SourcePort int
DestPort int
}
func ParseHAProxyHeader(data []byte) (HAProxyInfo, int, error) {
if bytes.HasPrefix(data, haproxyV2Signature) {
return parseHAProxyV2(data)
}
if bytes.HasPrefix(data, []byte("PROXY ")) {
return parseHAProxyV1(data)
}
return HAProxyInfo{}, 0, ErrInvalidMessage
}
func parseHAProxyV1(data []byte) (HAProxyInfo, int, error) {
end := bytes.Index(data, []byte("\r\n"))
if end < 0 {
return HAProxyInfo{}, 0, ErrNeedMore
}
fields := strings.Fields(string(data[:end]))
if len(fields) < 2 || fields[0] != "PROXY" {
return HAProxyInfo{}, 0, ErrInvalidMessage
}
info := HAProxyInfo{Version: 1, Command: "PROXY", Protocol: fields[1]}
if fields[1] == "UNKNOWN" {
return info, end + 2, nil
}
if len(fields) != 6 {
return HAProxyInfo{}, 0, ErrInvalidMessage
}
src := net.ParseIP(fields[2])
dst := net.ParseIP(fields[3])
srcPort, err := strconv.Atoi(fields[4])
if err != nil {
return HAProxyInfo{}, 0, ErrInvalidMessage
}
dstPort, err := strconv.Atoi(fields[5])
if err != nil {
return HAProxyInfo{}, 0, ErrInvalidMessage
}
if src == nil || dst == nil || srcPort < 0 || srcPort > 65535 || dstPort < 0 || dstPort > 65535 {
return HAProxyInfo{}, 0, ErrInvalidMessage
}
info.SourceIP = src
info.DestIP = dst
info.SourcePort = srcPort
info.DestPort = dstPort
return info, end + 2, nil
}
func parseHAProxyV2(data []byte) (HAProxyInfo, int, error) {
if len(data) < 16 {
return HAProxyInfo{}, 0, ErrNeedMore
}
versionCommand := data[12]
if versionCommand>>4 != 0x02 {
return HAProxyInfo{}, 0, ErrInvalidMessage
}
length := int(binary.BigEndian.Uint16(data[14:16]))
if len(data) < 16+length {
return HAProxyInfo{}, 0, ErrNeedMore
}
info := HAProxyInfo{Version: 2}
switch versionCommand & 0x0f {
case 0x00:
info.Command = "LOCAL"
case 0x01:
info.Command = "PROXY"
default:
return HAProxyInfo{}, 0, ErrInvalidMessage
}
payload := data[16 : 16+length]
switch data[13] {
case 0x11:
if len(payload) < 12 {
return HAProxyInfo{}, 0, ErrInvalidMessage
}
info.Protocol = "TCP4"
info.SourceIP = net.IPv4(payload[0], payload[1], payload[2], payload[3])
info.DestIP = net.IPv4(payload[4], payload[5], payload[6], payload[7])
info.SourcePort = int(binary.BigEndian.Uint16(payload[8:10]))
info.DestPort = int(binary.BigEndian.Uint16(payload[10:12]))
case 0x21:
if len(payload) < 36 {
return HAProxyInfo{}, 0, ErrInvalidMessage
}
info.Protocol = "TCP6"
info.SourceIP = append(net.IP(nil), payload[0:16]...)
info.DestIP = append(net.IP(nil), payload[16:32]...)
info.SourcePort = int(binary.BigEndian.Uint16(payload[32:34]))
info.DestPort = int(binary.BigEndian.Uint16(payload[34:36]))
case 0x00:
info.Protocol = "UNKNOWN"
default:
return HAProxyInfo{}, 0, ErrUnsupportedProtocol
}
return info, 16 + length, nil
}