forked from SpectoLabs/hoverfly
/
hoverfly.go
executable file
·233 lines (186 loc) · 6.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
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/elazarl/goproxy"
"bufio"
"flag"
"fmt"
"net"
"net/http"
"os"
"regexp"
)
// VirtualizeMode - default mode when Hoverfly looks for captured requests to respond
const VirtualizeMode = "virtualize"
// SynthesizeMode - all requests are sent to middleware to create response
const SynthesizeMode = "synthesize"
// ModifyMode - middleware is applied to outgoing and incoming traffic
const ModifyMode = "modify"
// CaptureMode - requests are captured and stored in cache
const CaptureMode = "capture"
// orPanic - wrapper for logging errors
func orPanic(err error) {
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Panic("Got error.")
}
}
func main() {
// Output to stderr instead of stdout, could also be a file.
log.SetOutput(os.Stderr)
log.SetFormatter(&log.TextFormatter{})
// getting proxy configuration
verbose := flag.Bool("v", false, "should every proxy request be logged to stdout")
// modes
capture := flag.Bool("capture", false, "should proxy capture requests")
synthesize := flag.Bool("synthesize", false, "should proxy capture requests")
modify := flag.Bool("modify", false, "should proxy only modify requests")
destination := flag.String("destination", ".", "destination URI to catch")
middleware := flag.String("middleware", "", "should proxy use middleware")
// proxy port
proxyPort := flag.String("pp", "", "proxy port - run proxy on another port (i.e. '-pp 9999' to run proxy on port 9999)")
// admin port
adminPort := flag.String("ap", "", "admin port - run admin interface on another port (i.e. '-ap 1234' to run admin UI on port 1234)")
flag.Parse()
// getting settings
cfg := InitSettings()
if *verbose {
// Only log the warning severity or above.
log.SetLevel(log.DebugLevel)
}
cfg.verbose = *verbose
// overriding environment variables (proxy and admin ports)
if *proxyPort != "" {
cfg.proxyPort = *proxyPort
}
if *adminPort != "" {
cfg.adminPort = *adminPort
}
// overriding default middleware setting
cfg.middleware = *middleware
// setting default mode
mode := VirtualizeMode
if *capture {
mode = CaptureMode
// checking whether user supplied other modes
if *synthesize == true || *modify == true {
log.Fatal("Two or more modes supplied, check your flags")
}
} else if *synthesize {
mode = SynthesizeMode
if cfg.middleware == "" {
log.Fatal("Synthesize mode chosen although middleware not supplied")
}
if *capture == true || *modify == true {
log.Fatal("Two or more modes supplied, check your flags")
}
} else if *modify {
mode = ModifyMode
if cfg.middleware == "" {
log.Fatal("Modify mode chosen although middleware not supplied")
}
if *capture == true || *synthesize == true {
log.Fatal("Two or more modes supplied, check your flags")
}
}
// overriding default settings
cfg.mode = mode
// overriding destination
cfg.destination = *destination
proxy, dbClient := getNewHoverfly(cfg)
defer dbClient.cache.db.Close()
log.Warn(http.ListenAndServe(fmt.Sprintf(":%s", cfg.proxyPort), proxy))
}
// getNewHoverfly returns a configured ProxyHttpServer and DBClient, also starts admin interface on configured port
func getNewHoverfly(cfg *Configuration) (*goproxy.ProxyHttpServer, DBClient) {
// getting boltDB
db := getDB(cfg.databaseName)
cache := Cache{
db: db,
requestsBucket: []byte(requestsBucketName),
}
// getting connections
d := DBClient{
cache: cache,
http: &http.Client{},
cfg: cfg,
}
// creating proxy
proxy := goproxy.NewProxyHttpServer()
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile(d.cfg.destination))).
HandleConnect(goproxy.AlwaysMitm)
// enable curl -p for all hosts on port 80
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile(d.cfg.destination))).
HijackConnect(func(req *http.Request, client net.Conn, ctx *goproxy.ProxyCtx) {
defer func() {
if e := recover(); e != nil {
ctx.Logf("error connecting to remote: %v", e)
client.Write([]byte("HTTP/1.1 500 Cannot reach destination\r\n\r\n"))
}
client.Close()
}()
clientBuf := bufio.NewReadWriter(bufio.NewReader(client), bufio.NewWriter(client))
remote, err := net.Dial("tcp", req.URL.Host)
orPanic(err)
remoteBuf := bufio.NewReadWriter(bufio.NewReader(remote), bufio.NewWriter(remote))
for {
req, err := http.ReadRequest(clientBuf.Reader)
orPanic(err)
orPanic(req.Write(remoteBuf))
orPanic(remoteBuf.Flush())
resp, err := http.ReadResponse(remoteBuf.Reader, req)
orPanic(err)
orPanic(resp.Write(clientBuf.Writer))
orPanic(clientBuf.Flush())
}
})
// processing connections
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile(cfg.destination))).DoFunc(
func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
return d.processRequest(r)
})
go d.startAdminInterface()
proxy.Verbose = d.cfg.verbose
// proxy starting message
log.WithFields(log.Fields{
"Destination": d.cfg.destination,
"ProxyPort": d.cfg.proxyPort,
"Mode": d.cfg.GetMode(),
}).Info("Proxy prepared...")
return proxy, d
}
// processRequest - processes incoming requests and based on proxy state (record/playback)
// returns HTTP response.
func (d *DBClient) processRequest(req *http.Request) (*http.Request, *http.Response) {
mode := d.cfg.GetMode()
if mode == CaptureMode {
log.Info("*** Capture ***")
newResponse, err := d.captureRequest(req)
if err != nil {
// something bad happened, passing through
return req, nil
}
// discarding original requests and returns supplied response
return req, newResponse
} else if mode == SynthesizeMode {
log.Info("*** Sinthesize ***")
response := synthesizeResponse(req, d.cfg.middleware)
return req, response
} else if mode == ModifyMode {
log.Info("*** Modify ***")
response, err := d.modifyRequestResponse(req, d.cfg.middleware)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"middleware": d.cfg.middleware,
}).Error("Got error when performing request modification")
return req, nil
}
// returning modified response
return req, response
}
log.Info("*** Virtualize ***")
newResponse := d.getResponse(req)
return req, newResponse
}