mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 01:09:57 +00:00
TUN-3441: Multiple-origin routing via ingress rules
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
errNoIngressRules = errors.New("No ingress rules were specified in the config file")
|
||||
ErrNoIngressRules = errors.New("No ingress rules were specified in the config file")
|
||||
errLastRuleNotCatchAll = errors.New("The last ingress rule must match all hostnames (i.e. it must be missing, or must be \"*\")")
|
||||
errBadWildcard = errors.New("Hostname patterns can have at most one wildcard character (\"*\") and it can only be used for subdomains, e.g. \"*.example.com\"")
|
||||
errNoIngressRulesMatch = errors.New("The URL didn't match any ingress rules")
|
||||
@@ -50,12 +50,24 @@ func (r Rule) String() string {
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func (r Rule) matches(requestURL *url.URL) bool {
|
||||
hostMatch := r.Hostname == "" || r.Hostname == "*" || matchHost(r.Hostname, requestURL.Hostname())
|
||||
pathMatch := r.Path == nil || r.Path.MatchString(requestURL.Path)
|
||||
func (r *Rule) Matches(hostname, path string) bool {
|
||||
hostMatch := r.Hostname == "" || r.Hostname == "*" || matchHost(r.Hostname, hostname)
|
||||
pathMatch := r.Path == nil || r.Path.MatchString(path)
|
||||
return hostMatch && pathMatch
|
||||
}
|
||||
|
||||
// FindMatchingRule returns the index of the Ingress Rule which matches the given
|
||||
// hostname and path. This function assumes the last rule matches everything,
|
||||
// which is the case if the rules were instantiated via the ingress#Validate method
|
||||
func FindMatchingRule(hostname, path string, rules []Rule) int {
|
||||
for i, rule := range rules {
|
||||
if rule.Matches(hostname, path) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return len(rules) - 1
|
||||
}
|
||||
|
||||
func matchHost(ruleHost, reqHost string) bool {
|
||||
if ruleHost == reqHost {
|
||||
return true
|
||||
@@ -94,6 +106,10 @@ func (ing ingress) validate() ([]Rule, error) {
|
||||
return nil, fmt.Errorf("The service %s must have a scheme and a hostname", r.Service)
|
||||
}
|
||||
|
||||
if service.Path != "" {
|
||||
return nil, fmt.Errorf("%s is an invalid address, ingress rules don't support proxying to a different path on the origin service. The path will be the same as the eyeball request's path.", r.Service)
|
||||
}
|
||||
|
||||
// Ensure that there are no wildcards anywhere except the first character
|
||||
// of the hostname.
|
||||
if strings.LastIndex(r.Hostname, "*") > 0 {
|
||||
@@ -145,7 +161,7 @@ func ParseIngress(rawYAML []byte) ([]Rule, error) {
|
||||
return nil, err
|
||||
}
|
||||
if len(ing.Ingress) == 0 {
|
||||
return nil, errNoIngressRules
|
||||
return nil, ErrNoIngressRules
|
||||
}
|
||||
return ing.validate()
|
||||
}
|
||||
@@ -155,12 +171,8 @@ func RuleCommand(rules []Rule, requestURL *url.URL) error {
|
||||
if requestURL.Hostname() == "" {
|
||||
return fmt.Errorf("%s is malformed and doesn't have a hostname", requestURL)
|
||||
}
|
||||
for i, r := range rules {
|
||||
if r.matches(requestURL) {
|
||||
fmt.Printf("Matched rule #%d\n", i+1)
|
||||
fmt.Println(r.String())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errNoIngressRulesMatch
|
||||
i := FindMatchingRule(requestURL.Hostname(), requestURL.Path, rules)
|
||||
fmt.Printf("Matched rule #%d\n", i+1)
|
||||
fmt.Println(rules[i].String())
|
||||
return nil
|
||||
}
|
||||
|
@@ -135,6 +135,33 @@ ingress:
|
||||
args: args{rawYAML: `
|
||||
ingress:
|
||||
- service: localhost:8000
|
||||
`},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Wildcard not at start",
|
||||
args: args{rawYAML: `
|
||||
ingress:
|
||||
- hostname: "test.*.example.com"
|
||||
service: https://localhost:8000
|
||||
`},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Can't use --url",
|
||||
args: args{rawYAML: `
|
||||
url: localhost:8080
|
||||
ingress:
|
||||
- hostname: "*.example.com"
|
||||
service: https://localhost:8000
|
||||
`},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Service can't have a path",
|
||||
args: args{rawYAML: `
|
||||
ingress:
|
||||
- service: https://localhost:8000/static/
|
||||
`},
|
||||
wantErr: true,
|
||||
},
|
||||
@@ -263,9 +290,31 @@ func Test_rule_matches(t *testing.T) {
|
||||
Path: tt.fields.Path,
|
||||
Service: tt.fields.Service,
|
||||
}
|
||||
if got := r.matches(tt.args.requestURL); got != tt.want {
|
||||
u := tt.args.requestURL
|
||||
if got := r.Matches(u.Hostname(), u.Path); got != tt.want {
|
||||
t.Errorf("rule.matches() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFindMatch(b *testing.B) {
|
||||
rulesYAML := `
|
||||
ingress:
|
||||
- hostname: tunnel1.example.com
|
||||
service: https://localhost:8000
|
||||
- hostname: tunnel2.example.com
|
||||
service: https://localhost:8001
|
||||
- hostname: "*"
|
||||
service: https://localhost:8002
|
||||
`
|
||||
rules, err := ParseIngress([]byte(rulesYAML))
|
||||
if err != nil {
|
||||
b.Error(err)
|
||||
}
|
||||
for n := 0; n < b.N; n++ {
|
||||
FindMatchingRule("tunnel1.example.com", "", rules)
|
||||
FindMatchingRule("tunnel2.example.com", "", rules)
|
||||
FindMatchingRule("tunnel3.example.com", "", rules)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user