Add support for specifying a specific HTTP Host: header on the origin.

This commit is contained in:
David Barr 2019-07-02 20:34:44 +10:00
parent 25a04e0c69
commit 9ff1611a6a
No known key found for this signature in database
GPG Key ID: 8BC1E18438835BB3
3 changed files with 16 additions and 0 deletions

View File

@ -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.",

View File

@ -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,

View File

@ -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")