11package ingress
22
33import (
4+ "encoding/json"
45 "time"
56
67 "github.com/urfave/cli/v2"
@@ -38,6 +39,34 @@ const (
3839 socksProxy = "socks"
3940)
4041
42+ // RemoteConfig models ingress settings that can be managed remotely, for example through the dashboard.
43+ type RemoteConfig struct {
44+ Ingress Ingress
45+ WarpRouting config.WarpRoutingConfig
46+ }
47+
48+ type remoteConfigJSON struct {
49+ GlobalOriginRequest config.OriginRequestConfig `json:"originRequest"`
50+ IngressRules []config.UnvalidatedIngressRule `json:"ingress"`
51+ WarpRouting config.WarpRoutingConfig `json:"warp-routing"`
52+ }
53+
54+ func (rc * RemoteConfig ) UnmarshalJSON (b []byte ) error {
55+ var rawConfig remoteConfigJSON
56+ if err := json .Unmarshal (b , & rawConfig ); err != nil {
57+ return err
58+ }
59+ ingress , err := validateIngress (rawConfig .IngressRules , originRequestFromConfig (rawConfig .GlobalOriginRequest ))
60+ if err != nil {
61+ return err
62+ }
63+
64+ rc .Ingress = ingress
65+ rc .WarpRouting = rawConfig .WarpRouting
66+
67+ return nil
68+ }
69+
4170func originRequestFromSingeRule (c * cli.Context ) OriginRequestConfig {
4271 var connectTimeout time.Duration = defaultConnectTimeout
4372 var tlsTimeout time.Duration = defaultTLSTimeout
@@ -119,7 +148,7 @@ func originRequestFromSingeRule(c *cli.Context) OriginRequestConfig {
119148 }
120149}
121150
122- func originRequestFromYAML ( y config.OriginRequestConfig ) OriginRequestConfig {
151+ func originRequestFromConfig ( c config.OriginRequestConfig ) OriginRequestConfig {
123152 out := OriginRequestConfig {
124153 ConnectTimeout : defaultConnectTimeout ,
125154 TLSTimeout : defaultTLSTimeout ,
@@ -128,50 +157,58 @@ func originRequestFromYAML(y config.OriginRequestConfig) OriginRequestConfig {
128157 KeepAliveTimeout : defaultKeepAliveTimeout ,
129158 ProxyAddress : defaultProxyAddress ,
130159 }
131- if y .ConnectTimeout != nil {
132- out .ConnectTimeout = * y .ConnectTimeout
160+ if c .ConnectTimeout != nil {
161+ out .ConnectTimeout = * c .ConnectTimeout
162+ }
163+ if c .TLSTimeout != nil {
164+ out .TLSTimeout = * c .TLSTimeout
133165 }
134- if y . TLSTimeout != nil {
135- out .TLSTimeout = * y . TLSTimeout
166+ if c . TCPKeepAlive != nil {
167+ out .TCPKeepAlive = * c . TCPKeepAlive
136168 }
137- if y . TCPKeepAlive != nil {
138- out .TCPKeepAlive = * y . TCPKeepAlive
169+ if c . NoHappyEyeballs != nil {
170+ out .NoHappyEyeballs = * c . NoHappyEyeballs
139171 }
140- if y . NoHappyEyeballs != nil {
141- out .NoHappyEyeballs = * y . NoHappyEyeballs
172+ if c . KeepAliveConnections != nil {
173+ out .KeepAliveConnections = * c . KeepAliveConnections
142174 }
143- if y . KeepAliveConnections != nil {
144- out .KeepAliveConnections = * y . KeepAliveConnections
175+ if c . KeepAliveTimeout != nil {
176+ out .KeepAliveTimeout = * c . KeepAliveTimeout
145177 }
146- if y . KeepAliveTimeout != nil {
147- out .KeepAliveTimeout = * y . KeepAliveTimeout
178+ if c . HTTPHostHeader != nil {
179+ out .HTTPHostHeader = * c . HTTPHostHeader
148180 }
149- if y . HTTPHostHeader != nil {
150- out .HTTPHostHeader = * y . HTTPHostHeader
181+ if c . OriginServerName != nil {
182+ out .OriginServerName = * c . OriginServerName
151183 }
152- if y . OriginServerName != nil {
153- out .OriginServerName = * y . OriginServerName
184+ if c . CAPool != nil {
185+ out .CAPool = * c . CAPool
154186 }
155- if y . CAPool != nil {
156- out .CAPool = * y . CAPool
187+ if c . NoTLSVerify != nil {
188+ out .NoTLSVerify = * c . NoTLSVerify
157189 }
158- if y . NoTLSVerify != nil {
159- out .NoTLSVerify = * y . NoTLSVerify
190+ if c . DisableChunkedEncoding != nil {
191+ out .DisableChunkedEncoding = * c . DisableChunkedEncoding
160192 }
161- if y . DisableChunkedEncoding != nil {
162- out .DisableChunkedEncoding = * y . DisableChunkedEncoding
193+ if c . BastionMode != nil {
194+ out .BastionMode = * c . BastionMode
163195 }
164- if y . BastionMode != nil {
165- out .BastionMode = * y . BastionMode
196+ if c . ProxyAddress != nil {
197+ out .ProxyAddress = * c . ProxyAddress
166198 }
167- if y . ProxyAddress != nil {
168- out .ProxyAddress = * y . ProxyAddress
199+ if c . ProxyPort != nil {
200+ out .ProxyPort = * c . ProxyPort
169201 }
170- if y . ProxyPort != nil {
171- out .ProxyPort = * y . ProxyPort
202+ if c . ProxyType != nil {
203+ out .ProxyType = * c . ProxyType
172204 }
173- if y .ProxyType != nil {
174- out .ProxyType = * y .ProxyType
205+ if len (c .IPRules ) > 0 {
206+ for _ , r := range c .IPRules {
207+ rule , err := ipaccess .NewRuleByCIDR (r .Prefix , r .Ports , r .Allow )
208+ if err == nil {
209+ out .IPRules = append (out .IPRules , rule )
210+ }
211+ }
175212 }
176213 return out
177214}
@@ -188,10 +225,10 @@ type OriginRequestConfig struct {
188225 TCPKeepAlive time.Duration `yaml:"tcpKeepAlive"`
189226 // HTTP proxy should disable "happy eyeballs" for IPv4/v6 fallback
190227 NoHappyEyeballs bool `yaml:"noHappyEyeballs"`
191- // HTTP proxy maximum keepalive connection pool size
192- KeepAliveConnections int `yaml:"keepAliveConnections"`
193228 // HTTP proxy timeout for closing an idle connection
194229 KeepAliveTimeout time.Duration `yaml:"keepAliveTimeout"`
230+ // HTTP proxy maximum keepalive connection pool size
231+ KeepAliveConnections int `yaml:"keepAliveConnections"`
195232 // Sets the HTTP Host header for the local webserver.
196233 HTTPHostHeader string `yaml:"httpHostHeader"`
197234 // Hostname on the origin server certificate.
@@ -308,6 +345,19 @@ func (defaults *OriginRequestConfig) setProxyType(overrides config.OriginRequest
308345 }
309346}
310347
348+ func (defaults * OriginRequestConfig ) setIPRules (overrides config.OriginRequestConfig ) {
349+ if val := overrides .IPRules ; len (val ) > 0 {
350+ ipAccessRule := make ([]ipaccess.Rule , len (overrides .IPRules ))
351+ for i , r := range overrides .IPRules {
352+ rule , err := ipaccess .NewRuleByCIDR (r .Prefix , r .Ports , r .Allow )
353+ if err == nil {
354+ ipAccessRule [i ] = rule
355+ }
356+ }
357+ defaults .IPRules = ipAccessRule
358+ }
359+ }
360+
311361// SetConfig gets config for the requests that cloudflared sends to origins.
312362// Each field has a setter method which sets a value for the field by trying to find:
313363// 1. The user config for this rule
@@ -332,5 +382,6 @@ func setConfig(defaults OriginRequestConfig, overrides config.OriginRequestConfi
332382 cfg .setProxyPort (overrides )
333383 cfg .setProxyAddress (overrides )
334384 cfg .setProxyType (overrides )
385+ cfg .setIPRules (overrides )
335386 return cfg
336387}
0 commit comments