2022-03-17 17:02:08 +00:00
|
|
|
package http
|
2016-03-27 00:05:17 +00:00
|
|
|
|
|
|
|
import (
|
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
|
|
|
"strings"
|
2021-03-22 20:16:04 +00:00
|
|
|
"unicode/utf8"
|
2017-04-17 20:47:53 +00:00
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-06-07 16:14:09 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/ip"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/requestdecorator"
|
2022-03-17 17:02:08 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/rules"
|
2019-01-30 15:24:07 +00:00
|
|
|
"github.com/vulcand/predicate"
|
2016-03-27 00:05:17 +00:00
|
|
|
)
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
const hostMatcher = "Host"
|
|
|
|
|
|
|
|
var httpFuncs = map[string]func(*mux.Route, ...string) error{
|
|
|
|
hostMatcher: host,
|
2020-07-08 10:18:03 +00:00
|
|
|
"HostHeader": host,
|
2019-01-30 15:24:07 +00:00
|
|
|
"HostRegexp": hostRegexp,
|
2021-06-07 16:14:09 +00:00
|
|
|
"ClientIP": clientIP,
|
2019-01-30 15:24:07 +00:00
|
|
|
"Path": path,
|
|
|
|
"PathPrefix": pathPrefix,
|
|
|
|
"Method": methods,
|
|
|
|
"Headers": headers,
|
|
|
|
"HeadersRegexp": headersRegexp,
|
|
|
|
"Query": query,
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
// Muxer handles routing with rules.
|
|
|
|
type Muxer struct {
|
2019-01-30 15:24:07 +00:00
|
|
|
*mux.Router
|
|
|
|
parser predicate.Parser
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
// NewMuxer returns a new muxer instance.
|
|
|
|
func NewMuxer() (*Muxer, error) {
|
|
|
|
var matchers []string
|
|
|
|
for matcher := range httpFuncs {
|
|
|
|
matchers = append(matchers, matcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
parser, err := rules.NewParser(matchers)
|
2019-01-30 15:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2019-01-30 15:24:07 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
return &Muxer{
|
2019-01-30 15:24:07 +00:00
|
|
|
Router: mux.NewRouter().SkipClean(true),
|
|
|
|
parser: parser,
|
|
|
|
}, nil
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
// AddRoute add a new route to the router.
|
2022-03-17 17:02:08 +00:00
|
|
|
func (r *Muxer) AddRoute(rule string, priority int, handler http.Handler) error {
|
2019-01-30 15:24:07 +00:00
|
|
|
parse, err := r.parser.Parse(rule)
|
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return fmt.Errorf("error while parsing rule %s: %w", rule, err)
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
buildTree, ok := parse.(rules.TreeBuilder)
|
2019-01-30 15:24:07 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("error while parsing rule %s", rule)
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2016-03-27 00:05:17 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
if priority == 0 {
|
|
|
|
priority = len(rule)
|
2017-12-19 16:00:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
route := r.NewRoute().Handler(handler).Priority(priority)
|
2020-12-17 09:06:03 +00:00
|
|
|
|
|
|
|
err = addRuleOnRoute(route, buildTree())
|
|
|
|
if err != nil {
|
|
|
|
route.BuildOnly()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-12-19 16:00:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
// ParseDomains extract domains from rule.
|
|
|
|
func ParseDomains(rule string) ([]string, error) {
|
|
|
|
var matchers []string
|
|
|
|
for matcher := range httpFuncs {
|
|
|
|
matchers = append(matchers, matcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
parser, err := rules.NewParser(matchers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parse, err := parser.Parse(rule)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTree, ok := parse.(rules.TreeBuilder)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error while parsing rule %s", rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildTree().ParseMatchers([]string{hostMatcher}), nil
|
2017-12-19 16:00:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func path(route *mux.Route, paths ...string) error {
|
|
|
|
rt := route.Subrouter()
|
2016-04-06 11:06:31 +00:00
|
|
|
|
|
|
|
for _, path := range paths {
|
2022-03-17 17:02:08 +00:00
|
|
|
if err := rt.Path(path).GetError(); err != nil {
|
|
|
|
return err
|
2019-01-30 15:24:07 +00:00
|
|
|
}
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2022-03-17 17:02:08 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
return nil
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func pathPrefix(route *mux.Route, paths ...string) error {
|
|
|
|
rt := route.Subrouter()
|
2017-03-24 11:07:59 +00:00
|
|
|
|
2017-04-25 18:13:39 +00:00
|
|
|
for _, path := range paths {
|
2022-03-17 17:02:08 +00:00
|
|
|
if err := rt.PathPrefix(path).GetError(); err != nil {
|
|
|
|
return err
|
2019-01-30 15:24:07 +00:00
|
|
|
}
|
2017-04-25 18:13:39 +00:00
|
|
|
}
|
2022-03-17 17:02:08 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
return nil
|
2017-04-25 18:13:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func host(route *mux.Route, hosts ...string) error {
|
|
|
|
for i, host := range hosts {
|
2021-03-22 20:16:04 +00:00
|
|
|
if !IsASCII(host) {
|
|
|
|
return fmt.Errorf("invalid value %q for \"Host\" matcher, non-ASCII characters are not allowed", host)
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
hosts[i] = strings.ToLower(host)
|
2017-10-30 11:54:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
route.MatcherFunc(func(req *http.Request, _ *mux.RouteMatch) bool {
|
2020-07-13 15:58:03 +00:00
|
|
|
reqHost := requestdecorator.GetCanonizedHost(req.Context())
|
|
|
|
if len(reqHost) == 0 {
|
2021-07-13 13:30:20 +00:00
|
|
|
// If the request is an HTTP/1.0 request, then a Host may not be defined.
|
|
|
|
if req.ProtoAtLeast(1, 1) {
|
|
|
|
log.FromContext(req.Context()).Warnf("Could not retrieve CanonizedHost, rejecting %s", req.Host)
|
|
|
|
}
|
|
|
|
|
2020-07-13 15:58:03 +00:00
|
|
|
return false
|
2019-01-30 15:24:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 15:58:03 +00:00
|
|
|
flatH := requestdecorator.GetCNAMEFlatten(req.Context())
|
|
|
|
if len(flatH) > 0 {
|
|
|
|
for _, host := range hosts {
|
|
|
|
if strings.EqualFold(reqHost, host) || strings.EqualFold(flatH, host) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
log.FromContext(req.Context()).Debugf("CNAMEFlattening: request %s which resolved to %s, is not matched to route %s", reqHost, flatH, host)
|
2020-07-08 10:18:03 +00:00
|
|
|
}
|
2020-07-13 15:58:03 +00:00
|
|
|
return false
|
2020-07-08 10:18:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 15:58:03 +00:00
|
|
|
for _, host := range hosts {
|
|
|
|
if reqHost == host {
|
2019-01-30 15:24:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-05-06 15:16:03 +00:00
|
|
|
|
2020-07-13 15:58:03 +00:00
|
|
|
// Check for match on trailing period on host
|
|
|
|
if last := len(host) - 1; last >= 0 && host[last] == '.' {
|
|
|
|
h := host[:last]
|
|
|
|
if reqHost == h {
|
|
|
|
return true
|
|
|
|
}
|
2019-05-06 15:16:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 15:58:03 +00:00
|
|
|
// Check for match on trailing period on request
|
|
|
|
if last := len(reqHost) - 1; last >= 0 && reqHost[last] == '.' {
|
|
|
|
h := reqHost[:last]
|
|
|
|
if h == host {
|
|
|
|
return true
|
|
|
|
}
|
2019-05-06 15:16:03 +00:00
|
|
|
}
|
2019-01-30 15:24:07 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return nil
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 16:14:09 +00:00
|
|
|
func clientIP(route *mux.Route, clientIPs ...string) error {
|
|
|
|
checker, err := ip.NewChecker(clientIPs)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not initialize IP Checker for \"ClientIP\" matcher: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
strategy := ip.RemoteAddrStrategy{}
|
|
|
|
|
|
|
|
route.MatcherFunc(func(req *http.Request, _ *mux.RouteMatch) bool {
|
|
|
|
ok, err := checker.Contains(strategy.GetIP(req))
|
|
|
|
if err != nil {
|
|
|
|
log.FromContext(req.Context()).Warnf("\"ClientIP\" matcher: could not match remote address : %w", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func hostRegexp(route *mux.Route, hosts ...string) error {
|
|
|
|
router := route.Subrouter()
|
|
|
|
for _, host := range hosts {
|
2021-03-22 20:16:04 +00:00
|
|
|
if !IsASCII(host) {
|
|
|
|
return fmt.Errorf("invalid value %q for HostRegexp matcher, non-ASCII characters are not allowed", host)
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
tmpRt := router.Host(host)
|
|
|
|
if tmpRt.GetError() != nil {
|
|
|
|
return tmpRt.GetError()
|
|
|
|
}
|
2017-03-24 11:07:59 +00:00
|
|
|
}
|
2019-01-30 15:24:07 +00:00
|
|
|
return nil
|
2017-03-24 11:07:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func methods(route *mux.Route, methods ...string) error {
|
|
|
|
return route.Methods(methods...).GetError()
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func headers(route *mux.Route, headers ...string) error {
|
|
|
|
return route.Headers(headers...).GetError()
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func headersRegexp(route *mux.Route, headers ...string) error {
|
|
|
|
return route.HeadersRegexp(headers...).GetError()
|
2016-03-27 00:05:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func query(route *mux.Route, query ...string) error {
|
2017-08-24 18:28:03 +00:00
|
|
|
var queries []string
|
|
|
|
for _, elem := range query {
|
|
|
|
queries = append(queries, strings.Split(elem, "=")...)
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
route.Queries(queries...)
|
|
|
|
// Queries can return nil so we can't chain the GetError()
|
|
|
|
return route.GetError()
|
2017-08-24 18:28:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
func addRuleOnRouter(router *mux.Router, rule *rules.Tree) error {
|
|
|
|
switch rule.Matcher {
|
2019-01-30 15:24:07 +00:00
|
|
|
case "and":
|
|
|
|
route := router.NewRoute()
|
2022-03-17 17:02:08 +00:00
|
|
|
err := addRuleOnRoute(route, rule.RuleLeft)
|
2019-01-30 15:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-06-06 07:21:00 +00:00
|
|
|
}
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
return addRuleOnRoute(route, rule.RuleRight)
|
2019-01-30 15:24:07 +00:00
|
|
|
case "or":
|
2022-03-17 17:02:08 +00:00
|
|
|
err := addRuleOnRouter(router, rule.RuleLeft)
|
2019-01-30 15:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-07 13:01:04 +00:00
|
|
|
}
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
return addRuleOnRouter(router, rule.RuleRight)
|
2019-01-30 15:24:07 +00:00
|
|
|
default:
|
2022-03-17 17:02:08 +00:00
|
|
|
err := rules.CheckRule(rule)
|
2019-01-30 15:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-06-06 07:21:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
if rule.Not {
|
|
|
|
return not(httpFuncs[rule.Matcher])(router.NewRoute(), rule.Value...)
|
2021-05-31 16:58:05 +00:00
|
|
|
}
|
2022-03-17 17:02:08 +00:00
|
|
|
|
|
|
|
return httpFuncs[rule.Matcher](router.NewRoute(), rule.Value...)
|
2019-01-30 15:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-05 18:42:45 +00:00
|
|
|
|
2021-05-31 16:58:05 +00:00
|
|
|
func not(m func(*mux.Route, ...string) error) func(*mux.Route, ...string) error {
|
|
|
|
return func(r *mux.Route, v ...string) error {
|
|
|
|
router := mux.NewRouter()
|
|
|
|
err := m(router.NewRoute(), v...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.MatcherFunc(func(req *http.Request, ma *mux.RouteMatch) bool {
|
|
|
|
return !router.Match(req, ma)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
func addRuleOnRoute(route *mux.Route, rule *rules.Tree) error {
|
|
|
|
switch rule.Matcher {
|
2019-01-30 15:24:07 +00:00
|
|
|
case "and":
|
2022-03-17 17:02:08 +00:00
|
|
|
err := addRuleOnRoute(route, rule.RuleLeft)
|
2016-08-05 18:42:45 +00:00
|
|
|
if err != nil {
|
2019-01-30 15:24:07 +00:00
|
|
|
return err
|
2016-08-05 18:42:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
return addRuleOnRoute(route, rule.RuleRight)
|
2019-01-30 15:24:07 +00:00
|
|
|
case "or":
|
|
|
|
subRouter := route.Subrouter()
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
err := addRuleOnRouter(subRouter, rule.RuleLeft)
|
2019-01-30 15:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-05 18:42:45 +00:00
|
|
|
}
|
2019-01-30 15:24:07 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
return addRuleOnRouter(subRouter, rule.RuleRight)
|
2019-01-30 15:24:07 +00:00
|
|
|
default:
|
2022-03-17 17:02:08 +00:00
|
|
|
err := rules.CheckRule(rule)
|
2019-01-30 15:24:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-06 11:06:31 +00:00
|
|
|
}
|
2018-03-23 12:30:03 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
if rule.Not {
|
|
|
|
return not(httpFuncs[rule.Matcher])(route, rule.Value...)
|
2021-05-31 16:58:05 +00:00
|
|
|
}
|
2016-08-05 18:42:45 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
return httpFuncs[rule.Matcher](route, rule.Value...)
|
2016-08-05 18:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-22 20:16:04 +00:00
|
|
|
|
|
|
|
// IsASCII checks if the given string contains only ASCII characters.
|
|
|
|
func IsASCII(s string) bool {
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] >= utf8.RuneSelf {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|