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

Allow setting ConnectDial on context #392

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goproxy

import (
"crypto/tls"
"net"
"net/http"
"regexp"
)
Expand All @@ -13,7 +14,10 @@ type ProxyCtx struct {
Req *http.Request
// Will contain the remote server's response (if available. nil if the request wasn't send yet)
Resp *http.Response
// Can be used to override the RoundTripper for a single request.
RoundTripper RoundTripper
// Can be used to override the ConnectDial for a single request.
ConnectDial func(network, addr string) (net.Conn, error)
// will contain the recent error that occurred while trying to send receive or parse traffic
Error error
// A handle for the user to keep data in the context, from the call of ReqHandler to the
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/elazarl/goproxy

go 1.14

require github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2
15 changes: 9 additions & 6 deletions https.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ func (proxy *ProxyHttpServer) dial(network, addr string) (c net.Conn, err error)
return net.Dial(network, addr)
}

func (proxy *ProxyHttpServer) connectDial(network, addr string) (c net.Conn, err error) {
if proxy.ConnectDial == nil {
return proxy.dial(network, addr)
func (proxy *ProxyHttpServer) connectDial(ctx *ProxyCtx, network, addr string) (c net.Conn, err error) {
if ctx != nil && ctx.ConnectDial != nil{
return ctx.ConnectDial(network, addr)
}
return proxy.ConnectDial(network, addr)
if proxy.ConnectDial != nil {
return proxy.ConnectDial(network, addr)
}
return proxy.dial(network, addr)
}

type halfClosable interface {
Expand Down Expand Up @@ -106,7 +109,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
if !hasPort.MatchString(host) {
host += ":80"
}
targetSiteCon, err := proxy.connectDial("tcp", host)
targetSiteCon, err := proxy.connectDial(ctx, "tcp", host)
if err != nil {
httpError(proxyClient, ctx, err)
return
Expand Down Expand Up @@ -137,7 +140,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
case ConnectHTTPMitm:
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("tcp", host)
targetSiteCon, err := proxy.connectDial(ctx, "tcp", host)
if err != nil {
ctx.Warnf("Error dialing to %s: %s", host, err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (proxy *ProxyHttpServer) serveWebsocketTLS(ctx *ProxyCtx, w http.ResponseWr
func (proxy *ProxyHttpServer) serveWebsocket(ctx *ProxyCtx, w http.ResponseWriter, req *http.Request) {
targetURL := url.URL{Scheme: "ws", Host: req.URL.Host, Path: req.URL.Path}

targetConn, err := proxy.connectDial("tcp", targetURL.Host)
targetConn, err := proxy.connectDial(ctx, "tcp", targetURL.Host)
if err != nil {
ctx.Warnf("Error dialing target site: %v", err)
return
Expand Down