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

Fix CONNECT with downstream proxy #341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 16 additions & 14 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/google/martian/v3/nosigpipe"
"github.com/google/martian/v3/proxyutil"
"github.com/google/martian/v3/trafficshape"
"golang.org/x/net/proxy"
)

var errClose = errors.New("closing connection")
Expand Down Expand Up @@ -606,30 +607,31 @@ func (p *Proxy) roundTrip(ctx *Context, req *http.Request) (*http.Response, erro
}

func (p *Proxy) connect(req *http.Request) (*http.Response, net.Conn, error) {
var (
conn net.Conn
err error
)

if p.proxyURL != nil {
log.Debugf("martian: CONNECT with downstream proxy: %s", p.proxyURL.Host)

conn, err := p.dial("tcp", p.proxyURL.Host)
dialer, err := proxy.FromURL(
p.proxyURL, &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
},
)
if err != nil {
return nil, nil, err
}
pbw := bufio.NewWriter(conn)
pbr := bufio.NewReader(conn)

req.Write(pbw)
pbw.Flush()

res, err := http.ReadResponse(pbr, req)
if err != nil {
return nil, nil, err
}
conn, err = dialer.Dial("tcp", req.URL.Host)
} else {
log.Debugf("martian: CONNECT to host directly: %s", req.URL.Host)

return res, conn, nil
conn, err = p.dial("tcp", req.URL.Host)
}

log.Debugf("martian: CONNECT to host directly: %s", req.URL.Host)

conn, err := p.dial("tcp", req.URL.Host)
if err != nil {
return nil, nil, err
}
Expand Down