mirror of
https://github.com/simon987/Architeuthis.git
synced 2025-04-10 13:36:41 +00:00
Switch to goproxy
This commit is contained in:
parent
0cbfca00e0
commit
9fff38f0f5
95
main.go
95
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()
|
||||
}
|
||||
proxy := New()
|
||||
proxy.Run()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user