From 9fff38f0f553a56b0b99791f2fa6fe0c33436c06 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 28 May 2019 12:13:26 -0400 Subject: [PATCH] Switch to goproxy --- main.go | 95 +++++++++++++++++++-------------------------------------- 1 file changed, 31 insertions(+), 64 deletions(-) diff --git a/main.go b/main.go index 3eb6cf2..1c91f5a 100644 --- a/main.go +++ b/main.go @@ -1,81 +1,48 @@ package main import ( - "bytes" - "fmt" + "flag" + "github.com/elazarl/goproxy" "github.com/sirupsen/logrus" - "github.com/valyala/fasthttp" + "net/http" + "regexp" "sync" ) type WebProxy struct { - server *fasthttp.Server + server *goproxy.ProxyHttpServer Limiters sync.Map } -var proxyClient = &fasthttp.Client{ -} - - -func LogRequestMiddleware(h fasthttp.RequestHandler) fasthttp.RequestHandler { - return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) { +func LogRequestMiddleware(h goproxy.FuncReqHandler) goproxy.ReqHandler { + return goproxy.FuncReqHandler(func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { logrus.WithFields(logrus.Fields{ - "path": string(ctx.Path()), - "header": ctx.Request.Header.String(), - }).Trace(string(ctx.Method())) + "host": string(r.Host), + }).Trace(r.Method) - h(ctx) + return h(r, ctx) }) } -func Index(ctx *fasthttp.RequestCtx) { - - if bytes.Equal([]byte("localhost:5050"), ctx.Host()) { - logrus.Warn("Ignoring same host request") - _, _ = fmt.Fprintf(ctx, "Ignoring same host request") - ctx.Response.Header.SetStatusCode(400) - return - } - - req := &ctx.Request - resp := &ctx.Response - - prepareRequest(req) - - if err := proxyClient.Do(req, resp); err != nil { - logrus.WithError(err).Error("error when proxying request") - } - - postprocessResponse(resp) -} - -func prepareRequest(req *fasthttp.Request) { - // do not proxy "Connection" header. - req.Header.Del("Connection") - - // strip other unneeded headers. - - // alter other request params before sending them to upstream host -} - -func postprocessResponse(resp *fasthttp.Response) { - // do not proxy "Connection" header - resp.Header.Del("Connection") - // strip other unneeded headers - - // alter other response data if needed -} - - func New() *WebProxy { proxy := new(WebProxy) - proxy.server = &fasthttp.Server{ - Handler: LogRequestMiddleware(Index), + proxy.server = goproxy.NewProxyHttpServer() - } + proxy.server.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*$"))). + HandleConnect(goproxy.AlwaysMitm) + + proxy.server.OnRequest().Do( + LogRequestMiddleware( + func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { + + logrus.Warn("TEST") + return r, nil + }, + ), + ) return proxy } @@ -84,16 +51,16 @@ func (proxy *WebProxy) Run() { logrus.Infof("Started web proxy at address %s", "localhost:5050") - err := proxy.server.ListenAndServe("localhost:5050") - if err != nil { - logrus.Fatalf("Error in ListenAndServe: %s", err) - } + addr := flag.String("addr", ":5050", "proxy listen address") + flag.Parse() + //proxy.Verbose = true + + go logrus.Fatal(http.ListenAndServe(*addr, proxy.server)) } func main() { - logrus.SetLevel(logrus.TraceLevel) - p := New() - p.Run() -} \ No newline at end of file + proxy := New() + proxy.Run() +}