config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. package ingress
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/urfave/cli/v2"
  6. "github.com/cloudflare/cloudflared/config"
  7. "github.com/cloudflare/cloudflared/ipaccess"
  8. "github.com/cloudflare/cloudflared/tlsconfig"
  9. )
  10. var (
  11. defaultHTTPConnectTimeout = config.CustomDuration{Duration: 30 * time.Second}
  12. defaultWarpRoutingConnectTimeout = config.CustomDuration{Duration: 5 * time.Second}
  13. defaultTLSTimeout = config.CustomDuration{Duration: 10 * time.Second}
  14. defaultTCPKeepAlive = config.CustomDuration{Duration: 30 * time.Second}
  15. defaultKeepAliveTimeout = config.CustomDuration{Duration: 90 * time.Second}
  16. )
  17. const (
  18. defaultProxyAddress = "127.0.0.1"
  19. defaultKeepAliveConnections = 100
  20. defaultMaxActiveFlows = 0 // unlimited
  21. SSHServerFlag = "ssh-server"
  22. Socks5Flag = "socks5"
  23. ProxyConnectTimeoutFlag = "proxy-connect-timeout"
  24. ProxyTLSTimeoutFlag = "proxy-tls-timeout"
  25. ProxyTCPKeepAliveFlag = "proxy-tcp-keepalive"
  26. ProxyNoHappyEyeballsFlag = "proxy-no-happy-eyeballs"
  27. ProxyKeepAliveConnectionsFlag = "proxy-keepalive-connections"
  28. ProxyKeepAliveTimeoutFlag = "proxy-keepalive-timeout"
  29. HTTPHostHeaderFlag = "http-host-header"
  30. OriginServerNameFlag = "origin-server-name"
  31. MatchSNIToHostFlag = "match-sni-to-host"
  32. NoTLSVerifyFlag = "no-tls-verify"
  33. NoChunkedEncodingFlag = "no-chunked-encoding"
  34. ProxyAddressFlag = "proxy-address"
  35. ProxyPortFlag = "proxy-port"
  36. Http2OriginFlag = "http2-origin"
  37. )
  38. const (
  39. socksProxy = "socks"
  40. )
  41. type WarpRoutingConfig struct {
  42. ConnectTimeout config.CustomDuration `yaml:"connectTimeout" json:"connectTimeout,omitempty"`
  43. MaxActiveFlows uint64 `yaml:"maxActiveFlows" json:"MaxActiveFlows,omitempty"`
  44. TCPKeepAlive config.CustomDuration `yaml:"tcpKeepAlive" json:"tcpKeepAlive,omitempty"`
  45. }
  46. func NewWarpRoutingConfig(raw *config.WarpRoutingConfig) WarpRoutingConfig {
  47. cfg := WarpRoutingConfig{
  48. ConnectTimeout: defaultWarpRoutingConnectTimeout,
  49. MaxActiveFlows: defaultMaxActiveFlows,
  50. TCPKeepAlive: defaultTCPKeepAlive,
  51. }
  52. if raw.ConnectTimeout != nil {
  53. cfg.ConnectTimeout = *raw.ConnectTimeout
  54. }
  55. if raw.MaxActiveFlows != nil {
  56. cfg.MaxActiveFlows = *raw.MaxActiveFlows
  57. }
  58. if raw.TCPKeepAlive != nil {
  59. cfg.TCPKeepAlive = *raw.TCPKeepAlive
  60. }
  61. return cfg
  62. }
  63. func (c *WarpRoutingConfig) RawConfig() config.WarpRoutingConfig {
  64. raw := config.WarpRoutingConfig{}
  65. if c.ConnectTimeout.Duration != defaultWarpRoutingConnectTimeout.Duration {
  66. raw.ConnectTimeout = &c.ConnectTimeout
  67. }
  68. if c.MaxActiveFlows != defaultMaxActiveFlows {
  69. raw.MaxActiveFlows = &c.MaxActiveFlows
  70. }
  71. if c.TCPKeepAlive.Duration != defaultTCPKeepAlive.Duration {
  72. raw.TCPKeepAlive = &c.TCPKeepAlive
  73. }
  74. return raw
  75. }
  76. // RemoteConfig models ingress settings that can be managed remotely, for example through the dashboard.
  77. type RemoteConfig struct {
  78. Ingress Ingress
  79. WarpRouting WarpRoutingConfig
  80. }
  81. type RemoteConfigJSON struct {
  82. GlobalOriginRequest *config.OriginRequestConfig `json:"originRequest,omitempty"`
  83. IngressRules []config.UnvalidatedIngressRule `json:"ingress"`
  84. WarpRouting config.WarpRoutingConfig `json:"warp-routing"`
  85. }
  86. func (rc *RemoteConfig) UnmarshalJSON(b []byte) error {
  87. var rawConfig RemoteConfigJSON
  88. if err := json.Unmarshal(b, &rawConfig); err != nil {
  89. return err
  90. }
  91. // if nil, just assume the default values.
  92. globalOriginRequestConfig := rawConfig.GlobalOriginRequest
  93. if globalOriginRequestConfig == nil {
  94. globalOriginRequestConfig = &config.OriginRequestConfig{}
  95. }
  96. ingress, err := validateIngress(rawConfig.IngressRules, originRequestFromConfig(*globalOriginRequestConfig))
  97. if err != nil {
  98. return err
  99. }
  100. rc.Ingress = ingress
  101. rc.WarpRouting = NewWarpRoutingConfig(&rawConfig.WarpRouting)
  102. return nil
  103. }
  104. func originRequestFromSingleRule(c *cli.Context) OriginRequestConfig {
  105. var connectTimeout = defaultHTTPConnectTimeout
  106. var tlsTimeout = defaultTLSTimeout
  107. var tcpKeepAlive = defaultTCPKeepAlive
  108. var noHappyEyeballs bool
  109. var keepAliveConnections = defaultKeepAliveConnections
  110. var keepAliveTimeout = defaultKeepAliveTimeout
  111. var httpHostHeader string
  112. var originServerName string
  113. var matchSNItoHost bool
  114. var caPool string
  115. var noTLSVerify bool
  116. var disableChunkedEncoding bool
  117. var bastionMode bool
  118. var proxyAddress = defaultProxyAddress
  119. var proxyPort uint
  120. var proxyType string
  121. var http2Origin bool
  122. if flag := ProxyConnectTimeoutFlag; c.IsSet(flag) {
  123. connectTimeout = config.CustomDuration{Duration: c.Duration(flag)}
  124. }
  125. if flag := ProxyTLSTimeoutFlag; c.IsSet(flag) {
  126. tlsTimeout = config.CustomDuration{Duration: c.Duration(flag)}
  127. }
  128. if flag := ProxyTCPKeepAliveFlag; c.IsSet(flag) {
  129. tcpKeepAlive = config.CustomDuration{Duration: c.Duration(flag)}
  130. }
  131. if flag := ProxyNoHappyEyeballsFlag; c.IsSet(flag) {
  132. noHappyEyeballs = c.Bool(flag)
  133. }
  134. if flag := ProxyKeepAliveConnectionsFlag; c.IsSet(flag) {
  135. keepAliveConnections = c.Int(flag)
  136. }
  137. if flag := ProxyKeepAliveTimeoutFlag; c.IsSet(flag) {
  138. keepAliveTimeout = config.CustomDuration{Duration: c.Duration(flag)}
  139. }
  140. if flag := HTTPHostHeaderFlag; c.IsSet(flag) {
  141. httpHostHeader = c.String(flag)
  142. }
  143. if flag := OriginServerNameFlag; c.IsSet(flag) {
  144. originServerName = c.String(flag)
  145. }
  146. if flag := MatchSNIToHostFlag; c.IsSet(flag) {
  147. matchSNItoHost = c.Bool(flag)
  148. }
  149. if flag := tlsconfig.OriginCAPoolFlag; c.IsSet(flag) {
  150. caPool = c.String(flag)
  151. }
  152. if flag := NoTLSVerifyFlag; c.IsSet(flag) {
  153. noTLSVerify = c.Bool(flag)
  154. }
  155. if flag := NoChunkedEncodingFlag; c.IsSet(flag) {
  156. disableChunkedEncoding = c.Bool(flag)
  157. }
  158. if flag := config.BastionFlag; c.IsSet(flag) {
  159. bastionMode = c.Bool(flag)
  160. }
  161. if flag := ProxyAddressFlag; c.IsSet(flag) {
  162. proxyAddress = c.String(flag)
  163. }
  164. if flag := ProxyPortFlag; c.IsSet(flag) {
  165. // Note TUN-3758 , we use Int because UInt is not supported with altsrc
  166. // nolint: gosec
  167. proxyPort = uint(c.Int(flag))
  168. }
  169. if flag := Http2OriginFlag; c.IsSet(flag) {
  170. http2Origin = c.Bool(flag)
  171. }
  172. if c.IsSet(Socks5Flag) {
  173. proxyType = socksProxy
  174. }
  175. return OriginRequestConfig{
  176. ConnectTimeout: connectTimeout,
  177. TLSTimeout: tlsTimeout,
  178. TCPKeepAlive: tcpKeepAlive,
  179. NoHappyEyeballs: noHappyEyeballs,
  180. KeepAliveConnections: keepAliveConnections,
  181. KeepAliveTimeout: keepAliveTimeout,
  182. HTTPHostHeader: httpHostHeader,
  183. OriginServerName: originServerName,
  184. MatchSNIToHost: matchSNItoHost,
  185. CAPool: caPool,
  186. NoTLSVerify: noTLSVerify,
  187. DisableChunkedEncoding: disableChunkedEncoding,
  188. BastionMode: bastionMode,
  189. ProxyAddress: proxyAddress,
  190. ProxyPort: proxyPort,
  191. ProxyType: proxyType,
  192. Http2Origin: http2Origin,
  193. }
  194. }
  195. func originRequestFromConfig(c config.OriginRequestConfig) OriginRequestConfig {
  196. out := OriginRequestConfig{
  197. ConnectTimeout: defaultHTTPConnectTimeout,
  198. TLSTimeout: defaultTLSTimeout,
  199. TCPKeepAlive: defaultTCPKeepAlive,
  200. KeepAliveConnections: defaultKeepAliveConnections,
  201. KeepAliveTimeout: defaultKeepAliveTimeout,
  202. ProxyAddress: defaultProxyAddress,
  203. }
  204. if c.ConnectTimeout != nil {
  205. out.ConnectTimeout = *c.ConnectTimeout
  206. }
  207. if c.TLSTimeout != nil {
  208. out.TLSTimeout = *c.TLSTimeout
  209. }
  210. if c.TCPKeepAlive != nil {
  211. out.TCPKeepAlive = *c.TCPKeepAlive
  212. }
  213. if c.NoHappyEyeballs != nil {
  214. out.NoHappyEyeballs = *c.NoHappyEyeballs
  215. }
  216. if c.KeepAliveConnections != nil {
  217. out.KeepAliveConnections = *c.KeepAliveConnections
  218. }
  219. if c.KeepAliveTimeout != nil {
  220. out.KeepAliveTimeout = *c.KeepAliveTimeout
  221. }
  222. if c.HTTPHostHeader != nil {
  223. out.HTTPHostHeader = *c.HTTPHostHeader
  224. }
  225. if c.OriginServerName != nil {
  226. out.OriginServerName = *c.OriginServerName
  227. }
  228. if c.MatchSNIToHost != nil {
  229. out.MatchSNIToHost = *c.MatchSNIToHost
  230. }
  231. if c.CAPool != nil {
  232. out.CAPool = *c.CAPool
  233. }
  234. if c.NoTLSVerify != nil {
  235. out.NoTLSVerify = *c.NoTLSVerify
  236. }
  237. if c.DisableChunkedEncoding != nil {
  238. out.DisableChunkedEncoding = *c.DisableChunkedEncoding
  239. }
  240. if c.BastionMode != nil {
  241. out.BastionMode = *c.BastionMode
  242. }
  243. if c.ProxyAddress != nil {
  244. out.ProxyAddress = *c.ProxyAddress
  245. }
  246. if c.ProxyPort != nil {
  247. out.ProxyPort = *c.ProxyPort
  248. }
  249. if c.ProxyType != nil {
  250. out.ProxyType = *c.ProxyType
  251. }
  252. if len(c.IPRules) > 0 {
  253. for _, r := range c.IPRules {
  254. rule, err := ipaccess.NewRuleByCIDR(r.Prefix, r.Ports, r.Allow)
  255. if err == nil {
  256. out.IPRules = append(out.IPRules, rule)
  257. }
  258. }
  259. }
  260. if c.Http2Origin != nil {
  261. out.Http2Origin = *c.Http2Origin
  262. }
  263. if c.Access != nil {
  264. out.Access = *c.Access
  265. }
  266. return out
  267. }
  268. // OriginRequestConfig configures how Cloudflared sends requests to origin
  269. // services.
  270. // Note: To specify a time.Duration in go-yaml, use e.g. "3s" or "24h".
  271. type OriginRequestConfig struct {
  272. // HTTP proxy timeout for establishing a new connection
  273. ConnectTimeout config.CustomDuration `yaml:"connectTimeout" json:"connectTimeout"`
  274. // HTTP proxy timeout for completing a TLS handshake
  275. TLSTimeout config.CustomDuration `yaml:"tlsTimeout" json:"tlsTimeout"`
  276. // HTTP proxy TCP keepalive duration
  277. TCPKeepAlive config.CustomDuration `yaml:"tcpKeepAlive" json:"tcpKeepAlive"`
  278. // HTTP proxy should disable "happy eyeballs" for IPv4/v6 fallback
  279. NoHappyEyeballs bool `yaml:"noHappyEyeballs" json:"noHappyEyeballs"`
  280. // HTTP proxy timeout for closing an idle connection
  281. KeepAliveTimeout config.CustomDuration `yaml:"keepAliveTimeout" json:"keepAliveTimeout"`
  282. // HTTP proxy maximum keepalive connection pool size
  283. KeepAliveConnections int `yaml:"keepAliveConnections" json:"keepAliveConnections"`
  284. // Sets the HTTP Host header for the local webserver.
  285. HTTPHostHeader string `yaml:"httpHostHeader" json:"httpHostHeader"`
  286. // Hostname on the origin server certificate.
  287. OriginServerName string `yaml:"originServerName" json:"originServerName"`
  288. // Auto configure the Hostname on the origin server certificate.
  289. MatchSNIToHost bool `yaml:"matchSNItoHost" json:"matchSNItoHost"`
  290. // Path to the CA for the certificate of your origin.
  291. // This option should be used only if your certificate is not signed by Cloudflare.
  292. CAPool string `yaml:"caPool" json:"caPool"`
  293. // Disables TLS verification of the certificate presented by your origin.
  294. // Will allow any certificate from the origin to be accepted.
  295. // Note: The connection from your machine to Cloudflare's Edge is still encrypted.
  296. NoTLSVerify bool `yaml:"noTLSVerify" json:"noTLSVerify"`
  297. // Disables chunked transfer encoding.
  298. // Useful if you are running a WSGI server.
  299. DisableChunkedEncoding bool `yaml:"disableChunkedEncoding" json:"disableChunkedEncoding"`
  300. // Runs as jump host
  301. BastionMode bool `yaml:"bastionMode" json:"bastionMode"`
  302. // Listen address for the proxy.
  303. ProxyAddress string `yaml:"proxyAddress" json:"proxyAddress"`
  304. // Listen port for the proxy.
  305. ProxyPort uint `yaml:"proxyPort" json:"proxyPort"`
  306. // What sort of proxy should be started
  307. ProxyType string `yaml:"proxyType" json:"proxyType"`
  308. // IP rules for the proxy service
  309. IPRules []ipaccess.Rule `yaml:"ipRules" json:"ipRules"`
  310. // Attempt to connect to origin with HTTP/2
  311. Http2Origin bool `yaml:"http2Origin" json:"http2Origin"`
  312. // Access holds all access related configs
  313. Access config.AccessConfig `yaml:"access" json:"access,omitempty"`
  314. }
  315. func (defaults *OriginRequestConfig) setConnectTimeout(overrides config.OriginRequestConfig) {
  316. if val := overrides.ConnectTimeout; val != nil {
  317. defaults.ConnectTimeout = *val
  318. }
  319. }
  320. func (defaults *OriginRequestConfig) setTLSTimeout(overrides config.OriginRequestConfig) {
  321. if val := overrides.TLSTimeout; val != nil {
  322. defaults.TLSTimeout = *val
  323. }
  324. }
  325. func (defaults *OriginRequestConfig) setNoHappyEyeballs(overrides config.OriginRequestConfig) {
  326. if val := overrides.NoHappyEyeballs; val != nil {
  327. defaults.NoHappyEyeballs = *val
  328. }
  329. }
  330. func (defaults *OriginRequestConfig) setKeepAliveConnections(overrides config.OriginRequestConfig) {
  331. if val := overrides.KeepAliveConnections; val != nil {
  332. defaults.KeepAliveConnections = *val
  333. }
  334. }
  335. func (defaults *OriginRequestConfig) setKeepAliveTimeout(overrides config.OriginRequestConfig) {
  336. if val := overrides.KeepAliveTimeout; val != nil {
  337. defaults.KeepAliveTimeout = *val
  338. }
  339. }
  340. func (defaults *OriginRequestConfig) setTCPKeepAlive(overrides config.OriginRequestConfig) {
  341. if val := overrides.TCPKeepAlive; val != nil {
  342. defaults.TCPKeepAlive = *val
  343. }
  344. }
  345. func (defaults *OriginRequestConfig) setHTTPHostHeader(overrides config.OriginRequestConfig) {
  346. if val := overrides.HTTPHostHeader; val != nil {
  347. defaults.HTTPHostHeader = *val
  348. }
  349. }
  350. func (defaults *OriginRequestConfig) setOriginServerName(overrides config.OriginRequestConfig) {
  351. if val := overrides.OriginServerName; val != nil {
  352. defaults.OriginServerName = *val
  353. }
  354. }
  355. func (defaults *OriginRequestConfig) setMatchSNIToHost(overrides config.OriginRequestConfig) {
  356. if val := overrides.MatchSNIToHost; val != nil {
  357. defaults.MatchSNIToHost = *val
  358. }
  359. }
  360. func (defaults *OriginRequestConfig) setCAPool(overrides config.OriginRequestConfig) {
  361. if val := overrides.CAPool; val != nil {
  362. defaults.CAPool = *val
  363. }
  364. }
  365. func (defaults *OriginRequestConfig) setNoTLSVerify(overrides config.OriginRequestConfig) {
  366. if val := overrides.NoTLSVerify; val != nil {
  367. defaults.NoTLSVerify = *val
  368. }
  369. }
  370. func (defaults *OriginRequestConfig) setDisableChunkedEncoding(overrides config.OriginRequestConfig) {
  371. if val := overrides.DisableChunkedEncoding; val != nil {
  372. defaults.DisableChunkedEncoding = *val
  373. }
  374. }
  375. func (defaults *OriginRequestConfig) setBastionMode(overrides config.OriginRequestConfig) {
  376. if val := overrides.BastionMode; val != nil {
  377. defaults.BastionMode = *val
  378. }
  379. }
  380. func (defaults *OriginRequestConfig) setProxyPort(overrides config.OriginRequestConfig) {
  381. if val := overrides.ProxyPort; val != nil {
  382. defaults.ProxyPort = *val
  383. }
  384. }
  385. func (defaults *OriginRequestConfig) setProxyAddress(overrides config.OriginRequestConfig) {
  386. if val := overrides.ProxyAddress; val != nil {
  387. defaults.ProxyAddress = *val
  388. }
  389. }
  390. func (defaults *OriginRequestConfig) setProxyType(overrides config.OriginRequestConfig) {
  391. if val := overrides.ProxyType; val != nil {
  392. defaults.ProxyType = *val
  393. }
  394. }
  395. func (defaults *OriginRequestConfig) setIPRules(overrides config.OriginRequestConfig) {
  396. if val := overrides.IPRules; len(val) > 0 {
  397. ipAccessRule := make([]ipaccess.Rule, len(overrides.IPRules))
  398. for i, r := range overrides.IPRules {
  399. rule, err := ipaccess.NewRuleByCIDR(r.Prefix, r.Ports, r.Allow)
  400. if err == nil {
  401. ipAccessRule[i] = rule
  402. }
  403. }
  404. defaults.IPRules = ipAccessRule
  405. }
  406. }
  407. func (defaults *OriginRequestConfig) setHttp2Origin(overrides config.OriginRequestConfig) {
  408. if val := overrides.Http2Origin; val != nil {
  409. defaults.Http2Origin = *val
  410. }
  411. }
  412. func (defaults *OriginRequestConfig) setAccess(overrides config.OriginRequestConfig) {
  413. if val := overrides.Access; val != nil {
  414. defaults.Access = *val
  415. }
  416. }
  417. // SetConfig gets config for the requests that cloudflared sends to origins.
  418. // Each field has a setter method which sets a value for the field by trying to find:
  419. // 1. The user config for this rule
  420. // 2. The user config for the overall ingress config
  421. // 3. Defaults chosen by the cloudflared team
  422. // 4. Golang zero values for that type
  423. //
  424. // If an earlier option isn't set, it will try the next option down.
  425. func setConfig(defaults OriginRequestConfig, overrides config.OriginRequestConfig) OriginRequestConfig {
  426. cfg := defaults
  427. cfg.setConnectTimeout(overrides)
  428. cfg.setTLSTimeout(overrides)
  429. cfg.setNoHappyEyeballs(overrides)
  430. cfg.setKeepAliveConnections(overrides)
  431. cfg.setKeepAliveTimeout(overrides)
  432. cfg.setTCPKeepAlive(overrides)
  433. cfg.setHTTPHostHeader(overrides)
  434. cfg.setOriginServerName(overrides)
  435. cfg.setMatchSNIToHost(overrides)
  436. cfg.setCAPool(overrides)
  437. cfg.setNoTLSVerify(overrides)
  438. cfg.setDisableChunkedEncoding(overrides)
  439. cfg.setBastionMode(overrides)
  440. cfg.setProxyPort(overrides)
  441. cfg.setProxyAddress(overrides)
  442. cfg.setProxyType(overrides)
  443. cfg.setIPRules(overrides)
  444. cfg.setHttp2Origin(overrides)
  445. cfg.setAccess(overrides)
  446. return cfg
  447. }
  448. func ConvertToRawOriginConfig(c OriginRequestConfig) config.OriginRequestConfig {
  449. var connectTimeout *config.CustomDuration
  450. var tlsTimeout *config.CustomDuration
  451. var tcpKeepAlive *config.CustomDuration
  452. var keepAliveConnections *int
  453. var keepAliveTimeout *config.CustomDuration
  454. var proxyAddress *string
  455. var access *config.AccessConfig
  456. if c.ConnectTimeout != defaultHTTPConnectTimeout {
  457. connectTimeout = &c.ConnectTimeout
  458. }
  459. if c.TLSTimeout != defaultTLSTimeout {
  460. tlsTimeout = &c.TLSTimeout
  461. }
  462. if c.TCPKeepAlive != defaultTCPKeepAlive {
  463. tcpKeepAlive = &c.TCPKeepAlive
  464. }
  465. if c.KeepAliveConnections != defaultKeepAliveConnections {
  466. keepAliveConnections = &c.KeepAliveConnections
  467. }
  468. if c.KeepAliveTimeout != defaultKeepAliveTimeout {
  469. keepAliveTimeout = &c.KeepAliveTimeout
  470. }
  471. if c.ProxyAddress != defaultProxyAddress {
  472. proxyAddress = &c.ProxyAddress
  473. }
  474. if c.Access.Required {
  475. access = &c.Access
  476. }
  477. return config.OriginRequestConfig{
  478. ConnectTimeout: connectTimeout,
  479. TLSTimeout: tlsTimeout,
  480. TCPKeepAlive: tcpKeepAlive,
  481. NoHappyEyeballs: defaultBoolToNil(c.NoHappyEyeballs),
  482. KeepAliveConnections: keepAliveConnections,
  483. KeepAliveTimeout: keepAliveTimeout,
  484. HTTPHostHeader: emptyStringToNil(c.HTTPHostHeader),
  485. OriginServerName: emptyStringToNil(c.OriginServerName),
  486. MatchSNIToHost: defaultBoolToNil(c.MatchSNIToHost),
  487. CAPool: emptyStringToNil(c.CAPool),
  488. NoTLSVerify: defaultBoolToNil(c.NoTLSVerify),
  489. DisableChunkedEncoding: defaultBoolToNil(c.DisableChunkedEncoding),
  490. BastionMode: defaultBoolToNil(c.BastionMode),
  491. ProxyAddress: proxyAddress,
  492. ProxyPort: zeroUIntToNil(c.ProxyPort),
  493. ProxyType: emptyStringToNil(c.ProxyType),
  494. IPRules: convertToRawIPRules(c.IPRules),
  495. Http2Origin: defaultBoolToNil(c.Http2Origin),
  496. Access: access,
  497. }
  498. }
  499. func convertToRawIPRules(ipRules []ipaccess.Rule) []config.IngressIPRule {
  500. result := make([]config.IngressIPRule, 0)
  501. for _, r := range ipRules {
  502. cidr := r.StringCIDR()
  503. newRule := config.IngressIPRule{
  504. Prefix: &cidr,
  505. Ports: r.Ports(),
  506. Allow: r.RulePolicy(),
  507. }
  508. result = append(result, newRule)
  509. }
  510. return result
  511. }
  512. func defaultBoolToNil(b bool) *bool {
  513. if !b {
  514. return nil
  515. }
  516. return &b
  517. }
  518. func emptyStringToNil(s string) *string {
  519. if s == "" {
  520. return nil
  521. }
  522. return &s
  523. }
  524. func zeroUIntToNil(v uint) *uint {
  525. if v == 0 {
  526. return nil
  527. }
  528. return &v
  529. }