| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package rewrite
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Result is the result of a rewrite
 | 
					
						
							|  |  |  | type Result int
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	// RewriteIgnored is returned when rewrite is not done on request.
 | 
					
						
							|  |  |  | 	RewriteIgnored Result = iota
 | 
					
						
							|  |  |  | 	// RewriteDone is returned when rewrite is done on request.
 | 
					
						
							|  |  |  | 	RewriteDone
 | 
					
						
							|  |  |  | 	// RewriteStatus is returned when rewrite is not needed and status code should be set
 | 
					
						
							|  |  |  | 	// for the request.
 | 
					
						
							|  |  |  | 	RewriteStatus
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite is middleware to rewrite requests internally before being handled.
 | 
					
						
							|  |  |  | type Rewrite struct {
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	Next     middleware.Handler
 | 
					
						
							|  |  |  | 	Rules    []Rule
 | 
					
						
							|  |  |  | 	noRevert bool
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // ServeDNS implements the middleware.Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2016-04-21 22:02:26 +01:00
										 |  |  | 	wr := NewResponseReverter(w, r)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	for _, rule := range rw.Rules {
 | 
					
						
							|  |  |  | 		switch result := rule.Rewrite(r); result {
 | 
					
						
							|  |  |  | 		case RewriteDone:
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 			if rw.noRevert {
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 				return middleware.NextOrFailure(rw.Name(), rw.Next, ctx, w, r)
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 			return middleware.NextOrFailure(rw.Name(), rw.Next, ctx, wr, r)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		case RewriteIgnored:
 | 
					
						
							|  |  |  | 			break
 | 
					
						
							|  |  |  | 		case RewriteStatus:
 | 
					
						
							|  |  |  | 			// only valid for complex rules.
 | 
					
						
							|  |  |  | 			// if cRule, ok := rule.(*ComplexRule); ok && cRule.Status != 0 {
 | 
					
						
							|  |  |  | 			// return cRule.Status, nil
 | 
					
						
							|  |  |  | 			// }
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 	return middleware.NextOrFailure(rw.Name(), rw.Next, ctx, w, r)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:48:37 +00:00
										 |  |  | // Name implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (rw Rewrite) Name() string { return "rewrite" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | // Rule describes a rewrite rule.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | type Rule interface {
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	// Rewrite rewrites the current request.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	Rewrite(*dns.Msg) Result
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newRule(args ...string) (Rule, error) {
 | 
					
						
							|  |  |  | 	if len(args) == 0 {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("No rule type specified for rewrite")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ruleType := strings.ToLower(args[0])
 | 
					
						
							|  |  |  | 	if ruleType != "edns0" && len(args) != 3 {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("%s rules must have exactly two arguments", ruleType)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	switch ruleType {
 | 
					
						
							|  |  |  | 	case "name":
 | 
					
						
							|  |  |  | 		return newNameRule(args[1], args[2])
 | 
					
						
							|  |  |  | 	case "class":
 | 
					
						
							|  |  |  | 		return newClassRule(args[1], args[2])
 | 
					
						
							|  |  |  | 	case "type":
 | 
					
						
							|  |  |  | 		return newTypeRule(args[1], args[2])
 | 
					
						
							|  |  |  | 	case "edns0":
 | 
					
						
							|  |  |  | 		return newEdns0Rule(args[1:]...)
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("invalid rule type %q", args[0])
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 |