mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 18:53:43 -04:00
Cleanup ParseHostOrFile (#2100)
Create plugin/pkg/transport that holds the transport related functions. This needed to be a new pkg to prevent cyclic import errors. This cleans up a bunch of duplicated code in core/dnsserver that also tried to parse a transport (now all done in transport.Parse). Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
49
plugin/pkg/transport/transport.go
Normal file
49
plugin/pkg/transport/transport.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Parse returns the transport defined in s and a string where the
|
||||
// transport prefix is removed (if there was any). If no transport is defined
|
||||
// we default to TransportDNS
|
||||
func Parse(s string) (transport string, addr string) {
|
||||
switch {
|
||||
case strings.HasPrefix(s, TLS+"://"):
|
||||
s = s[len(TLS+"://"):]
|
||||
return TLS, s
|
||||
|
||||
case strings.HasPrefix(s, DNS+"://"):
|
||||
s = s[len(DNS+"://"):]
|
||||
return DNS, s
|
||||
|
||||
case strings.HasPrefix(s, GRPC+"://"):
|
||||
s = s[len(GRPC+"://"):]
|
||||
return GRPC, s
|
||||
|
||||
case strings.HasPrefix(s, HTTPS+"://"):
|
||||
s = s[len(HTTPS+"://"):]
|
||||
|
||||
return HTTPS, s
|
||||
}
|
||||
|
||||
return DNS, s
|
||||
}
|
||||
|
||||
// Supported transports.
|
||||
const (
|
||||
DNS = "dns"
|
||||
TLS = "tls"
|
||||
GRPC = "grpc"
|
||||
HTTPS = "https"
|
||||
)
|
||||
|
||||
// Port numbers for the various protocols
|
||||
const (
|
||||
// TLSPort is the default port for DNS-over-TLS.
|
||||
TLSPort = "853"
|
||||
// GRPCPort is the default port for DNS-over-gRPC.
|
||||
GRPCPort = "443"
|
||||
// HTTPSPort is the default port for DNS-over-HTTPS.
|
||||
HTTPSPort = "443"
|
||||
)
|
||||
21
plugin/pkg/transport/transport_test.go
Normal file
21
plugin/pkg/transport/transport_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package transport
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
for i, test := range []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"dns://.:53", DNS},
|
||||
{"2003::1/64.:53", DNS},
|
||||
{"grpc://example.org:1443 ", GRPC},
|
||||
{"tls://example.org ", TLS},
|
||||
{"https://example.org ", HTTPS},
|
||||
} {
|
||||
actual, _ := Parse(test.input)
|
||||
if actual != test.expected {
|
||||
t.Errorf("Test %d: Expected %s but got %s", i, test.expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user