Skip to content

Commit

Permalink
Allow adding aliases for subdomains
Browse files Browse the repository at this point in the history
  • Loading branch information
giodamelio committed Apr 26, 2023
1 parent 245c6e3 commit e823a7d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ $ export TSDNS_DNSSERVER_PORT=2222

- Webhook endpoint allowing automatic refreshing of devices when a new device is added
- LetsEncrypt DNS-01 Challenge integration
- Config based static records/aliases
- ~Config based static records/aliases~
- Simple web ui listing status
- Status url for Prometheus or monitoring
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func loadConfig() {
viper.SetDefault("dns-server.port", 53)
viper.SetDefault("tailscale.hostname", "tailscale-custom-domain-dns")
viper.SetDefault("tailscale.ephemeral", false)
viper.SetDefault("aliases.subdomains", map[string]string{})

// Read the config
configFile, err := os.Open(configPath)
Expand Down
4 changes: 4 additions & 0 deletions examples/tailscale-custom-domain-dns.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ interval = "1h"
# Alias for the root domain without any subdomain
# Example: root = "machine-one"
root = ""

[aliases.subdomains]
# Aliases for subdomains, they take the form of:
# alias = "existing-device-name"
19 changes: 18 additions & 1 deletion server/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ func makeHandler(readDevices chan ReadDevicesOp, host string) DnsHandler {
// Get just the subdomain name from the request
name := strings.ReplaceAll(question.Name, "."+host, "")

// Get possible aliases
aliases := viper.GetStringMapString("aliases.subdomains")

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 {
Expand All @@ -136,7 +138,22 @@ func makeHandler(readDevices chan ReadDevicesOp, host string) DnsHandler {
m.Answer = rrs
}
}
} else if alias, ok := aliases[name]; ok {
// Respond with an alias
if device, ok := deviceMap[alias]; ok {
log.
Debug().
Str("host", name).
Msgf(`Serving alias "%s" redirects to "%s"`, question.Name, alias)

rrs = constructResponses(name, device, question, host)
m.Answer = rrs
}
} else if device, ok := deviceMap[name]; ok {
log.
Debug().
Msgf(`Serving normal response "%s"`, question.Name)

// Respond if a device with the hostname exists
rrs = constructResponses(name, device, question, host)
}
Expand Down

0 comments on commit e823a7d

Please sign in to comment.