support http proxy correctly

This commit is contained in:
Cooper Oh 2024-06-20 16:17:03 +09:00
parent 1d5465562b
commit 76d8f8c503
2 changed files with 37 additions and 28 deletions

View File

@ -17,6 +17,8 @@ import (
"net/url" "net/url"
"strings" "strings"
"time" "time"
"golang.org/x/net/proxy"
) )
// ErrBadHandshake is returned when the server response to opening handshake is // ErrBadHandshake is returned when the server response to opening handshake is
@ -281,11 +283,30 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
if proxyURL != nil { switch {
netDial, err = proxyFromURL(proxyURL, netDial) 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:
dialer, err := proxy.FromURL(proxyURL, netDial)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
if d, ok := dialer.(proxy.ContextDialer); ok {
netDial = d.DialContext
} else {
netDial = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
}
} }
} }

View File

@ -14,8 +14,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"golang.org/x/net/proxy"
) )
type netDialerFunc func(ctx context.Context, network, addr string) (net.Conn, error) type netDialerFunc func(ctx context.Context, network, addr string) (net.Conn, error)
@ -28,28 +26,12 @@ func (fn netDialerFunc) DialContext(ctx context.Context, network, addr string) (
return fn(ctx, network, addr) return fn(ctx, network, addr)
} }
func proxyFromURL(proxyURL *url.URL, forwardDial netDialerFunc) (netDialerFunc, error) { type httpsProxyDialer struct {
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 {
proxyURL *url.URL proxyURL *url.URL
forwardDial netDialerFunc 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) hostPort, _ := hostPortNoPort(hpd.proxyURL)
conn, err := hpd.forwardDial(ctx, network, hostPort) conn, err := hpd.forwardDial(ctx, network, hostPort)
if err != nil { if err != nil {
@ -57,12 +39,8 @@ func (hpd *httpProxyDialer) DialContext(ctx context.Context, network string, add
} }
connectHeader := make(http.Header) connectHeader := make(http.Header)
if user := hpd.proxyURL.User; user != nil { if pa := proxyAuth(hpd.proxyURL.User); pa != "" {
proxyUser := user.Username() connectHeader.Set("Proxy-Authorization", pa)
if proxyPassword, passwordSet := user.Password(); passwordSet {
credential := base64.StdEncoding.EncodeToString([]byte(proxyUser + ":" + proxyPassword))
connectHeader.Set("Proxy-Authorization", "Basic "+credential)
}
} }
connectReq := &http.Request{ connectReq := &http.Request{
@ -103,3 +81,13 @@ func (hpd *httpProxyDialer) DialContext(ctx context.Context, network string, add
} }
return conn, nil 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 ""
}