Add support for proxy authentication

This only supports the simplest case: a username and password in the proxy URL,
and http basic auth.
This commit is contained in:
Matthew McPherrin
2020-01-09 22:56:50 -08:00
parent 199f2fe2a4
commit e336bddafc
2 changed files with 24 additions and 6 deletions
+20
View File
@@ -2,8 +2,12 @@ package starttls
import (
"crypto/tls"
"fmt"
"net"
"net/url"
"time"
"github.com/mwitkow/go-http-dialer"
)
type timeoutError struct{}
@@ -51,3 +55,19 @@ func dialWithDialer(dialer Dialer, timeout time.Duration, network, addr string,
return conn, nil
}
func wrapDialerWithProxy(dialer Dialer, connectProxy *url.URL, tlsConfig *tls.Config) (Dialer, error) {
dialerOpt := http_dialer.WithDialer(dialer.(*net.Dialer))
tlsOpt := http_dialer.WithTls(tlsConfig)
if connectProxy.User != nil {
password, ok := connectProxy.User.Password()
if !ok {
return nil, fmt.Errorf("proxy username without password not currently supported")
}
auth := http_dialer.WithProxyAuth(http_dialer.AuthBasic(connectProxy.User.Username(), password))
dialer = http_dialer.New(connectProxy, dialerOpt, tlsOpt, auth)
} else {
dialer = http_dialer.New(connectProxy, dialerOpt, tlsOpt)
}
return dialer, nil
}
+4 -6
View File
@@ -30,8 +30,6 @@ import (
"github.com/square/certigo/starttls/ldap"
"github.com/square/certigo/starttls/mysql"
pq "github.com/square/certigo/starttls/psql"
http_dialer "github.com/mwitkow/go-http-dialer"
)
// Protocols are the names of supported protocols
@@ -142,10 +140,10 @@ func GetConnectionState(startTLSType, connectName, connectTo, identity, clientCe
}
if connectProxy != nil {
dialer = http_dialer.New(
connectProxy,
http_dialer.WithDialer(dialer.(*net.Dialer)),
http_dialer.WithTls(tlsConfig))
dialer, err = wrapDialerWithProxy(dialer, connectProxy, tlsConfig)
if err != nil {
return nil, nil, err
}
}
go func() {