2016-08-19 17:14:17 -07:00
|
|
|
package dnsserver
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type zoneAddr struct {
|
2017-03-13 20:24:37 +00:00
|
|
|
Zone string
|
|
|
|
|
Port string
|
|
|
|
|
Transport string // dns, tls or grpc
|
2016-08-19 17:14:17 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-13 20:24:37 +00:00
|
|
|
// String return the string represenation 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
|
|
|
|
|
}
|
2016-08-19 17:14:17 -07:00
|
|
|
|
|
|
|
|
// normalizeZone parses an zone string into a structured format with separate
|
|
|
|
|
// host, and port portions, as well as the original input string.
|
2016-09-19 11:26:00 +01:00
|
|
|
//
|
|
|
|
|
// TODO(miek): possibly move this to middleware/normalize.go
|
2016-08-19 17:14:17 -07:00
|
|
|
func normalizeZone(str string) (zoneAddr, error) {
|
|
|
|
|
var err error
|
|
|
|
|
|
2017-03-13 20:24:37 +00:00
|
|
|
// 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+"://"):]
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
host, port, err := net.SplitHostPort(str)
|
|
|
|
|
if err != nil {
|
|
|
|
|
host, port, err = net.SplitHostPort(str + ":")
|
|
|
|
|
// no error check here; return err at end of function
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-13 20:24:37 +00:00
|
|
|
if len(host) > 255 { // TODO(miek): this should take escaping into account.
|
2016-08-19 17:14:17 -07:00
|
|
|
return zoneAddr{}, fmt.Errorf("specified zone is too long: %d > 255", len(host))
|
|
|
|
|
}
|
|
|
|
|
_, d := dns.IsDomainName(host)
|
|
|
|
|
if !d {
|
|
|
|
|
return zoneAddr{}, fmt.Errorf("zone is not a valid domain name: %s", host)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if port == "" {
|
2017-03-13 20:24:37 +00:00
|
|
|
if trans == TransportDNS {
|
|
|
|
|
port = Port
|
|
|
|
|
}
|
|
|
|
|
if trans == TransportTLS {
|
|
|
|
|
port = TLSPort
|
|
|
|
|
}
|
|
|
|
|
if trans == TransportGRPC {
|
|
|
|
|
port = GRPCPort
|
|
|
|
|
}
|
2016-08-19 17:14:17 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-13 20:24:37 +00:00
|
|
|
return zoneAddr{Zone: strings.ToLower(dns.Fqdn(host)), Port: port, Transport: trans}, err
|
2016-08-19 17:14:17 -07:00
|
|
|
}
|
2017-03-13 20:24:37 +00:00
|
|
|
|
|
|
|
|
// Supported transports.
|
|
|
|
|
const (
|
|
|
|
|
TransportDNS = "dns"
|
|
|
|
|
TransportTLS = "tls"
|
|
|
|
|
TransportGRPC = "grpc"
|
|
|
|
|
)
|