2018-02-26 14:34:04 +00:00
|
|
|
package rules
|
2016-03-27 00:05:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-08-05 18:42:45 +00:00
|
|
|
"fmt"
|
2016-04-06 11:06:31 +00:00
|
|
|
"net/http"
|
2016-03-27 00:05:17 +00:00
|
|
|
"reflect"
|
2016-04-06 11:06:31 +00:00
|
|
|
"sort"
|
2016-03-27 00:05:17 +00:00
|
|
|
"strings"
|
2017-04-17 20:47:53 +00:00
|
|
|
|
|
|
|
"github.com/containous/mux"
|
2018-07-03 14:44:05 +00:00
|
|
|
"github.com/containous/traefik/hostresolver"
|
|
|
|
"github.com/containous/traefik/log"
|
2018-07-09 13:08:04 +00:00
|
|
|
"github.com/containous/traefik/middlewares"
|
2017-04-17 20:47:53 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2016-03-27 00:05:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Rules holds rule parsing and configuration
|
|
|
|
type Rules struct {
|
2018-07-03 14:44:05 +00:00
|
|
|
Route *types.ServerRoute
|
|
|
|
err error
|
|
|
|
HostResolver *hostresolver.Resolver
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 11:06:31 +00:00
|
|
|
func (r *Rules) host(hosts ...string) *mux.Route {
|
2018-07-09 13:08:04 +00:00
|
|
|
for i, host := range hosts {
|
|
|
|
hosts[i] = strings.ToLower(host)
|
|
|
|
}
|
|
|
|
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route.MatcherFunc(func(req *http.Request, route *mux.RouteMatch) bool {
|
2018-07-09 13:08:04 +00:00
|
|
|
reqHost := middlewares.GetCanonizedHost(req.Context())
|
|
|
|
if len(reqHost) == 0 {
|
|
|
|
return false
|
2016-04-07 13:01:04 +00:00
|
|
|
}
|
2018-07-03 14:44:05 +00:00
|
|
|
|
|
|
|
if r.HostResolver != nil && r.HostResolver.CnameFlattening {
|
2018-07-09 13:08:04 +00:00
|
|
|
reqH, flatH := r.HostResolver.CNAMEFlatten(reqHost)
|
2018-07-03 14:44:05 +00:00
|
|
|
for _, host := range hosts {
|
2018-07-09 13:08:04 +00:00
|
|
|
if strings.EqualFold(reqH, host) || strings.EqualFold(flatH, host) {
|
2018-07-03 14:44:05 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
log.Debugf("CNAMEFlattening: request %s which resolved to %s, is not matched to route %s", reqH, flatH, host)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-06 11:06:31 +00:00
|
|
|
for _, host := range hosts {
|
2018-07-09 13:08:04 +00:00
|
|
|
if reqHost == host {
|
2016-04-06 11:06:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 08:18:03 +00:00
|
|
|
func (r *Rules) hostRegexp(hostPatterns ...string) *mux.Route {
|
2018-02-26 14:34:04 +00:00
|
|
|
router := r.Route.Route.Subrouter()
|
2018-10-25 08:18:03 +00:00
|
|
|
for _, hostPattern := range hostPatterns {
|
|
|
|
router.Host(hostPattern)
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 11:06:31 +00:00
|
|
|
func (r *Rules) path(paths ...string) *mux.Route {
|
2018-02-26 14:34:04 +00:00
|
|
|
router := r.Route.Route.Subrouter()
|
2016-04-06 11:06:31 +00:00
|
|
|
for _, path := range paths {
|
2018-07-09 13:08:04 +00:00
|
|
|
router.Path(path)
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rules) pathPrefix(paths ...string) *mux.Route {
|
2018-02-26 14:34:04 +00:00
|
|
|
router := r.Route.Route.Subrouter()
|
2016-04-06 11:06:31 +00:00
|
|
|
for _, path := range paths {
|
2017-12-19 16:00:12 +00:00
|
|
|
buildPath(path, router)
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:00:12 +00:00
|
|
|
func buildPath(path string, router *mux.Router) {
|
|
|
|
// {} are used to define a regex pattern in http://www.gorillatoolkit.org/pkg/mux.
|
|
|
|
// if we find a { in the path, that means we use regex, then the gorilla/mux implementation is chosen
|
|
|
|
// otherwise, we use a lightweight implementation
|
2018-07-09 13:08:04 +00:00
|
|
|
if strings.Contains(path, "{") {
|
|
|
|
router.PathPrefix(path)
|
2017-12-19 16:00:12 +00:00
|
|
|
} else {
|
2018-07-09 13:08:04 +00:00
|
|
|
m := &prefixMatcher{prefix: path}
|
2017-12-19 16:00:12 +00:00
|
|
|
router.NewRoute().MatcherFunc(m.Match)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type prefixMatcher struct {
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *prefixMatcher) Match(r *http.Request, _ *mux.RouteMatch) bool {
|
|
|
|
return strings.HasPrefix(r.URL.Path, m.prefix) || strings.HasPrefix(r.URL.Path, m.prefix+"/")
|
|
|
|
}
|
|
|
|
|
2016-04-06 11:06:31 +00:00
|
|
|
type bySize []string
|
|
|
|
|
|
|
|
func (a bySize) Len() int { return len(a) }
|
|
|
|
func (a bySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a bySize) Less(i, j int) bool { return len(a[i]) > len(a[j]) }
|
|
|
|
|
|
|
|
func (r *Rules) pathStrip(paths ...string) *mux.Route {
|
|
|
|
sort.Sort(bySize(paths))
|
2018-02-26 14:34:04 +00:00
|
|
|
r.Route.StripPrefixes = paths
|
|
|
|
router := r.Route.Route.Subrouter()
|
2016-04-06 11:06:31 +00:00
|
|
|
for _, path := range paths {
|
2016-11-17 14:36:10 +00:00
|
|
|
router.Path(strings.TrimSpace(path))
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 11:07:59 +00:00
|
|
|
func (r *Rules) pathStripRegex(paths ...string) *mux.Route {
|
|
|
|
sort.Sort(bySize(paths))
|
2018-02-26 14:34:04 +00:00
|
|
|
r.Route.StripPrefixesRegex = paths
|
|
|
|
router := r.Route.Route.Subrouter()
|
2017-03-24 11:07:59 +00:00
|
|
|
for _, path := range paths {
|
2018-07-09 13:08:04 +00:00
|
|
|
router.Path(path)
|
2017-03-24 11:07:59 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2017-03-24 11:07:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 18:13:39 +00:00
|
|
|
func (r *Rules) replacePath(paths ...string) *mux.Route {
|
|
|
|
for _, path := range paths {
|
2018-02-26 14:34:04 +00:00
|
|
|
r.Route.ReplacePath = path
|
2017-04-25 18:13:39 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2017-04-25 18:13:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 11:54:03 +00:00
|
|
|
func (r *Rules) replacePathRegex(paths ...string) *mux.Route {
|
|
|
|
for _, path := range paths {
|
2018-02-26 14:34:04 +00:00
|
|
|
r.Route.ReplacePathRegex = path
|
2017-10-30 11:54:03 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2017-10-30 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-02 12:40:18 +00:00
|
|
|
func (r *Rules) addPrefix(paths ...string) *mux.Route {
|
|
|
|
for _, path := range paths {
|
2018-02-26 14:34:04 +00:00
|
|
|
r.Route.AddPrefix = path
|
2016-12-02 12:40:18 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2016-12-02 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 11:06:31 +00:00
|
|
|
func (r *Rules) pathPrefixStrip(paths ...string) *mux.Route {
|
|
|
|
sort.Sort(bySize(paths))
|
2018-02-26 14:34:04 +00:00
|
|
|
r.Route.StripPrefixes = paths
|
|
|
|
router := r.Route.Route.Subrouter()
|
2016-04-06 11:06:31 +00:00
|
|
|
for _, path := range paths {
|
2017-12-19 16:00:12 +00:00
|
|
|
buildPath(path, router)
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 11:07:59 +00:00
|
|
|
func (r *Rules) pathPrefixStripRegex(paths ...string) *mux.Route {
|
|
|
|
sort.Sort(bySize(paths))
|
2018-02-26 14:34:04 +00:00
|
|
|
r.Route.StripPrefixesRegex = paths
|
|
|
|
router := r.Route.Route.Subrouter()
|
2017-03-24 11:07:59 +00:00
|
|
|
for _, path := range paths {
|
2018-07-09 13:08:04 +00:00
|
|
|
router.PathPrefix(path)
|
2017-03-24 11:07:59 +00:00
|
|
|
}
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route
|
2017-03-24 11:07:59 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 00:05:17 +00:00
|
|
|
func (r *Rules) methods(methods ...string) *mux.Route {
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route.Methods(methods...)
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rules) headers(headers ...string) *mux.Route {
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route.Headers(headers...)
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rules) headersRegexp(headers ...string) *mux.Route {
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route.HeadersRegexp(headers...)
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 18:28:03 +00:00
|
|
|
func (r *Rules) query(query ...string) *mux.Route {
|
|
|
|
var queries []string
|
|
|
|
for _, elem := range query {
|
|
|
|
queries = append(queries, strings.Split(elem, "=")...)
|
|
|
|
}
|
|
|
|
|
2018-02-26 14:34:04 +00:00
|
|
|
return r.Route.Route.Queries(queries...)
|
2017-08-24 18:28:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 18:42:45 +00:00
|
|
|
func (r *Rules) parseRules(expression string, onRule func(functionName string, function interface{}, arguments []string) error) error {
|
2016-03-27 00:05:17 +00:00
|
|
|
functions := map[string]interface{}{
|
2017-03-24 11:07:59 +00:00
|
|
|
"Host": r.host,
|
|
|
|
"HostRegexp": r.hostRegexp,
|
|
|
|
"Path": r.path,
|
|
|
|
"PathStrip": r.pathStrip,
|
|
|
|
"PathStripRegex": r.pathStripRegex,
|
|
|
|
"PathPrefix": r.pathPrefix,
|
|
|
|
"PathPrefixStrip": r.pathPrefixStrip,
|
|
|
|
"PathPrefixStripRegex": r.pathPrefixStripRegex,
|
|
|
|
"Method": r.methods,
|
|
|
|
"Headers": r.headers,
|
|
|
|
"HeadersRegexp": r.headersRegexp,
|
|
|
|
"AddPrefix": r.addPrefix,
|
|
|
|
"ReplacePath": r.replacePath,
|
2017-10-30 11:54:03 +00:00
|
|
|
"ReplacePathRegex": r.replacePathRegex,
|
2017-08-24 18:28:03 +00:00
|
|
|
"Query": r.query,
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
2016-06-22 20:07:02 +00:00
|
|
|
|
|
|
|
if len(expression) == 0 {
|
2018-03-23 12:30:03 +00:00
|
|
|
return errors.New("empty rule")
|
2016-06-22 20:07:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 00:05:17 +00:00
|
|
|
f := func(c rune) bool {
|
2016-04-06 11:06:31 +00:00
|
|
|
return c == ':'
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:21:00 +00:00
|
|
|
// Allow multiple rules separated by ;
|
|
|
|
splitRule := func(c rune) bool {
|
|
|
|
return c == ';'
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
2016-06-06 07:21:00 +00:00
|
|
|
|
|
|
|
parsedRules := strings.FieldsFunc(expression, splitRule)
|
|
|
|
|
|
|
|
for _, rule := range parsedRules {
|
|
|
|
// get function
|
|
|
|
parsedFunctions := strings.FieldsFunc(rule, f)
|
|
|
|
if len(parsedFunctions) == 0 {
|
2017-05-26 15:03:14 +00:00
|
|
|
return fmt.Errorf("error parsing rule: '%s'", rule)
|
2016-06-06 07:21:00 +00:00
|
|
|
}
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2016-08-05 18:42:45 +00:00
|
|
|
functionName := strings.TrimSpace(parsedFunctions[0])
|
|
|
|
parsedFunction, ok := functions[functionName]
|
2016-06-06 07:21:00 +00:00
|
|
|
if !ok {
|
2017-05-26 15:03:14 +00:00
|
|
|
return fmt.Errorf("error parsing rule: '%s'. Unknown function: '%s'", rule, parsedFunctions[0])
|
2016-04-07 13:01:04 +00:00
|
|
|
}
|
2016-06-06 07:21:00 +00:00
|
|
|
parsedFunctions = append(parsedFunctions[:0], parsedFunctions[1:]...)
|
2018-03-23 12:30:03 +00:00
|
|
|
|
|
|
|
// get function
|
2016-06-06 07:21:00 +00:00
|
|
|
fargs := func(c rune) bool {
|
|
|
|
return c == ','
|
|
|
|
}
|
|
|
|
parsedArgs := strings.FieldsFunc(strings.Join(parsedFunctions, ":"), fargs)
|
|
|
|
if len(parsedArgs) == 0 {
|
2017-05-26 15:03:14 +00:00
|
|
|
return fmt.Errorf("error parsing args from rule: '%s'", rule)
|
2016-06-06 07:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range parsedArgs {
|
2016-08-05 18:42:45 +00:00
|
|
|
parsedArgs[i] = strings.TrimSpace(parsedArgs[i])
|
2016-06-06 07:21:00 +00:00
|
|
|
}
|
2016-08-05 18:42:45 +00:00
|
|
|
|
|
|
|
err := onRule(functionName, parsedFunction, parsedArgs)
|
|
|
|
if err != nil {
|
2018-03-23 12:30:03 +00:00
|
|
|
return fmt.Errorf("parsing error on rule: %v", err)
|
2016-08-05 18:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses rules expressions
|
|
|
|
func (r *Rules) Parse(expression string) (*mux.Route, error) {
|
|
|
|
var resultRoute *mux.Route
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2016-08-05 18:42:45 +00:00
|
|
|
err := r.parseRules(expression, func(functionName string, function interface{}, arguments []string) error {
|
|
|
|
inputs := make([]reflect.Value, len(arguments))
|
|
|
|
for i := range arguments {
|
|
|
|
inputs[i] = reflect.ValueOf(arguments[i])
|
|
|
|
}
|
|
|
|
method := reflect.ValueOf(function)
|
2016-06-06 07:21:00 +00:00
|
|
|
if method.IsValid() {
|
|
|
|
resultRoute = method.Call(inputs)[0].Interface().(*mux.Route)
|
|
|
|
if r.err != nil {
|
2016-08-05 18:42:45 +00:00
|
|
|
return r.err
|
2016-06-06 07:21:00 +00:00
|
|
|
}
|
2018-10-04 08:20:03 +00:00
|
|
|
if resultRoute == nil {
|
|
|
|
return fmt.Errorf("invalid expression: %s", expression)
|
|
|
|
}
|
2016-06-06 07:21:00 +00:00
|
|
|
if resultRoute.GetError() != nil {
|
2016-08-05 18:42:45 +00:00
|
|
|
return resultRoute.GetError()
|
2016-06-06 07:21:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-23 12:30:03 +00:00
|
|
|
return fmt.Errorf("method not found: '%s'", functionName)
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2016-08-05 18:42:45 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
return nil, fmt.Errorf("error parsing rule: %v", err)
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2016-06-06 07:21:00 +00:00
|
|
|
return resultRoute, nil
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
2016-08-05 18:42:45 +00:00
|
|
|
|
|
|
|
// ParseDomains parses rules expressions and returns domains
|
|
|
|
func (r *Rules) ParseDomains(expression string) ([]string, error) {
|
2018-03-23 12:30:03 +00:00
|
|
|
var domains []string
|
2018-09-04 15:14:04 +00:00
|
|
|
isHostRule := false
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2016-08-05 18:42:45 +00:00
|
|
|
err := r.parseRules(expression, func(functionName string, function interface{}, arguments []string) error {
|
|
|
|
if functionName == "Host" {
|
2018-09-04 15:14:04 +00:00
|
|
|
isHostRule = true
|
2016-08-05 18:42:45 +00:00
|
|
|
domains = append(domains, arguments...)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-26 15:03:14 +00:00
|
|
|
return nil, fmt.Errorf("error parsing domains: %v", err)
|
2016-08-05 18:42:45 +00:00
|
|
|
}
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2018-09-04 15:14:04 +00:00
|
|
|
var cleanDomains []string
|
|
|
|
for _, domain := range domains {
|
2018-09-07 08:11:30 +00:00
|
|
|
canonicalDomain := strings.ToLower(domain)
|
2018-09-04 15:14:04 +00:00
|
|
|
if len(canonicalDomain) > 0 {
|
|
|
|
cleanDomains = append(cleanDomains, canonicalDomain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an error if an Host rule is detected but no domain are parsed
|
|
|
|
if isHostRule && len(cleanDomains) == 0 {
|
|
|
|
return nil, fmt.Errorf("unable to parse correctly the domains in the Host rule from %q", expression)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cleanDomains, nil
|
2016-08-05 18:42:45 +00:00
|
|
|
}
|