| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | package rewrite
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"regexp"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2018-02-14 07:00:04 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2018-07-13 14:32:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | type nameRule struct {
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 	NextAction string
 | 
					
						
							|  |  |  | 	From       string
 | 
					
						
							|  |  |  | 	To         string
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type prefixNameRule struct {
 | 
					
						
							|  |  |  | 	NextAction  string
 | 
					
						
							|  |  |  | 	Prefix      string
 | 
					
						
							|  |  |  | 	Replacement string
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | type suffixNameRule struct {
 | 
					
						
							|  |  |  | 	NextAction  string
 | 
					
						
							|  |  |  | 	Suffix      string
 | 
					
						
							|  |  |  | 	Replacement string
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | type substringNameRule struct {
 | 
					
						
							|  |  |  | 	NextAction  string
 | 
					
						
							|  |  |  | 	Substring   string
 | 
					
						
							|  |  |  | 	Replacement string
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type regexNameRule struct {
 | 
					
						
							|  |  |  | 	NextAction  string
 | 
					
						
							|  |  |  | 	Pattern     *regexp.Regexp
 | 
					
						
							|  |  |  | 	Replacement string
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 	ResponseRule
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	// ExactMatch matches only on exact match of the name in the question section of a request
 | 
					
						
							|  |  |  | 	ExactMatch = "exact"
 | 
					
						
							|  |  |  | 	// PrefixMatch matches when the name begins with the matching string
 | 
					
						
							|  |  |  | 	PrefixMatch = "prefix"
 | 
					
						
							|  |  |  | 	// SuffixMatch matches when the name ends with the matching string
 | 
					
						
							|  |  |  | 	SuffixMatch = "suffix"
 | 
					
						
							|  |  |  | 	// SubstringMatch matches on partial match of the name in the question section of a request
 | 
					
						
							|  |  |  | 	SubstringMatch = "substring"
 | 
					
						
							|  |  |  | 	// RegexMatch matches when the name in the question section of a request matches a regular expression
 | 
					
						
							|  |  |  | 	RegexMatch = "regex"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the current request based upon exact match of the name
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // in the question section of the request.
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | func (rule *nameRule) Rewrite(ctx context.Context, state request.Request) Result {
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	if rule.From == state.Name() {
 | 
					
						
							|  |  |  | 		state.Req.Question[0].Name = rule.To
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | 		return RewriteDone
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return RewriteIgnored
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // Rewrite rewrites the current request when the name begins with the matching string.
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | func (rule *prefixNameRule) Rewrite(ctx context.Context, state request.Request) Result {
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	if strings.HasPrefix(state.Name(), rule.Prefix) {
 | 
					
						
							|  |  |  | 		state.Req.Question[0].Name = rule.Replacement + strings.TrimLeft(state.Name(), rule.Prefix)
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 		return RewriteDone
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return RewriteIgnored
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // Rewrite rewrites the current request when the name ends with the matching string.
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | func (rule *suffixNameRule) Rewrite(ctx context.Context, state request.Request) Result {
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	if strings.HasSuffix(state.Name(), rule.Suffix) {
 | 
					
						
							|  |  |  | 		state.Req.Question[0].Name = strings.TrimRight(state.Name(), rule.Suffix) + rule.Replacement
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 		return RewriteDone
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return RewriteIgnored
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the current request based upon partial match of the
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // name in the question section of the request.
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | func (rule *substringNameRule) Rewrite(ctx context.Context, state request.Request) Result {
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	if strings.Contains(state.Name(), rule.Substring) {
 | 
					
						
							|  |  |  | 		state.Req.Question[0].Name = strings.Replace(state.Name(), rule.Substring, rule.Replacement, -1)
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 		return RewriteDone
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return RewriteIgnored
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the current request when the name in the question
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // section of the request matches a regular expression.
 | 
					
						
							| 
									
										
										
										
											2018-07-08 03:18:01 -04:00
										 |  |  | func (rule *regexNameRule) Rewrite(ctx context.Context, state request.Request) Result {
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	regexGroups := rule.Pattern.FindStringSubmatch(state.Name())
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 	if len(regexGroups) == 0 {
 | 
					
						
							|  |  |  | 		return RewriteIgnored
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	s := rule.Replacement
 | 
					
						
							|  |  |  | 	for groupIndex, groupValue := range regexGroups {
 | 
					
						
							|  |  |  | 		groupIndexStr := "{" + strconv.Itoa(groupIndex) + "}"
 | 
					
						
							|  |  |  | 		if strings.Contains(s, groupIndexStr) {
 | 
					
						
							|  |  |  | 			s = strings.Replace(s, groupIndexStr, groupValue, -1)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 	state.Req.Question[0].Name = s
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 	return RewriteDone
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // newNameRule creates a name matching rule based on exact, partial, or regex match
 | 
					
						
							|  |  |  | func newNameRule(nextAction string, args ...string) (Rule, error) {
 | 
					
						
							|  |  |  | 	if len(args) < 2 {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("too few arguments for a name rule")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(args) == 3 {
 | 
					
						
							|  |  |  | 		switch strings.ToLower(args[0]) {
 | 
					
						
							|  |  |  | 		case ExactMatch:
 | 
					
						
							|  |  |  | 			return &nameRule{nextAction, plugin.Name(args[1]).Normalize(), plugin.Name(args[2]).Normalize()}, nil
 | 
					
						
							|  |  |  | 		case PrefixMatch:
 | 
					
						
							|  |  |  | 			return &prefixNameRule{nextAction, plugin.Name(args[1]).Normalize(), plugin.Name(args[2]).Normalize()}, nil
 | 
					
						
							|  |  |  | 		case SuffixMatch:
 | 
					
						
							|  |  |  | 			return &suffixNameRule{nextAction, plugin.Name(args[1]).Normalize(), plugin.Name(args[2]).Normalize()}, nil
 | 
					
						
							|  |  |  | 		case SubstringMatch:
 | 
					
						
							|  |  |  | 			return &substringNameRule{nextAction, plugin.Name(args[1]).Normalize(), plugin.Name(args[2]).Normalize()}, nil
 | 
					
						
							|  |  |  | 		case RegexMatch:
 | 
					
						
							|  |  |  | 			regexPattern, err := regexp.Compile(args[1])
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("Invalid regex pattern in a name rule: %s", args[1])
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 			return ®exNameRule{nextAction, regexPattern, plugin.Name(args[2]).Normalize(), ResponseRule{Type: "name"}}, nil
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 		default:
 | 
					
						
							|  |  |  | 			return nil, fmt.Errorf("A name rule supports only exact, prefix, suffix, substring, and regex name matching")
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 	if len(args) == 7 {
 | 
					
						
							|  |  |  | 		if strings.ToLower(args[0]) == RegexMatch {
 | 
					
						
							|  |  |  | 			if args[3] != "answer" {
 | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("exceeded the number of arguments for a regex name rule")
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			switch strings.ToLower(args[4]) {
 | 
					
						
							|  |  |  | 			case "name":
 | 
					
						
							|  |  |  | 			default:
 | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("exceeded the number of arguments for a regex name rule")
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			regexPattern, err := regexp.Compile(args[1])
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("Invalid regex pattern in a name rule: %s", args)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			responseRegexPattern, err := regexp.Compile(args[5])
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return nil, fmt.Errorf("Invalid regex pattern in a name rule: %s", args)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			return ®exNameRule{
 | 
					
						
							|  |  |  | 				nextAction,
 | 
					
						
							|  |  |  | 				regexPattern,
 | 
					
						
							|  |  |  | 				plugin.Name(args[2]).Normalize(),
 | 
					
						
							|  |  |  | 				ResponseRule{
 | 
					
						
							|  |  |  | 					Active:      true,
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 					Type:        "name",
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 					Pattern:     responseRegexPattern,
 | 
					
						
							|  |  |  | 					Replacement: plugin.Name(args[6]).Normalize(),
 | 
					
						
							|  |  |  | 				},
 | 
					
						
							|  |  |  | 			}, nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("the rewrite of response is supported only for name regex rule")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(args) > 3 && len(args) != 7 {
 | 
					
						
							| 
									
										
										
										
											2018-04-25 15:48:32 -04:00
										 |  |  | 		return nil, fmt.Errorf("response rewrites must consist only of a name rule with 3 arguments and an answer rule with 3 arguments")
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-12-13 11:31:19 -05:00
										 |  |  | 	return &nameRule{nextAction, plugin.Name(args[0]).Normalize(), plugin.Name(args[1]).Normalize()}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Mode returns the processing nextAction
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | func (rule *nameRule) Mode() string          { return rule.NextAction }
 | 
					
						
							|  |  |  | func (rule *prefixNameRule) Mode() string    { return rule.NextAction }
 | 
					
						
							|  |  |  | func (rule *suffixNameRule) Mode() string    { return rule.NextAction }
 | 
					
						
							|  |  |  | func (rule *substringNameRule) Mode() string { return rule.NextAction }
 | 
					
						
							|  |  |  | func (rule *regexNameRule) Mode() string     { return rule.NextAction }
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GetResponseRule return a rule to rewrite the response with. Currently not implemented.
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | func (rule *nameRule) GetResponseRule() ResponseRule { return ResponseRule{} }
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GetResponseRule return a rule to rewrite the response with. Currently not implemented.
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | func (rule *prefixNameRule) GetResponseRule() ResponseRule { return ResponseRule{} }
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GetResponseRule return a rule to rewrite the response with. Currently not implemented.
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | func (rule *suffixNameRule) GetResponseRule() ResponseRule { return ResponseRule{} }
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GetResponseRule return a rule to rewrite the response with. Currently not implemented.
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | func (rule *substringNameRule) GetResponseRule() ResponseRule { return ResponseRule{} }
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GetResponseRule return a rule to rewrite the response with.
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | func (rule *regexNameRule) GetResponseRule() ResponseRule { return rule.ResponseRule }
 | 
					
						
							| 
									
										
										
										
											2018-07-13 14:32:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:55:55 +02:00
										 |  |  | // validName returns true if s is valid domain name and shorter than 256 characters.
 | 
					
						
							| 
									
										
										
										
											2018-07-13 14:32:07 +01:00
										 |  |  | func validName(s string) bool {
 | 
					
						
							|  |  |  | 	_, ok := dns.IsDomainName(s)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(dns.Name(s).String()) > 255 {
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true
 | 
					
						
							|  |  |  | }
 |