config.go 18 KB

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