2020-10-15 21:41:03 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
2022-01-28 14:37:17 +00:00
|
|
|
"encoding/json"
|
2020-10-15 21:41:03 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2021-03-08 16:46:23 +00:00
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/config"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/cloudflare/cloudflared/ipaccess"
|
2021-03-08 16:46:23 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tlsconfig"
|
2020-10-15 21:41:03 +00:00
|
|
|
)
|
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
var (
|
2022-06-13 16:44:27 +00:00
|
|
|
defaultHTTPConnectTimeout = config.CustomDuration{Duration: 30 * time.Second}
|
|
|
|
defaultWarpRoutingConnectTimeout = config.CustomDuration{Duration: 5 * time.Second}
|
|
|
|
defaultTLSTimeout = config.CustomDuration{Duration: 10 * time.Second}
|
|
|
|
defaultTCPKeepAlive = config.CustomDuration{Duration: 30 * time.Second}
|
|
|
|
defaultKeepAliveTimeout = config.CustomDuration{Duration: 90 * time.Second}
|
2022-03-14 17:51:10 +00:00
|
|
|
)
|
2020-10-15 21:41:03 +00:00
|
|
|
|
2022-03-14 17:51:10 +00:00
|
|
|
const (
|
|
|
|
defaultProxyAddress = "127.0.0.1"
|
|
|
|
defaultKeepAliveConnections = 100
|
2020-10-15 21:41:03 +00:00
|
|
|
SSHServerFlag = "ssh-server"
|
|
|
|
Socks5Flag = "socks5"
|
|
|
|
ProxyConnectTimeoutFlag = "proxy-connect-timeout"
|
|
|
|
ProxyTLSTimeoutFlag = "proxy-tls-timeout"
|
2021-05-06 20:10:47 +00:00
|
|
|
ProxyTCPKeepAliveFlag = "proxy-tcp-keepalive"
|
2020-10-15 21:41:03 +00:00
|
|
|
ProxyNoHappyEyeballsFlag = "proxy-no-happy-eyeballs"
|
|
|
|
ProxyKeepAliveConnectionsFlag = "proxy-keepalive-connections"
|
|
|
|
ProxyKeepAliveTimeoutFlag = "proxy-keepalive-timeout"
|
|
|
|
HTTPHostHeaderFlag = "http-host-header"
|
|
|
|
OriginServerNameFlag = "origin-server-name"
|
|
|
|
NoTLSVerifyFlag = "no-tls-verify"
|
|
|
|
NoChunkedEncodingFlag = "no-chunked-encoding"
|
|
|
|
ProxyAddressFlag = "proxy-address"
|
|
|
|
ProxyPortFlag = "proxy-port"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
socksProxy = "socks"
|
|
|
|
)
|
|
|
|
|
2022-06-13 16:44:27 +00:00
|
|
|
type WarpRoutingConfig struct {
|
|
|
|
Enabled bool `yaml:"enabled" json:"enabled"`
|
|
|
|
ConnectTimeout config.CustomDuration `yaml:"connectTimeout" json:"connectTimeout,omitempty"`
|
|
|
|
TCPKeepAlive config.CustomDuration `yaml:"tcpKeepAlive" json:"tcpKeepAlive,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWarpRoutingConfig(raw *config.WarpRoutingConfig) WarpRoutingConfig {
|
|
|
|
cfg := WarpRoutingConfig{
|
|
|
|
Enabled: raw.Enabled,
|
|
|
|
ConnectTimeout: defaultWarpRoutingConnectTimeout,
|
|
|
|
TCPKeepAlive: defaultTCPKeepAlive,
|
|
|
|
}
|
|
|
|
if raw.ConnectTimeout != nil {
|
|
|
|
cfg.ConnectTimeout = *raw.ConnectTimeout
|
|
|
|
}
|
|
|
|
if raw.TCPKeepAlive != nil {
|
|
|
|
cfg.TCPKeepAlive = *raw.TCPKeepAlive
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WarpRoutingConfig) RawConfig() config.WarpRoutingConfig {
|
|
|
|
raw := config.WarpRoutingConfig{
|
|
|
|
Enabled: c.Enabled,
|
|
|
|
}
|
|
|
|
if c.ConnectTimeout.Duration != defaultWarpRoutingConnectTimeout.Duration {
|
|
|
|
raw.ConnectTimeout = &c.ConnectTimeout
|
|
|
|
}
|
|
|
|
if c.TCPKeepAlive.Duration != defaultTCPKeepAlive.Duration {
|
|
|
|
raw.TCPKeepAlive = &c.TCPKeepAlive
|
|
|
|
}
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
2022-01-28 14:37:17 +00:00
|
|
|
// RemoteConfig models ingress settings that can be managed remotely, for example through the dashboard.
|
|
|
|
type RemoteConfig struct {
|
|
|
|
Ingress Ingress
|
2022-06-13 16:44:27 +00:00
|
|
|
WarpRouting WarpRoutingConfig
|
2022-01-28 14:37:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 10:51:06 +00:00
|
|
|
type RemoteConfigJSON struct {
|
|
|
|
GlobalOriginRequest *config.OriginRequestConfig `json:"originRequest,omitempty"`
|
2022-01-28 14:37:17 +00:00
|
|
|
IngressRules []config.UnvalidatedIngressRule `json:"ingress"`
|
|
|
|
WarpRouting config.WarpRoutingConfig `json:"warp-routing"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RemoteConfig) UnmarshalJSON(b []byte) error {
|
2022-04-27 10:51:06 +00:00
|
|
|
var rawConfig RemoteConfigJSON
|
|
|
|
|
2022-01-28 14:37:17 +00:00
|
|
|
if err := json.Unmarshal(b, &rawConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-27 10:51:06 +00:00
|
|
|
|
|
|
|
// if nil, just assume the default values.
|
|
|
|
globalOriginRequestConfig := rawConfig.GlobalOriginRequest
|
|
|
|
if globalOriginRequestConfig == nil {
|
|
|
|
globalOriginRequestConfig = &config.OriginRequestConfig{}
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress, err := validateIngress(rawConfig.IngressRules, originRequestFromConfig(*globalOriginRequestConfig))
|
2022-01-28 14:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.Ingress = ingress
|
2022-06-13 16:44:27 +00:00
|
|
|
rc.WarpRouting = NewWarpRoutingConfig(&rawConfig.WarpRouting)
|
2022-01-28 14:37:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
func originRequestFromSingeRule(c *cli.Context) OriginRequestConfig {
|
2022-06-13 16:44:27 +00:00
|
|
|
var connectTimeout = defaultHTTPConnectTimeout
|
|
|
|
var tlsTimeout = defaultTLSTimeout
|
|
|
|
var tcpKeepAlive = defaultTCPKeepAlive
|
2020-10-15 21:41:03 +00:00
|
|
|
var noHappyEyeballs bool
|
2022-06-13 16:44:27 +00:00
|
|
|
var keepAliveConnections = defaultKeepAliveConnections
|
|
|
|
var keepAliveTimeout = defaultKeepAliveTimeout
|
2020-10-15 21:41:03 +00:00
|
|
|
var httpHostHeader string
|
|
|
|
var originServerName string
|
|
|
|
var caPool string
|
|
|
|
var noTLSVerify bool
|
|
|
|
var disableChunkedEncoding bool
|
|
|
|
var bastionMode bool
|
2020-11-13 23:27:55 +00:00
|
|
|
var proxyAddress = defaultProxyAddress
|
2020-10-15 21:41:03 +00:00
|
|
|
var proxyPort uint
|
|
|
|
var proxyType string
|
|
|
|
if flag := ProxyConnectTimeoutFlag; c.IsSet(flag) {
|
2022-03-14 17:51:10 +00:00
|
|
|
connectTimeout = config.CustomDuration{Duration: c.Duration(flag)}
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
if flag := ProxyTLSTimeoutFlag; c.IsSet(flag) {
|
2022-03-14 17:51:10 +00:00
|
|
|
tlsTimeout = config.CustomDuration{Duration: c.Duration(flag)}
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2021-05-06 20:10:47 +00:00
|
|
|
if flag := ProxyTCPKeepAliveFlag; c.IsSet(flag) {
|
2022-03-14 17:51:10 +00:00
|
|
|
tcpKeepAlive = config.CustomDuration{Duration: c.Duration(flag)}
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
if flag := ProxyNoHappyEyeballsFlag; c.IsSet(flag) {
|
|
|
|
noHappyEyeballs = c.Bool(flag)
|
|
|
|
}
|
|
|
|
if flag := ProxyKeepAliveConnectionsFlag; c.IsSet(flag) {
|
|
|
|
keepAliveConnections = c.Int(flag)
|
|
|
|
}
|
|
|
|
if flag := ProxyKeepAliveTimeoutFlag; c.IsSet(flag) {
|
2022-03-14 17:51:10 +00:00
|
|
|
keepAliveTimeout = config.CustomDuration{Duration: c.Duration(flag)}
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
if flag := HTTPHostHeaderFlag; c.IsSet(flag) {
|
|
|
|
httpHostHeader = c.String(flag)
|
|
|
|
}
|
|
|
|
if flag := OriginServerNameFlag; c.IsSet(flag) {
|
|
|
|
originServerName = c.String(flag)
|
|
|
|
}
|
|
|
|
if flag := tlsconfig.OriginCAPoolFlag; c.IsSet(flag) {
|
|
|
|
caPool = c.String(flag)
|
|
|
|
}
|
|
|
|
if flag := NoTLSVerifyFlag; c.IsSet(flag) {
|
|
|
|
noTLSVerify = c.Bool(flag)
|
|
|
|
}
|
|
|
|
if flag := NoChunkedEncodingFlag; c.IsSet(flag) {
|
|
|
|
disableChunkedEncoding = c.Bool(flag)
|
|
|
|
}
|
|
|
|
if flag := config.BastionFlag; c.IsSet(flag) {
|
|
|
|
bastionMode = c.Bool(flag)
|
|
|
|
}
|
|
|
|
if flag := ProxyAddressFlag; c.IsSet(flag) {
|
|
|
|
proxyAddress = c.String(flag)
|
|
|
|
}
|
|
|
|
if flag := ProxyPortFlag; c.IsSet(flag) {
|
2021-01-14 13:08:55 +00:00
|
|
|
// Note TUN-3758 , we use Int because UInt is not supported with altsrc
|
|
|
|
proxyPort = uint(c.Int(flag))
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
if c.IsSet(Socks5Flag) {
|
|
|
|
proxyType = socksProxy
|
|
|
|
}
|
|
|
|
return OriginRequestConfig{
|
|
|
|
ConnectTimeout: connectTimeout,
|
|
|
|
TLSTimeout: tlsTimeout,
|
|
|
|
TCPKeepAlive: tcpKeepAlive,
|
|
|
|
NoHappyEyeballs: noHappyEyeballs,
|
|
|
|
KeepAliveConnections: keepAliveConnections,
|
|
|
|
KeepAliveTimeout: keepAliveTimeout,
|
|
|
|
HTTPHostHeader: httpHostHeader,
|
|
|
|
OriginServerName: originServerName,
|
|
|
|
CAPool: caPool,
|
|
|
|
NoTLSVerify: noTLSVerify,
|
|
|
|
DisableChunkedEncoding: disableChunkedEncoding,
|
|
|
|
BastionMode: bastionMode,
|
|
|
|
ProxyAddress: proxyAddress,
|
|
|
|
ProxyPort: proxyPort,
|
|
|
|
ProxyType: proxyType,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 14:37:17 +00:00
|
|
|
func originRequestFromConfig(c config.OriginRequestConfig) OriginRequestConfig {
|
2020-10-15 21:41:03 +00:00
|
|
|
out := OriginRequestConfig{
|
2022-06-13 16:44:27 +00:00
|
|
|
ConnectTimeout: defaultHTTPConnectTimeout,
|
2020-10-15 21:41:03 +00:00
|
|
|
TLSTimeout: defaultTLSTimeout,
|
|
|
|
TCPKeepAlive: defaultTCPKeepAlive,
|
|
|
|
KeepAliveConnections: defaultKeepAliveConnections,
|
|
|
|
KeepAliveTimeout: defaultKeepAliveTimeout,
|
|
|
|
ProxyAddress: defaultProxyAddress,
|
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.ConnectTimeout != nil {
|
2022-03-14 17:51:10 +00:00
|
|
|
out.ConnectTimeout = *c.ConnectTimeout
|
2022-01-28 14:37:17 +00:00
|
|
|
}
|
|
|
|
if c.TLSTimeout != nil {
|
2022-03-14 17:51:10 +00:00
|
|
|
out.TLSTimeout = *c.TLSTimeout
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.TCPKeepAlive != nil {
|
2022-03-14 17:51:10 +00:00
|
|
|
out.TCPKeepAlive = *c.TCPKeepAlive
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.NoHappyEyeballs != nil {
|
|
|
|
out.NoHappyEyeballs = *c.NoHappyEyeballs
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.KeepAliveConnections != nil {
|
|
|
|
out.KeepAliveConnections = *c.KeepAliveConnections
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.KeepAliveTimeout != nil {
|
2022-03-14 17:51:10 +00:00
|
|
|
out.KeepAliveTimeout = *c.KeepAliveTimeout
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.HTTPHostHeader != nil {
|
|
|
|
out.HTTPHostHeader = *c.HTTPHostHeader
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.OriginServerName != nil {
|
|
|
|
out.OriginServerName = *c.OriginServerName
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.CAPool != nil {
|
|
|
|
out.CAPool = *c.CAPool
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.NoTLSVerify != nil {
|
|
|
|
out.NoTLSVerify = *c.NoTLSVerify
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.DisableChunkedEncoding != nil {
|
|
|
|
out.DisableChunkedEncoding = *c.DisableChunkedEncoding
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.BastionMode != nil {
|
|
|
|
out.BastionMode = *c.BastionMode
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.ProxyAddress != nil {
|
|
|
|
out.ProxyAddress = *c.ProxyAddress
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.ProxyPort != nil {
|
|
|
|
out.ProxyPort = *c.ProxyPort
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if c.ProxyType != nil {
|
|
|
|
out.ProxyType = *c.ProxyType
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
2022-01-28 14:37:17 +00:00
|
|
|
if len(c.IPRules) > 0 {
|
|
|
|
for _, r := range c.IPRules {
|
|
|
|
rule, err := ipaccess.NewRuleByCIDR(r.Prefix, r.Ports, r.Allow)
|
|
|
|
if err == nil {
|
|
|
|
out.IPRules = append(out.IPRules, rule)
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// OriginRequestConfig configures how Cloudflared sends requests to origin
|
|
|
|
// services.
|
|
|
|
// Note: To specify a time.Duration in go-yaml, use e.g. "3s" or "24h".
|
|
|
|
type OriginRequestConfig struct {
|
|
|
|
// HTTP proxy timeout for establishing a new connection
|
2022-03-14 17:51:10 +00:00
|
|
|
ConnectTimeout config.CustomDuration `yaml:"connectTimeout" json:"connectTimeout"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy timeout for completing a TLS handshake
|
2022-03-14 17:51:10 +00:00
|
|
|
TLSTimeout config.CustomDuration `yaml:"tlsTimeout" json:"tlsTimeout"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy TCP keepalive duration
|
2022-03-14 17:51:10 +00:00
|
|
|
TCPKeepAlive config.CustomDuration `yaml:"tcpKeepAlive" json:"tcpKeepAlive"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy should disable "happy eyeballs" for IPv4/v6 fallback
|
2022-03-14 17:51:10 +00:00
|
|
|
NoHappyEyeballs bool `yaml:"noHappyEyeballs" json:"noHappyEyeballs"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// HTTP proxy timeout for closing an idle connection
|
2022-03-14 17:51:10 +00:00
|
|
|
KeepAliveTimeout config.CustomDuration `yaml:"keepAliveTimeout" json:"keepAliveTimeout"`
|
2022-01-28 14:37:17 +00:00
|
|
|
// HTTP proxy maximum keepalive connection pool size
|
2022-03-14 17:51:10 +00:00
|
|
|
KeepAliveConnections int `yaml:"keepAliveConnections" json:"keepAliveConnections"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Sets the HTTP Host header for the local webserver.
|
2022-03-14 17:51:10 +00:00
|
|
|
HTTPHostHeader string `yaml:"httpHostHeader" json:"httpHostHeader"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Hostname on the origin server certificate.
|
2022-03-14 17:51:10 +00:00
|
|
|
OriginServerName string `yaml:"originServerName" json:"originServerName"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Path to the CA for the certificate of your origin.
|
|
|
|
// This option should be used only if your certificate is not signed by Cloudflare.
|
2022-03-14 17:51:10 +00:00
|
|
|
CAPool string `yaml:"caPool" json:"caPool"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Disables TLS verification of the certificate presented by your origin.
|
|
|
|
// Will allow any certificate from the origin to be accepted.
|
|
|
|
// Note: The connection from your machine to Cloudflare's Edge is still encrypted.
|
2022-03-14 17:51:10 +00:00
|
|
|
NoTLSVerify bool `yaml:"noTLSVerify" json:"noTLSVerify"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Disables chunked transfer encoding.
|
|
|
|
// Useful if you are running a WSGI server.
|
2022-03-14 17:51:10 +00:00
|
|
|
DisableChunkedEncoding bool `yaml:"disableChunkedEncoding" json:"disableChunkedEncoding"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Runs as jump host
|
2022-03-14 17:51:10 +00:00
|
|
|
BastionMode bool `yaml:"bastionMode" json:"bastionMode"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Listen address for the proxy.
|
2022-03-14 17:51:10 +00:00
|
|
|
ProxyAddress string `yaml:"proxyAddress" json:"proxyAddress"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// Listen port for the proxy.
|
2022-03-14 17:51:10 +00:00
|
|
|
ProxyPort uint `yaml:"proxyPort" json:"proxyPort"`
|
2020-10-15 21:41:03 +00:00
|
|
|
// What sort of proxy should be started
|
2022-03-14 17:51:10 +00:00
|
|
|
ProxyType string `yaml:"proxyType" json:"proxyType"`
|
2021-03-01 22:26:37 +00:00
|
|
|
// IP rules for the proxy service
|
2022-03-14 17:51:10 +00:00
|
|
|
IPRules []ipaccess.Rule `yaml:"ipRules" json:"ipRules"`
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setConnectTimeout(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.ConnectTimeout; val != nil {
|
2022-03-14 17:51:10 +00:00
|
|
|
defaults.ConnectTimeout = *val
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setTLSTimeout(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.TLSTimeout; val != nil {
|
2022-03-14 17:51:10 +00:00
|
|
|
defaults.TLSTimeout = *val
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setNoHappyEyeballs(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.NoHappyEyeballs; val != nil {
|
|
|
|
defaults.NoHappyEyeballs = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setKeepAliveConnections(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.KeepAliveConnections; val != nil {
|
|
|
|
defaults.KeepAliveConnections = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setKeepAliveTimeout(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.KeepAliveTimeout; val != nil {
|
2022-03-14 17:51:10 +00:00
|
|
|
defaults.KeepAliveTimeout = *val
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setTCPKeepAlive(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.TCPKeepAlive; val != nil {
|
2022-03-14 17:51:10 +00:00
|
|
|
defaults.TCPKeepAlive = *val
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setHTTPHostHeader(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.HTTPHostHeader; val != nil {
|
|
|
|
defaults.HTTPHostHeader = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setOriginServerName(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.OriginServerName; val != nil {
|
|
|
|
defaults.OriginServerName = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setCAPool(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.CAPool; val != nil {
|
|
|
|
defaults.CAPool = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setNoTLSVerify(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.NoTLSVerify; val != nil {
|
|
|
|
defaults.NoTLSVerify = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setDisableChunkedEncoding(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.DisableChunkedEncoding; val != nil {
|
|
|
|
defaults.DisableChunkedEncoding = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setBastionMode(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.BastionMode; val != nil {
|
|
|
|
defaults.BastionMode = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setProxyPort(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.ProxyPort; val != nil {
|
|
|
|
defaults.ProxyPort = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setProxyAddress(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.ProxyAddress; val != nil {
|
|
|
|
defaults.ProxyAddress = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (defaults *OriginRequestConfig) setProxyType(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.ProxyType; val != nil {
|
|
|
|
defaults.ProxyType = *val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 14:37:17 +00:00
|
|
|
func (defaults *OriginRequestConfig) setIPRules(overrides config.OriginRequestConfig) {
|
|
|
|
if val := overrides.IPRules; len(val) > 0 {
|
|
|
|
ipAccessRule := make([]ipaccess.Rule, len(overrides.IPRules))
|
|
|
|
for i, r := range overrides.IPRules {
|
|
|
|
rule, err := ipaccess.NewRuleByCIDR(r.Prefix, r.Ports, r.Allow)
|
|
|
|
if err == nil {
|
|
|
|
ipAccessRule[i] = rule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defaults.IPRules = ipAccessRule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
// SetConfig gets config for the requests that cloudflared sends to origins.
|
|
|
|
// Each field has a setter method which sets a value for the field by trying to find:
|
|
|
|
// 1. The user config for this rule
|
|
|
|
// 2. The user config for the overall ingress config
|
|
|
|
// 3. Defaults chosen by the cloudflared team
|
|
|
|
// 4. Golang zero values for that type
|
|
|
|
// If an earlier option isn't set, it will try the next option down.
|
2020-10-30 21:37:40 +00:00
|
|
|
func setConfig(defaults OriginRequestConfig, overrides config.OriginRequestConfig) OriginRequestConfig {
|
2020-10-15 21:41:03 +00:00
|
|
|
cfg := defaults
|
|
|
|
cfg.setConnectTimeout(overrides)
|
|
|
|
cfg.setTLSTimeout(overrides)
|
|
|
|
cfg.setNoHappyEyeballs(overrides)
|
|
|
|
cfg.setKeepAliveConnections(overrides)
|
|
|
|
cfg.setKeepAliveTimeout(overrides)
|
|
|
|
cfg.setTCPKeepAlive(overrides)
|
|
|
|
cfg.setHTTPHostHeader(overrides)
|
|
|
|
cfg.setOriginServerName(overrides)
|
|
|
|
cfg.setCAPool(overrides)
|
|
|
|
cfg.setNoTLSVerify(overrides)
|
|
|
|
cfg.setDisableChunkedEncoding(overrides)
|
|
|
|
cfg.setBastionMode(overrides)
|
|
|
|
cfg.setProxyPort(overrides)
|
|
|
|
cfg.setProxyAddress(overrides)
|
|
|
|
cfg.setProxyType(overrides)
|
2022-01-28 14:37:17 +00:00
|
|
|
cfg.setIPRules(overrides)
|
2020-10-15 21:41:03 +00:00
|
|
|
return cfg
|
|
|
|
}
|
2022-04-27 10:51:06 +00:00
|
|
|
|
|
|
|
func ConvertToRawOriginConfig(c OriginRequestConfig) config.OriginRequestConfig {
|
|
|
|
var connectTimeout *config.CustomDuration
|
|
|
|
var tlsTimeout *config.CustomDuration
|
|
|
|
var tcpKeepAlive *config.CustomDuration
|
|
|
|
var keepAliveConnections *int
|
|
|
|
var keepAliveTimeout *config.CustomDuration
|
|
|
|
var proxyAddress *string
|
|
|
|
|
2022-06-13 16:44:27 +00:00
|
|
|
if c.ConnectTimeout != defaultHTTPConnectTimeout {
|
2022-04-27 10:51:06 +00:00
|
|
|
connectTimeout = &c.ConnectTimeout
|
|
|
|
}
|
|
|
|
if c.TLSTimeout != defaultTLSTimeout {
|
|
|
|
tlsTimeout = &c.TLSTimeout
|
|
|
|
}
|
|
|
|
if c.TCPKeepAlive != defaultTCPKeepAlive {
|
|
|
|
tcpKeepAlive = &c.TCPKeepAlive
|
|
|
|
}
|
|
|
|
if c.KeepAliveConnections != defaultKeepAliveConnections {
|
|
|
|
keepAliveConnections = &c.KeepAliveConnections
|
|
|
|
}
|
|
|
|
if c.KeepAliveTimeout != defaultKeepAliveTimeout {
|
|
|
|
keepAliveTimeout = &c.KeepAliveTimeout
|
|
|
|
}
|
|
|
|
if c.ProxyAddress != defaultProxyAddress {
|
|
|
|
proxyAddress = &c.ProxyAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.OriginRequestConfig{
|
|
|
|
ConnectTimeout: connectTimeout,
|
|
|
|
TLSTimeout: tlsTimeout,
|
|
|
|
TCPKeepAlive: tcpKeepAlive,
|
|
|
|
NoHappyEyeballs: defaultBoolToNil(c.NoHappyEyeballs),
|
|
|
|
KeepAliveConnections: keepAliveConnections,
|
|
|
|
KeepAliveTimeout: keepAliveTimeout,
|
|
|
|
HTTPHostHeader: emptyStringToNil(c.HTTPHostHeader),
|
|
|
|
OriginServerName: emptyStringToNil(c.OriginServerName),
|
|
|
|
CAPool: emptyStringToNil(c.CAPool),
|
|
|
|
NoTLSVerify: defaultBoolToNil(c.NoTLSVerify),
|
|
|
|
DisableChunkedEncoding: defaultBoolToNil(c.DisableChunkedEncoding),
|
|
|
|
BastionMode: defaultBoolToNil(c.BastionMode),
|
|
|
|
ProxyAddress: proxyAddress,
|
|
|
|
ProxyPort: zeroUIntToNil(c.ProxyPort),
|
|
|
|
ProxyType: emptyStringToNil(c.ProxyType),
|
|
|
|
IPRules: convertToRawIPRules(c.IPRules),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertToRawIPRules(ipRules []ipaccess.Rule) []config.IngressIPRule {
|
|
|
|
result := make([]config.IngressIPRule, 0)
|
|
|
|
for _, r := range ipRules {
|
|
|
|
cidr := r.StringCIDR()
|
|
|
|
|
|
|
|
newRule := config.IngressIPRule{
|
|
|
|
Prefix: &cidr,
|
|
|
|
Ports: r.Ports(),
|
|
|
|
Allow: r.RulePolicy(),
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, newRule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultBoolToNil(b bool) *bool {
|
|
|
|
if b == false {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
|
|
|
func emptyStringToNil(s string) *string {
|
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func zeroUIntToNil(v uint) *uint {
|
|
|
|
if v == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v
|
|
|
|
}
|