Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Export Handlers (req, resp, https) #445

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ There are 3 kinds of useful handlers to manipulate the behavior, as follows:
```go
// handler called after receiving HTTP CONNECT from the client, and before proxy establish connection
// with destination host
httpsHandlers []HttpsHandler
HttpsHandlers []HttpsHandler

// handler called before proxy send HTTP request to destination host
reqHandlers []ReqHandler
ReqHandlers []ReqHandler

// handler called after proxy receives HTTP Response from destination host, and before proxy forward
// the Response to the client.
respHandlers []RespHandler
RespHandlers []RespHandler
```

Depending on what you want to manipulate, the ways to add handlers to each handler list are:
Expand Down
8 changes: 4 additions & 4 deletions dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (pcond *ReqProxyConds) DoFunc(f func(req *http.Request, ctx *ProxyCtx) (*ht
// // given request to the proxy, will test if cond1.HandleReq(req,ctx) && cond2.HandleReq(req,ctx) are true
// // if they are, will call handler.Handle(req,ctx)
func (pcond *ReqProxyConds) Do(h ReqHandler) {
pcond.proxy.reqHandlers = append(pcond.proxy.reqHandlers,
*pcond.proxy.ReqHandlers = append(*pcond.proxy.ReqHandlers,
FuncReqHandler(func(r *http.Request, ctx *ProxyCtx) (*http.Request, *http.Response) {
for _, cond := range pcond.reqConds {
if !cond.HandleReq(r, ctx) {
Expand All @@ -229,7 +229,7 @@ func (pcond *ReqProxyConds) Do(h ReqHandler) {
// will use the default tls configuration.
// proxy.OnRequest().HandleConnect(goproxy.AlwaysReject) // rejects all CONNECT requests
func (pcond *ReqProxyConds) HandleConnect(h HttpsHandler) {
pcond.proxy.httpsHandlers = append(pcond.proxy.httpsHandlers,
*pcond.proxy.HttpsHandlers = append(*pcond.proxy.HttpsHandlers,
FuncHttpsHandler(func(host string, ctx *ProxyCtx) (*ConnectAction, string) {
for _, cond := range pcond.reqConds {
if !cond.HandleReq(ctx.Req, ctx) {
Expand Down Expand Up @@ -257,7 +257,7 @@ func (pcond *ReqProxyConds) HandleConnectFunc(f func(host string, ctx *ProxyCtx)
}

func (pcond *ReqProxyConds) HijackConnect(f func(req *http.Request, client net.Conn, ctx *ProxyCtx)) {
pcond.proxy.httpsHandlers = append(pcond.proxy.httpsHandlers,
*pcond.proxy.HttpsHandlers = append(*pcond.proxy.HttpsHandlers,
FuncHttpsHandler(func(host string, ctx *ProxyCtx) (*ConnectAction, string) {
for _, cond := range pcond.reqConds {
if !cond.HandleReq(ctx.Req, ctx) {
Expand Down Expand Up @@ -285,7 +285,7 @@ func (pcond *ProxyConds) DoFunc(f func(resp *http.Response, ctx *ProxyCtx) *http
// ProxyConds.Do will register the RespHandler on the proxy, h.Handle(resp,ctx) will be called on every
// request that matches the conditions aggregated in pcond.
func (pcond *ProxyConds) Do(h RespHandler) {
pcond.proxy.respHandlers = append(pcond.proxy.respHandlers,
*pcond.proxy.RespHandlers = append(*pcond.proxy.RespHandlers,
FuncRespHandler(func(resp *http.Response, ctx *ProxyCtx) *http.Response {
for _, cond := range pcond.reqConds {
if !cond.HandleReq(ctx.Req, ctx) {
Expand Down
4 changes: 2 additions & 2 deletions https.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
panic("Cannot hijack connection " + e.Error())
}

ctx.Logf("Running %d CONNECT handlers", len(proxy.httpsHandlers))
ctx.Logf("Running %d CONNECT handlers", len(*proxy.HttpsHandlers))
todo, host := OkConnect, r.URL.Host
for i, h := range proxy.httpsHandlers {
for i, h := range *proxy.HttpsHandlers {
newtodo, newhost := h.HandleConnect(host, ctx)

// If found a result, break the loop immediately
Expand Down
16 changes: 8 additions & 8 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type ProxyHttpServer struct {
Verbose bool
Logger Logger
NonproxyHandler http.Handler
reqHandlers []ReqHandler
respHandlers []RespHandler
httpsHandlers []HttpsHandler
ReqHandlers *[]ReqHandler
RespHandlers *[]RespHandler
HttpsHandlers *[]HttpsHandler
Tr *http.Transport
// ConnectDial will be used to create TCP connections for CONNECT requests
// if nil Tr.Dial will be used
Expand Down Expand Up @@ -59,7 +59,7 @@ func isEof(r *bufio.Reader) bool {

func (proxy *ProxyHttpServer) filterRequest(r *http.Request, ctx *ProxyCtx) (req *http.Request, resp *http.Response) {
req = r
for _, h := range proxy.reqHandlers {
for _, h := range *proxy.ReqHandlers {
req, resp = h.Handle(r, ctx)
// non-nil resp means the handler decided to skip sending the request
// and return canned response instead.
Expand All @@ -71,7 +71,7 @@ func (proxy *ProxyHttpServer) filterRequest(r *http.Request, ctx *ProxyCtx) (req
}
func (proxy *ProxyHttpServer) filterResponse(respOrig *http.Response, ctx *ProxyCtx) (resp *http.Response) {
resp = respOrig
for _, h := range proxy.respHandlers {
for _, h := range *proxy.RespHandlers {
ctx.Resp = resp
resp = h.Handle(resp, ctx)
}
Expand Down Expand Up @@ -210,9 +210,9 @@ func (proxy *ProxyHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
func NewProxyHttpServer() *ProxyHttpServer {
proxy := ProxyHttpServer{
Logger: log.New(os.Stderr, "", log.LstdFlags),
reqHandlers: []ReqHandler{},
respHandlers: []RespHandler{},
httpsHandlers: []HttpsHandler{},
ReqHandlers: &[]ReqHandler{},
RespHandlers: &[]RespHandler{},
HttpsHandlers: &[]HttpsHandler{},
NonproxyHandler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
http.Error(w, "This is a proxy server. Does not respond to non-proxy requests.", 500)
}),
Expand Down