cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
config/model.go
114lines · modecode
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/cloudflare/cloudflared/tunneldns" |
| 10 | ) |
| 11 | |
| 12 | // Forwarder represents a client side listener to forward traffic to the edge |
| 13 | type Forwarder struct { |
| 14 | URL string `json:"url"` |
| 15 | Listener string `json:"listener"` |
| 16 | TokenClientID string `json:"service_token_id" yaml:"serviceTokenID"` |
| 17 | TokenSecret string `json:"secret_token_id" yaml:"serviceTokenSecret"` |
| 18 | Destination string `json:"destination"` |
| 19 | IsFedramp bool `json:"is_fedramp" yaml:"isFedramp"` |
| 20 | } |
| 21 | |
| 22 | // Tunnel represents a tunnel that should be started |
| 23 | type Tunnel struct { |
| 24 | URL string `json:"url"` |
| 25 | Origin string `json:"origin"` |
| 26 | ProtocolType string `json:"type"` |
| 27 | } |
| 28 | |
| 29 | // DNSResolver represents a client side DNS resolver |
| 30 | type DNSResolver struct { |
| 31 | Enabled bool `json:"enabled"` |
| 32 | Address string `json:"address,omitempty"` |
| 33 | Port uint16 `json:"port,omitempty"` |
| 34 | Upstreams []string `json:"upstreams,omitempty"` |
| 35 | Bootstraps []string `json:"bootstraps,omitempty"` |
| 36 | MaxUpstreamConnections int `json:"max_upstream_connections,omitempty"` |
| 37 | } |
| 38 | |
| 39 | // Root is the base options to configure the service |
| 40 | type Root struct { |
| 41 | LogDirectory string `json:"log_directory" yaml:"logDirectory,omitempty"` |
| 42 | LogLevel string `json:"log_level" yaml:"logLevel,omitempty"` |
| 43 | Forwarders []Forwarder `json:"forwarders,omitempty" yaml:"forwarders,omitempty"` |
| 44 | Tunnels []Tunnel `json:"tunnels,omitempty" yaml:"tunnels,omitempty"` |
| 45 | Resolver DNSResolver `json:"resolver,omitempty" yaml:"resolver,omitempty"` |
| 46 | } |
| 47 | |
| 48 | // Hash returns the computed values to see if the forwarder values change |
| 49 | func (f *Forwarder) Hash() string { |
| 50 | h := sha256.New() |
| 51 | _, _ = io.WriteString(h, f.URL) |
| 52 | _, _ = io.WriteString(h, f.Listener) |
| 53 | _, _ = io.WriteString(h, f.TokenClientID) |
| 54 | _, _ = io.WriteString(h, f.TokenSecret) |
| 55 | _, _ = io.WriteString(h, f.Destination) |
| 56 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 57 | } |
| 58 | |
| 59 | // Hash returns the computed values to see if the forwarder values change |
| 60 | func (r *DNSResolver) Hash() string { |
| 61 | h := sha256.New() |
| 62 | _, _ = io.WriteString(h, r.Address) |
| 63 | _, _ = io.WriteString(h, strings.Join(r.Bootstraps, ",")) |
| 64 | _, _ = io.WriteString(h, strings.Join(r.Upstreams, ",")) |
| 65 | _, _ = io.WriteString(h, fmt.Sprintf("%d", r.Port)) |
| 66 | _, _ = io.WriteString(h, fmt.Sprintf("%d", r.MaxUpstreamConnections)) |
| 67 | _, _ = io.WriteString(h, fmt.Sprintf("%v", r.Enabled)) |
| 68 | return fmt.Sprintf("%x", h.Sum(nil)) |
| 69 | } |
| 70 | |
| 71 | // EnabledOrDefault returns the enabled property |
| 72 | func (r *DNSResolver) EnabledOrDefault() bool { |
| 73 | return r.Enabled |
| 74 | } |
| 75 | |
| 76 | // AddressOrDefault returns the address or returns the default if empty |
| 77 | func (r *DNSResolver) AddressOrDefault() string { |
| 78 | if r.Address != "" { |
| 79 | return r.Address |
| 80 | } |
| 81 | return "localhost" |
| 82 | } |
| 83 | |
| 84 | // PortOrDefault return the port or returns the default if 0 |
| 85 | func (r *DNSResolver) PortOrDefault() uint16 { |
| 86 | if r.Port > 0 { |
| 87 | return r.Port |
| 88 | } |
| 89 | return 53 |
| 90 | } |
| 91 | |
| 92 | // UpstreamsOrDefault returns the upstreams or returns the default if empty |
| 93 | func (r *DNSResolver) UpstreamsOrDefault() []string { |
| 94 | if len(r.Upstreams) > 0 { |
| 95 | return r.Upstreams |
| 96 | } |
| 97 | return []string{"https://1.1.1.1/dns-query", "https://1.0.0.1/dns-query"} |
| 98 | } |
| 99 | |
| 100 | // BootstrapsOrDefault returns the bootstraps or returns the default if empty |
| 101 | func (r *DNSResolver) BootstrapsOrDefault() []string { |
| 102 | if len(r.Bootstraps) > 0 { |
| 103 | return r.Bootstraps |
| 104 | } |
| 105 | return []string{"https://162.159.36.1/dns-query", "https://162.159.46.1/dns-query", "https://[2606:4700:4700::1111]/dns-query", "https://[2606:4700:4700::1001]/dns-query"} |
| 106 | } |
| 107 | |
| 108 | // MaxUpstreamConnectionsOrDefault return the max upstream connections or returns the default if negative |
| 109 | func (r *DNSResolver) MaxUpstreamConnectionsOrDefault() int { |
| 110 | if r.MaxUpstreamConnections >= 0 { |
| 111 | return r.MaxUpstreamConnections |
| 112 | } |
| 113 | return tunneldns.MaxUpstreamConnsDefault |
| 114 | } |
| 115 | |