Skip to content

Commit ee87c43

Browse files
authored
Merge pull request #656 from nikr-canva/http2-origins
Add Http2Origin option to force HTTP/2 origin connections
2 parents bccc58b + 5ed3d4e commit ee87c43

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

cmd/cloudflared/tunnel/cmd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,13 @@ func configureProxyFlags(shouldHide bool) []cli.Flag {
837837
EnvVars: []string{"TUNNEL_NO_CHUNKED_ENCODING"},
838838
Hidden: shouldHide,
839839
}),
840+
altsrc.NewBoolFlag(&cli.BoolFlag{
841+
Name: ingress.Http2OriginFlag,
842+
Usage: "Enables HTTP/2 origin servers.",
843+
EnvVars: []string{"TUNNEL_ORIGIN_ENABLE_HTTP2"},
844+
Hidden: shouldHide,
845+
Value: false,
846+
}),
840847
}
841848
return append(flags, sshFlags(shouldHide)...)
842849
}

config/configuration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ type OriginRequestConfig struct {
227227
ProxyType *string `yaml:"proxyType" json:"proxyType,omitempty"`
228228
// IP rules for the proxy service
229229
IPRules []IngressIPRule `yaml:"ipRules" json:"ipRules,omitempty"`
230+
// Attempt to connect to origin with HTTP/2
231+
Http2Origin *bool `yaml:"http2Origin" json:"http2Origin,omitempty"`
230232
}
231233

232234
type IngressIPRule struct {

config/configuration_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ var rawJsonConfig = []byte(`
144144
"ports": [443, 4443],
145145
"allow": true
146146
}
147-
]
147+
],
148+
"http2Origin": true
148149
}
149150
`)
150151

@@ -191,6 +192,7 @@ func assertConfig(
191192
assert.Equal(t, true, *config.NoTLSVerify)
192193
assert.Equal(t, uint(9000), *config.ProxyPort)
193194
assert.Equal(t, "socks", *config.ProxyType)
195+
assert.Equal(t, true, *config.Http2Origin)
194196

195197
privateV4 := "10.0.0.0/8"
196198
privateV6 := "fc00::/7"

ingress/config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
NoChunkedEncodingFlag = "no-chunked-encoding"
3737
ProxyAddressFlag = "proxy-address"
3838
ProxyPortFlag = "proxy-port"
39+
Http2OriginFlag = "http2-origin"
3940
)
4041

4142
const (
@@ -128,6 +129,7 @@ func originRequestFromSingeRule(c *cli.Context) OriginRequestConfig {
128129
var proxyAddress = defaultProxyAddress
129130
var proxyPort uint
130131
var proxyType string
132+
var http2Origin bool
131133
if flag := ProxyConnectTimeoutFlag; c.IsSet(flag) {
132134
connectTimeout = config.CustomDuration{Duration: c.Duration(flag)}
133135
}
@@ -171,9 +173,13 @@ func originRequestFromSingeRule(c *cli.Context) OriginRequestConfig {
171173
// Note TUN-3758 , we use Int because UInt is not supported with altsrc
172174
proxyPort = uint(c.Int(flag))
173175
}
176+
if flag := Http2OriginFlag; c.IsSet(flag) {
177+
http2Origin = c.Bool(flag)
178+
}
174179
if c.IsSet(Socks5Flag) {
175180
proxyType = socksProxy
176181
}
182+
177183
return OriginRequestConfig{
178184
ConnectTimeout: connectTimeout,
179185
TLSTimeout: tlsTimeout,
@@ -190,6 +196,7 @@ func originRequestFromSingeRule(c *cli.Context) OriginRequestConfig {
190196
ProxyAddress: proxyAddress,
191197
ProxyPort: proxyPort,
192198
ProxyType: proxyType,
199+
Http2Origin: http2Origin,
193200
}
194201
}
195202

@@ -255,6 +262,9 @@ func originRequestFromConfig(c config.OriginRequestConfig) OriginRequestConfig {
255262
}
256263
}
257264
}
265+
if c.Http2Origin != nil {
266+
out.Http2Origin = *c.Http2Origin
267+
}
258268
return out
259269
}
260270

