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

Connect related changes - add optional hooks #504

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
3 changes: 2 additions & 1 deletion examples/cascadeproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func main() {
middleProxy.Tr.Proxy = func(req *http.Request) (*url.URL, error) {
return url.Parse("http://localhost:8082")
}
connectReqHandler := func(req *http.Request) {
connectReqHandler := func(req *http.Request) error {
SetBasicAuth(username, password, req)
return nil
}
middleProxy.ConnectDial = middleProxy.NewConnectDialToProxyWithHandler("http://localhost:8082", connectReqHandler)
middleProxy.OnRequest().Do(goproxy.FuncReqHandler(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
Expand Down
46 changes: 38 additions & 8 deletions https.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
return
}
ctx.Logf("Accepting CONNECT to %s", host)
proxyClient.Write([]byte("HTTP/1.0 200 Connection established\r\n\r\n"))
if todo.Hijack != nil {
todo.Hijack(r, proxyClient, ctx)
} else {
proxyClient.Write([]byte("HTTP/1.0 200 Connection established\r\n\r\n"))
}

targetTCP, targetOK := targetSiteCon.(halfClosable)
proxyClientTCP, clientOK := proxyClient.(halfClosable)
Expand All @@ -156,7 +160,11 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
case ConnectHijack:
todo.Hijack(r, proxyClient, ctx)
case ConnectHTTPMitm:
proxyClient.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
if todo.Hijack != nil {
todo.Hijack(r, proxyClient, ctx)
} else {
proxyClient.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
}
ctx.Logf("Assuming CONNECT is plain HTTP tunneling, mitm proxying it")
targetSiteCon, err := proxy.connectDial(ctx, "tcp", host)
if err != nil {
Expand Down Expand Up @@ -193,7 +201,11 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
}
}
case ConnectMitm:
proxyClient.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
if todo.Hijack != nil {
todo.Hijack(r, proxyClient, ctx)
} else {
proxyClient.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
}
ctx.Logf("Assuming CONNECT is TLS, mitm proxying it")
// this goes in a separate goroutine, so that the net/http server won't think we're
// still handling the request even after hijacking the connection. Those HTTP CONNECT
Expand Down Expand Up @@ -363,7 +375,11 @@ func (proxy *ProxyHttpServer) NewConnectDialToProxy(https_proxy string) func(net
return proxy.NewConnectDialToProxyWithHandler(https_proxy, nil)
}

func (proxy *ProxyHttpServer) NewConnectDialToProxyWithHandler(https_proxy string, connectReqHandler func(req *http.Request)) func(network, addr string) (net.Conn, error) {
func (proxy *ProxyHttpServer) NewConnectDialToProxyWithHandler(https_proxy string, connectReqHandler func(req *http.Request) error) func(network, addr string) (net.Conn, error) {
return proxy.NewConnectDialToProxyWithMoreHandlers(https_proxy, nil, nil)
}

func (proxy *ProxyHttpServer) NewConnectDialToProxyWithMoreHandlers(https_proxy string, connectReqHandler func(req *http.Request) error, connectRespHandler func(req *http.Response) error) func(network, addr string) (net.Conn, error) {
u, err := url.Parse(https_proxy)
if err != nil {
return nil
Expand All @@ -380,7 +396,9 @@ func (proxy *ProxyHttpServer) NewConnectDialToProxyWithHandler(https_proxy strin
Header: make(http.Header),
}
if connectReqHandler != nil {
connectReqHandler(connectReq)
if err := connectReqHandler(connectReq); err != nil {
return nil, err
}
}
c, err := proxy.dial(network, u.Host)
if err != nil {
Expand All @@ -397,7 +415,12 @@ func (proxy *ProxyHttpServer) NewConnectDialToProxyWithHandler(https_proxy strin
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
if connectRespHandler != nil {
if err := connectRespHandler(resp); err != nil {
c.Close()
return nil, err
}
} else if resp.StatusCode != 200 {
resp, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -425,7 +448,9 @@ func (proxy *ProxyHttpServer) NewConnectDialToProxyWithHandler(https_proxy strin
Header: make(http.Header),
}
if connectReqHandler != nil {
connectReqHandler(connectReq)
if err := connectReqHandler(connectReq); err != nil {
return nil, err
}
}
connectReq.Write(c)
// Read response.
Expand All @@ -438,7 +463,12 @@ func (proxy *ProxyHttpServer) NewConnectDialToProxyWithHandler(https_proxy strin
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
if connectRespHandler != nil {
if err := connectRespHandler(resp); err != nil {
c.Close()
return nil, err
}
} else if resp.StatusCode != 200 {
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 500))
if err != nil {
return nil, err
Expand Down