-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplexer.go
198 lines (171 loc) · 5.16 KB
/
multiplexer.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
package main
import (
"go.uber.org/zap"
"net"
"regexp"
)
type ProtocolMatchResult int
const (
ProtocolMatched ProtocolMatchResult = iota
ProtocolNotMatched
ProtocolMatchUnspecified
)
const (
DefaultBytesLength = 512 // Less than the required miminal MSS size for ipv4.
SmallBytesLength = 64 // For most eal world packet.
)
type ConnAuxInfo struct {
ppi *PROXYProtocolInfo
initialBytes []byte
connRemoteAddr net.Addr
}
func (auxInfo *ConnAuxInfo) GetUpstreamDialer() net.Dialer {
clientAddr := auxInfo.connRemoteAddr
if auxInfo.ppi != nil {
clientAddr = auxInfo.ppi.SourceAddr
}
dialer := net.Dialer{}
if Opts.EnableTransparentProxy && ShouldEnableTransparentMode(clientAddr) {
dialer.LocalAddr = clientAddr
dialer.Control = DialUpstreamControl(GetPortFromAddr(dialer.LocalAddr))
}
return dialer
}
type ProtocolMatcher interface {
Name() string
UpstreamAddr() string
TcpMatch(conn net.Conn, auxInfo *ConnAuxInfo) ProtocolMatchResult
UdpMatch(conn net.PacketConn, auxInfo *ConnAuxInfo) ProtocolMatchResult
}
type RegexMatcher struct {
upstreamAddr string
matcher regexMatcher
}
type bytesTransformation = func([]byte) []byte
type regexMatcher struct {
name string
numRequiredBytes int
transformInitialBytes bytesTransformation
patterns []*regexp.Regexp
}
func (m *RegexMatcher) Name() string {
return m.matcher.name
}
func (m *RegexMatcher) UpstreamAddr() string {
return m.upstreamAddr
}
func (m *RegexMatcher) TcpMatch(conn net.Conn, auxInfo *ConnAuxInfo) ProtocolMatchResult {
if m.matcher.numRequiredBytes > len(auxInfo.initialBytes) {
//TODO: Here we should read from conn again, and assign the result back to auxInfo
return ProtocolMatchUnspecified
}
for _, pattern := range m.matcher.patterns {
bytes := m.matcher.transformInitialBytes(auxInfo.initialBytes)
if pattern.Match(bytes) {
return ProtocolMatched
}
}
return ProtocolNotMatched
}
//TODO: Port multiplexing for udp
func (m *RegexMatcher) UdpMatch(conn net.PacketConn, auxInfo *ConnAuxInfo) ProtocolMatchResult {
return ProtocolNotMatched
}
func sameBytes(bytes []byte) []byte {
return bytes
}
func bytesAfter(n int) bytesTransformation {
return func(bytes []byte) []byte {
return bytes[n:]
}
}
func bytesBefore(n int) bytesTransformation {
return func(bytes []byte) []byte {
return bytes[:n]
}
}
func bytesWithin(lower int, upper int) bytesTransformation {
return func(bytes []byte) []byte {
return bytes[lower:upper]
}
}
var (
tlsMatcher = regexMatcher{
"tls",
5,
sameBytes,
[]*regexp.Regexp{regexp.MustCompile("^\x16\x03\x01")},
}
httpMatcher = regexMatcher{
"http",
len(" HTTP "),
bytesBefore(SmallBytesLength),
[]*regexp.Regexp{regexp.MustCompile("^[A-Z]+ .+ HTTP/")},
}
sshMatcher = regexMatcher{
"ssh",
len("SSH-"),
bytesBefore(SmallBytesLength),
[]*regexp.Regexp{regexp.MustCompile("^SSH-")},
}
eternalTerminalMatcher = regexMatcher{
"eternal terminal",
8,
bytesBefore(SmallBytesLength),
[]*regexp.Regexp{regexp.MustCompile("^\x14\x00\x00\x00\x00\x00\x00\x00")},
}
)
func NewTLSMatcher(upstreamAddr string) *RegexMatcher {
return &RegexMatcher{
upstreamAddr: upstreamAddr,
matcher: tlsMatcher,
}
}
func NewHTTPMatcher(upstreamAddr string) *RegexMatcher {
return &RegexMatcher{
upstreamAddr: upstreamAddr,
matcher: httpMatcher,
}
}
func NewSSHMatcher(upstreamAddr string) *RegexMatcher {
return &RegexMatcher{
upstreamAddr: upstreamAddr,
matcher: sshMatcher,
}
}
func NewEternalTerminalMatcher(upstreamAddr string) *RegexMatcher {
return &RegexMatcher{
upstreamAddr: upstreamAddr,
matcher: eternalTerminalMatcher,
}
}
func tcpGetTargetAddr(conn net.Conn, auxInfo *ConnAuxInfo, logger *zap.Logger) string {
for _, matcher := range Opts.ProtocolMatchers {
if matcher.TcpMatch(conn, auxInfo) == ProtocolMatched {
logger.Debug("Successfully matched packet", zap.String("matcher.Name()", matcher.Name()), zap.String("matcher.UpstreamAddr()", matcher.UpstreamAddr()))
return matcher.UpstreamAddr()
}
}
logger.Debug("No matcher the packet, using default upstream addr", zap.String("Opts.UpstreamAddr)", Opts.UpstreamAddr))
return Opts.UpstreamAddr
}
func tcpGetUpstreamConn(conn net.Conn, auxInfo *ConnAuxInfo, logger *zap.Logger) (net.Conn, error) {
targetAddr := tcpGetTargetAddr(conn, auxInfo, logger)
dialer := auxInfo.GetUpstreamDialer()
return dialer.Dial("tcp", targetAddr)
}
func udpGetTargetAddr(conn net.PacketConn, auxInfo *ConnAuxInfo, logger *zap.Logger) string {
for _, matcher := range Opts.ProtocolMatchers {
if matcher.UdpMatch(conn, auxInfo) == ProtocolMatched {
logger.Debug("Successfully matched packet", zap.String("matcher.Name()", matcher.Name()), zap.String("matcher.UpstreamAddr()", matcher.UpstreamAddr()))
return matcher.UpstreamAddr()
}
}
logger.Debug("No matcher the packet, using default upstream addr", zap.String("Opts.UpstreamAddr)", Opts.UpstreamAddr))
return Opts.UpstreamAddr
}
func udpGetUpstreamConn(conn net.PacketConn, auxInfo *ConnAuxInfo, logger *zap.Logger) (net.Conn, error) {
targetAddr := udpGetTargetAddr(conn, auxInfo, logger)
dialer := auxInfo.GetUpstreamDialer()
return dialer.Dial("udp", targetAddr)
}