2021-10-02 10:13:05 +00:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-10-09 10:22:13 +00:00
|
|
|
"strings"
|
2021-10-02 10:13:05 +00:00
|
|
|
|
2021-10-09 10:22:13 +00:00
|
|
|
"inet.af/netaddr"
|
2021-10-02 10:13:05 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
|
|
|
)
|
|
|
|
|
2021-10-09 22:40:25 +00:00
|
|
|
// generateMagicDNSRootDomains generates a list of DNS entries to be included in the
|
|
|
|
// routing for DNS in the MapResponse struct. This list of DNS instructs the OS
|
|
|
|
// on what domains the Tailscale embedded DNS server should be used for.
|
2021-10-09 10:22:13 +00:00
|
|
|
func generateMagicDNSRootDomains(ipPrefix netaddr.IPPrefix, baseDomain string) (*[]dnsname.FQDN, error) {
|
|
|
|
base, err := dnsname.ToFQDN(baseDomain)
|
2021-10-02 10:13:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:47:09 +00:00
|
|
|
// TODO(juanfont): we are not handing out IPv6 addresses yet
|
2021-10-05 17:00:40 +00:00
|
|
|
// and in fact this is Tailscale.com's range (note the fd7a:115c:a1e0: range in the fc00::/7 network)
|
2021-10-02 10:13:05 +00:00
|
|
|
ipv6base := dnsname.FQDN("0.e.1.a.c.5.1.1.a.7.d.f.ip6.arpa.")
|
|
|
|
fqdns := []dnsname.FQDN{base, ipv6base}
|
|
|
|
|
2021-10-09 22:40:25 +00:00
|
|
|
// Conversion to the std lib net.IPnet, a bit easier to operate
|
2021-10-09 10:22:13 +00:00
|
|
|
netRange := ipPrefix.IPNet()
|
|
|
|
maskBits, _ := netRange.Mask.Size()
|
|
|
|
|
2021-10-09 22:40:25 +00:00
|
|
|
// lastByte is the last IP byte covered by the mask
|
2021-10-09 10:22:13 +00:00
|
|
|
lastByte := maskBits / 8
|
2021-10-09 22:40:25 +00:00
|
|
|
|
|
|
|
// unmaskedBits is the number of bits not under the mask in the byte lastByte
|
2021-10-09 10:22:13 +00:00
|
|
|
unmaskedBits := 8 - maskBits%8
|
2021-10-09 22:40:25 +00:00
|
|
|
|
|
|
|
// min is the value in the lastByte byte of the IP
|
|
|
|
// max is basically 2^unmaskedBits - i.e., the value when all the unmaskedBits are set to 1
|
2021-10-09 10:22:13 +00:00
|
|
|
min := uint(netRange.IP[lastByte])
|
|
|
|
max := uint((min + 1<<uint(unmaskedBits)) - 1)
|
|
|
|
|
2021-10-09 22:40:25 +00:00
|
|
|
// here we generate the base domain (e.g., 100.in-addr.arpa., 16.172.in-addr.arpa., etc.)
|
2021-10-09 10:22:13 +00:00
|
|
|
rdnsSlice := []string{}
|
|
|
|
for i := lastByte - 1; i >= 0; i-- {
|
|
|
|
rdnsSlice = append(rdnsSlice, fmt.Sprintf("%d", netRange.IP[i]))
|
|
|
|
}
|
|
|
|
rdnsSlice = append(rdnsSlice, "in-addr.arpa.")
|
|
|
|
rdnsBase := strings.Join(rdnsSlice, ".")
|
|
|
|
|
|
|
|
for i := min; i <= max; i++ {
|
|
|
|
fqdn, err := dnsname.ToFQDN(fmt.Sprintf("%d.%s", i, rdnsBase))
|
2021-10-02 10:13:05 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fqdns = append(fqdns, fqdn)
|
|
|
|
}
|
|
|
|
return &fqdns, nil
|
|
|
|
}
|