model.go 3.7 KB

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