| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | package rewrite
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"context"
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"regexp"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | type ttlResponseRule struct {
 | 
					
						
							|  |  |  | 	TTL uint32
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (r *ttlResponseRule) RewriteResponse(rr dns.RR) {
 | 
					
						
							|  |  |  | 	rr.Header().Ttl = r.TTL
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ttlRuleBase struct {
 | 
					
						
							|  |  |  | 	nextAction string
 | 
					
						
							|  |  |  | 	response   ttlResponseRule
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newTTLRuleBase(nextAction string, ttl uint32) ttlRuleBase {
 | 
					
						
							|  |  |  | 	return ttlRuleBase{
 | 
					
						
							|  |  |  | 		nextAction: nextAction,
 | 
					
						
							|  |  |  | 		response:   ttlResponseRule{TTL: ttl},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (rule *ttlRuleBase) responseRule(match bool) (ResponseRules, Result) {
 | 
					
						
							|  |  |  | 	if match {
 | 
					
						
							|  |  |  | 		return ResponseRules{&rule.response}, RewriteDone
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil, RewriteIgnored
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Mode returns the processing nextAction
 | 
					
						
							|  |  |  | func (rule *ttlRuleBase) Mode() string { return rule.nextAction }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | type exactTTLRule struct {
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 	ttlRuleBase
 | 
					
						
							|  |  |  | 	From string
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | type prefixTTLRule struct {
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 	ttlRuleBase
 | 
					
						
							|  |  |  | 	Prefix string
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | type suffixTTLRule struct {
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 	ttlRuleBase
 | 
					
						
							|  |  |  | 	Suffix string
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | type substringTTLRule struct {
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 	ttlRuleBase
 | 
					
						
							|  |  |  | 	Substring string
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | type regexTTLRule struct {
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 	ttlRuleBase
 | 
					
						
							|  |  |  | 	Pattern *regexp.Regexp
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the current request based upon exact match of the name
 | 
					
						
							|  |  |  | // in the question section of the request.
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | func (rule *exactTTLRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
 | 
					
						
							|  |  |  | 	return rule.responseRule(rule.From == state.Name())
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the current request when the name begins with the matching string.
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | func (rule *prefixTTLRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
 | 
					
						
							|  |  |  | 	return rule.responseRule(strings.HasPrefix(state.Name(), rule.Prefix))
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the current request when the name ends with the matching string.
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | func (rule *suffixTTLRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
 | 
					
						
							|  |  |  | 	return rule.responseRule(strings.HasSuffix(state.Name(), rule.Suffix))
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the current request based upon partial match of the
 | 
					
						
							|  |  |  | // name in the question section of the request.
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | func (rule *substringTTLRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
 | 
					
						
							|  |  |  | 	return rule.responseRule(strings.Contains(state.Name(), rule.Substring))
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the current request when the name in the question
 | 
					
						
							|  |  |  | // section of the request matches a regular expression.
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | func (rule *regexTTLRule) Rewrite(ctx context.Context, state request.Request) (ResponseRules, Result) {
 | 
					
						
							|  |  |  | 	return rule.responseRule(len(rule.Pattern.FindStringSubmatch(state.Name())) != 0)
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | // newTTLRule creates a name matching rule based on exact, partial, or regex match
 | 
					
						
							|  |  |  | func newTTLRule(nextAction string, args ...string) (Rule, error) {
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 	if len(args) < 2 {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("too few (%d) arguments for a ttl rule", len(args))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	var s string
 | 
					
						
							|  |  |  | 	if len(args) == 2 {
 | 
					
						
							|  |  |  | 		s = args[1]
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(args) == 3 {
 | 
					
						
							|  |  |  | 		s = args[2]
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 	ttl, valid := isValidTTL(s)
 | 
					
						
							|  |  |  | 	if !valid {
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 		return nil, fmt.Errorf("invalid TTL '%s' for a ttl rule", s)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(args) == 3 {
 | 
					
						
							|  |  |  | 		switch strings.ToLower(args[0]) {
 | 
					
						
							|  |  |  | 		case ExactMatch:
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			return &exactTTLRule{
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 				newTTLRuleBase(nextAction, ttl),
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				plugin.Name(args[1]).Normalize(),
 | 
					
						
							|  |  |  | 			}, nil
 | 
					
						
							|  |  |  | 		case PrefixMatch:
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			return &prefixTTLRule{
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 				newTTLRuleBase(nextAction, ttl),
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				plugin.Name(args[1]).Normalize(),
 | 
					
						
							|  |  |  | 			}, nil
 | 
					
						
							|  |  |  | 		case SuffixMatch:
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			return &suffixTTLRule{
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 				newTTLRuleBase(nextAction, ttl),
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				plugin.Name(args[1]).Normalize(),
 | 
					
						
							|  |  |  | 			}, nil
 | 
					
						
							|  |  |  | 		case SubstringMatch:
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			return &substringTTLRule{
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 				newTTLRuleBase(nextAction, ttl),
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				plugin.Name(args[1]).Normalize(),
 | 
					
						
							|  |  |  | 			}, nil
 | 
					
						
							|  |  |  | 		case RegexMatch:
 | 
					
						
							|  |  |  | 			regexPattern, err := regexp.Compile(args[1])
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							| 
									
										
										
										
											2019-02-27 20:25:02 +07:00
										 |  |  | 				return nil, fmt.Errorf("invalid regex pattern in a ttl rule: %s", args[1])
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			return ®exTTLRule{
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 				newTTLRuleBase(nextAction, ttl),
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				regexPattern,
 | 
					
						
							|  |  |  | 			}, nil
 | 
					
						
							|  |  |  | 		default:
 | 
					
						
							| 
									
										
										
										
											2019-10-01 07:41:29 +01:00
										 |  |  | 			return nil, fmt.Errorf("ttl rule supports only exact, prefix, suffix, substring, and regex name matching")
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if len(args) > 3 {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("many few arguments for a ttl rule")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 	return &exactTTLRule{
 | 
					
						
							| 
									
										
										
										
											2021-05-04 10:05:45 +02:00
										 |  |  | 		newTTLRuleBase(nextAction, ttl),
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 		plugin.Name(args[0]).Normalize(),
 | 
					
						
							|  |  |  | 	}, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | // validTTL returns true if v is valid TTL value.
 | 
					
						
							|  |  |  | func isValidTTL(v string) (uint32, bool) {
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 	i, err := strconv.Atoi(v)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return uint32(0), false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if i > 2147483647 {
 | 
					
						
							|  |  |  | 		return uint32(0), false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if i < 0 {
 | 
					
						
							|  |  |  | 		return uint32(0), false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return uint32(i), true
 | 
					
						
							|  |  |  | }
 |