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