@@ -6,15 +6,12 @@ import (
66
77 "bufio"
88 "flag"
9- "fmt"
109 "net"
1110 "net/http"
1211 "os"
1312 "regexp"
1413)
1514
16- const DefaultPort = ":8500"
17-
1815// modes
1916const VirtualizeMode = "virtualize"
2017const SynthesizeMode = "synthesize"
@@ -46,16 +43,17 @@ func main() {
4643 middleware := flag .String ("middleware" , "" , "should proxy use middleware" )
4744 flag .Parse ()
4845
46+ // getting settings
47+ cfg := InitSettings ()
48+
4949 if * verbose {
5050 // Only log the warning severity or above.
5151 log .SetLevel (log .DebugLevel )
5252 }
53-
54- // getting settings
55- initSettings ()
53+ cfg .verbose = * verbose
5654
5755 // overriding default middleware setting
58- AppConfig .middleware = * middleware
56+ cfg .middleware = * middleware
5957
6058 // setting default mode
6159 mode := VirtualizeMode
@@ -69,7 +67,7 @@ func main() {
6967 } else if * synthesize {
7068 mode = SynthesizeMode
7169
72- if AppConfig .middleware == "" {
70+ if cfg .middleware == "" {
7371 log .Fatal ("Synthesize mode chosen although middleware not supplied" )
7472 }
7573
@@ -79,7 +77,7 @@ func main() {
7977 } else if * modify {
8078 mode = ModifyMode
8179
82- if AppConfig .middleware == "" {
80+ if cfg .middleware == "" {
8381 log .Fatal ("Modify mode chosen although middleware not supplied" )
8482 }
8583
@@ -89,22 +87,22 @@ func main() {
8987 }
9088
9189 // overriding default settings
92- AppConfig .mode = mode
90+ cfg .mode = mode
9391
9492 // overriding destination
95- AppConfig .destination = * destination
93+ cfg .destination = * destination
9694
97- // getting default database
98- port := os .Getenv ("ProxyPort" )
99- if port == "" {
100- port = DefaultPort
101- } else {
102- port = fmt .Sprintf (":%s" , port )
103- }
95+ proxy , dbClient := getNewHoverfly (cfg )
96+ defer dbClient .cache .db .Close ()
97+
98+ log .Warn (http .ListenAndServe (cfg .proxyPort , proxy ))
99+ }
100+
101+ // getNewHoverfly returns a configured ProxyHttpServer and DBClient, also starts admin interface on configured port
102+ func getNewHoverfly (cfg * Configuration ) (* goproxy.ProxyHttpServer , DBClient ) {
104103
105104 // getting boltDB
106- db := getDB (AppConfig .databaseName )
107- defer db .Close ()
105+ db := getDB (cfg .databaseName )
108106
109107 cache := Cache {
110108 db : db ,
@@ -115,16 +113,17 @@ func main() {
115113 d := DBClient {
116114 cache : cache ,
117115 http : & http.Client {},
116+ cfg : cfg ,
118117 }
119118
120119 // creating proxy
121120 proxy := goproxy .NewProxyHttpServer ()
122121
123- proxy .OnRequest (goproxy .ReqHostMatches (regexp .MustCompile ("^.*$" ))).
122+ proxy .OnRequest (goproxy .ReqHostMatches (regexp .MustCompile (d . cfg . destination ))).
124123 HandleConnect (goproxy .AlwaysMitm )
125124
126125 // enable curl -p for all hosts on port 80
127- proxy .OnRequest (goproxy .ReqHostMatches (regexp .MustCompile (* destination ))).
126+ proxy .OnRequest (goproxy .ReqHostMatches (regexp .MustCompile (d . cfg . destination ))).
128127 HijackConnect (func (req * http.Request , client net.Conn , ctx * goproxy.ProxyCtx ) {
129128 defer func () {
130129 if e := recover (); e != nil {
@@ -151,29 +150,30 @@ func main() {
151150 })
152151
153152 // processing connections
154- proxy .OnRequest (goproxy .ReqHostMatches (regexp .MustCompile (* destination ))).DoFunc (
153+ proxy .OnRequest (goproxy .ReqHostMatches (regexp .MustCompile (cfg . destination ))).DoFunc (
155154 func (r * http.Request , ctx * goproxy.ProxyCtx ) (* http.Request , * http.Response ) {
156155 return d .processRequest (r )
157156 })
158157
159158 go d .startAdminInterface ()
160159
161- proxy .Verbose = * verbose
160+ proxy .Verbose = d . cfg . verbose
162161 // proxy starting message
163162 log .WithFields (log.Fields {
164- "Destination" : * destination ,
165- "ProxyPort" : port ,
166- "Mode" : AppConfig . mode ,
167- }).Info ("Proxy is starting ..." )
163+ "Destination" : d . cfg . destination ,
164+ "ProxyPort" : d . cfg . proxyPort ,
165+ "Mode" : d . cfg . GetMode () ,
166+ }).Info ("Proxy prepared ..." )
168167
169- log . Warn ( http . ListenAndServe ( port , proxy ))
168+ return proxy , d
170169}
171170
172171// processRequest - processes incoming requests and based on proxy state (record/playback)
173172// returns HTTP response.
174173func (d * DBClient ) processRequest (req * http.Request ) (* http.Request , * http.Response ) {
175174
176- if AppConfig .mode == CaptureMode {
175+ mode := d .cfg .GetMode ()
176+ if mode == CaptureMode {
177177 log .Info ("*** Capture ***" )
178178 newResponse , err := d .captureRequest (req )
179179 if err != nil {
@@ -184,19 +184,19 @@ func (d *DBClient) processRequest(req *http.Request) (*http.Request, *http.Respo
184184 return req , newResponse
185185 }
186186
187- } else if AppConfig . mode == SynthesizeMode {
187+ } else if mode == SynthesizeMode {
188188 log .Info ("*** Sinthesize ***" )
189- response := synthesizeResponse (req , AppConfig .middleware )
189+ response := synthesizeResponse (req , d . cfg .middleware )
190190 return req , response
191191
192- } else if AppConfig . mode == ModifyMode {
192+ } else if mode == ModifyMode {
193193 log .Info ("*** Modify ***" )
194- response , err := d .modifyRequestResponse (req , AppConfig .middleware )
194+ response , err := d .modifyRequestResponse (req , d . cfg .middleware )
195195
196196 if err != nil {
197197 log .WithFields (log.Fields {
198198 "error" : err .Error (),
199- "middleware" : AppConfig .middleware ,
199+ "middleware" : d . cfg .middleware ,
200200 }).Error ("Got error when performing request modification" )
201201 return req , nil
202202 }
0 commit comments