| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package rewrite
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | import (
 | 
					
						
							|  |  |  | 	"regexp"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							|  |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ResponseRule contains a rule to rewrite a response with.
 | 
					
						
							|  |  |  | type ResponseRule struct {
 | 
					
						
							|  |  |  | 	Active      bool
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 	Type        string
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 	Pattern     *regexp.Regexp
 | 
					
						
							|  |  |  | 	Replacement string
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 	TTL         uint32
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // ResponseReverter reverses the operations done on the question section of a packet.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // This is need because the client will otherwise disregards the response, i.e.
 | 
					
						
							| 
									
										
										
										
											2018-07-13 14:32:07 +01:00
										 |  |  | // dig will complain with ';; Question section mismatch: got example.org/HINFO/IN'
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | type ResponseReverter struct {
 | 
					
						
							|  |  |  | 	dns.ResponseWriter
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 	originalQuestion dns.Question
 | 
					
						
							|  |  |  | 	ResponseRewrite  bool
 | 
					
						
							|  |  |  | 	ResponseRules    []ResponseRule
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // NewResponseReverter returns a pointer to a new ResponseReverter.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | func NewResponseReverter(w dns.ResponseWriter, r *dns.Msg) *ResponseReverter {
 | 
					
						
							|  |  |  | 	return &ResponseReverter{
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 		ResponseWriter:   w,
 | 
					
						
							|  |  |  | 		originalQuestion: r.Question[0],
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 15:39:50 +01:00
										 |  |  | // WriteMsg records the status code and calls the underlying ResponseWriter's WriteMsg method.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | func (r *ResponseReverter) WriteMsg(res *dns.Msg) error {
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 	res.Question[0] = r.originalQuestion
 | 
					
						
							|  |  |  | 	if r.ResponseRewrite {
 | 
					
						
							|  |  |  | 		for _, rr := range res.Answer {
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			var (
 | 
					
						
							|  |  |  | 				isNameRewritten bool
 | 
					
						
							|  |  |  | 				isTTLRewritten  bool
 | 
					
						
							|  |  |  | 				name            = rr.Header().Name
 | 
					
						
							|  |  |  | 				ttl             = rr.Header().Ttl
 | 
					
						
							|  |  |  | 			)
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 			for _, rule := range r.ResponseRules {
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				if rule.Type == "" {
 | 
					
						
							|  |  |  | 					rule.Type = "name"
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				switch rule.Type {
 | 
					
						
							|  |  |  | 				case "name":
 | 
					
						
							|  |  |  | 					regexGroups := rule.Pattern.FindStringSubmatch(name)
 | 
					
						
							|  |  |  | 					if len(regexGroups) == 0 {
 | 
					
						
							|  |  |  | 						continue
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 					s := rule.Replacement
 | 
					
						
							|  |  |  | 					for groupIndex, groupValue := range regexGroups {
 | 
					
						
							|  |  |  | 						groupIndexStr := "{" + strconv.Itoa(groupIndex) + "}"
 | 
					
						
							|  |  |  | 						if strings.Contains(s, groupIndexStr) {
 | 
					
						
							|  |  |  | 							s = strings.Replace(s, groupIndexStr, groupValue, -1)
 | 
					
						
							|  |  |  | 						}
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					name = s
 | 
					
						
							|  |  |  | 					isNameRewritten = true
 | 
					
						
							|  |  |  | 				case "ttl":
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 					ttl = rule.TTL
 | 
					
						
							|  |  |  | 					isTTLRewritten = true
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			if isNameRewritten {
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				rr.Header().Name = name
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2019-06-25 08:00:33 +01:00
										 |  |  | 			if isTTLRewritten {
 | 
					
						
							| 
									
										
										
										
											2018-08-29 10:41:03 -04:00
										 |  |  | 				rr.Header().Ttl = ttl
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	return r.ResponseWriter.WriteMsg(res)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Write is a wrapper that records the size of the message that gets written.
 | 
					
						
							|  |  |  | func (r *ResponseReverter) Write(buf []byte) (int, error) {
 | 
					
						
							|  |  |  | 	n, err := r.ResponseWriter.Write(buf)
 | 
					
						
							|  |  |  | 	return n, err
 | 
					
						
							|  |  |  | }
 |