mirror of
https://github.com/simon987/Architeuthis.git
synced 2025-04-19 23:46:41 +00:00
Switch to goproxy
This commit is contained in:
parent
0cbfca00e0
commit
9fff38f0f5
93
main.go
93
main.go
@ -1,81 +1,48 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"flag"
|
||||||
"fmt"
|
"github.com/elazarl/goproxy"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/valyala/fasthttp"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WebProxy struct {
|
type WebProxy struct {
|
||||||
server *fasthttp.Server
|
server *goproxy.ProxyHttpServer
|
||||||
Limiters sync.Map
|
Limiters sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
var proxyClient = &fasthttp.Client{
|
func LogRequestMiddleware(h goproxy.FuncReqHandler) goproxy.ReqHandler {
|
||||||
}
|
return goproxy.FuncReqHandler(func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
|
||||||
|
|
||||||
|
|
||||||
func LogRequestMiddleware(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
|
||||||
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
|
|
||||||
|
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"path": string(ctx.Path()),
|
"host": string(r.Host),
|
||||||
"header": ctx.Request.Header.String(),
|
}).Trace(r.Method)
|
||||||
}).Trace(string(ctx.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 {
|
func New() *WebProxy {
|
||||||
|
|
||||||
proxy := new(WebProxy)
|
proxy := new(WebProxy)
|
||||||
|
|
||||||
proxy.server = &fasthttp.Server{
|
proxy.server = goproxy.NewProxyHttpServer()
|
||||||
Handler: LogRequestMiddleware(Index),
|
|
||||||
|
|
||||||
}
|
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
|
return proxy
|
||||||
}
|
}
|
||||||
@ -84,16 +51,16 @@ func (proxy *WebProxy) Run() {
|
|||||||
|
|
||||||
logrus.Infof("Started web proxy at address %s", "localhost:5050")
|
logrus.Infof("Started web proxy at address %s", "localhost:5050")
|
||||||
|
|
||||||
err := proxy.server.ListenAndServe("localhost:5050")
|
addr := flag.String("addr", ":5050", "proxy listen address")
|
||||||
if err != nil {
|
flag.Parse()
|
||||||
logrus.Fatalf("Error in ListenAndServe: %s", err)
|
//proxy.Verbose = true
|
||||||
}
|
|
||||||
|
go logrus.Fatal(http.ListenAndServe(*addr, proxy.server))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
logrus.SetLevel(logrus.TraceLevel)
|
logrus.SetLevel(logrus.TraceLevel)
|
||||||
|
|
||||||
p := New()
|
proxy := New()
|
||||||
p.Run()
|
proxy.Run()
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user