@@ -298,6 +308,8 @@ type OriginRequestConfig struct {
298308
ProxyType string `yaml:"proxyType" json:"proxyType"`
299309
// IP rules for the proxy service
300310
IPRules []ipaccess.Rule `yaml:"ipRules" json:"ipRules"`
311+
// Attempt to connect to origin with HTTP/2
312+
Http2Origin bool `yaml:"http2Origin" json:"http2Origin"`
301313
}
302314

303315
func (defaults *OriginRequestConfig) setConnectTimeout(overrides config.OriginRequestConfig) {
@@ -403,6 +415,12 @@ func (defaults *OriginRequestConfig) setIPRules(overrides config.OriginRequestCo
403415
}
404416
}
405417

418+
func (defaults *OriginRequestConfig) setHttp2Origin(overrides config.OriginRequestConfig) {
419+
if val := overrides.Http2Origin; val != nil {
420+
defaults.Http2Origin = *val
421+
}
422+
}
423+
406424
// SetConfig gets config for the requests that cloudflared sends to origins.
407425
// Each field has a setter method which sets a value for the field by trying to find:
408426
// 1. The user config for this rule
@@ -428,6 +446,7 @@ func setConfig(defaults OriginRequestConfig, overrides config.OriginRequestConfi
428446
cfg.setProxyAddress(overrides)
429447
cfg.setProxyType(overrides)
430448
cfg.setIPRules(overrides)
449+
cfg.setHttp2Origin(overrides)
431450
return cfg
432451
}
433452

@@ -475,6 +494,7 @@ func ConvertToRawOriginConfig(c OriginRequestConfig) config.OriginRequestConfig
475494
ProxyPort: zeroUIntToNil(c.ProxyPort),
476495
ProxyType: emptyStringToNil(c.ProxyType),
477496
IPRules: convertToRawIPRules(c.IPRules),
497+
Http2Origin: defaultBoolToNil(c.Http2Origin),
478498
}
479499
}
480500

