| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package rewrite
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-11-13 14:20:49 -05:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"regexp"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/replacer"
 | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // Operators that are defined.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | const (
 | 
					
						
							|  |  |  | 	Is         = "is"
 | 
					
						
							|  |  |  | 	Not        = "not"
 | 
					
						
							|  |  |  | 	Has        = "has"
 | 
					
						
							|  |  |  | 	NotHas     = "not_has"
 | 
					
						
							|  |  |  | 	StartsWith = "starts_with"
 | 
					
						
							|  |  |  | 	EndsWith   = "ends_with"
 | 
					
						
							|  |  |  | 	Match      = "match"
 | 
					
						
							|  |  |  | 	NotMatch   = "not_match"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | var repl = replacer.New()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // condition is a rewrite condition.
 | 
					
						
							|  |  |  | type condition func(string, string) bool
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var conditions = map[string]condition{
 | 
					
						
							|  |  |  | 	Is:         isFunc,
 | 
					
						
							|  |  |  | 	Not:        notFunc,
 | 
					
						
							|  |  |  | 	Has:        hasFunc,
 | 
					
						
							|  |  |  | 	NotHas:     notHasFunc,
 | 
					
						
							|  |  |  | 	StartsWith: startsWithFunc,
 | 
					
						
							|  |  |  | 	EndsWith:   endsWithFunc,
 | 
					
						
							|  |  |  | 	Match:      matchFunc,
 | 
					
						
							|  |  |  | 	NotMatch:   notMatchFunc,
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // isFunc is condition for Is operator. It checks for equality.
 | 
					
						
							|  |  |  | func isFunc(a, b string) bool { return a == b }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // notFunc is condition for Not operator. It checks for inequality.
 | 
					
						
							|  |  |  | func notFunc(a, b string) bool { return a != b }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // hasFunc is condition for Has operator. It checks if b is a substring of a.
 | 
					
						
							|  |  |  | func hasFunc(a, b string) bool { return strings.Contains(a, b) }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // notHasFunc is condition for NotHas operator. It checks if b is not a substring of a.
 | 
					
						
							|  |  |  | func notHasFunc(a, b string) bool { return !strings.Contains(a, b) }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // startsWithFunc is condition for StartsWith operator. It checks if b is a prefix of a.
 | 
					
						
							|  |  |  | func startsWithFunc(a, b string) bool { return strings.HasPrefix(a, b) }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // endsWithFunc is condition for EndsWith operator. It checks if b is a suffix of a.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | func endsWithFunc(a, b string) bool {
 | 
					
						
							| 
									
										
										
										
											2016-04-12 22:34:44 +01:00
										 |  |  | 	// TODO(miek): IsSubDomain
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	return strings.HasSuffix(a, b)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // matchFunc is condition for Match operator. It does regexp matching of a against pattern in b
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // and returns if they match.
 | 
					
						
							|  |  |  | func matchFunc(a, b string) bool {
 | 
					
						
							|  |  |  | 	matched, _ := regexp.MatchString(b, a)
 | 
					
						
							|  |  |  | 	return matched
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // notMatchFunc is condition for NotMatch operator. It does regexp matching of a against pattern in b
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // and returns if they do not match.
 | 
					
						
							|  |  |  | func notMatchFunc(a, b string) bool {
 | 
					
						
							|  |  |  | 	matched, _ := regexp.MatchString(b, a)
 | 
					
						
							|  |  |  | 	return !matched
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // If is statement for a rewrite condition.
 | 
					
						
							|  |  |  | type If struct {
 | 
					
						
							|  |  |  | 	A        string
 | 
					
						
							|  |  |  | 	Operator string
 | 
					
						
							|  |  |  | 	B        string
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // True returns true if the condition is true and false otherwise.
 | 
					
						
							|  |  |  | // If r is not nil, it replaces placeholders before comparison.
 | 
					
						
							|  |  |  | func (i If) True(r *dns.Msg) bool {
 | 
					
						
							|  |  |  | 	if c, ok := conditions[i.Operator]; ok {
 | 
					
						
							|  |  |  | 		a, b := i.A, i.B
 | 
					
						
							|  |  |  | 		if r != nil {
 | 
					
						
							| 
									
										
										
										
											2019-02-12 07:38:49 +00:00
										 |  |  | 			ctx := context.TODO()
 | 
					
						
							|  |  |  | 			state := request.Request{Req: r, W: nil} // hmm W nil?
 | 
					
						
							|  |  |  | 			a = repl.Replace(ctx, state, nil, i.A)
 | 
					
						
							|  |  |  | 			b = repl.Replace(ctx, state, nil, i.B)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		return c(a, b)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return false
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewIf creates a new If condition.
 | 
					
						
							|  |  |  | func NewIf(a, operator, b string) (If, error) {
 | 
					
						
							|  |  |  | 	if _, ok := conditions[operator]; !ok {
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | 		return If{}, fmt.Errorf("invalid operator %v", operator)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return If{
 | 
					
						
							|  |  |  | 		A:        a,
 | 
					
						
							|  |  |  | 		Operator: operator,
 | 
					
						
							|  |  |  | 		B:        b,
 | 
					
						
							|  |  |  | 	}, nil
 | 
					
						
							|  |  |  | }
 |