diff --git a/ziti/config.go b/ziti/config.go index 4c6a499b..ed91c95c 100644 --- a/ziti/config.go +++ b/ziti/config.go @@ -19,6 +19,7 @@ package ziti import ( "crypto/x509" "encoding/json" + "github.com/michaelquigley/pfxlog" "github.com/openziti/edge-api/rest_util" "github.com/openziti/identity" apis "github.com/openziti/sdk-golang/edge-apis" @@ -116,6 +117,8 @@ func NewConfigFromFile(confFile string) (*Config, error) { return nil, errors.Errorf("failed to load ziti configuration (%s): %v", confFile, err) } + c.RouterProxy = routerProxyFromEnvironment + return &c, nil } @@ -127,3 +130,44 @@ func NewConfigFromFile(confFile string) (*Config, error) { func GetControllerWellKnownCaPool(controllerAddr string) (*x509.CertPool, error) { return rest_util.GetControllerWellKnownCaPool(controllerAddr) } + +// routerProxyFromEnvironment will return a ProxyConfiguration for the given address based on the environment variables +func routerProxyFromEnvironment(addr string) *transport.ProxyConfiguration { + // Create a request with the address to parse + parsedURL, errParse := parseTLS(addr) + if errParse != nil { + pfxlog.Logger().Warnf("Could not parse URL. Error: %s", errParse.Error()) + return nil + } + req := &http.Request{URL: parsedURL} + + // Parse the HTTPS_PROXY or HTTP_PROXY env for this address + proxyURL, errProxy := http.ProxyFromEnvironment(req) + if errProxy != nil { + pfxlog.Logger().Warnf("Could not determine proxy from environment. Error: %s", errProxy.Error()) + return nil + } + if proxyURL == nil { + return nil // no proxy + } + + return &transport.ProxyConfiguration{ + Type: transport.ProxyTypeHttpConnect, + Address: proxyURL.Host, + } +} + +// parseTLS is a helper function to parse a raw URL string that may be prefixed with "tls:". +// If the URL is prefixed with "tls:", it will prepend "https://" and reparse it. +func parseTLS(raw string) (*url.URL, error) { + u, err := url.Parse(raw) + if err != nil { + return nil, err + } + + if u.Scheme == "tls" { + // Prepend standard "https://" and reparse + return url.Parse("https://" + u.Opaque) + } + return u, nil +}