ingress/origin_service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ func newHTTPTransport(service OriginService, cfg OriginRequestConfig, log *zerol
290290
TLSHandshakeTimeout: cfg.TLSTimeout.Duration,
291291
ExpectContinueTimeout: 1 * time.Second,
292292
TLSClientConfig: &tls.Config{RootCAs: originCertPool, InsecureSkipVerify: cfg.NoTLSVerify},
293+
ForceAttemptHTTP2: cfg.Http2Origin,
293294
}
294295
if _, isHelloWorld := service.(*helloWorld); !isHelloWorld && cfg.OriginServerName != "" {
295296
httpTransport.TLSClientConfig.ServerName = cfg.OriginServerName

ingress/rule_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,25 @@ func TestMarshalJSON(t *testing.T) {
182182
{
183183
name: "Nil",
184184
path: nil,
185-
expected: `{"hostname":"example.com","path":null,"service":"https://localhost:8000","originRequest":{"connectTimeout":30,"tlsTimeout":10,"tcpKeepAlive":30,"noHappyEyeballs":false,"keepAliveTimeout":90,"keepAliveConnections":100,"httpHostHeader":"","originServerName":"","caPool":"","noTLSVerify":false,"disableChunkedEncoding":false,"bastionMode":false,"proxyAddress":"127.0.0.1","proxyPort":0,"proxyType":"","ipRules":null}}`,
185+
expected: `{"hostname":"example.com","path":null,"service":"https://localhost:8000","originRequest":{"connectTimeout":30,"tlsTimeout":10,"tcpKeepAlive":30,"noHappyEyeballs":false,"keepAliveTimeout":90,"keepAliveConnections":100,"httpHostHeader":"","originServerName":"","caPool":"","noTLSVerify":false,"disableChunkedEncoding":false,"bastionMode":false,"proxyAddress":"127.0.0.1","proxyPort":0,"proxyType":"","ipRules":null,"http2Origin":false}}`,
186186
want: true,
187187
},
188188
{
189189
name: "Nil regex",
190190
path: &Regexp{Regexp: nil},
191-
expected: `{"hostname":"example.com","path":null,"service":"https://localhost:8000","originRequest":{"connectTimeout":30,"tlsTimeout":10,"tcpKeepAlive":30,"noHappyEyeballs":false,"keepAliveTimeout":90,"keepAliveConnections":100,"httpHostHeader":"","originServerName":"","caPool":"","noTLSVerify":false,"disableChunkedEncoding":false,"bastionMode":false,"proxyAddress":"127.0.0.1","proxyPort":0,"proxyType":"","ipRules":null}}`,
191+
expected: `{"hostname":"example.com","path":null,"service":"https://localhost:8000","originRequest":{"connectTimeout":30,"tlsTimeout":10,"tcpKeepAlive":30,"noHappyEyeballs":false,"keepAliveTimeout":90,"keepAliveConnections":100,"httpHostHeader":"","originServerName":"","caPool":"","noTLSVerify":false,"disableChunkedEncoding":false,"bastionMode":false,"proxyAddress":"127.0.0.1","proxyPort":0,"proxyType":"","ipRules":null,"http2Origin":false}}`,
192192
want: true,
193193
},
194194
{
195195
name: "Empty",
196196
path: &Regexp{Regexp: regexp.MustCompile("")},
197-
expected: `{"hostname":"example.com","path":"","service":"https://localhost:8000","originRequest":{"connectTimeout":30,"tlsTimeout":10,"tcpKeepAlive":30,"noHappyEyeballs":false,"keepAliveTimeout":90,"keepAliveConnections":100,"httpHostHeader":"","originServerName":"","caPool":"","noTLSVerify":false,"disableChunkedEncoding":false,"bastionMode":false,"proxyAddress":"127.0.0.1","proxyPort":0,"proxyType":"","ipRules":null}}`,
197+
expected: `{"hostname":"example.com","path":"","service":"https://localhost:8000","originRequest":{"connectTimeout":30,"tlsTimeout":10,"tcpKeepAlive":30,"noHappyEyeballs":false,"keepAliveTimeout":90,"keepAliveConnections":100,"httpHostHeader":"","originServerName":"","caPool":"","noTLSVerify":false,"disableChunkedEncoding":false,"bastionMode":false,"proxyAddress":"127.0.0.1","proxyPort":0,"proxyType":"","ipRules":null,"http2Origin":false}}`,
198198
want: true,
199199
},
200200
{
201201
name: "Basic",
202202
path: &Regexp{Regexp: regexp.MustCompile("/echo")},
203-
expected: `{"hostname":"example.com","path":"/echo","service":"https://localhost:8000","originRequest":{"connectTimeout":30,"tlsTimeout":10,"tcpKeepAlive":30,"noHappyEyeballs":false,"keepAliveTimeout":90,"keepAliveConnections":100,"httpHostHeader":"","originServerName":"","caPool":"","noTLSVerify":false,"disableChunkedEncoding":false,"bastionMode":false,"proxyAddress":"127.0.0.1","proxyPort":0,"proxyType":"","ipRules":null}}`,
203+
expected: `{"hostname":"example.com","path":"/echo","service":"https://localhost:8000","originRequest":{"connectTimeout":30,"tlsTimeout":10,"tcpKeepAlive":30,"noHappyEyeballs":false,"keepAliveTimeout":90,"keepAliveConnections":100,"httpHostHeader":"","originServerName":"","caPool":"","noTLSVerify":false,"disableChunkedEncoding":false,"bastionMode":false,"proxyAddress":"127.0.0.1","proxyPort":0,"proxyType":"","ipRules":null,"http2Origin":false}}`,
204204
want: true,
205205
},
206206
}

0 commit comments

Comments
 (0)