func startInTheMiddle(args Args) { proxy.WaitForExitSignal() tr := transport.Transport{Proxy: transport.ProxyFromEnvironment} proxy.Start(proxy.Options{ Ip: args.Ip, Port: args.Port, ExportFolder: args.ExportFolder, Record: args.Record, OnRequest: func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { ctx.RoundTripper = goproxy.RoundTripperFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (resp *http.Response, err error) { ctx.UserData, resp, err = tr.DetailedRoundTrip(req) return }) reqBody, err := httputil.DumpRequest(req, true) if err != nil { logger.Error(err) os.Exit(1) } r := httper.NewRequest(string(reqBody)) logger.Info(inPFunc("--> ") + r.ToString()) if !args.Record { resp, err := cacher.Find(req) if err == nil { logger.Debug("Cache HIT") return req, resp } logger.Debug("Cache MISSED") } return req, nil }, OnResponse: func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { respBody, err := httputil.DumpResponse(resp, true) if err != nil { logger.Error(err) os.Exit(1) } r := httper.NewResponse(string(respBody)) logger.Info(outPFunc("<-- ") + r.ToString()) return resp }, }) }
func WaitForExitSignal() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) signal.Notify(c, syscall.SIGTERM) go func() { <-c if !options.Record { logger.Info("In the middle has been terminated from REPLAY mode.") } else { logger.Info("Flush cache to file.") cacher.Flush() logger.Info("In the middle has been terminated from RECORD mode. See stub.json in export folder for exported session.") } os.Exit(1) }() }
func Start(op Options) { options = op proxy = goproxy.NewProxyHttpServer() proxy.Verbose = false proxy.OnRequest().DoFunc(onRequestHandler) proxy.OnResponse().DoFunc(onResponseHandler) cacher.SetExportFolder(options.ExportFolder) if !options.Record { cacher.Load("stub.json") logger.Info("In the middle has been started in REPLAY mode. Press ^C to terminate In the middle.") } else { logger.Info("In the middle has been started in RECORD mode. Press ^C to terminate In the middle.") } logger.Info("Current settings") logger.Info(options) http.ListenAndServe(options.Ip+":"+options.Port, proxy) }
func testArgs() (args Args) { flag.StringVar(&args.Ip, "ip", "0.0.0.0", "Listening IP address") flag.StringVar(&args.Port, "port", "8080", "Listening port") flag.StringVar(&args.ExportFolder, "export", "./fixtures", "Exporting folder") flag.BoolVar(&args.Record, "record", false, "Record mode (Record all activities)") flag.BoolVar(&args.Help, "?", false, "Show usage") flag.Parse() if args.Help { logger.Info("Usage of In the middle:") flag.PrintDefaults() os.Exit(0) } return }
func welcome() { logger.Info("*****************") logger.Info("* IN THE MIDDLE *") logger.Info("*****************") }