rule.go 1.4 KB

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