mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 18:53:13 -05:00 
			
		
		
		
	* Rewrite plugin - rewrite SRV targets and names in response answer and additional records Signed-off-by: Nic Colledge <nic@njcolledge.net> * Added README content to describe new behaviour Signed-off-by: Nic Colledge <nic@njcolledge.net> * Added more record types to rewrite handling based on PR/Issue feedback Signed-off-by: Nic Colledge <nic@njcolledge.net> * Updated README.md for plugin Signed-off-by: Nic Colledge <nic@njcolledge.net> * Updated unit tests. Small refactor of getTarget... function. Signed-off-by: Nic Colledge <nic@njcolledge.net> * Refactor to add response value rewrite as answer value option Signed-off-by: Nic Colledge <nic@njcolledge.net> * Removed TODO comment, added test for NAPTR record. Signed-off-by: Nic Colledge <nic@njcolledge.net>
		
			
				
	
	
		
			48 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package rewrite
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
	"strings"
 | 
						|
 | 
						|
	"github.com/coredns/coredns/request"
 | 
						|
 | 
						|
	"github.com/miekg/dns"
 | 
						|
)
 | 
						|
 | 
						|
type classRule struct {
 | 
						|
	fromClass  uint16
 | 
						|
	toClass    uint16
 | 
						|
	NextAction string
 | 
						|
}
 | 
						|
 | 
						|
// newClassRule creates a class matching rule
 | 
						|
func newClassRule(nextAction string, args ...string) (Rule, error) {
 | 
						|
	var from, to uint16
 | 
						|
	var ok bool
 | 
						|
	if from, ok = dns.StringToClass[strings.ToUpper(args[0])]; !ok {
 | 
						|
		return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[0]))
 | 
						|
	}
 | 
						|
	if to, ok = dns.StringToClass[strings.ToUpper(args[1])]; !ok {
 | 
						|
		return nil, fmt.Errorf("invalid class %q", strings.ToUpper(args[1]))
 | 
						|
	}
 | 
						|
	return &classRule{from, to, nextAction}, nil
 | 
						|
}
 | 
						|
 | 
						|
// Rewrite rewrites the current request.
 | 
						|
func (rule *classRule) Rewrite(ctx context.Context, state request.Request) Result {
 | 
						|
	if rule.fromClass > 0 && rule.toClass > 0 {
 | 
						|
		if state.Req.Question[0].Qclass == rule.fromClass {
 | 
						|
			state.Req.Question[0].Qclass = rule.toClass
 | 
						|
			return RewriteDone
 | 
						|
		}
 | 
						|
	}
 | 
						|
	return RewriteIgnored
 | 
						|
}
 | 
						|
 | 
						|
// Mode returns the processing mode.
 | 
						|
func (rule *classRule) Mode() string { return rule.NextAction }
 | 
						|
 | 
						|
// GetResponseRules return rules to rewrite the response with. Currently not implemented.
 | 
						|
func (rule *classRule) GetResponseRules() []ResponseRule { return []ResponseRule{} }
 |