net/dns: add test for DoH upgrade of system DNS
Someone asked me if we use DNS-over-HTTPS if the system's resolver is an IP address that supports DoH and there's no global nameserver set (i.e. no "Override DNS servers" set). I didn't know the answer offhand, and it took a while for me to figure it out. The answer is yes, in cases where we take over the system's DNS configuration and read the base config, we do upgrade any DoH-capable resolver to use DoH. Here's a test that verifies this behaviour (and hopefully helps as documentation the next time someone has this question). Updates #cleanup Signed-off-by: Andrew Dunham <andrew@tailscale.com>
This commit is contained in:
committed by
Andrew Dunham
parent
0e1b2b15f1
commit
8d875a301c
@@ -13,12 +13,14 @@ import (
|
||||
"log"
|
||||
"math/big"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"tailscale.com/feature/buildfeatures"
|
||||
"tailscale.com/util/testenv"
|
||||
)
|
||||
|
||||
// dohOfIP maps from public DNS IPs to their DoH base URL.
|
||||
@@ -367,3 +369,39 @@ func IPIsDoHOnlyServer(ip netip.Addr) bool {
|
||||
controlDv6RangeA.Contains(ip) || controlDv6RangeB.Contains(ip) ||
|
||||
ip == controlDv4One || ip == controlDv4Two
|
||||
}
|
||||
|
||||
var testMu sync.Mutex
|
||||
|
||||
// RegisterTestDoHEndpoint registers a test DoH endpoint mapping for use in tests.
|
||||
// It maps the given IP to the DoH base URL, and the URL back to the IP.
|
||||
//
|
||||
// This function panics if called outside of tests, and cannot be called
|
||||
// concurrently with any usage of this package (i.e. before any DNS forwarders
|
||||
// are created). It is safe to call concurrently with itself.
|
||||
//
|
||||
// It returns a cleanup function that removes the registration.
|
||||
func RegisterTestDoHEndpoint(ip netip.Addr, dohBase string) func() {
|
||||
if !testenv.InTest() {
|
||||
panic("RegisterTestDoHEndpoint called outside of tests")
|
||||
}
|
||||
populateOnce.Do(populate)
|
||||
|
||||
testMu.Lock()
|
||||
defer testMu.Unlock()
|
||||
|
||||
dohOfIP[ip] = dohBase
|
||||
dohIPsOfBase[dohBase] = append(dohIPsOfBase[dohBase], ip)
|
||||
|
||||
return func() {
|
||||
testMu.Lock()
|
||||
defer testMu.Unlock()
|
||||
|
||||
delete(dohOfIP, ip)
|
||||
dohIPsOfBase[dohBase] = slices.DeleteFunc(dohIPsOfBase[dohBase], func(addr netip.Addr) bool {
|
||||
return addr == ip
|
||||
})
|
||||
if len(dohIPsOfBase[dohBase]) == 0 {
|
||||
delete(dohIPsOfBase, dohBase)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user