mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 17:53:21 -04:00 
			
		
		
		
	* Require Field for rewrite rules * review feedback changes * fix ut * fix typo, add warning message
		
			
				
	
	
		
			30 lines
		
	
	
		
			689 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			689 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Package rewrite is middleware for rewriting requests internally to something different.
 | |
| package rewrite
 | |
| 
 | |
| import (
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/miekg/coredns/middleware"
 | |
| 	"github.com/miekg/dns"
 | |
| )
 | |
| 
 | |
| // NameRule is a name rewrite rule.
 | |
| type NameRule struct {
 | |
| 	From, To string
 | |
| }
 | |
| 
 | |
| // Initializer
 | |
| func (rule NameRule) New(args ...string) Rule {
 | |
| 	from, to := args[0], strings.Join(args[1:], " ")
 | |
| 	return &NameRule{middleware.Name(from).Normalize(), middleware.Name(to).Normalize()}
 | |
| }
 | |
| 
 | |
| // Rewrite rewrites the the current request.
 | |
| func (rule NameRule) Rewrite(r *dns.Msg) Result {
 | |
| 	if rule.From == r.Question[0].Name {
 | |
| 		r.Question[0].Name = rule.To
 | |
| 		return RewriteDone
 | |
| 	}
 | |
| 	return RewriteIgnored
 | |
| }
 |