Skip to content

Adding router proxy support #747

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions ziti/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}