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

Support http proxy correctly #939

Closed
wants to merge 2 commits into from
Closed
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
38 changes: 36 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"net/url"
"strings"
"time"

"golang.org/x/net/proxy"
)

// ErrBadHandshake is returned when the server response to opening handshake is
Expand Down Expand Up @@ -281,8 +283,40 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
if err != nil {
return nil, nil, err
}
if proxyURL != nil {
netDial, err = proxyFromURL(proxyURL, netDial)

getDefaultDialerFunc := func() (netDialerFunc, error) {
dialer, err := proxy.FromURL(proxyURL, netDial)
if err != nil {
return nil, err
}
if d, ok := dialer.(proxy.ContextDialer); ok {
return d.DialContext, nil
} else {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}, nil
}
}

switch {
case proxyURL.Scheme == "socks5":
netDial, err = getDefaultDialerFunc()
if err != nil {
return nil, nil, err
}
case proxyURL == nil:
// Do nothing. Not using a proxy.
case u.Scheme == "http":
if pa := proxyAuth(proxyURL.User); pa != "" {
req.Header.Set("Proxy-Authorization", pa)
}
netDial = func(ctx context.Context, network, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, proxyURL.Host)
}
case u.Scheme == "https":
netDial = (&httpsProxyDialer{proxyURL: proxyURL, forwardDial: netDial}).DialContext
default:
netDial, err = getDefaultDialerFunc()
if err != nil {
return nil, nil, err
}
Expand Down
31 changes: 15 additions & 16 deletions client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,19 @@ func TestProxyDial(t *testing.T) {
cstDialer := cstDialer // make local copy for modification on next line.
cstDialer.Proxy = http.ProxyURL(surl)

connect := false
origHandler := s.Server.Config.Handler

// Capture the request Host header.
s.Server.Config.Handler = http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodConnect {
connect = true
// HTTPS_PROXY comes here.
w.WriteHeader(http.StatusOK)
return
}

if !connect {
t.Log("connect not received")
http.Error(w, "connect not received", http.StatusMethodNotAllowed)
return
}
// HTTP_PROXY comes here.
origHandler.ServeHTTP(w, r)
return
})

ws, _, err := cstDialer.Dial(s.URL, nil)
Expand All @@ -211,25 +206,29 @@ func TestProxyAuthorizationDial(t *testing.T) {
cstDialer := cstDialer // make local copy for modification on next line.
cstDialer.Proxy = http.ProxyURL(surl)

connect := false
origHandler := s.Server.Config.Handler

// Capture the request Host header.
s.Server.Config.Handler = http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
proxyAuth := r.Header.Get("Proxy-Authorization")
expectedProxyAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("username:password"))
if r.Method == http.MethodConnect && proxyAuth == expectedProxyAuth {
connect = true
w.WriteHeader(http.StatusOK)
if proxyAuth != expectedProxyAuth {
msg := fmt.Sprintf("expected proxy authorization is %q, but %q is given", expectedProxyAuth, proxyAuth)

t.Log(msg)
http.Error(
w,
msg,
http.StatusProxyAuthRequired,
)
return
}

if !connect {
t.Log("connect with proxy authorization not received")
http.Error(w, "connect with proxy authorization not received", http.StatusMethodNotAllowed)
return
if r.Method == http.MethodConnect {
w.WriteHeader(http.StatusOK)
}

origHandler.ServeHTTP(w, r)
})

Expand Down
40 changes: 14 additions & 26 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"net/http"
"net/url"
"strings"

"golang.org/x/net/proxy"
)

type netDialerFunc func(ctx context.Context, network, addr string) (net.Conn, error)
Expand All @@ -28,41 +26,21 @@ func (fn netDialerFunc) DialContext(ctx context.Context, network, addr string) (
return fn(ctx, network, addr)
}

func proxyFromURL(proxyURL *url.URL, forwardDial netDialerFunc) (netDialerFunc, error) {
if proxyURL.Scheme == "http" {
return (&httpProxyDialer{proxyURL: proxyURL, forwardDial: forwardDial}).DialContext, nil
}
dialer, err := proxy.FromURL(proxyURL, forwardDial)
if err != nil {
return nil, err
}
if d, ok := dialer.(proxy.ContextDialer); ok {
return d.DialContext, nil
}
return func(ctx context.Context, net, addr string) (net.Conn, error) {
return dialer.Dial(net, addr)
}, nil
}

type httpProxyDialer struct {
type httpsProxyDialer struct {
proxyURL *url.URL
forwardDial netDialerFunc
}

func (hpd *httpProxyDialer) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
func (hpd *httpsProxyDialer) DialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
hostPort, _ := hostPortNoPort(hpd.proxyURL)
conn, err := hpd.forwardDial(ctx, network, hostPort)
if err != nil {
return nil, err
}

connectHeader := make(http.Header)
if user := hpd.proxyURL.User; user != nil {
proxyUser := user.Username()
if proxyPassword, passwordSet := user.Password(); passwordSet {
credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
connectHeader.Set("Proxy-Authorization", "Basic "+credential)
}
if pa := proxyAuth(hpd.proxyURL.User); pa != "" {
connectHeader.Set("Proxy-Authorization", pa)
}

connectReq := &http.Request{
Expand Down Expand Up @@ -103,3 +81,13 @@ func (hpd *httpProxyDialer) DialContext(ctx context.Context, network string, add
}
return conn, nil
}

func proxyAuth(user *url.Userinfo) string {
if user != nil {
proxyUser := user.Username()
if proxyPassword, passwordSet := user.Password(); passwordSet {
return "Basic " + base64.StdEncoding.EncodeToString([]byte(proxyUser+":"+proxyPassword))
}
}
return ""
}