2021-10-02 10:13:05 +00:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-02 07:15:05 +00:00
|
|
|
"net/netip"
|
2022-11-07 20:10:06 +00:00
|
|
|
"net/url"
|
2021-10-09 10:22:13 +00:00
|
|
|
"strings"
|
2021-10-02 10:13:05 +00:00
|
|
|
|
2022-05-30 09:49:35 +00:00
|
|
|
mapset "github.com/deckarep/golang-set/v2"
|
2022-09-02 07:15:05 +00:00
|
|
|
"go4.org/netipx"
|
2021-10-17 09:57:53 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-11-07 20:10:06 +00:00
|
|
|
"tailscale.com/types/dnstype"
|
2021-10-02 10:13:05 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
|
|
|
)
|
|
|
|
|
2021-11-14 17:31:51 +00:00
|
|
|
const (
|
2021-11-15 17:24:24 +00:00
|
|
|
ByteSize = 8
|
2021-11-14 17:31:51 +00:00
|
|
|
)
|
|
|
|
|
2022-01-15 15:18:49 +00:00
|
|
|
const (
|
|
|
|
ipv4AddressLength = 32
|
|
|
|
ipv6AddressLength = 128
|
|
|
|
)
|
|
|
|
|
2022-11-07 20:10:06 +00:00
|
|
|
const (
|
|
|
|
nextDNSDoHPrefix = "https://dns.nextdns.io"
|
|
|
|
)
|
|
|
|
|
2021-10-10 10:34:55 +00:00
|
|
|
// generateMagicDNSRootDomains generates a list of DNS entries to be included in `Routes` in `MapResponse`.
|
|
|
|
// This list of reverse DNS entries instructs the OS on what subnets and domains the Tailscale embedded DNS
|
|
|
|
// server (listening in 100.100.100.100 udp/53) should be used for.
|
|
|
|
//
|
|
|
|
// Tailscale.com includes in the list:
|
|
|
|
// - the `BaseDomain` of the user
|
|
|
|
// - the reverse DNS entry for IPv6 (0.e.1.a.c.5.1.1.a.7.d.f.ip6.arpa., see below more on IPv6)
|
|
|
|
// - the reverse DNS entries for the IPv4 subnets covered by the user's `IPPrefix`.
|
|
|
|
// In the public SaaS this is [64-127].100.in-addr.arpa.
|
|
|
|
//
|
|
|
|
// The main purpose of this function is then generating the list of IPv4 entries. For the 100.64.0.0/10, this
|
|
|
|
// is clear, and could be hardcoded. But we are allowing any range as `IPPrefix`, so we need to find out the
|
|
|
|
// subnets when we have 172.16.0.0/16 (i.e., [0-255].16.172.in-addr.arpa.), or any other subnet.
|
|
|
|
//
|
|
|
|
// How IN-ADDR.ARPA domains work is defined in RFC1035 (section 3.5). Tailscale.com seems to adhere to this,
|
|
|
|
// and do not make use of RFC2317 ("Classless IN-ADDR.ARPA delegation") - hence generating the entries for the next
|
|
|
|
// class block only.
|
|
|
|
|
|
|
|
// From the netmask we can find out the wildcard bits (the bits that are not set in the netmask).
|
|
|
|
// This allows us to then calculate the subnets included in the subsequent class block and generate the entries.
|
2022-09-02 07:15:05 +00:00
|
|
|
func generateMagicDNSRootDomains(ipPrefixes []netip.Prefix) []dnsname.FQDN {
|
2022-01-16 13:16:59 +00:00
|
|
|
fqdns := make([]dnsname.FQDN, 0, len(ipPrefixes))
|
|
|
|
for _, ipPrefix := range ipPrefixes {
|
2022-09-02 07:15:05 +00:00
|
|
|
var generateDNSRoot func(netip.Prefix) []dnsname.FQDN
|
|
|
|
switch ipPrefix.Addr().BitLen() {
|
2022-01-15 15:18:49 +00:00
|
|
|
case ipv4AddressLength:
|
|
|
|
generateDNSRoot = generateIPv4DNSRootDomain
|
|
|
|
|
|
|
|
case ipv6AddressLength:
|
|
|
|
generateDNSRoot = generateIPv6DNSRootDomain
|
2021-10-02 10:13:05 +00:00
|
|
|
|
2022-01-16 13:16:59 +00:00
|
|
|
default:
|
2022-01-25 22:11:15 +00:00
|
|
|
panic(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"unsupported IP version with address length %d",
|
2022-09-02 07:15:05 +00:00
|
|
|
ipPrefix.Addr().BitLen(),
|
2022-01-25 22:11:15 +00:00
|
|
|
),
|
|
|
|
)
|
2022-01-16 13:16:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 15:18:49 +00:00
|
|
|
fqdns = append(fqdns, generateDNSRoot(ipPrefix)...)
|
2022-01-16 13:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fqdns
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:15:05 +00:00
|
|
|
func generateIPv4DNSRootDomain(ipPrefix netip.Prefix) []dnsname.FQDN {
|
2021-10-09 22:40:25 +00:00
|
|
|
// Conversion to the std lib net.IPnet, a bit easier to operate
|
2022-09-02 07:15:05 +00:00
|
|
|
netRange := netipx.PrefixIPNet(ipPrefix)
|
2021-10-09 10:22:13 +00:00
|
|
|
maskBits, _ := netRange.Mask.Size()
|
|
|
|
|
2021-10-10 10:34:55 +00:00
|
|
|
// lastOctet is the last IP byte covered by the mask
|
2021-11-15 17:24:24 +00:00
|
|
|
lastOctet := maskBits / ByteSize
|
2021-10-09 22:40:25 +00:00
|
|
|
|
2021-10-10 10:34:55 +00:00
|
|
|
// wildcardBits is the number of bits not under the mask in the lastOctet
|
2021-11-15 17:24:24 +00:00
|
|
|
wildcardBits := ByteSize - maskBits%ByteSize
|
2021-10-09 22:40:25 +00:00
|
|
|
|
2021-10-10 10:34:55 +00:00
|
|
|
// min is the value in the lastOctet byte of the IP
|
|
|
|
// max is basically 2^wildcardBits - i.e., the value when all the wildcardBits are set to 1
|
|
|
|
min := uint(netRange.IP[lastOctet])
|
2021-11-14 16:49:54 +00:00
|
|
|
max := (min + 1<<uint(wildcardBits)) - 1
|
2021-10-09 10:22:13 +00:00
|
|
|
|
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{}
|
2021-10-10 10:34:55 +00:00
|
|
|
for i := lastOctet - 1; i >= 0; i-- {
|
2021-10-09 10:22:13 +00:00
|
|
|
rdnsSlice = append(rdnsSlice, fmt.Sprintf("%d", netRange.IP[i]))
|
|
|
|
}
|
|
|
|
rdnsSlice = append(rdnsSlice, "in-addr.arpa.")
|
|
|
|
rdnsBase := strings.Join(rdnsSlice, ".")
|
|
|
|
|
2022-01-15 15:18:49 +00:00
|
|
|
fqdns := make([]dnsname.FQDN, 0, max-min+1)
|
2021-10-09 10:22:13 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-11-14 15:46:09 +00:00
|
|
|
|
2022-01-15 15:18:49 +00:00
|
|
|
return fqdns
|
|
|
|
}
|
|
|
|
|
2022-09-02 07:15:05 +00:00
|
|
|
func generateIPv6DNSRootDomain(ipPrefix netip.Prefix) []dnsname.FQDN {
|
2022-01-15 15:18:49 +00:00
|
|
|
const nibbleLen = 4
|
|
|
|
|
2022-09-02 07:15:05 +00:00
|
|
|
maskBits, _ := netipx.PrefixIPNet(ipPrefix).Mask.Size()
|
|
|
|
expanded := ipPrefix.Addr().StringExpanded()
|
2022-01-15 15:18:49 +00:00
|
|
|
nibbleStr := strings.Map(func(r rune) rune {
|
|
|
|
if r == ':' {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}, expanded)
|
|
|
|
|
|
|
|
// TODO?: that does not look the most efficient implementation,
|
|
|
|
// but the inputs are not so long as to cause problems,
|
|
|
|
// and from what I can see, the generateMagicDNSRootDomains
|
|
|
|
// function is called only once over the lifetime of a server process.
|
|
|
|
prefixConstantParts := []string{}
|
|
|
|
for i := 0; i < maskBits/nibbleLen; i++ {
|
2022-01-25 22:11:15 +00:00
|
|
|
prefixConstantParts = append(
|
|
|
|
[]string{string(nibbleStr[i])},
|
|
|
|
prefixConstantParts...)
|
2022-01-15 15:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
makeDomain := func(variablePrefix ...string) (dnsname.FQDN, error) {
|
|
|
|
prefix := strings.Join(append(variablePrefix, prefixConstantParts...), ".")
|
|
|
|
|
|
|
|
return dnsname.ToFQDN(fmt.Sprintf("%s.ip6.arpa", prefix))
|
|
|
|
}
|
|
|
|
|
|
|
|
var fqdns []dnsname.FQDN
|
|
|
|
if maskBits%4 == 0 {
|
|
|
|
dom, _ := makeDomain()
|
|
|
|
fqdns = append(fqdns, dom)
|
|
|
|
} else {
|
|
|
|
domCount := 1 << (maskBits % nibbleLen)
|
|
|
|
fqdns = make([]dnsname.FQDN, 0, domCount)
|
|
|
|
for i := 0; i < domCount; i++ {
|
|
|
|
varNibble := fmt.Sprintf("%x", i)
|
|
|
|
dom, err := makeDomain(varNibble)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fqdns = append(fqdns, dom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fqdns
|
2021-10-02 10:13:05 +00:00
|
|
|
}
|
2021-10-17 09:57:53 +00:00
|
|
|
|
2022-11-07 20:10:06 +00:00
|
|
|
// If any nextdns DoH resolvers are present in the list of resolvers it will
|
2023-05-01 14:52:03 +00:00
|
|
|
// take metadata from the node metadata and instruct tailscale to add it
|
2022-11-07 20:10:06 +00:00
|
|
|
// to the requests. This makes it possible to identify from which device the
|
|
|
|
// requests come in the NextDNS dashboard.
|
|
|
|
//
|
|
|
|
// This will produce a resolver like:
|
|
|
|
// `https://dns.nextdns.io/<nextdns-id>?device_name=node-name&device_model=linux&device_ip=100.64.0.1`
|
2023-05-01 14:52:03 +00:00
|
|
|
func addNextDNSMetadata(resolvers []*dnstype.Resolver, node Node) {
|
2022-11-07 20:10:06 +00:00
|
|
|
for _, resolver := range resolvers {
|
|
|
|
if strings.HasPrefix(resolver.Addr, nextDNSDoHPrefix) {
|
|
|
|
attrs := url.Values{
|
2023-05-01 14:52:03 +00:00
|
|
|
"device_name": []string{node.Hostname},
|
|
|
|
"device_model": []string{node.HostInfo.OS},
|
2022-11-07 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 14:52:03 +00:00
|
|
|
if len(node.IPAddresses) > 0 {
|
|
|
|
attrs.Add("device_ip", node.IPAddresses[0].String())
|
2022-11-07 20:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resolver.Addr = fmt.Sprintf("%s?%s", resolver.Addr, attrs.Encode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 08:36:45 +00:00
|
|
|
func getMapResponseDNSConfig(
|
|
|
|
dnsConfigOrig *tailcfg.DNSConfig,
|
|
|
|
baseDomain string,
|
2023-05-01 14:52:03 +00:00
|
|
|
node Node,
|
|
|
|
peers Nodes,
|
2021-11-14 17:03:21 +00:00
|
|
|
) *tailcfg.DNSConfig {
|
2022-11-07 20:10:06 +00:00
|
|
|
var dnsConfig *tailcfg.DNSConfig = dnsConfigOrig.Clone()
|
2021-10-17 10:10:03 +00:00
|
|
|
if dnsConfigOrig != nil && dnsConfigOrig.Proxied { // if MagicDNS is enabled
|
2023-01-17 16:43:44 +00:00
|
|
|
// Only inject the Search Domain of the current user - shared nodes should use their full FQDN
|
2021-11-13 08:36:45 +00:00
|
|
|
dnsConfig.Domains = append(
|
|
|
|
dnsConfig.Domains,
|
2021-12-29 09:58:10 +00:00
|
|
|
fmt.Sprintf(
|
|
|
|
"%s.%s",
|
2023-05-01 14:52:03 +00:00
|
|
|
node.User.Name,
|
2021-12-29 09:58:10 +00:00
|
|
|
baseDomain,
|
|
|
|
),
|
2021-11-13 08:36:45 +00:00
|
|
|
)
|
2021-10-17 09:57:53 +00:00
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
userSet := mapset.NewSet[User]()
|
2023-05-01 14:52:03 +00:00
|
|
|
userSet.Add(node.User)
|
2021-10-17 09:57:53 +00:00
|
|
|
for _, p := range peers {
|
2023-01-17 16:43:44 +00:00
|
|
|
userSet.Add(p.User)
|
2021-10-17 09:57:53 +00:00
|
|
|
}
|
2023-01-17 16:43:44 +00:00
|
|
|
for _, user := range userSet.ToSlice() {
|
|
|
|
dnsRoute := fmt.Sprintf("%v.%v", user.Name, baseDomain)
|
2021-10-17 21:58:09 +00:00
|
|
|
dnsConfig.Routes[dnsRoute] = nil
|
2021-10-17 09:57:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-17 10:10:03 +00:00
|
|
|
dnsConfig = dnsConfigOrig
|
2021-10-17 09:57:53 +00:00
|
|
|
}
|
2021-11-14 15:46:09 +00:00
|
|
|
|
2023-05-01 14:52:03 +00:00
|
|
|
addNextDNSMetadata(dnsConfig.Resolvers, node)
|
2022-11-07 20:10:06 +00:00
|
|
|
|
2021-11-14 17:03:21 +00:00
|
|
|
return dnsConfig
|
2021-10-17 09:57:53 +00:00
|
|
|
}
|