From 9ff1611a6a3b1dc50a1e416c4682b95239721c3a Mon Sep 17 00:00:00 2001 From: David Barr <38654497+davebarrau@users.noreply.github.com> Date: Tue, 2 Jul 2019 20:34:44 +1000 Subject: [PATCH] Add support for specifying a specific HTTP Host: header on the origin. --- cmd/cloudflared/tunnel/cmd.go | 6 ++++++ cmd/cloudflared/tunnel/configuration.go | 1 + origin/tunnel.go | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 14dcced0..3da645e0 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -488,6 +488,12 @@ func tunnelFlags(shouldHide bool) []cli.Flag { EnvVars: []string{"TUNNEL_HOSTNAME"}, Hidden: shouldHide, }), + altsrc.NewStringFlag(&cli.StringFlag{ + Name: "httphost", + Usage: "Sets the HTTP Host header for the local webserver.", + EnvVars: []string{"TUNNEL_HTTPHOST"}, + Hidden: shouldHide, + }), altsrc.NewStringFlag(&cli.StringFlag{ Name: "origin-server-name", Usage: "Hostname on the origin server certificate.", diff --git a/cmd/cloudflared/tunnel/configuration.go b/cmd/cloudflared/tunnel/configuration.go index d0595d48..7e184115 100644 --- a/cmd/cloudflared/tunnel/configuration.go +++ b/cmd/cloudflared/tunnel/configuration.go @@ -251,6 +251,7 @@ func prepareTunnelConfig( HTTPTransport: httpTransport, HeartbeatInterval: c.Duration("heartbeat-interval"), Hostname: hostname, + HTTPHost: c.String("httphost"), IncidentLookup: origin.NewIncidentLookup(), IsAutoupdated: c.Bool("is-autoupdated"), IsFreeTunnel: isFreeTunnel, diff --git a/origin/tunnel.go b/origin/tunnel.go index bbc80046..89899e39 100644 --- a/origin/tunnel.go +++ b/origin/tunnel.go @@ -52,6 +52,7 @@ type TunnelConfig struct { HTTPTransport http.RoundTripper HeartbeatInterval time.Duration Hostname string + HTTPHost string IncidentLookup IncidentLookup IsAutoupdated bool IsFreeTunnel bool @@ -520,6 +521,7 @@ func FindCfRayHeader(h1 *http.Request) string { type TunnelHandler struct { originUrl string + httpHost string muxer *h2mux.Muxer httpClient http.RoundTripper tlsConfig *tls.Config @@ -545,6 +547,7 @@ func NewTunnelHandler(ctx context.Context, } h := &TunnelHandler{ originUrl: originURL, + httpHost: config.HTTPHost, httpClient: config.HTTPTransport, tlsConfig: config.ClientTlsConfig, tags: config.Tags, @@ -629,6 +632,7 @@ func (h *TunnelHandler) createRequest(stream *h2mux.MuxedStream) (*http.Request, if err != nil { return nil, errors.Wrap(err, "Unexpected error from http.NewRequest") } + err = H2RequestHeadersToH1Request(stream.Headers, req) if err != nil { return nil, errors.Wrap(err, "invalid request received") @@ -666,6 +670,11 @@ func (h *TunnelHandler) serveHTTP(stream *h2mux.MuxedStream, req *http.Request) // Request origin to keep connection alive to improve performance req.Header.Set("Connection", "keep-alive") + if h.httpHost != "" { + req.Header.Set("Host", h.httpHost) + req.Host = h.httpHost + } + response, err := h.httpClient.RoundTrip(req) if err != nil { return nil, errors.Wrap(err, "Error proxying request to origin")