| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | package rewrite
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | type classRule struct {
 | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 	fromClass  uint16
 | 
					
						
							|  |  |  | 	toClass    uint16
 | 
					
						
							|  |  |  | 	NextAction string
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | // newClassRule creates a class matching rule
 | 
					
						
							|  |  |  | func newClassRule(nextAction string, args ...string) (Rule, error) {
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	var from, to uint16
 | 
					
						
							|  |  |  | 	var ok bool
 | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 	if from, ok = dns.StringToClass[strings.ToUpper(args[0])]; !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[0]))
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 	if to, ok = dns.StringToClass[strings.ToUpper(args[1])]; !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[1]))
 | 
					
						
							| 
									
										
										
										
											2017-03-06 16:32:17 -05:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 	return &classRule{from, to, nextAction}, nil
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Rewrite rewrites the the current request.
 | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:07 -07:00
										 |  |  | func (rule *classRule) Rewrite(w dns.ResponseWriter, r *dns.Msg) Result {
 | 
					
						
							| 
									
										
										
										
											2017-02-07 16:53:16 -05:00
										 |  |  | 	if rule.fromClass > 0 && rule.toClass > 0 {
 | 
					
						
							|  |  |  | 		if r.Question[0].Qclass == rule.fromClass {
 | 
					
						
							|  |  |  | 			r.Question[0].Qclass = rule.toClass
 | 
					
						
							|  |  |  | 			return RewriteDone
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return RewriteIgnored
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Mode returns the processing mode
 | 
					
						
							|  |  |  | func (rule *classRule) Mode() string {
 | 
					
						
							| 
									
										
										
										
											2017-12-14 13:25:36 -05:00
										 |  |  | 	return rule.NextAction
 | 
					
						
							| 
									
										
										
										
											2017-09-20 13:06:53 -07:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2018-01-18 10:41:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // GetResponseRule return a rule to rewrite the response with. Currently not implemented.
 | 
					
						
							|  |  |  | func (rule *classRule) GetResponseRule() ResponseRule {
 | 
					
						
							|  |  |  | 	return ResponseRule{}
 | 
					
						
							|  |  |  | }
 |