diff --git a/nameserver/module.go b/nameserver/module.go index e85443fd..6ef47d71 100644 --- a/nameserver/module.go +++ b/nameserver/module.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net" + "os" "strconv" "github.com/safing/portbase/log" @@ -46,6 +47,12 @@ func start() error { return fmt.Errorf("failed to parse nameserver listen address: %w", err) } + hostname, err = os.Hostname() + if err != nil { + log.Warningf("nameserver: failed to get hostname: %s", err) + } + hostname += "." + // Start listener(s). if ip2 == nil { // Start a single listener. diff --git a/nameserver/nameserver.go b/nameserver/nameserver.go index 44a37b2f..6c09e601 100644 --- a/nameserver/nameserver.go +++ b/nameserver/nameserver.go @@ -19,6 +19,8 @@ import ( "github.com/miekg/dns" ) +var hostname string + func handleRequestAsWorker(w dns.ResponseWriter, query *dns.Msg) { err := module.RunWorker("dns request", func(ctx context.Context) error { return handleRequest(ctx, w, query) @@ -87,8 +89,8 @@ func handleRequest(ctx context.Context, w dns.ResponseWriter, request *dns.Msg) return reply(nsutil.Refused("unsupported qclass")) } - // Handle request for localhost. - if strings.HasSuffix(q.FQDN, "localhost.") { + // Handle request for localhost and the hostname. + if strings.HasSuffix(q.FQDN, "localhost.") || q.FQDN == hostname { tracer.Tracef("nameserver: returning localhost records") return reply(nsutil.Localhost()) }