Skip to content

Commit

Permalink
Allow adding an alias for the root domain
Browse files Browse the repository at this point in the history
  • Loading branch information
giodamelio committed Apr 26, 2023
1 parent 82ae680 commit 245c6e3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/tailscale-custom-domain-dns.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ port = 53
[fetcher]
# How frequently the server will fetch the list of devices from your organization.
interval = "1h"

[aliases]
# Alias for the root domain without any subdomain
# Example: root = "machine-one"
root = ""
37 changes: 32 additions & 5 deletions server/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ func buildRR(name string, question dns.Question, address netip.Addr, host string
var err error

if (question.Qtype == dns.TypeA || question.Qtype == dns.TypeANY) && address.Is4() {
rr, err = dns.NewRR(fmt.Sprintf("%s.%s A %s", name, host, address.String()))
if name == "" {
rr, err = dns.NewRR(fmt.Sprintf("%s A %s", host, address.String()))
} else {
rr, err = dns.NewRR(fmt.Sprintf("%s.%s A %s", name, host, address.String()))
}
}

if (question.Qtype == dns.TypeAAAA || question.Qtype == dns.TypeANY) && address.Is6() {
rr, err = dns.NewRR(fmt.Sprintf("%s.%s AAAA %s", name, host, address.String()))
if name == "" {
rr, err = dns.NewRR(fmt.Sprintf("%s AAAA %s", host, address.String()))
} else {
rr, err = dns.NewRR(fmt.Sprintf("%s.%s AAAA %s", name, host, address.String()))
}
}

if err != nil {
Expand Down Expand Up @@ -112,9 +120,28 @@ func makeHandler(readDevices chan ReadDevicesOp, host string) DnsHandler {
// Get just the subdomain name from the request
name := strings.ReplaceAll(question.Name, "."+host, "")

// Respond if a device with the hostname exists
if device, ok := deviceMap[name]; ok {
rrs := constructResponses(name, device, question, host)
var rrs []dns.RR
if question.Name == host {
// If the hostname is bare, check for a root alias

if viper.IsSet("aliases.root") {
name := viper.GetString("aliases.root")
if device, ok := deviceMap[name]; ok {
log.
Debug().
Str("host", name).
Msgf(`Serving root alias for hostname "%s"`, name)

rrs = constructResponses("", device, question, host)
m.Answer = rrs
}
}
} else if device, ok := deviceMap[name]; ok {
// Respond if a device with the hostname exists
rrs = constructResponses(name, device, question, host)
}

if len(rrs) != 0 {
log.Trace().Any("records", rrs).Msg("Sending records to client")
m.Answer = rrs
}
Expand Down

0 comments on commit 245c6e3

Please sign in to comment.