forked from chrislusf/teeproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteeproxy.go
More file actions
286 lines (250 loc) · 7.12 KB
/
teeproxy.go
File metadata and controls
286 lines (250 loc) · 7.12 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
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
package main
import (
"bytes"
"flag"
"io"
"net"
"net/http"
"net/url"
"runtime"
"time"
"io/ioutil"
"math/rand"
"strings"
log "github.com/Sirupsen/logrus"
"fmt"
"sync/atomic"
"sync"
)
func LogDebugWithTime(s string, r string) {
log.WithFields(log.Fields{
"Timestamp": time.Now(),
"request_id": r,
}).Debug(s)
}
func LogErrorWithTime(s string, r string) {
log.WithFields(log.Fields{
"Timestamp": time.Now(),
"request_id": r,
}).Error(s)
}
// Console flags
var (
listen = flag.String("l", ":8888", "port to accept requests")
targetProduction = flag.String("a", "localhost:8080", "where production traffic goes. http://localhost:8080/production")
altTarget = flag.String("b", "localhost:8081", "where testing traffic goes. response are skipped. http://localhost:8081/test")
debug = flag.Bool("debug", false, "more logging, showing ignored output")
productionTimeout = flag.Int("a.timeout", 3, "timeout in seconds for production traffic")
alternateTimeout = flag.Int("b.timeout", 3, "timeout in seconds for alternate site traffic")
workers = flag.Int("w", 1, "number of workers allowed to send traffic to b")
)
var tasks = make(chan request_task, 128)
var ops uint64 = 0
// handler contains the address of the main Target and the one for the Alternative target
type handler struct {
Target string
Alternative string
}
type request_task struct {
Request http.Request
ID string
}
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error {
return nil
}
// Be sure we have a scheme in our host URIs, to accomodate the rather crappy go-native tokenization
func LocalParseURL(rawurl string) (u *url.URL, err error) {
if !strings.Contains(rawurl, "http://") && !strings.Contains(rawurl, "https://") {
rawurl = "http://" + rawurl
}
return url.Parse(rawurl)
}
// Generate working request_ids for debug
func RandomString(strlen int) string {
rand.Seed(time.Now().UTC().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
func MakeRequestID() (id string) {
return RandomString(32)
}
// channelized worker queue for dispatching mirrored requests
func worker(tasks <-chan request_task, quit <-chan bool, tick <-chan bool, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case task, ok := <-tasks:
if !ok {
return
}
// create and dispatch asynch. request to alternate
alt_target_parse, err := LocalParseURL(*altTarget)
if err != nil {
LogErrorWithTime(fmt.Sprintf("Failed to parse Target: %s: %v\n", *altTarget, err), task.ID)
}
task.Request.URL.Host = alt_target_parse.Host
task.Request.URL.Scheme = alt_target_parse.Scheme
LogDebugWithTime("Bulding Alternate Request", task.ID)
clientHttpConn1 := &http.Client{
Timeout: time.Duration(time.Duration(*alternateTimeout) * time.Second),
}
_, err = clientHttpConn1.Do(&task.Request)
if err != nil {
LogErrorWithTime(fmt.Sprintf("Failed to send to %s: %v\n", task.Request.Host, err), task.ID)
return
}
LogDebugWithTime("Altnernate Request Finished", task.ID)
case <-quit:
return
case <-tick:
if len(tasks) <= 64{
log.WithFields(log.Fields{
"Tick": time.Now(),
"Buffered Jobs": len(tasks),
}).Debug("Status")
} else {
log.WithFields(log.Fields{
"Tick": time.Now(),
"Buffered Jobs": len(tasks),
}).Warn("Status")
}
}
}
}
// ServeHTTP duplicates the incoming request (req) and does the request to the Target and the Alternate target discading the Alternate response
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// process accounting
thisop := atomic.LoadUint64(&ops)
atomic.AddUint64(&ops, 1)
requestID := MakeRequestID()
// preliminary logging
LogDebugWithTime(fmt.Sprintf("New Request Op: %d", thisop), requestID)
log.WithFields(
log.Fields{
"request_op": thisop,
"request_id": requestID,
"request_method": req.Method,
"request_path": req.URL.RequestURI(),
}).Debug("Incomming Request")
// cppy the request
alt_request, target_request := DuplicateRequest(req)
tasks <- request_task {
Request: *alt_request,
ID: requestID,
}
// run same request with the target
defer func() {
if r := recover(); r != nil && *debug {
log.Warn("Recovered in f", r)
}
}()
LogDebugWithTime("Bulding Target Request", requestID)
target_parse, err := LocalParseURL(h.Target)
if err != nil {
LogErrorWithTime(fmt.Sprintf("Failed to parse Target: %s: %v\n", h.Target, err), requestID)
}
target_request.URL.Host = target_parse.Host
target_request.URL.Scheme = target_parse.Scheme
clientHttpConn2 := &http.Client{
Timeout: time.Duration(time.Duration(*productionTimeout) * time.Second),
}
resp2, err := clientHttpConn2.Do(target_request)
LogDebugWithTime("Target Reply Received", requestID)
if err != nil {
LogErrorWithTime(fmt.Sprintf("Failed to send to %s: %v\n", h.Target, err), requestID)
return
}
for k, v := range resp2.Header {
w.Header()[k] = v
}
body, _ := ioutil.ReadAll(resp2.Body)
resp2.Body.Close()
w.WriteHeader(resp2.StatusCode)
w.Write(body)
LogDebugWithTime("Target Reply Returned to Client", requestID)
}
func main() {
flag.Parse()
// We shouldn't have to worry about this is 1.5... nonetheless:
max_workers := runtime.GOMAXPROCS(runtime.NumCPU() * 32)
if *workers > max_workers {
*workers = max_workers
}
// set up for STDOUT
log.Info("Debugging: ", *debug)
if *debug {
log.SetLevel(log.DebugLevel)
}
log.Debug("Workers: ", *workers)
log.Debug("Start Time: ", time.Now())
// our queue channels
quit := make(chan bool)
tick := make(chan bool)
var wg sync.WaitGroup
// spawn workers
for i := 0; i < *workers; i++ {
wg.Add(1)
go worker(tasks, quit, tick, &wg)
}
// set up HTTP listener
local, err := net.Listen("tcp", *listen)
if err != nil {
LogErrorWithTime(fmt.Sprintf("Failed to listen to %s\n", *listen), "MAIN")
return
}
h := handler{
Target: *targetProduction,
Alternative: *altTarget,
}
// start marking time
go func() {
for {
time.Sleep(time.Second * 1)
tick <- true
}
}()
// run service (NOTE: feel free to replace 'http' for 'fcgi' if you don't care about proxying headers
http.Serve(local, h)
// ... and our cleanup
defer func() {
close(quit)
wg.Wait()
}()
}
func DuplicateRequest(request *http.Request) (request1 *http.Request, request2 *http.Request) {
b1 := new(bytes.Buffer)
b2 := new(bytes.Buffer)
w := io.MultiWriter(b1, b2)
io.Copy(w, request.Body)
defer request.Body.Close()
request1 = &http.Request{
Method: request.Method,
URL: request.URL,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: request.Header,
Body: nopCloser{b1},
Host: request.Host,
ContentLength: request.ContentLength,
}
request2 = &http.Request{
Method: request.Method,
URL: request.URL,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: request.Header,
Body: nopCloser{b2},
Host: request.Host,
ContentLength: request.ContentLength,
}
return
}