Skip to content

Commit

Permalink
Generated MagicDNS search domains (only in 100.64.0.0/10)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfont committed Oct 2, 2021
1 parent e432e98 commit 45e71ec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"gorm.io/gorm"
"inet.af/netaddr"
"tailscale.com/tailcfg"
"tailscale.com/types/dnstype"
"tailscale.com/types/wgkey"
)

Expand Down Expand Up @@ -104,6 +105,17 @@ func NewHeadscale(cfg Config) (*Headscale, error) {
return nil, err
}

if h.cfg.DNSConfig != nil && h.cfg.DNSConfig.Proxied { // if MagicDNS
magicDNSDomains, err := h.generateMagicDNSRootDomains()
if err != nil {
return nil, err
}
h.cfg.DNSConfig.Routes = make(map[string][]dnstype.Resolver)
for _, d := range *magicDNSDomains {
h.cfg.DNSConfig.Routes[d.WithoutTrailingDot()] = nil
}
}

return &h, nil
}

Expand Down
30 changes: 30 additions & 0 deletions dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package headscale

import (
"fmt"

"tailscale.com/util/dnsname"
)

func (h *Headscale) generateMagicDNSRootDomains() (*[]dnsname.FQDN, error) {
base, err := dnsname.ToFQDN(h.cfg.BaseDomain)
if err != nil {
return nil, err
}

// TODO(juanfont): we are not handing over IPv6 addresses yet
// and in fact this is Tailscale.com's range (not the fd7a:115c:a1e0: range in the fc00::/7 network)
ipv6base := dnsname.FQDN("0.e.1.a.c.5.1.1.a.7.d.f.ip6.arpa.")
fqdns := []dnsname.FQDN{base, ipv6base}

for i := 64; i <= 127; i++ {
fqdn, err := dnsname.ToFQDN(fmt.Sprintf("%d.100.in-addr.arpa.", i))
if err != nil {
// TODO: propagate error
continue
}
fqdns = append(fqdns, fqdn)
}

return &fqdns, nil
}

0 comments on commit 45e71ec

Please sign in to comment.