-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracker.go
299 lines (259 loc) · 7.75 KB
/
tracker.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
// Copyright 2020 Mike Helmick
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chaff
import (
"crypto/rand"
"encoding/base64"
"fmt"
"log"
"net/http"
"sync"
"sync/atomic"
"time"
)
const (
Header = "X-Chaff"
DefaultCapacity = 100
MaxRandomBytes = 1000000
)
// Tracker represents the status of a latency and request size tracker.
// It contains middleware that can be injected to automate keeping a rolling
// history of requests.
//
// It also implements http.Handler and can be used to server the chaff request
// handler.
//
// Response details are sent through a buffered channel. If the channel is full
// (i.e. this library is falling behind or requests volumes are too large),
// then some individual requests will be dropped.
type Tracker struct {
mu sync.RWMutex
buffer []*request
size int
cap int
pos int
ch chan *request
done chan struct{}
resp Responder
maxLatencyMs uint64
}
type request struct {
latencyMs uint64
bodySize uint64
headerSize uint64
}
func newRequest(start, end time.Time, headerSize, bodySize uint64) *request {
return &request{
latencyMs: uint64(end.Sub(start).Milliseconds()),
headerSize: headerSize,
bodySize: bodySize,
}
}
// New creates a new tracker with the `DefaultCapacity`.
func New(opts ...Option) *Tracker {
t, _ := NewTracker(&PlainResponder{}, DefaultCapacity, opts...)
return t
}
// Option defines a method for applying options when configuring a new tracker.
type Option func(*Tracker)
// WithMaxLatency puts a cap on the tunnel latency.
func WithMaxLatency(maxLatencyMs uint64) Option {
return func(t *Tracker) {
t.maxLatencyMs = maxLatencyMs
}
}
// NewTracker creates a tracker with custom capacity.
// Launches a goroutine to update the request metrics.
// To shut this down, use the .Close() method.
// The Responder parameter is used to write the output. If non is specified,
// the tracker will default to the "PlainResponder" which just writes the raw
// chaff bytes.
func NewTracker(resp Responder, cap int, opts ...Option) (*Tracker, error) {
if cap < 1 || cap > DefaultCapacity {
return nil, fmt.Errorf("cap must be 1 <= cap <= 100, got: %v", cap)
}
if resp == nil {
return nil, fmt.Errorf("responder must be non-nil")
}
t := &Tracker{
buffer: make([]*request, 0, int(cap)),
size: 0,
cap: cap,
pos: 0,
ch: make(chan *request, cap),
done: make(chan struct{}),
resp: resp,
maxLatencyMs: 0,
}
// Apply options.
for _, opt := range opts {
opt(t)
}
go t.updater()
return t, nil
}
// recordRequest actually puts a request in the circular buffer.
func (t *Tracker) recordRequest(record *request) {
t.mu.Lock()
defer t.mu.Unlock()
if t.size < t.cap {
t.buffer = append(t.buffer, record)
t.size++
return
}
// Working as a circular buffer, just overrite and move on.
t.buffer[t.pos] = record
t.pos = (t.pos + 1) % t.cap
}
// updater is the go routine that is launched to pull requst details from
// the request channel.
func (t *Tracker) updater() {
for {
select {
case record := <-t.ch:
t.recordRequest(record)
case <-t.done:
return
}
}
}
// Close will stop the updating goroutine and closes all channels.
func (t *Tracker) Close() {
t.done <- struct{}{}
close(t.ch)
close(t.done)
}
// CalculateProfile takes a read lock over the source data and
// returns the current average latency and request sizes.
func (t *Tracker) CalculateProfile() *request {
t.mu.RLock()
defer t.mu.RUnlock()
if t.size == 0 {
return &request{}
}
var latency, hSize, bSize uint64
for _, r := range t.buffer {
latency += r.latencyMs
hSize += uint64(r.headerSize)
bSize += uint64(r.bodySize)
}
divisor := uint64(t.size)
latencyMs := latency / divisor
if max := t.maxLatencyMs; max > 0 && latencyMs > max {
latencyMs = max
}
return &request{
latencyMs: latencyMs,
headerSize: uint64(hSize / divisor),
bodySize: uint64(bSize / divisor),
}
}
// RandomData generates size bytes of random base64 data.
func RandomData(size uint64) string {
// Account for base64 overhead
size = 3 * size / 4
if size <= 0 {
return ""
}
if size > MaxRandomBytes {
size = MaxRandomBytes
}
buffer := make([]byte, size)
_, err := rand.Read(buffer)
if err != nil {
return http.StatusText(http.StatusInternalServerError)
}
return base64.StdEncoding.EncodeToString(buffer)
}
// ServeHTTP implements http.Handler. See HandleChaff for more details.
func (t *Tracker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.HandleChaff().ServeHTTP(w, r)
}
func (t *Tracker) ChaffHandler(responder Responder) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
details := t.CalculateProfile()
if err := responder.Write(details.headerSize, details.bodySize, w, r); err != nil {
log.Printf("error writing chaff response: %v", err)
}
t.normalizeLatnecy(start, details.latencyMs)
})
}
// HandleChaff is the chaff request handler. Based on the current request
// profile the requst will be held for a certian period of time and then return
// approximate size random data.
func (t *Tracker) HandleChaff() http.Handler {
return t.ChaffHandler(t.resp)
}
// Track wraps a http handler and collects metrics about the request for
// replaying later during a chaff response. It's suitable for use as a
// middleware function in common Go web frameworks.
func (t *Tracker) Track(next http.Handler) http.Handler {
return t.HandleTrack(nil, next)
}
// HandleTrack wraps the given http handler and detector. If the request is
// deemed to be chaff (as determined by the Detector), the system sends a chaff
// response. Otherwise it returns the real response and adds it to the tracker.
func (t *Tracker) HandleTrack(d Detector, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if d != nil && d.IsChaff(r) {
// Send chaff response
t.HandleChaff().ServeHTTP(w, r)
return
}
// Handle the real request, gathering metadata
start := time.Now()
proxyWriter := &writeThrough{w: w}
next.ServeHTTP(proxyWriter, r)
end := time.Now()
// Grab the size of the headers that are present.
var headerSize uint64
for k, vals := range w.Header() {
headerSize += uint64(len(k))
for _, v := range vals {
headerSize += uint64(len(v))
}
}
// Save metadata
select {
case t.ch <- newRequest(start, end, headerSize, proxyWriter.Size()):
default: // channel full, drop request.
}
})
}
func (t *Tracker) normalizeLatnecy(start time.Time, targetMs uint64) {
elapsed := time.Since(start)
if rem := targetMs - uint64(elapsed.Milliseconds()); rem > 0 {
time.Sleep(time.Duration(rem) * time.Millisecond)
}
}
// write through wraps an http.ResponseWriter so that we can count the number of
// bytes that are written by the delegate handler.
type writeThrough struct {
size uint64
w http.ResponseWriter
}
func (wt *writeThrough) Header() http.Header {
return wt.w.Header()
}
func (wt *writeThrough) Write(b []byte) (int, error) {
atomic.AddUint64(&wt.size, uint64(len(b)))
return wt.w.Write(b)
}
func (wt *writeThrough) WriteHeader(statusCode int) {
wt.w.WriteHeader(statusCode)
}
func (wt *writeThrough) Size() uint64 {
return atomic.LoadUint64(&wt.size)
}