Ejemplo n.º 1
0
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
		},
	})
}
Ejemplo n.º 2
0
func Match(req *http.Request, m *MatchOption) bool {
	t := m.Type
	if t == "" {
		t = "plain"
	}

	reqBody, err := httputil.DumpRequest(req, true)
	if err != nil {
		return false
	}

	r := httper.NewRequest(string(reqBody))

	return matchers[t].Match(&r, m)
}