Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions plugin/pkg/parse/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ import (
"fmt"
"net"
"os"
"strings"

"github.com/coredns/coredns/plugin/pkg/transport"

"github.com/miekg/dns"
)

// Strips the zone, but preserves any port that comes after the zone
func stripZone(host string) string {
if strings.Contains(host, "%") {
lastPercent := strings.LastIndex(host, "%")
newHost := host[:lastPercent]
return newHost
}
return host
}

// HostPortOrFile parses the strings in s, each string can either be a
// address, [scheme://]address:port or a filename. The address part is checked
// and in case of filename a resolv.conf like file is (assumed) and parsed and
Expand All @@ -21,10 +32,11 @@ func HostPortOrFile(s ...string) ([]string, error) {
trans, host := Transport(h)

addr, _, err := net.SplitHostPort(host)

if err != nil {
// Parse didn't work, it is not a addr:port combo
if net.ParseIP(host) == nil {
// Not an IP address.
hostNoZone := stripZone(host)
if net.ParseIP(hostNoZone) == nil {
ss, err := tryFile(host)
if err == nil {
servers = append(servers, ss...)
Expand All @@ -47,8 +59,7 @@ func HostPortOrFile(s ...string) ([]string, error) {
continue
}

if net.ParseIP(addr) == nil {
// Not an IP address.
if net.ParseIP(stripZone(addr)) == nil {
ss, err := tryFile(host)
if err == nil {
servers = append(servers, ss...)
Expand Down
20 changes: 20 additions & 0 deletions plugin/pkg/parse/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ func TestHostPortOrFile(t *testing.T) {
"127.0.0.1:53",
false,
},
{
"fe80::1",
"[fe80::1]:53",
false,
},
{
"fe80::1%ens3",
"[fe80::1%ens3]:53",
false,
},
{
"[fd01::1]:153",
"[fd01::1]:153",
false,
},
{
"[fd01::1%ens3]:153",
"[fd01::1%ens3]:153",
false,
},
}

err := ioutil.WriteFile("resolv.conf", []byte("nameserver 127.0.0.1\n"), 0600)
Expand Down