-
Notifications
You must be signed in to change notification settings - Fork 8
/
proxy.go
283 lines (240 loc) · 7.16 KB
/
proxy.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
/*
A simple routing proxy in Go. Accepts incoming connections on ports 80 and 443.
Connections on port 80 are assumed to be HTTP. A hostname is extracted from each using
the HTTP "Host" header.
Connections on port 443 are assumed to be TLS. A hostname is extracted from the
server name indication in the ClientHello bytes. Currently non-TLS SSL connections
and TLS connections without SNIs are dropped messily.
Once a hostname has been extracted from the incoming connection, the proxy looks up
a set of backends on a redis server, which is assumed to be running on 127.0.0.1:6379.
The key for the set is hostnames:<the hostname from the connection>:backends.
If there is no set stored in redis for the backend, it will check
hostnames:httpDefault:backends for HTTP connections, or hostnames:httpsDefault:backends
for HTTPS. If these latter two lookups fail or return empty sets, it will drop
the connection.
A backend is then selected at random from the list that was supplied by redis, and
the whole client connection is sent down to the appropriate port on that backend.
The proxy will keep proxying data back and forth until one of the endpoints closes
the connection.
*/
package main
import (
"bufio"
"container/list"
"errors"
"fmt"
"github.com/fzzy/radix/redis"
"io"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
)
func getBackend(hostname string, defaultBackendType string, redisClient *redis.Client) (string, error) {
fmt.Println("Looking up", hostname)
backends, error := redisClient.Cmd("smembers", "hostnames:"+hostname+":backends").List()
if error != nil {
fmt.Println("Error in redis lookup for hostname backend", error)
return "", error
}
if len(backends) == 0 {
backends, error = redisClient.Cmd("smembers", "hostnames:"+defaultBackendType+":backends").List()
if error != nil {
fmt.Println("Error in redis lookup for default backend", error)
return "", error
}
if len(backends) == 0 {
fmt.Println("No default backend of type", defaultBackendType)
return "", errors.New("Could not find default backend of type " + defaultBackendType)
}
}
fmt.Println("Found backends:", backends)
backend := backends[int(rand.Float32()*float32(len(backends)))]
return backend, nil
}
func copyAndClose(dst io.WriteCloser, src io.Reader) {
io.Copy(dst, src)
dst.Close()
}
func handleHTTPConnection(downstream net.Conn, redisClient *redis.Client) {
reader := bufio.NewReader(downstream)
hostname := ""
readLines := list.New()
for hostname == "" {
bytes, _, error := reader.ReadLine()
if error != nil {
fmt.Println("Error reading", error)
downstream.Close()
return
}
line := string(bytes)
readLines.PushBack(line)
if line == "" {
// End of HTTP headers
break
}
if strings.HasPrefix(line, "Host: ") {
hostname = strings.TrimPrefix(line, "Host: ")
break
}
}
backendAddress, error := getBackend(hostname, "httpDefault", redisClient)
if error != nil {
fmt.Println("Couldn't get backend for ", hostname, "-- got error", error)
downstream.Close()
return
}
upstream, error := net.Dial("tcp", backendAddress+":80")
if error != nil {
fmt.Println("Couldn't connect to backend", error)
downstream.Close()
return
}
for element := readLines.Front(); element != nil; element = element.Next() {
line := element.Value.(string)
upstream.Write([]byte(line))
upstream.Write([]byte("\n"))
}
go copyAndClose(upstream, reader)
go copyAndClose(downstream, upstream)
}
func handleHTTPSConnection(downstream net.Conn, redisClient *redis.Client) {
firstByte := make([]byte, 1)
_, error := downstream.Read(firstByte)
if error != nil {
fmt.Println("Couldn't read first byte :-(")
return
}
if firstByte[0] != 0x16 {
fmt.Println("Not TLS :-(")
}
versionBytes := make([]byte, 2)
_, error = downstream.Read(versionBytes)
if error != nil {
fmt.Println("Couldn't read version bytes :-(")
return
}
if versionBytes[0] < 3 || (versionBytes[0] == 3 && versionBytes[1] < 1) {
fmt.Println("SSL < 3.1 so it's still not TLS")
return
}
restLengthBytes := make([]byte, 2)
_, error = downstream.Read(restLengthBytes)
if error != nil {
fmt.Println("Couldn't read restLength bytes :-(")
return
}
restLength := (int(restLengthBytes[0]) << 8) + int(restLengthBytes[1])
rest := make([]byte, restLength)
_, error = downstream.Read(rest)
if error != nil {
fmt.Println("Couldn't read rest of bytes")
return
}
current := 0
handshakeType := rest[0]
current += 1
if handshakeType != 0x1 {
fmt.Println("Not a ClientHello")
return
}
// Skip over another length
current += 3
// Skip over protocolversion
current += 2
// Skip over random number
current += 4 + 28
// Skip over session ID
sessionIDLength := int(rest[current])
current += 1
current += sessionIDLength
cipherSuiteLength := (int(rest[current]) << 8) + int(rest[current+1])
current += 2
current += cipherSuiteLength
compressionMethodLength := int(rest[current])
current += 1
current += compressionMethodLength
if current > restLength {
fmt.Println("no extensions")
return
}
// Skip over extensionsLength
// extensionsLength := (int(rest[current]) << 8) + int(rest[current + 1])
current += 2
hostname := ""
for current < restLength && hostname == "" {
extensionType := (int(rest[current]) << 8) + int(rest[current+1])
current += 2
extensionDataLength := (int(rest[current]) << 8) + int(rest[current+1])
current += 2
if extensionType == 0 {
// Skip over number of names as we're assuming there's just one
current += 2
nameType := rest[current]
current += 1
if nameType != 0 {
fmt.Println("Not a hostname")
return
}
nameLen := (int(rest[current]) << 8) + int(rest[current+1])
current += 2
hostname = string(rest[current : current+nameLen])
}
current += extensionDataLength
}
if hostname == "" {
fmt.Println("No hostname")
return
}
backendAddress, error := getBackend(hostname, "httpsDefault", redisClient)
if error != nil {
fmt.Println("Couldn't get backend for ", hostname, "-- got error", error)
return
}
upstream, error := net.Dial("tcp", backendAddress+":443")
if error != nil {
log.Fatal(error)
return
}
upstream.Write(firstByte)
upstream.Write(versionBytes)
upstream.Write(restLengthBytes)
upstream.Write(rest)
go copyAndClose(upstream, downstream)
go copyAndClose(downstream, upstream)
}
func reportDone(done chan int) {
done <- 1
}
func doProxy(done chan int, port int, handle func(net.Conn, *redis.Client), redisClient *redis.Client) {
defer reportDone(done)
listener, error := net.Listen("tcp", "0.0.0.0:"+strconv.Itoa(port))
if error != nil {
fmt.Println("Couldn't start listening", error)
return
}
fmt.Println("Started proxy on", port, "-- listening...")
for {
connection, error := listener.Accept()
if error != nil {
fmt.Println("Accept error", error)
return
}
go handle(connection, redisClient)
}
}
func main() {
redisClient, error := redis.Dial("tcp", "127.0.0.1:6379")
if error != nil {
fmt.Println("Error connecting to redis", error)
os.Exit(1)
}
httpDone := make(chan int)
go doProxy(httpDone, 80, handleHTTPConnection, redisClient)
httpsDone := make(chan int)
go doProxy(httpsDone, 443, handleHTTPSConnection, redisClient)
<-httpDone
<-httpsDone
}