| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package rewrite | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Rewrite is plugin to rewrite requests internally before being handled. | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | type Rewrite struct { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	Next     plugin.Handler | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	Rules    []Rule | 
					
						
							|  |  |  | 	noRevert bool | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // ServeDNS implements the plugin.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 { | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 		switch result := rule.Rewrite(w, r); result { | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		case RewriteDone: | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 			if rw.noRevert { | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 				return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, w, r) | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			return plugin.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 | 
					
						
							|  |  |  | 			// } | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	return plugin.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. | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | 	Rewrite(dns.ResponseWriter, *dns.Msg) Result | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func newRule(args ...string) (Rule, error) { | 
					
						
							|  |  |  | 	if len(args) == 0 { | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 		return nil, fmt.Errorf("no rule type specified for rewrite") | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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
										 |  |  | } |