Skip to content

Commit

Permalink
consul: Default recursor port 53. Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Feb 23, 2014
1 parent 94171d3 commit 49ba21a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
28 changes: 28 additions & 0 deletions command/agent/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func NewDNSServer(agent *Agent, logOutput io.Writer, domain, bind, recursor stri
mux.HandleFunc(consulDomain, srv.handleTest)
}
if recursor != "" {
recursor, err := recursorAddr(recursor)
if err != nil {
return nil, fmt.Errorf("Invalid recursor address: %v", err)
}
srv.recursor = recursor
mux.HandleFunc(".", srv.handleRecurse)
}

Expand Down Expand Up @@ -124,6 +129,29 @@ func NewDNSServer(agent *Agent, logOutput io.Writer, domain, bind, recursor stri
return srv, nil
}

// recursorAddr is used to add a port to the recursor if omitted.
func recursorAddr(recursor string) (string, error) {
// Add the port if none
START:
_, _, err := net.SplitHostPort(recursor)
if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
recursor = fmt.Sprintf("%s:%d", recursor, 53)
goto START
}
if err != nil {
return "", err
}

// Get the address
addr, err := net.ResolveTCPAddr("tcp", recursor)
if err != nil {
return "", err
}

// Return string
return addr.String(), nil
}

// handleQUery is used to handle DNS queries in the configured domain
func (d *DNSServer) handleQuery(resp dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
Expand Down
10 changes: 10 additions & 0 deletions command/agent/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ func makeDNSServer(t *testing.T) (string, *DNSServer) {
return dir, server
}

func TestRecursorAddr(t *testing.T) {
addr, err := recursorAddr("8.8.8.8")
if err != nil {
t.Fatalf("err: %v", err)
}
if addr != "8.8.8.8:53" {
t.Fatalf("bad: %v", addr)
}
}

func TestDNS_IsAlive(t *testing.T) {
dir, srv := makeDNSServer(t)
defer os.RemoveAll(dir)
Expand Down

0 comments on commit 49ba21a

Please sign in to comment.