rule.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package ingress
  2. import (
  3. "crypto/tls"
  4. "net/http"
  5. "regexp"
  6. "strings"
  7. )
  8. // Rule routes traffic from a hostname/path on the public internet to the
  9. // service running on the given URL.
  10. type Rule struct {
  11. // Requests for this hostname will be proxied to this rule's service.
  12. Hostname string
  13. // Path is an optional regex that can specify path-driven ingress rules.
  14. Path *regexp.Regexp
  15. // A (probably local) address. Requests for a hostname which matches this
  16. // rule's hostname pattern will be proxied to the service running on this
  17. // address.
  18. Service OriginService
  19. // Configure the request cloudflared sends to this specific origin.
  20. Config OriginRequestConfig
  21. // Configures TLS for the cloudflared -> origin request
  22. ClientTLSConfig *tls.Config
  23. // Configures HTTP for the cloudflared -> origin request
  24. HTTPTransport http.RoundTripper
  25. }
  26. // MultiLineString is for outputting rules in a human-friendly way when Cloudflared
  27. // is used as a CLI tool (not as a daemon).
  28. func (r Rule) MultiLineString() string {
  29. var out strings.Builder
  30. if r.Hostname != "" {
  31. out.WriteString("\thostname: ")
  32. out.WriteString(r.Hostname)
  33. out.WriteRune('\n')
  34. }
  35. if r.Path != nil {
  36. out.WriteString("\tpath: ")
  37. out.WriteString(r.Path.String())
  38. out.WriteRune('\n')
  39. }
  40. out.WriteString("\tservice: ")
  41. out.WriteString(r.Service.String())
  42. return out.String()
  43. }
  44. // Matches checks if the rule matches a given hostname/path combination.
  45. func (r *Rule) Matches(hostname, path string) bool {
  46. hostMatch := r.Hostname == "" || r.Hostname == "*" || matchHost(r.Hostname, hostname)
  47. pathMatch := r.Path == nil || r.Path.MatchString(path)
  48. return hostMatch && pathMatch
  49. }