/
hoverfly.go
256 lines (206 loc) · 7.17 KB
/
hoverfly.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
package hoverfly
import (
"fmt"
"net"
"net/http"
"sync"
"time"
"github.com/SpectoLabs/goproxy"
"github.com/SpectoLabs/hoverfly/core/action"
"github.com/SpectoLabs/hoverfly/core/authentication/backends"
"github.com/SpectoLabs/hoverfly/core/cache"
"github.com/SpectoLabs/hoverfly/core/delay"
v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/core/journal"
"github.com/SpectoLabs/hoverfly/core/matching"
"github.com/SpectoLabs/hoverfly/core/metrics"
"github.com/SpectoLabs/hoverfly/core/models"
"github.com/SpectoLabs/hoverfly/core/modes"
"github.com/SpectoLabs/hoverfly/core/state"
"github.com/SpectoLabs/hoverfly/core/templating"
log "github.com/sirupsen/logrus"
)
// Hoverfly provides access to hoverfly - updating/starting/stopping proxy, http client and configuration, cache access
type Hoverfly struct {
CacheMatcher matching.CacheMatcher
Authentication backends.Authentication
HTTP *http.Client
Cfg *Configuration
Counter *metrics.CounterByMode
Proxy *goproxy.ProxyHttpServer
SL *StoppableListener
mu sync.Mutex
version string
modeMap map[string]modes.Mode
state *state.State
Simulation *models.Simulation
StoreLogsHook *StoreLogsHook
Journal *journal.Journal
templator *templating.Templator
PostServeActionDetails *action.PostServeActionDetails
responsesDiff map[v2.SimpleRequestDefinitionView][]v2.DiffReport
}
func NewHoverfly() *Hoverfly {
authBackend := backends.NewCacheBasedAuthBackend(cache.NewInMemoryCache(), cache.NewInMemoryCache())
hoverfly := &Hoverfly{
Simulation: models.NewSimulation(),
Authentication: authBackend,
Counter: metrics.NewModeCounter([]string{modes.Simulate, modes.Synthesize, modes.Modify, modes.Capture, modes.Spy, modes.Diff}),
StoreLogsHook: NewStoreLogsHook(),
Journal: journal.NewJournal(),
Cfg: InitSettings(),
state: state.NewState(),
templator: templating.NewTemplator(),
responsesDiff: make(map[v2.SimpleRequestDefinitionView][]v2.DiffReport),
PostServeActionDetails: action.NewPostServeActionDetails(),
}
hoverfly.version = "v1.8.0"
log.AddHook(hoverfly.StoreLogsHook)
modeMap := make(map[string]modes.Mode)
modeMap[modes.Capture] = &modes.CaptureMode{Hoverfly: hoverfly}
modeMap[modes.Simulate] = &modes.SimulateMode{Hoverfly: hoverfly, MatchingStrategy: "strongest"}
modeMap[modes.Modify] = &modes.ModifyMode{Hoverfly: hoverfly}
modeMap[modes.Synthesize] = &modes.SynthesizeMode{Hoverfly: hoverfly}
modeMap[modes.Spy] = &modes.SpyMode{Hoverfly: hoverfly}
modeMap[modes.Diff] = &modes.DiffMode{Hoverfly: hoverfly}
hoverfly.modeMap = modeMap
hoverfly.HTTP = GetDefaultHoverflyHTTPClient(hoverfly.Cfg.TLSVerification, hoverfly.Cfg.UpstreamProxy)
return hoverfly
}
func NewHoverflyWithConfiguration(cfg *Configuration) *Hoverfly {
hoverfly := NewHoverfly()
var requestCache cache.FastCache
if !cfg.DisableCache {
if cfg.CacheSize > 0 {
requestCache, _ = cache.NewLRUCache(cfg.CacheSize)
} else {
// Backward compatibility, always set default cache if cache size is not configured
requestCache = cache.NewDefaultLRUCache()
}
}
hoverfly.CacheMatcher = matching.CacheMatcher{
Webserver: cfg.Webserver,
RequestCache: requestCache,
}
hoverfly.Cfg = cfg
hoverfly.HTTP = GetDefaultHoverflyHTTPClient(cfg.TLSVerification, cfg.UpstreamProxy)
return hoverfly
}
// GetNewHoverfly returns a configured ProxyHttpServer and DBClient
func GetNewHoverfly(cfg *Configuration, requestCache cache.FastCache, authentication backends.Authentication) *Hoverfly {
hoverfly := NewHoverfly()
if cfg.DisableCache {
requestCache = nil
}
hoverfly.CacheMatcher = matching.CacheMatcher{
RequestCache: requestCache,
Webserver: cfg.Webserver,
}
hoverfly.Authentication = authentication
hoverfly.HTTP = GetDefaultHoverflyHTTPClient(cfg.TLSVerification, cfg.UpstreamProxy)
hoverfly.Cfg = cfg
return hoverfly
}
// StartProxy - starts proxy with current configuration, this method is non blocking.
func (hf *Hoverfly) StartProxy() error {
if hf.Cfg.ProxyPort == "" {
return fmt.Errorf("Proxy port is not set!")
}
if hf.Cfg.Webserver {
hf.Proxy = NewWebserverProxy(hf)
} else {
hf.Proxy = NewProxy(hf)
}
log.WithFields(log.Fields{
"destination": hf.Cfg.Destination,
"port": hf.Cfg.ProxyPort,
"mode": hf.Cfg.GetMode(),
}).Info("current proxy configuration")
// creating TCP listener
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%s", hf.Cfg.ListenOnHost, hf.Cfg.ProxyPort))
if err != nil {
return err
}
sl, err := NewStoppableListener(listener)
if err != nil {
return err
}
hf.SL = sl
server := http.Server{}
hf.Cfg.ProxyControlWG.Add(1)
go func() {
defer func() {
log.Info("sending done signal")
hf.Cfg.ProxyControlWG.Done()
}()
log.Info("serving proxy")
server.Handler = hf.Proxy
log.Warn(server.Serve(sl))
}()
return nil
}
// StopProxy - stops proxy
func (hf *Hoverfly) StopProxy() {
hf.SL.Stop()
hf.Cfg.ProxyControlWG.Wait()
}
// processRequest - processes incoming requests and based on proxy state (record/playback)
// returns HTTP response.
func (hf *Hoverfly) processRequest(req *http.Request) *http.Response {
if hf.Cfg.CORS.Enabled {
response := hf.Cfg.CORS.InterceptPreflightRequest(req)
if response != nil {
return response
}
}
requestDetails, err := models.NewRequestDetailsFromHttpRequest(req)
if err != nil {
return modes.ErrorResponse(req, err, "Could not interpret HTTP request").Response
}
modeName := hf.Cfg.GetMode()
mode := hf.modeMap[modeName]
result, err := mode.Process(req, requestDetails)
if err == nil && hf.Cfg.CORS.Enabled {
hf.Cfg.CORS.AddCORSHeaders(req, result.Response)
}
// and definitely don't delay people in capture mode
// Don't delete the error
if err != nil || modeName == modes.Capture {
return result.Response
}
if result.IsResponseDelayable() {
log.Debug("Applying response delay")
hf.applyResponseDelay(result)
} else {
log.Debug("Applying global delay")
hf.applyGlobalDelay(requestDetails)
}
if result.PostServeActionInputDetails != nil {
if postServeAction, ok := hf.PostServeActionDetails.Actions[result.PostServeActionInputDetails.PostServeAction]; ok {
go postServeAction.ExecuteLocally(result.PostServeActionInputDetails.Pair)
}
}
return result.Response
}
func (hf *Hoverfly) applyResponseDelay(result modes.ProcessResult) {
if result.FixedDelay > 0 {
time.Sleep(time.Duration(result.FixedDelay) * time.Millisecond)
}
if result.LogNormalDelay != nil {
logNormalDelay := delay.NewLogNormalGenerator(
result.LogNormalDelay.Min, result.LogNormalDelay.Max,
result.LogNormalDelay.Mean, result.LogNormalDelay.Median,
).GenerateDelay()
time.Sleep(time.Duration(logNormalDelay) * time.Millisecond)
}
}
func (hf *Hoverfly) applyGlobalDelay(requestDetails models.RequestDetails) {
respDelay := hf.Simulation.ResponseDelays.GetDelay(requestDetails)
if respDelay != nil {
respDelay.Execute()
}
respDelayLogNormal := hf.Simulation.ResponseDelaysLogNormal.GetDelay(requestDetails)
if respDelayLogNormal != nil {
respDelayLogNormal.Execute()
}
}