2023-10-27 21:20:10 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2023-11-01 23:56:30 +00:00
|
|
|
// Package appc implements App Connectors.
|
|
|
|
// An AppConnector provides DNS domain oriented routing of traffic. An App
|
|
|
|
// Connector becomes a DNS server for a peer, authoritative for the set of
|
|
|
|
// configured domains. DNS resolution of the target domain triggers dynamic
|
|
|
|
// publication of routes to ensure that traffic to the domain is routed through
|
|
|
|
// the App Connector.
|
2023-10-27 21:20:10 +00:00
|
|
|
package appc
|
|
|
|
|
|
|
|
import (
|
2024-01-18 18:18:25 +00:00
|
|
|
"context"
|
2024-07-12 14:08:47 +00:00
|
|
|
"fmt"
|
2023-10-27 21:20:10 +00:00
|
|
|
"net/netip"
|
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2024-05-22 16:56:03 +00:00
|
|
|
"time"
|
2023-10-27 21:20:10 +00:00
|
|
|
|
2023-10-31 21:59:18 +00:00
|
|
|
xmaps "golang.org/x/exp/maps"
|
2023-10-27 21:20:10 +00:00
|
|
|
"golang.org/x/net/dns/dnsmessage"
|
|
|
|
"tailscale.com/types/logger"
|
2023-10-31 21:59:18 +00:00
|
|
|
"tailscale.com/types/views"
|
2024-07-12 14:08:47 +00:00
|
|
|
"tailscale.com/util/clientmetric"
|
2023-11-08 18:57:16 +00:00
|
|
|
"tailscale.com/util/dnsname"
|
2024-01-18 18:18:25 +00:00
|
|
|
"tailscale.com/util/execqueue"
|
2024-01-31 22:33:14 +00:00
|
|
|
"tailscale.com/util/mak"
|
2024-04-15 17:16:02 +00:00
|
|
|
"tailscale.com/util/slicesx"
|
2023-10-27 21:20:10 +00:00
|
|
|
)
|
|
|
|
|
2024-05-22 16:56:03 +00:00
|
|
|
// rateLogger responds to calls to update by adding a count for the current period and
|
|
|
|
// calling the callback if any previous period has finished since update was last called
|
|
|
|
type rateLogger struct {
|
|
|
|
interval time.Duration
|
|
|
|
start time.Time
|
|
|
|
periodStart time.Time
|
|
|
|
periodCount int64
|
|
|
|
now func() time.Time
|
2024-06-07 17:29:47 +00:00
|
|
|
callback func(int64, time.Time, int64)
|
2024-05-22 16:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *rateLogger) currentIntervalStart(now time.Time) time.Time {
|
|
|
|
millisSince := now.Sub(rl.start).Milliseconds() % rl.interval.Milliseconds()
|
|
|
|
return now.Add(-(time.Duration(millisSince)) * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2024-06-07 17:29:47 +00:00
|
|
|
func (rl *rateLogger) update(numRoutes int64) {
|
2024-05-22 16:56:03 +00:00
|
|
|
now := rl.now()
|
|
|
|
periodEnd := rl.periodStart.Add(rl.interval)
|
|
|
|
if periodEnd.Before(now) {
|
|
|
|
if rl.periodCount != 0 {
|
2024-06-07 17:29:47 +00:00
|
|
|
rl.callback(rl.periodCount, rl.periodStart, numRoutes)
|
2024-05-22 16:56:03 +00:00
|
|
|
}
|
|
|
|
rl.periodCount = 0
|
|
|
|
rl.periodStart = rl.currentIntervalStart(now)
|
|
|
|
}
|
|
|
|
rl.periodCount++
|
|
|
|
}
|
|
|
|
|
2024-06-07 17:29:47 +00:00
|
|
|
func newRateLogger(now func() time.Time, interval time.Duration, callback func(int64, time.Time, int64)) *rateLogger {
|
2024-05-22 16:56:03 +00:00
|
|
|
nowTime := now()
|
|
|
|
return &rateLogger{
|
|
|
|
callback: callback,
|
|
|
|
now: now,
|
|
|
|
interval: interval,
|
|
|
|
start: nowTime,
|
|
|
|
periodStart: nowTime,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
// RouteAdvertiser is an interface that allows the AppConnector to advertise
|
|
|
|
// newly discovered routes that need to be served through the AppConnector.
|
|
|
|
type RouteAdvertiser interface {
|
2024-01-23 00:57:31 +00:00
|
|
|
// AdvertiseRoute adds one or more route advertisements skipping any that
|
|
|
|
// are already advertised.
|
|
|
|
AdvertiseRoute(...netip.Prefix) error
|
2024-01-17 19:35:55 +00:00
|
|
|
|
2024-01-23 00:57:31 +00:00
|
|
|
// UnadvertiseRoute removes any matching route advertisements.
|
|
|
|
UnadvertiseRoute(...netip.Prefix) error
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 14:08:47 +00:00
|
|
|
var (
|
|
|
|
metricStoreRoutesRateBuckets = []int64{1, 2, 3, 4, 5, 10, 100, 1000}
|
|
|
|
metricStoreRoutesNBuckets = []int64{1, 2, 3, 4, 5, 10, 100, 1000, 10000}
|
|
|
|
metricStoreRoutesRate []*clientmetric.Metric
|
|
|
|
metricStoreRoutesN []*clientmetric.Metric
|
|
|
|
)
|
|
|
|
|
|
|
|
func initMetricStoreRoutes() {
|
|
|
|
for _, n := range metricStoreRoutesRateBuckets {
|
|
|
|
metricStoreRoutesRate = append(metricStoreRoutesRate, clientmetric.NewCounter(fmt.Sprintf("appc_store_routes_rate_%d", n)))
|
|
|
|
}
|
|
|
|
metricStoreRoutesRate = append(metricStoreRoutesRate, clientmetric.NewCounter("appc_store_routes_rate_over"))
|
|
|
|
for _, n := range metricStoreRoutesNBuckets {
|
|
|
|
metricStoreRoutesN = append(metricStoreRoutesN, clientmetric.NewCounter(fmt.Sprintf("appc_store_routes_n_routes_%d", n)))
|
|
|
|
}
|
|
|
|
metricStoreRoutesN = append(metricStoreRoutesN, clientmetric.NewCounter("appc_store_routes_n_routes_over"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func recordMetric(val int64, buckets []int64, metrics []*clientmetric.Metric) {
|
|
|
|
if len(buckets) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// finds the first bucket where val <=, or len(buckets) if none match
|
|
|
|
// for bucket values of 1, 10, 100; 0-1 goes to [0], 2-10 goes to [1], 11-100 goes to [2], 101+ goes to [3]
|
|
|
|
bucket, _ := slices.BinarySearch(buckets, val)
|
|
|
|
metrics[bucket].Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func metricStoreRoutes(rate, nRoutes int64) {
|
|
|
|
if len(metricStoreRoutesRate) == 0 {
|
|
|
|
initMetricStoreRoutes()
|
|
|
|
}
|
|
|
|
recordMetric(rate, metricStoreRoutesRateBuckets, metricStoreRoutesRate)
|
|
|
|
recordMetric(nRoutes, metricStoreRoutesNBuckets, metricStoreRoutesN)
|
|
|
|
}
|
|
|
|
|
2024-04-11 21:06:12 +00:00
|
|
|
// RouteInfo is a data structure used to persist the in memory state of an AppConnector
|
|
|
|
// so that we can know, even after a restart, which routes came from ACLs and which were
|
|
|
|
// learned from domains.
|
|
|
|
type RouteInfo struct {
|
|
|
|
// Control is the routes from the 'routes' section of an app connector acl.
|
|
|
|
Control []netip.Prefix `json:",omitempty"`
|
|
|
|
// Domains are the routes discovered by observing DNS lookups for configured domains.
|
|
|
|
Domains map[string][]netip.Addr `json:",omitempty"`
|
|
|
|
// Wildcards are the configured DNS lookup domains to observe. When a DNS query matches Wildcards,
|
|
|
|
// its result is added to Domains.
|
|
|
|
Wildcards []string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-11-01 23:56:30 +00:00
|
|
|
// AppConnector is an implementation of an AppConnector that performs
|
2023-10-27 21:20:10 +00:00
|
|
|
// its function as a subsystem inside of a tailscale node. At the control plane
|
|
|
|
// side App Connector routing is configured in terms of domains rather than IP
|
|
|
|
// addresses.
|
|
|
|
// The AppConnectors responsibility inside tailscaled is to apply the routing
|
|
|
|
// and domain configuration as supplied in the map response.
|
|
|
|
// DNS requests for configured domains are observed. If the domains resolve to
|
|
|
|
// routes not yet served by the AppConnector the local node configuration is
|
|
|
|
// updated to advertise the new route.
|
2023-11-01 23:56:30 +00:00
|
|
|
type AppConnector struct {
|
2023-10-27 21:20:10 +00:00
|
|
|
logf logger.Logf
|
|
|
|
routeAdvertiser RouteAdvertiser
|
|
|
|
|
2024-04-11 17:12:13 +00:00
|
|
|
// storeRoutesFunc will be called to persist routes if it is not nil.
|
|
|
|
storeRoutesFunc func(*RouteInfo) error
|
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
// mu guards the fields that follow
|
|
|
|
mu sync.Mutex
|
2024-01-17 19:35:55 +00:00
|
|
|
|
2024-02-06 23:07:56 +00:00
|
|
|
// domains is a map of lower case domain names with no trailing dot, to an
|
|
|
|
// ordered list of resolved IP addresses.
|
2023-10-27 21:20:10 +00:00
|
|
|
domains map[string][]netip.Addr
|
2023-11-08 18:57:16 +00:00
|
|
|
|
2024-01-17 19:35:55 +00:00
|
|
|
// controlRoutes is the list of routes that were last supplied by control.
|
|
|
|
controlRoutes []netip.Prefix
|
|
|
|
|
2023-11-08 18:57:16 +00:00
|
|
|
// wildcards is the list of domain strings that match subdomains.
|
|
|
|
wildcards []string
|
2024-01-18 18:18:25 +00:00
|
|
|
|
|
|
|
// queue provides ordering for update operations
|
|
|
|
queue execqueue.ExecQueue
|
2024-05-22 16:56:03 +00:00
|
|
|
|
|
|
|
writeRateMinute *rateLogger
|
|
|
|
writeRateDay *rateLogger
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 23:56:30 +00:00
|
|
|
// NewAppConnector creates a new AppConnector.
|
2024-04-11 17:12:13 +00:00
|
|
|
func NewAppConnector(logf logger.Logf, routeAdvertiser RouteAdvertiser, routeInfo *RouteInfo, storeRoutesFunc func(*RouteInfo) error) *AppConnector {
|
|
|
|
ac := &AppConnector{
|
2023-10-27 21:20:10 +00:00
|
|
|
logf: logger.WithPrefix(logf, "appc: "),
|
|
|
|
routeAdvertiser: routeAdvertiser,
|
2024-04-11 17:12:13 +00:00
|
|
|
storeRoutesFunc: storeRoutesFunc,
|
|
|
|
}
|
|
|
|
if routeInfo != nil {
|
|
|
|
ac.domains = routeInfo.Domains
|
|
|
|
ac.wildcards = routeInfo.Wildcards
|
|
|
|
ac.controlRoutes = routeInfo.Control
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
2024-06-07 17:29:47 +00:00
|
|
|
ac.writeRateMinute = newRateLogger(time.Now, time.Minute, func(c int64, s time.Time, l int64) {
|
|
|
|
ac.logf("routeInfo write rate: %d in minute starting at %v (%d routes)", c, s, l)
|
2024-07-12 14:08:47 +00:00
|
|
|
metricStoreRoutes(c, l)
|
2024-05-22 16:56:03 +00:00
|
|
|
})
|
2024-06-07 17:29:47 +00:00
|
|
|
ac.writeRateDay = newRateLogger(time.Now, 24*time.Hour, func(c int64, s time.Time, l int64) {
|
|
|
|
ac.logf("routeInfo write rate: %d in 24 hours starting at %v (%d routes)", c, s, l)
|
2024-05-22 16:56:03 +00:00
|
|
|
})
|
2024-04-11 17:12:13 +00:00
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldStoreRoutes returns true if the appconnector was created with the controlknob on
|
|
|
|
// and is storing its discovered routes persistently.
|
|
|
|
func (e *AppConnector) ShouldStoreRoutes() bool {
|
|
|
|
return e.storeRoutesFunc != nil
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 20:34:14 +00:00
|
|
|
// storeRoutesLocked takes the current state of the AppConnector and persists it
|
|
|
|
func (e *AppConnector) storeRoutesLocked() error {
|
|
|
|
if !e.ShouldStoreRoutes() {
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-07 17:29:47 +00:00
|
|
|
|
|
|
|
// log write rate and write size
|
|
|
|
numRoutes := int64(len(e.controlRoutes))
|
|
|
|
for _, rs := range e.domains {
|
|
|
|
numRoutes += int64(len(rs))
|
|
|
|
}
|
|
|
|
e.writeRateMinute.update(numRoutes)
|
|
|
|
e.writeRateDay.update(numRoutes)
|
|
|
|
|
2024-04-12 20:34:14 +00:00
|
|
|
return e.storeRoutesFunc(&RouteInfo{
|
|
|
|
Control: e.controlRoutes,
|
|
|
|
Domains: e.domains,
|
|
|
|
Wildcards: e.wildcards,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-15 18:30:00 +00:00
|
|
|
// ClearRoutes removes all route state from the AppConnector.
|
|
|
|
func (e *AppConnector) ClearRoutes() error {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
e.controlRoutes = nil
|
|
|
|
e.domains = nil
|
|
|
|
e.wildcards = nil
|
|
|
|
return e.storeRoutesLocked()
|
|
|
|
}
|
|
|
|
|
2024-01-18 18:18:25 +00:00
|
|
|
// UpdateDomainsAndRoutes starts an asynchronous update of the configuration
|
|
|
|
// given the new domains and routes.
|
|
|
|
func (e *AppConnector) UpdateDomainsAndRoutes(domains []string, routes []netip.Prefix) {
|
|
|
|
e.queue.Add(func() {
|
|
|
|
// Add the new routes first.
|
|
|
|
e.updateRoutes(routes)
|
|
|
|
e.updateDomains(domains)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDomains asynchronously replaces the current set of configured domains
|
|
|
|
// with the supplied set of domains. Domains must not contain a trailing dot,
|
|
|
|
// and should be lower case. If the domain contains a leading '*' label it
|
|
|
|
// matches all subdomains of a domain.
|
2023-11-01 23:56:30 +00:00
|
|
|
func (e *AppConnector) UpdateDomains(domains []string) {
|
2024-01-18 18:18:25 +00:00
|
|
|
e.queue.Add(func() {
|
|
|
|
e.updateDomains(domains)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for the currently scheduled asynchronous configuration changes to
|
|
|
|
// complete.
|
|
|
|
func (e *AppConnector) Wait(ctx context.Context) {
|
|
|
|
e.queue.Wait(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *AppConnector) updateDomains(domains []string) {
|
2023-10-27 21:20:10 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
2023-11-08 18:57:16 +00:00
|
|
|
var oldDomains map[string][]netip.Addr
|
|
|
|
oldDomains, e.domains = e.domains, make(map[string][]netip.Addr, len(domains))
|
2023-11-10 00:05:50 +00:00
|
|
|
e.wildcards = e.wildcards[:0]
|
2023-10-27 21:20:10 +00:00
|
|
|
for _, d := range domains {
|
|
|
|
d = strings.ToLower(d)
|
2023-11-08 18:57:16 +00:00
|
|
|
if len(d) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(d, "*.") {
|
|
|
|
e.wildcards = append(e.wildcards, d[2:])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
e.domains[d] = oldDomains[d]
|
|
|
|
delete(oldDomains, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that still-live wildcards addresses are preserved as well.
|
|
|
|
for d, addrs := range oldDomains {
|
|
|
|
for _, wc := range e.wildcards {
|
|
|
|
if dnsname.HasSuffix(d, wc) {
|
|
|
|
e.domains[d] = addrs
|
2024-04-15 17:16:02 +00:00
|
|
|
delete(oldDomains, d)
|
2023-11-08 18:57:16 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
2024-04-15 17:16:02 +00:00
|
|
|
|
|
|
|
// Everything left in oldDomains is a domain we're no longer tracking
|
|
|
|
// and if we are storing route info we can unadvertise the routes
|
|
|
|
if e.ShouldStoreRoutes() {
|
|
|
|
toRemove := []netip.Prefix{}
|
|
|
|
for _, addrs := range oldDomains {
|
|
|
|
for _, a := range addrs {
|
|
|
|
toRemove = append(toRemove, netip.PrefixFrom(a, a.BitLen()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := e.routeAdvertiser.UnadvertiseRoute(toRemove...); err != nil {
|
|
|
|
e.logf("failed to unadvertise routes on domain removal: %v: %v: %v", xmaps.Keys(oldDomains), toRemove, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:57:16 +00:00
|
|
|
e.logf("handling domains: %v and wildcards: %v", xmaps.Keys(e.domains), e.wildcards)
|
2023-10-31 21:59:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 18:18:25 +00:00
|
|
|
// updateRoutes merges the supplied routes into the currently configured routes. The routes supplied
|
2024-01-17 19:35:55 +00:00
|
|
|
// by control for UpdateRoutes are supplemental to the routes discovered by DNS resolution, but are
|
|
|
|
// also more often whole ranges. UpdateRoutes will remove any single address routes that are now
|
|
|
|
// covered by new ranges.
|
2024-01-18 18:18:25 +00:00
|
|
|
func (e *AppConnector) updateRoutes(routes []netip.Prefix) {
|
2024-01-17 19:35:55 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
// If there was no change since the last update, no work to do.
|
|
|
|
if slices.Equal(e.controlRoutes, routes) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-23 00:57:31 +00:00
|
|
|
if err := e.routeAdvertiser.AdvertiseRoute(routes...); err != nil {
|
|
|
|
e.logf("failed to advertise routes: %v: %v", routes, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var toRemove []netip.Prefix
|
|
|
|
|
2024-04-15 17:16:02 +00:00
|
|
|
// If we're storing routes and know e.controlRoutes is a good
|
|
|
|
// representation of what should be in AdvertisedRoutes we can stop
|
|
|
|
// advertising routes that used to be in e.controlRoutes but are not
|
|
|
|
// in routes.
|
|
|
|
if e.ShouldStoreRoutes() {
|
|
|
|
toRemove = routesWithout(e.controlRoutes, routes)
|
|
|
|
}
|
|
|
|
|
2024-01-17 19:35:55 +00:00
|
|
|
nextRoute:
|
|
|
|
for _, r := range routes {
|
|
|
|
for _, addr := range e.domains {
|
|
|
|
for _, a := range addr {
|
2024-01-23 00:18:57 +00:00
|
|
|
if r.Contains(a) && netip.PrefixFrom(a, a.BitLen()) != r {
|
2024-01-17 19:35:55 +00:00
|
|
|
pfx := netip.PrefixFrom(a, a.BitLen())
|
2024-01-23 00:57:31 +00:00
|
|
|
toRemove = append(toRemove, pfx)
|
2024-01-17 19:35:55 +00:00
|
|
|
continue nextRoute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 00:57:31 +00:00
|
|
|
if err := e.routeAdvertiser.UnadvertiseRoute(toRemove...); err != nil {
|
|
|
|
e.logf("failed to unadvertise routes: %v: %v", toRemove, err)
|
|
|
|
}
|
|
|
|
|
2024-01-17 19:35:55 +00:00
|
|
|
e.controlRoutes = routes
|
2024-04-12 20:34:14 +00:00
|
|
|
if err := e.storeRoutesLocked(); err != nil {
|
|
|
|
e.logf("failed to store route info: %v", err)
|
|
|
|
}
|
2024-01-17 19:35:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 21:59:18 +00:00
|
|
|
// Domains returns the currently configured domain list.
|
2023-11-01 23:56:30 +00:00
|
|
|
func (e *AppConnector) Domains() views.Slice[string] {
|
2023-10-31 21:59:18 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
return views.SliceOf(xmaps.Keys(e.domains))
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 21:34:52 +00:00
|
|
|
// DomainRoutes returns a map of domains to resolved IP
|
|
|
|
// addresses.
|
|
|
|
func (e *AppConnector) DomainRoutes() map[string][]netip.Addr {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
drCopy := make(map[string][]netip.Addr)
|
|
|
|
for k, v := range e.domains {
|
2023-11-15 19:50:13 +00:00
|
|
|
drCopy[k] = append(drCopy[k], v...)
|
2023-11-07 21:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return drCopy
|
|
|
|
}
|
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
// ObserveDNSResponse is a callback invoked by the DNS resolver when a DNS
|
|
|
|
// response is being returned over the PeerAPI. The response is parsed and
|
|
|
|
// matched against the configured domains, if matched the routeAdvertiser is
|
|
|
|
// advised to advertise the discovered route.
|
2023-11-01 23:56:30 +00:00
|
|
|
func (e *AppConnector) ObserveDNSResponse(res []byte) {
|
2023-10-27 21:20:10 +00:00
|
|
|
var p dnsmessage.Parser
|
|
|
|
if _, err := p.Start(res); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := p.SkipAllQuestions(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:33:14 +00:00
|
|
|
// cnameChain tracks a chain of CNAMEs for a given query in order to reverse
|
|
|
|
// a CNAME chain back to the original query for flattening. The keys are
|
|
|
|
// CNAME record targets, and the value is the name the record answers, so
|
|
|
|
// for www.example.com CNAME example.com, the map would contain
|
|
|
|
// ["example.com"] = "www.example.com".
|
|
|
|
var cnameChain map[string]string
|
|
|
|
|
|
|
|
// addressRecords is a list of address records found in the response.
|
|
|
|
var addressRecords map[string][]netip.Addr
|
|
|
|
|
2023-10-27 21:20:10 +00:00
|
|
|
for {
|
|
|
|
h, err := p.AnswerHeader()
|
|
|
|
if err == dnsmessage.ErrSectionDone {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.Class != dnsmessage.ClassINET {
|
|
|
|
if err := p.SkipAnswer(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
|
|
|
|
switch h.Type {
|
|
|
|
case dnsmessage.TypeCNAME, dnsmessage.TypeA, dnsmessage.TypeAAAA:
|
|
|
|
default:
|
2023-10-27 21:20:10 +00:00
|
|
|
if err := p.SkipAnswer(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:33:14 +00:00
|
|
|
domain := strings.TrimSuffix(strings.ToLower(h.Name.String()), ".")
|
|
|
|
if len(domain) == 0 {
|
|
|
|
continue
|
2023-11-08 18:57:16 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 22:33:14 +00:00
|
|
|
if h.Type == dnsmessage.TypeCNAME {
|
|
|
|
res, err := p.CNAMEResource()
|
|
|
|
if err != nil {
|
2023-10-27 21:20:10 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
cname := strings.TrimSuffix(strings.ToLower(res.CNAME.String()), ".")
|
|
|
|
if len(cname) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mak.Set(&cnameChain, cname, domain)
|
2023-10-27 21:20:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch h.Type {
|
|
|
|
case dnsmessage.TypeA:
|
|
|
|
r, err := p.AResource()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
addr := netip.AddrFrom4(r.A)
|
|
|
|
mak.Set(&addressRecords, domain, append(addressRecords[domain], addr))
|
2023-10-27 21:20:10 +00:00
|
|
|
case dnsmessage.TypeAAAA:
|
|
|
|
r, err := p.AAAAResource()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
addr := netip.AddrFrom16(r.AAAA)
|
|
|
|
mak.Set(&addressRecords, domain, append(addressRecords[domain], addr))
|
2023-10-27 21:20:10 +00:00
|
|
|
default:
|
|
|
|
if err := p.SkipAnswer(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
for domain, addrs := range addressRecords {
|
|
|
|
domain, isRouted := e.findRoutedDomainLocked(domain, cnameChain)
|
|
|
|
|
|
|
|
// domain and none of the CNAMEs in the chain are routed
|
|
|
|
if !isRouted {
|
2023-10-27 21:20:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
|
|
|
|
// advertise each address we have learned for the routed domain, that
|
|
|
|
// was not already known.
|
2024-02-06 23:07:56 +00:00
|
|
|
var toAdvertise []netip.Prefix
|
2024-01-31 22:33:14 +00:00
|
|
|
for _, addr := range addrs {
|
2024-02-06 23:07:56 +00:00
|
|
|
if !e.isAddrKnownLocked(domain, addr) {
|
|
|
|
toAdvertise = append(toAdvertise, netip.PrefixFrom(addr, addr.BitLen()))
|
2024-01-31 22:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 23:07:56 +00:00
|
|
|
|
2024-06-24 21:45:04 +00:00
|
|
|
if len(toAdvertise) > 0 {
|
|
|
|
e.logf("[v2] observed new routes for %s: %s", domain, toAdvertise)
|
|
|
|
e.scheduleAdvertisement(domain, toAdvertise...)
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// starting from the given domain that resolved to an address, find it, or any
|
|
|
|
// of the domains in the CNAME chain toward resolving it, that are routed
|
|
|
|
// domains, returning the routed domain name and a bool indicating whether a
|
|
|
|
// routed domain was found.
|
|
|
|
// e.mu must be held.
|
|
|
|
func (e *AppConnector) findRoutedDomainLocked(domain string, cnameChain map[string]string) (string, bool) {
|
|
|
|
var isRouted bool
|
|
|
|
for {
|
|
|
|
_, isRouted = e.domains[domain]
|
|
|
|
if isRouted {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// match wildcard domains
|
|
|
|
for _, wc := range e.wildcards {
|
|
|
|
if dnsname.HasSuffix(domain, wc) {
|
|
|
|
e.domains[domain] = nil
|
|
|
|
isRouted = true
|
|
|
|
break
|
2024-01-17 19:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
|
|
|
|
next, ok := cnameChain[domain]
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
domain = next
|
|
|
|
}
|
|
|
|
return domain, isRouted
|
|
|
|
}
|
|
|
|
|
|
|
|
// isAddrKnownLocked returns true if the address is known to be associated with
|
|
|
|
// the given domain. Known domain tables are updated for covered routes to speed
|
|
|
|
// up future matches.
|
|
|
|
// e.mu must be held.
|
|
|
|
func (e *AppConnector) isAddrKnownLocked(domain string, addr netip.Addr) bool {
|
2024-02-06 23:07:56 +00:00
|
|
|
if e.hasDomainAddrLocked(domain, addr) {
|
2024-01-31 22:33:14 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, route := range e.controlRoutes {
|
|
|
|
if route.Contains(addr) {
|
|
|
|
// record the new address associated with the domain for faster matching in subsequent
|
|
|
|
// requests and for diagnostic records.
|
2024-02-06 23:07:56 +00:00
|
|
|
e.addDomainAddrLocked(domain, addr)
|
2024-01-31 22:33:14 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// scheduleAdvertisement schedules an advertisement of the given address
|
|
|
|
// associated with the given domain.
|
2024-02-06 23:07:56 +00:00
|
|
|
func (e *AppConnector) scheduleAdvertisement(domain string, routes ...netip.Prefix) {
|
2024-01-31 22:33:14 +00:00
|
|
|
e.queue.Add(func() {
|
2024-02-06 23:07:56 +00:00
|
|
|
if err := e.routeAdvertiser.AdvertiseRoute(routes...); err != nil {
|
|
|
|
e.logf("failed to advertise routes for %s: %v: %v", domain, routes, err)
|
2024-01-31 22:33:14 +00:00
|
|
|
return
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
|
|
|
e.mu.Lock()
|
2024-01-31 22:33:14 +00:00
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
2024-02-06 23:07:56 +00:00
|
|
|
for _, route := range routes {
|
|
|
|
if !route.IsSingleIP() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := route.Addr()
|
|
|
|
if !e.hasDomainAddrLocked(domain, addr) {
|
|
|
|
e.addDomainAddrLocked(domain, addr)
|
|
|
|
e.logf("[v2] advertised route for %v: %v", domain, addr)
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
}
|
2024-04-12 20:34:14 +00:00
|
|
|
if err := e.storeRoutesLocked(); err != nil {
|
|
|
|
e.logf("failed to store route info: %v", err)
|
|
|
|
}
|
2024-01-31 22:33:14 +00:00
|
|
|
})
|
2023-10-27 21:20:10 +00:00
|
|
|
}
|
2024-02-06 23:07:56 +00:00
|
|
|
|
|
|
|
// hasDomainAddrLocked returns true if the address has been observed in a
|
|
|
|
// resolution of domain.
|
|
|
|
func (e *AppConnector) hasDomainAddrLocked(domain string, addr netip.Addr) bool {
|
|
|
|
_, ok := slices.BinarySearchFunc(e.domains[domain], addr, compareAddr)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// addDomainAddrLocked adds the address to the list of addresses resolved for
|
|
|
|
// domain and ensures the list remains sorted. Does not attempt to deduplicate.
|
|
|
|
func (e *AppConnector) addDomainAddrLocked(domain string, addr netip.Addr) {
|
|
|
|
e.domains[domain] = append(e.domains[domain], addr)
|
|
|
|
slices.SortFunc(e.domains[domain], compareAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareAddr(l, r netip.Addr) int {
|
|
|
|
return l.Compare(r)
|
|
|
|
}
|
2024-04-15 17:16:02 +00:00
|
|
|
|
|
|
|
// routesWithout returns a without b where a and b
|
|
|
|
// are unsorted slices of netip.Prefix
|
|
|
|
func routesWithout(a, b []netip.Prefix) []netip.Prefix {
|
|
|
|
m := make(map[netip.Prefix]bool, len(b))
|
|
|
|
for _, p := range b {
|
|
|
|
m[p] = true
|
|
|
|
}
|
|
|
|
return slicesx.Filter(make([]netip.Prefix, 0, len(a)), a, func(p netip.Prefix) bool {
|
|
|
|
return !m[p]
|
|
|
|
})
|
|
|
|
}
|