diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 3da645e0..9888b5aa 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -489,9 +489,9 @@ func tunnelFlags(shouldHide bool) []cli.Flag { Hidden: shouldHide, }), altsrc.NewStringFlag(&cli.StringFlag{ - Name: "httphost", + Name: "http-host-header", Usage: "Sets the HTTP Host header for the local webserver.", - EnvVars: []string{"TUNNEL_HTTPHOST"}, + EnvVars: []string{"TUNNEL_HTTP_HOST_HEADER"}, Hidden: shouldHide, }), altsrc.NewStringFlag(&cli.StringFlag{ diff --git a/cmd/cloudflared/tunnel/configuration.go b/cmd/cloudflared/tunnel/configuration.go index 7e184115..c682e49d 100644 --- a/cmd/cloudflared/tunnel/configuration.go +++ b/cmd/cloudflared/tunnel/configuration.go @@ -251,7 +251,7 @@ func prepareTunnelConfig( HTTPTransport: httpTransport, HeartbeatInterval: c.Duration("heartbeat-interval"), Hostname: hostname, - HTTPHost: c.String("httphost"), + HTTPHostHeader: c.String("http-host-header"), IncidentLookup: origin.NewIncidentLookup(), IsAutoupdated: c.Bool("is-autoupdated"), IsFreeTunnel: isFreeTunnel, diff --git a/origin/tunnel.go b/origin/tunnel.go index 89899e39..b3dc0ff8 100644 --- a/origin/tunnel.go +++ b/origin/tunnel.go @@ -52,7 +52,7 @@ type TunnelConfig struct { HTTPTransport http.RoundTripper HeartbeatInterval time.Duration Hostname string - HTTPHost string + HTTPHostHeader string IncidentLookup IncidentLookup IsAutoupdated bool IsFreeTunnel bool @@ -520,13 +520,13 @@ func FindCfRayHeader(h1 *http.Request) string { } type TunnelHandler struct { - originUrl string - httpHost string - muxer *h2mux.Muxer - httpClient http.RoundTripper - tlsConfig *tls.Config - tags []tunnelpogs.Tag - metrics *TunnelMetrics + originUrl string + httpHostHeader string + muxer *h2mux.Muxer + httpClient http.RoundTripper + tlsConfig *tls.Config + tags []tunnelpogs.Tag + metrics *TunnelMetrics // connectionID is only used by metrics, and prometheus requires labels to be string connectionID string logger *log.Logger @@ -547,7 +547,7 @@ func NewTunnelHandler(ctx context.Context, } h := &TunnelHandler{ originUrl: originURL, - httpHost: config.HTTPHost, + httpHostHeader: config.HTTPHostHeader, httpClient: config.HTTPTransport, tlsConfig: config.ClientTlsConfig, tags: config.Tags, @@ -642,6 +642,11 @@ func (h *TunnelHandler) createRequest(stream *h2mux.MuxedStream) (*http.Request, } func (h *TunnelHandler) serveWebsocket(stream *h2mux.MuxedStream, req *http.Request) (*http.Response, error) { + if h.httpHostHeader != "" { + req.Header.Set("Host", h.httpHostHeader) + req.Host = h.httpHostHeader + } + conn, response, err := websocket.ClientConnect(req, h.tlsConfig) if err != nil { return nil, err @@ -670,9 +675,9 @@ 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 + if h.httpHostHeader != "" { + req.Header.Set("Host", h.httpHostHeader) + req.Host = h.httpHostHeader } response, err := h.httpClient.RoundTrip(req)