mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	* Rename middleware to plugin first pass; mostly used 'sed', few spots where I manually changed text. This still builds a coredns binary. * fmt error * Rename AddMiddleware to AddPlugin * Readd AddMiddleware to remain backwards compat
		
			
				
	
	
		
			79 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package dnsserver
 | |
| 
 | |
| import (
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/coredns/coredns/plugin"
 | |
| 
 | |
| 	"github.com/miekg/dns"
 | |
| )
 | |
| 
 | |
| type zoneAddr struct {
 | |
| 	Zone      string
 | |
| 	Port      string
 | |
| 	Transport string // dns, tls or grpc
 | |
| }
 | |
| 
 | |
| // String return the string representation of z.
 | |
| func (z zoneAddr) String() string { return z.Transport + "://" + z.Zone + ":" + z.Port }
 | |
| 
 | |
| // Transport returns the protocol of the string s
 | |
| func Transport(s string) string {
 | |
| 	switch {
 | |
| 	case strings.HasPrefix(s, TransportTLS+"://"):
 | |
| 		return TransportTLS
 | |
| 	case strings.HasPrefix(s, TransportDNS+"://"):
 | |
| 		return TransportDNS
 | |
| 	case strings.HasPrefix(s, TransportGRPC+"://"):
 | |
| 		return TransportGRPC
 | |
| 	}
 | |
| 	return TransportDNS
 | |
| }
 | |
| 
 | |
| // normalizeZone parses an zone string into a structured format with separate
 | |
| // host, and port portions, as well as the original input string.
 | |
| func normalizeZone(str string) (zoneAddr, error) {
 | |
| 	var err error
 | |
| 
 | |
| 	// Default to DNS if there isn't a transport protocol prefix.
 | |
| 	trans := TransportDNS
 | |
| 
 | |
| 	switch {
 | |
| 	case strings.HasPrefix(str, TransportTLS+"://"):
 | |
| 		trans = TransportTLS
 | |
| 		str = str[len(TransportTLS+"://"):]
 | |
| 	case strings.HasPrefix(str, TransportDNS+"://"):
 | |
| 		trans = TransportDNS
 | |
| 		str = str[len(TransportDNS+"://"):]
 | |
| 	case strings.HasPrefix(str, TransportGRPC+"://"):
 | |
| 		trans = TransportGRPC
 | |
| 		str = str[len(TransportGRPC+"://"):]
 | |
| 	}
 | |
| 
 | |
| 	host, port, err := plugin.SplitHostPort(str)
 | |
| 	if err != nil {
 | |
| 		return zoneAddr{}, err
 | |
| 	}
 | |
| 
 | |
| 	if port == "" {
 | |
| 		if trans == TransportDNS {
 | |
| 			port = Port
 | |
| 		}
 | |
| 		if trans == TransportTLS {
 | |
| 			port = TLSPort
 | |
| 		}
 | |
| 		if trans == TransportGRPC {
 | |
| 			port = GRPCPort
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return zoneAddr{Zone: dns.Fqdn(host), Port: port, Transport: trans}, nil
 | |
| }
 | |
| 
 | |
| // Supported transports.
 | |
| const (
 | |
| 	TransportDNS  = "dns"
 | |
| 	TransportTLS  = "tls"
 | |
| 	TransportGRPC = "grpc"
 | |
| )
 |