2020-10-15 21:41:03 +00:00
|
|
|
package ingress
|
|
|
|
|
|
|
|
import (
|
2022-03-14 17:51:10 +00:00
|
|
|
"encoding/json"
|
2020-10-15 21:41:03 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Rule routes traffic from a hostname/path on the public internet to the
|
|
|
|
// service running on the given URL.
|
|
|
|
type Rule struct {
|
|
|
|
// Requests for this hostname will be proxied to this rule's service.
|
2022-03-14 17:51:10 +00:00
|
|
|
Hostname string `json:"hostname"`
|
2020-10-15 21:41:03 +00:00
|
|
|
|
|
|
|
// Path is an optional regex that can specify path-driven ingress rules.
|
2022-03-14 17:51:10 +00:00
|
|
|
Path *Regexp `json:"path"`
|
2020-10-15 21:41:03 +00:00
|
|
|
|
|
|
|
// A (probably local) address. Requests for a hostname which matches this
|
|
|
|
// rule's hostname pattern will be proxied to the service running on this
|
|
|
|
// address.
|
2022-03-14 17:51:10 +00:00
|
|
|
Service OriginService `json:"service"`
|
2020-10-15 21:41:03 +00:00
|
|
|
|
2022-03-29 10:31:52 +00:00
|
|
|
// Location is an optional string that can specify the Service path (overwriting the Hostname path).
|
|
|
|
Location string `json:"location"`
|
|
|
|
|
2020-10-15 21:41:03 +00:00
|
|
|
// Configure the request cloudflared sends to this specific origin.
|
2022-03-14 17:51:10 +00:00
|
|
|
Config OriginRequestConfig `json:"originRequest"`
|
2020-10-15 21:41:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MultiLineString is for outputting rules in a human-friendly way when Cloudflared
|
|
|
|
// is used as a CLI tool (not as a daemon).
|
|
|
|
func (r Rule) MultiLineString() string {
|
|
|
|
var out strings.Builder
|
|
|
|
if r.Hostname != "" {
|
|
|
|
out.WriteString("\thostname: ")
|
|
|
|
out.WriteString(r.Hostname)
|
|
|
|
out.WriteRune('\n')
|
|
|
|
}
|
2022-03-14 17:51:10 +00:00
|
|
|
if r.Path != nil && r.Path.Regexp != nil {
|
2020-10-15 21:41:03 +00:00
|
|
|
out.WriteString("\tpath: ")
|
2022-03-14 17:51:10 +00:00
|
|
|
out.WriteString(r.Path.Regexp.String())
|
2020-10-15 21:41:03 +00:00
|
|
|
out.WriteRune('\n')
|
|
|
|
}
|
|
|
|
out.WriteString("\tservice: ")
|
|
|
|
out.WriteString(r.Service.String())
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matches checks if the rule matches a given hostname/path combination.
|
|
|
|
func (r *Rule) Matches(hostname, path string) bool {
|
|
|
|
hostMatch := r.Hostname == "" || r.Hostname == "*" || matchHost(r.Hostname, hostname)
|
2022-03-14 17:51:10 +00:00
|
|
|
pathMatch := r.Path == nil || r.Path.Regexp == nil || r.Path.Regexp.MatchString(path)
|
2020-10-15 21:41:03 +00:00
|
|
|
return hostMatch && pathMatch
|
|
|
|
}
|
2022-03-14 17:51:10 +00:00
|
|
|
|
|
|
|
// Regexp adds unmarshalling from json for regexp.Regexp
|
|
|
|
type Regexp struct {
|
|
|
|
*regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Regexp) MarshalJSON() ([]byte, error) {
|
|
|
|
if r.Regexp == nil {
|
|
|
|
return json.Marshal(nil)
|
|
|
|
}
|
|
|
|
return json.Marshal(r.Regexp.String())
|
|
|
|
}
|