Skip to content

Commit

Permalink
Fix: support HTTPS backend (#33)
Browse files Browse the repository at this point in the history
* Fix: forwarded request Host has to be overridden with origin request Host

* Add custom host header config when building proxy
  • Loading branch information
fahrinh authored Aug 19, 2019
1 parent 1f188a0 commit 34c766e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config.template.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"main-target": "http://server-mono.localhost",
"main-header-host": "server-mono",
"canary-target": "http://server-micro.localhost",
"canary-header-host": "server-micro",
"sidecar-url": "http://sidecar.localhost",
"trim-prefix": "/prefix/path/to/strip",
"circuit-breaker": {
Expand Down
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package config

// Config holds the configuration values to be used throughout the application.
type Config struct {
MainTarget string `mapstructure:"main-target"`
CanaryTarget string `mapstructure:"canary-target"`
SidecarURL string `mapstructure:"sidecar-url"`
MainTarget string `mapstructure:"main-target"`
MainHeaderHost string `mapstructure:"main-header-host"`
CanaryTarget string `mapstructure:"canary-target"`
CanaryHeaderHost string `mapstructure:"canary-header-host"`
SidecarURL string `mapstructure:"sidecar-url"`

// TrimPrefix if set will modify subsequent request path to main, canary, and sidecar service
// by removing TrimPrefix substring in the request path string
Expand Down
22 changes: 17 additions & 5 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ type Proxy struct {

// BuildProxies constructs a Proxy object with mainTargetURL as the URL for Main proxy
// and canaryTargetURL as the URL for Canary proxy
func BuildProxies(configClient config.HTTPClientConfig, mainTargetURL, canaryTargetURL string) (*Proxy, error) {
func BuildProxies(configClient config.HTTPClientConfig, mainTargetURL, mainHeaderHost, canaryTargetURL, canaryHeaderHost string) (*Proxy, error) {

proxyMain, err := newReverseProxy(mainTargetURL)
proxyMain, err := newReverseProxy(mainTargetURL, mainHeaderHost)
if err != nil {
return nil, errors.Trace(err)
}
proxyMain.Transport = newTransport(configClient.MaxIdleConns, configClient.IdleConnTimeout, configClient.DisableCompression, configClient.TLS)
proxyMain.ErrorLog = log.New(os.Stderr, "[proxy-main] ", log.LstdFlags|log.Llongfile)

proxyCanary, err := newReverseProxy(canaryTargetURL)
proxyCanary, err := newReverseProxy(canaryTargetURL, canaryHeaderHost)
if err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -56,11 +56,23 @@ func newTransport(maxIdleConns, idleConnTimeout int, disableCompression bool, tl
}
}

func newReverseProxy(target string) (*httputil.ReverseProxy, error) {
func newReverseProxy(target, customHost string) (*httputil.ReverseProxy, error) {
url, err := url.ParseRequestURI(target)
if err != nil {
return nil, errors.Trace(err)
}

return httputil.NewSingleHostReverseProxy(url), nil
proxy := httputil.NewSingleHostReverseProxy(url)

director := proxy.Director
proxy.Director = func(req *http.Request) {
director(req)
if customHost != "" {
req.Host = customHost
} else {
req.Host = req.URL.Host
}
}

return proxy, nil
}
2 changes: 1 addition & 1 deletion proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Test_newReverseProxy(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

got, err := newReverseProxy(c.urlTarget)
got, err := newReverseProxy(c.urlTarget, "")

if (err != nil) != c.wantErr {
t.Errorf("newReverseProxy() error = %v, wantErr %v", err, c.wantErr)
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Server struct {
func NewServer(config config.Config) (*Server, error) {
server := &Server{config: config}

proxies, err := canaryrouter.BuildProxies(config.Client.MainAndCanary, config.MainTarget, config.CanaryTarget)
proxies, err := canaryrouter.BuildProxies(config.Client.MainAndCanary, config.MainTarget, config.MainHeaderHost, config.CanaryTarget, config.CanaryHeaderHost)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down

0 comments on commit 34c766e

Please sign in to comment.