2022-11-03 18:39:37 +01:00
|
|
|
package dns
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
2024-05-07 12:28:30 +02:00
|
|
|
"runtime"
|
2024-03-12 19:06:16 +01:00
|
|
|
"strings"
|
2023-06-12 14:43:55 +02:00
|
|
|
"sync"
|
2024-10-24 10:53:46 +02:00
|
|
|
"time"
|
2023-06-12 14:43:55 +02:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/mitchellh/hashstructure/v2"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-11-02 19:04:33 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/listener"
|
2024-03-12 19:06:16 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2024-10-24 10:53:46 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
2023-06-12 14:43:55 +02:00
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
|
|
|
)
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
// ReadyListener is a notification mechanism what indicate the server is ready to handle host dns address changes
|
|
|
|
type ReadyListener interface {
|
|
|
|
OnReady()
|
|
|
|
}
|
2022-11-03 18:39:37 +01:00
|
|
|
|
2023-12-18 11:46:58 +01:00
|
|
|
// IosDnsManager is a dns manager interface for iOS
|
|
|
|
type IosDnsManager interface {
|
|
|
|
ApplyDns(string)
|
|
|
|
}
|
|
|
|
|
2022-11-07 15:38:21 +01:00
|
|
|
// Server is a dns server interface
|
|
|
|
type Server interface {
|
2023-06-15 12:25:18 +02:00
|
|
|
Initialize() error
|
2022-11-07 15:38:21 +01:00
|
|
|
Stop()
|
2023-06-12 14:43:55 +02:00
|
|
|
DnsIP() string
|
2022-11-07 15:38:21 +01:00
|
|
|
UpdateDNSServer(serial uint64, update nbdns.Config) error
|
2023-07-14 21:56:22 +02:00
|
|
|
OnUpdatedHostDNSServer(strings []string)
|
2023-11-02 19:04:33 +01:00
|
|
|
SearchDomains() []string
|
2024-01-23 17:23:12 +01:00
|
|
|
ProbeAvailability()
|
2022-11-07 15:38:21 +01:00
|
|
|
}
|
2023-06-12 14:43:55 +02:00
|
|
|
|
|
|
|
type registeredHandlerMap map[string]handlerWithStop
|
|
|
|
|
|
|
|
// DefaultServer dns server object
|
|
|
|
type DefaultServer struct {
|
|
|
|
ctx context.Context
|
|
|
|
ctxCancel context.CancelFunc
|
|
|
|
mux sync.Mutex
|
2023-07-14 21:56:22 +02:00
|
|
|
service service
|
2023-06-12 14:43:55 +02:00
|
|
|
dnsMuxMap registeredHandlerMap
|
|
|
|
localResolver *localResolver
|
2023-07-14 21:56:22 +02:00
|
|
|
wgInterface WGIface
|
2023-06-12 14:43:55 +02:00
|
|
|
hostManager hostManager
|
|
|
|
updateSerial uint64
|
|
|
|
previousConfigHash uint64
|
2023-12-18 11:46:58 +01:00
|
|
|
currentConfig HostDNSConfig
|
2023-07-14 21:56:22 +02:00
|
|
|
|
|
|
|
// permanent related properties
|
2024-05-07 12:28:30 +02:00
|
|
|
permanent bool
|
|
|
|
hostsDNSHolder *hostsDNSHolder
|
2023-11-02 19:04:33 +01:00
|
|
|
|
|
|
|
// make sense on mobile only
|
|
|
|
searchDomainNotifier *notifier
|
2023-12-18 11:46:58 +01:00
|
|
|
iosDnsManager IosDnsManager
|
2024-03-12 19:06:16 +01:00
|
|
|
|
|
|
|
statusRecorder *peer.Status
|
2024-10-24 10:53:46 +02:00
|
|
|
stateManager *statemanager.Manager
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type handlerWithStop interface {
|
|
|
|
dns.Handler
|
|
|
|
stop()
|
2024-01-23 17:23:12 +01:00
|
|
|
probeAvailability()
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type muxUpdate struct {
|
|
|
|
domain string
|
|
|
|
handler handlerWithStop
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultServer returns a new dns server
|
2024-10-24 10:53:46 +02:00
|
|
|
func NewDefaultServer(ctx context.Context, wgInterface WGIface, customAddress string, statusRecorder *peer.Status, stateManager *statemanager.Manager) (*DefaultServer, error) {
|
2023-06-12 14:43:55 +02:00
|
|
|
var addrPort *netip.AddrPort
|
|
|
|
if customAddress != "" {
|
|
|
|
parsedAddrPort, err := netip.ParseAddrPort(customAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse the custom dns address, got error: %s", err)
|
|
|
|
}
|
|
|
|
addrPort = &parsedAddrPort
|
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
var dnsService service
|
|
|
|
if wgInterface.IsUserspaceBind() {
|
2024-08-02 18:43:00 +02:00
|
|
|
dnsService = NewServiceViaMemory(wgInterface)
|
2023-07-14 21:56:22 +02:00
|
|
|
} else {
|
|
|
|
dnsService = newServiceViaListener(wgInterface, addrPort)
|
|
|
|
}
|
2023-06-12 14:43:55 +02:00
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
return newDefaultServer(ctx, wgInterface, dnsService, statusRecorder, stateManager), nil
|
2023-07-14 21:56:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultServerPermanentUpstream returns a new dns server. It optimized for mobile systems
|
2024-03-12 19:06:16 +01:00
|
|
|
func NewDefaultServerPermanentUpstream(
|
|
|
|
ctx context.Context,
|
|
|
|
wgInterface WGIface,
|
|
|
|
hostsDnsList []string,
|
|
|
|
config nbdns.Config,
|
|
|
|
listener listener.NetworkChangeListener,
|
|
|
|
statusRecorder *peer.Status,
|
|
|
|
) *DefaultServer {
|
2023-07-14 21:56:22 +02:00
|
|
|
log.Debugf("host dns address list is: %v", hostsDnsList)
|
2024-10-24 10:53:46 +02:00
|
|
|
ds := newDefaultServer(ctx, wgInterface, NewServiceViaMemory(wgInterface), statusRecorder, nil)
|
2024-05-07 12:28:30 +02:00
|
|
|
ds.hostsDNSHolder.set(hostsDnsList)
|
2023-07-14 21:56:22 +02:00
|
|
|
ds.permanent = true
|
|
|
|
ds.addHostRootZone()
|
2023-11-02 19:04:33 +01:00
|
|
|
ds.currentConfig = dnsConfigToHostDNSConfig(config, ds.service.RuntimeIP(), ds.service.RuntimePort())
|
|
|
|
ds.searchDomainNotifier = newNotifier(ds.SearchDomains())
|
|
|
|
ds.searchDomainNotifier.setListener(listener)
|
2023-07-14 21:56:22 +02:00
|
|
|
setServerDns(ds)
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
2023-12-18 11:46:58 +01:00
|
|
|
// NewDefaultServerIos returns a new dns server. It optimized for ios
|
2024-03-12 19:06:16 +01:00
|
|
|
func NewDefaultServerIos(
|
|
|
|
ctx context.Context,
|
|
|
|
wgInterface WGIface,
|
|
|
|
iosDnsManager IosDnsManager,
|
|
|
|
statusRecorder *peer.Status,
|
|
|
|
) *DefaultServer {
|
2024-10-24 10:53:46 +02:00
|
|
|
ds := newDefaultServer(ctx, wgInterface, NewServiceViaMemory(wgInterface), statusRecorder, nil)
|
2023-12-18 11:46:58 +01:00
|
|
|
ds.iosDnsManager = iosDnsManager
|
|
|
|
return ds
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
func newDefaultServer(ctx context.Context, wgInterface WGIface, dnsService service, statusRecorder *peer.Status, stateManager *statemanager.Manager) *DefaultServer {
|
2023-07-14 21:56:22 +02:00
|
|
|
ctx, stop := context.WithCancel(ctx)
|
2023-06-12 14:43:55 +02:00
|
|
|
defaultServer := &DefaultServer{
|
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: stop,
|
2023-07-14 21:56:22 +02:00
|
|
|
service: dnsService,
|
2023-06-12 14:43:55 +02:00
|
|
|
dnsMuxMap: make(registeredHandlerMap),
|
|
|
|
localResolver: &localResolver{
|
|
|
|
registeredMap: make(registrationMap),
|
|
|
|
},
|
2024-03-12 19:06:16 +01:00
|
|
|
wgInterface: wgInterface,
|
|
|
|
statusRecorder: statusRecorder,
|
2024-10-24 10:53:46 +02:00
|
|
|
stateManager: stateManager,
|
2024-05-07 12:28:30 +02:00
|
|
|
hostsDNSHolder: newHostsDNSHolder(),
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
return defaultServer
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
// Initialize instantiate host manager and the dns service
|
2023-06-15 12:25:18 +02:00
|
|
|
func (s *DefaultServer) Initialize() (err error) {
|
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
|
|
|
|
if s.hostManager != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
if s.permanent {
|
|
|
|
err = s.service.Listen()
|
|
|
|
if err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
return fmt.Errorf("service listen: %w", err)
|
2023-07-14 21:56:22 +02:00
|
|
|
}
|
2023-06-22 14:59:21 +02:00
|
|
|
}
|
2023-07-14 21:56:22 +02:00
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
s.stateManager.RegisterState(&ShutdownState{})
|
2023-12-18 11:46:58 +01:00
|
|
|
s.hostManager, err = s.initialize()
|
2024-01-30 09:58:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initialize: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2023-06-15 12:25:18 +02:00
|
|
|
}
|
|
|
|
|
2023-06-22 14:59:21 +02:00
|
|
|
// DnsIP returns the DNS resolver server IP address
|
|
|
|
//
|
|
|
|
// When kernel space interface used it return real DNS server listener IP address
|
|
|
|
// For bind interface, fake DNS resolver address returned (second last IP address from Nebird network)
|
2023-06-12 14:43:55 +02:00
|
|
|
func (s *DefaultServer) DnsIP() string {
|
2023-07-14 21:56:22 +02:00
|
|
|
return s.service.RuntimeIP()
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the server
|
|
|
|
func (s *DefaultServer) Stop() {
|
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
s.ctxCancel()
|
|
|
|
|
2023-06-20 20:33:26 +02:00
|
|
|
if s.hostManager != nil {
|
2024-10-24 10:53:46 +02:00
|
|
|
if err := s.hostManager.restoreHostDNS(); err != nil {
|
|
|
|
log.Error("failed to restore host DNS settings: ", err)
|
|
|
|
} else if err := s.stateManager.DeleteState(&ShutdownState{}); err != nil {
|
|
|
|
log.Errorf("failed to delete shutdown dns state: %v", err)
|
2023-06-20 20:33:26 +02:00
|
|
|
}
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
s.service.Stop()
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
// OnUpdatedHostDNSServer update the DNS servers addresses for root zones
|
|
|
|
// It will be applied if the mgm server do not enforce DNS settings for root zone
|
|
|
|
func (s *DefaultServer) OnUpdatedHostDNSServer(hostsDnsList []string) {
|
2024-05-07 12:28:30 +02:00
|
|
|
s.hostsDNSHolder.set(hostsDnsList)
|
2023-06-12 14:43:55 +02:00
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
_, ok := s.dnsMuxMap[nbdns.RootZone]
|
|
|
|
if ok {
|
|
|
|
log.Debugf("on new host DNS config but skip to apply it")
|
|
|
|
return
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
2023-07-14 21:56:22 +02:00
|
|
|
log.Debugf("update host DNS settings: %+v", hostsDnsList)
|
|
|
|
s.addHostRootZone()
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDNSServer processes an update received from the management service
|
|
|
|
func (s *DefaultServer) UpdateDNSServer(serial uint64, update nbdns.Config) error {
|
|
|
|
select {
|
|
|
|
case <-s.ctx.Done():
|
|
|
|
log.Infof("not updating DNS server as context is closed")
|
|
|
|
return s.ctx.Err()
|
|
|
|
default:
|
|
|
|
if serial < s.updateSerial {
|
|
|
|
return fmt.Errorf("not applying dns update, error: "+
|
|
|
|
"network update is %d behind the last applied update", s.updateSerial-serial)
|
|
|
|
}
|
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
|
2023-06-15 12:25:18 +02:00
|
|
|
if s.hostManager == nil {
|
|
|
|
return fmt.Errorf("dns service is not initialized yet")
|
|
|
|
}
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
hash, err := hashstructure.Hash(update, hashstructure.FormatV2, &hashstructure.HashOptions{
|
|
|
|
ZeroNil: true,
|
|
|
|
IgnoreZeroValue: true,
|
|
|
|
SlicesAsSets: true,
|
|
|
|
UseStringer: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to hash the dns configuration update, got error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.previousConfigHash == hash {
|
|
|
|
log.Debugf("not applying the dns configuration update as there is nothing new")
|
|
|
|
s.updateSerial = serial
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.applyConfiguration(update); err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
return fmt.Errorf("apply configuration: %w", err)
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s.updateSerial = serial
|
|
|
|
s.previousConfigHash = hash
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:04:33 +01:00
|
|
|
func (s *DefaultServer) SearchDomains() []string {
|
|
|
|
var searchDomains []string
|
|
|
|
|
2023-12-18 11:46:58 +01:00
|
|
|
for _, dConf := range s.currentConfig.Domains {
|
|
|
|
if dConf.Disabled {
|
2023-11-02 19:04:33 +01:00
|
|
|
continue
|
|
|
|
}
|
2023-12-18 11:46:58 +01:00
|
|
|
if dConf.MatchOnly {
|
2023-11-02 19:04:33 +01:00
|
|
|
continue
|
|
|
|
}
|
2023-12-18 11:46:58 +01:00
|
|
|
searchDomains = append(searchDomains, dConf.Domain)
|
2023-11-02 19:04:33 +01:00
|
|
|
}
|
|
|
|
return searchDomains
|
|
|
|
}
|
|
|
|
|
2024-01-23 17:23:12 +01:00
|
|
|
// ProbeAvailability tests each upstream group's servers for availability
|
|
|
|
// and deactivates the group if no server responds
|
|
|
|
func (s *DefaultServer) ProbeAvailability() {
|
2024-03-15 18:57:18 +01:00
|
|
|
var wg sync.WaitGroup
|
2024-01-23 17:23:12 +01:00
|
|
|
for _, mux := range s.dnsMuxMap {
|
2024-03-15 18:57:18 +01:00
|
|
|
wg.Add(1)
|
|
|
|
go func(mux handlerWithStop) {
|
|
|
|
defer wg.Done()
|
|
|
|
mux.probeAvailability()
|
|
|
|
}(mux)
|
2024-01-23 17:23:12 +01:00
|
|
|
}
|
2024-03-15 18:57:18 +01:00
|
|
|
wg.Wait()
|
2024-01-23 17:23:12 +01:00
|
|
|
}
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
func (s *DefaultServer) applyConfiguration(update nbdns.Config) error {
|
2023-12-18 11:46:58 +01:00
|
|
|
// is the service should be Disabled, we stop the listener or fake resolver
|
2023-06-12 14:43:55 +02:00
|
|
|
// and proceed with a regular update to clean up the handlers and records
|
2023-07-14 21:56:22 +02:00
|
|
|
if update.ServiceEnable {
|
|
|
|
_ = s.service.Listen()
|
|
|
|
} else if !s.permanent {
|
|
|
|
s.service.Stop()
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
localMuxUpdates, localRecords, err := s.buildLocalHandlerUpdate(update.CustomZones)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("not applying dns update, error: %v", err)
|
|
|
|
}
|
|
|
|
upstreamMuxUpdates, err := s.buildUpstreamHandlerUpdate(update.NameServerGroups)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("not applying dns update, error: %v", err)
|
|
|
|
}
|
2023-11-27 16:40:02 +01:00
|
|
|
muxUpdates := append(localMuxUpdates, upstreamMuxUpdates...) //nolint:gocritic
|
2023-06-12 14:43:55 +02:00
|
|
|
|
|
|
|
s.updateMux(muxUpdates)
|
|
|
|
s.updateLocalResolver(localRecords)
|
2023-07-14 21:56:22 +02:00
|
|
|
s.currentConfig = dnsConfigToHostDNSConfig(update, s.service.RuntimeIP(), s.service.RuntimePort())
|
2023-06-12 14:43:55 +02:00
|
|
|
|
|
|
|
hostUpdate := s.currentConfig
|
2023-07-14 21:56:22 +02:00
|
|
|
if s.service.RuntimePort() != defaultPort && !s.hostManager.supportCustomPort() {
|
2023-06-12 14:43:55 +02:00
|
|
|
log.Warnf("the DNS manager of this peer doesn't support custom port. Disabling primary DNS setup. " +
|
2023-08-17 12:27:04 +02:00
|
|
|
"Learn more at: https://docs.netbird.io/how-to/manage-dns-in-your-network#local-resolver")
|
2023-12-18 11:46:58 +01:00
|
|
|
hostUpdate.RouteAll = false
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
if err = s.hostManager.applyDNSConfig(hostUpdate, s.stateManager); err != nil {
|
2023-06-12 14:43:55 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
// persist dns state right away
|
|
|
|
ctx, cancel := context.WithTimeout(s.ctx, 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := s.stateManager.PersistState(ctx); err != nil {
|
|
|
|
log.Errorf("Failed to persist dns state: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-11-02 19:04:33 +01:00
|
|
|
if s.searchDomainNotifier != nil {
|
|
|
|
s.searchDomainNotifier.onNewSearchDomains(s.SearchDomains())
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:06:16 +01:00
|
|
|
s.updateNSGroupStates(update.NameServerGroups)
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultServer) buildLocalHandlerUpdate(customZones []nbdns.CustomZone) ([]muxUpdate, map[string]nbdns.SimpleRecord, error) {
|
|
|
|
var muxUpdates []muxUpdate
|
|
|
|
localRecords := make(map[string]nbdns.SimpleRecord, 0)
|
|
|
|
|
|
|
|
for _, customZone := range customZones {
|
|
|
|
|
|
|
|
if len(customZone.Records) == 0 {
|
|
|
|
return nil, nil, fmt.Errorf("received an empty list of records")
|
|
|
|
}
|
|
|
|
|
|
|
|
muxUpdates = append(muxUpdates, muxUpdate{
|
|
|
|
domain: customZone.Domain,
|
|
|
|
handler: s.localResolver,
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, record := range customZone.Records {
|
|
|
|
var class uint16 = dns.ClassINET
|
|
|
|
if record.Class != nbdns.DefaultClass {
|
|
|
|
return nil, nil, fmt.Errorf("received an invalid class type: %s", record.Class)
|
|
|
|
}
|
|
|
|
key := buildRecordKey(record.Name, class, uint16(record.Type))
|
|
|
|
localRecords[key] = record
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return muxUpdates, localRecords, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultServer) buildUpstreamHandlerUpdate(nameServerGroups []*nbdns.NameServerGroup) ([]muxUpdate, error) {
|
|
|
|
|
|
|
|
var muxUpdates []muxUpdate
|
|
|
|
for _, nsGroup := range nameServerGroups {
|
|
|
|
if len(nsGroup.NameServers) == 0 {
|
|
|
|
log.Warn("received a nameserver group with empty nameserver list")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:06:16 +01:00
|
|
|
handler, err := newUpstreamResolver(
|
|
|
|
s.ctx,
|
|
|
|
s.wgInterface.Name(),
|
|
|
|
s.wgInterface.Address().IP,
|
|
|
|
s.wgInterface.Address().Network,
|
|
|
|
s.statusRecorder,
|
2024-05-07 12:28:30 +02:00
|
|
|
s.hostsDNSHolder,
|
2024-03-12 19:06:16 +01:00
|
|
|
)
|
2023-12-18 11:46:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create a new upstream resolver, error: %v", err)
|
|
|
|
}
|
2023-06-12 14:43:55 +02:00
|
|
|
for _, ns := range nsGroup.NameServers {
|
|
|
|
if ns.NSType != nbdns.UDPNameServerType {
|
2023-11-01 17:11:16 +01:00
|
|
|
log.Warnf("skipping nameserver %s with type %s, this peer supports only %s",
|
2023-06-12 14:43:55 +02:00
|
|
|
ns.IP.String(), ns.NSType.String(), nbdns.UDPNameServerType.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
handler.upstreamServers = append(handler.upstreamServers, getNSHostPort(ns))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(handler.upstreamServers) == 0 {
|
|
|
|
handler.stop()
|
|
|
|
log.Errorf("received a nameserver group with an invalid nameserver list")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// when upstream fails to resolve domain several times over all it servers
|
|
|
|
// it will calls this hook to exclude self from the configuration and
|
|
|
|
// reapply DNS settings, but it not touch the original configuration and serial number
|
|
|
|
// because it is temporal deactivation until next try
|
|
|
|
//
|
2023-11-01 17:11:16 +01:00
|
|
|
// after some period defined by upstream it tries to reactivate self by calling this hook
|
2023-06-12 14:43:55 +02:00
|
|
|
// everything we need here is just to re-apply current configuration because it already
|
|
|
|
// contains this upstream settings (temporal deactivation not removed it)
|
|
|
|
handler.deactivate, handler.reactivate = s.upstreamCallbacks(nsGroup, handler)
|
|
|
|
|
|
|
|
if nsGroup.Primary {
|
|
|
|
muxUpdates = append(muxUpdates, muxUpdate{
|
|
|
|
domain: nbdns.RootZone,
|
|
|
|
handler: handler,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nsGroup.Domains) == 0 {
|
|
|
|
handler.stop()
|
|
|
|
return nil, fmt.Errorf("received a non primary nameserver group with an empty domain list")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range nsGroup.Domains {
|
|
|
|
if domain == "" {
|
|
|
|
handler.stop()
|
|
|
|
return nil, fmt.Errorf("received a nameserver group with an empty domain element")
|
|
|
|
}
|
|
|
|
muxUpdates = append(muxUpdates, muxUpdate{
|
|
|
|
domain: domain,
|
|
|
|
handler: handler,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 17:23:12 +01:00
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
return muxUpdates, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultServer) updateMux(muxUpdates []muxUpdate) {
|
|
|
|
muxUpdateMap := make(registeredHandlerMap)
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
var isContainRootUpdate bool
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
for _, update := range muxUpdates {
|
2023-07-14 21:56:22 +02:00
|
|
|
s.service.RegisterMux(update.domain, update.handler)
|
2023-06-12 14:43:55 +02:00
|
|
|
muxUpdateMap[update.domain] = update.handler
|
|
|
|
if existingHandler, ok := s.dnsMuxMap[update.domain]; ok {
|
|
|
|
existingHandler.stop()
|
|
|
|
}
|
2023-07-14 21:56:22 +02:00
|
|
|
|
|
|
|
if update.domain == nbdns.RootZone {
|
|
|
|
isContainRootUpdate = true
|
|
|
|
}
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, existingHandler := range s.dnsMuxMap {
|
|
|
|
_, found := muxUpdateMap[key]
|
|
|
|
if !found {
|
2023-07-14 21:56:22 +02:00
|
|
|
if !isContainRootUpdate && key == nbdns.RootZone {
|
|
|
|
s.addHostRootZone()
|
|
|
|
existingHandler.stop()
|
|
|
|
} else {
|
|
|
|
existingHandler.stop()
|
|
|
|
s.service.DeregisterMux(key)
|
|
|
|
}
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.dnsMuxMap = muxUpdateMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultServer) updateLocalResolver(update map[string]nbdns.SimpleRecord) {
|
|
|
|
for key := range s.localResolver.registeredMap {
|
|
|
|
_, found := update[key]
|
|
|
|
if !found {
|
|
|
|
s.localResolver.deleteRecord(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedMap := make(registrationMap)
|
|
|
|
for key, record := range update {
|
|
|
|
err := s.localResolver.registerRecord(record)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("got an error while registering the record (%s), error: %v", record.String(), err)
|
|
|
|
}
|
|
|
|
updatedMap[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.localResolver.registeredMap = updatedMap
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNSHostPort(ns nbdns.NameServer) string {
|
|
|
|
return fmt.Sprintf("%s:%d", ns.IP.String(), ns.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// upstreamCallbacks returns two functions, the first one is used to deactivate
|
|
|
|
// the upstream resolver from the configuration, the second one is used to
|
|
|
|
// reactivate it. Not allowed to call reactivate before deactivate.
|
|
|
|
func (s *DefaultServer) upstreamCallbacks(
|
|
|
|
nsGroup *nbdns.NameServerGroup,
|
|
|
|
handler dns.Handler,
|
2024-03-12 19:06:16 +01:00
|
|
|
) (deactivate func(error), reactivate func()) {
|
2023-06-12 14:43:55 +02:00
|
|
|
var removeIndex map[string]int
|
2024-03-12 19:06:16 +01:00
|
|
|
deactivate = func(err error) {
|
2023-06-12 14:43:55 +02:00
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
|
|
|
|
l := log.WithField("nameservers", nsGroup.NameServers)
|
2024-03-12 19:06:16 +01:00
|
|
|
l.Info("Temporarily deactivating nameservers group due to timeout")
|
2023-06-12 14:43:55 +02:00
|
|
|
|
|
|
|
removeIndex = make(map[string]int)
|
|
|
|
for _, domain := range nsGroup.Domains {
|
|
|
|
removeIndex[domain] = -1
|
|
|
|
}
|
|
|
|
if nsGroup.Primary {
|
|
|
|
removeIndex[nbdns.RootZone] = -1
|
2023-12-18 11:46:58 +01:00
|
|
|
s.currentConfig.RouteAll = false
|
2024-05-07 12:28:30 +02:00
|
|
|
s.service.DeregisterMux(nbdns.RootZone)
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
2023-12-18 11:46:58 +01:00
|
|
|
for i, item := range s.currentConfig.Domains {
|
|
|
|
if _, found := removeIndex[item.Domain]; found {
|
|
|
|
s.currentConfig.Domains[i].Disabled = true
|
|
|
|
s.service.DeregisterMux(item.Domain)
|
|
|
|
removeIndex[item.Domain] = i
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-07 12:28:30 +02:00
|
|
|
|
2024-10-24 10:53:46 +02:00
|
|
|
if err := s.hostManager.applyDNSConfig(s.currentConfig, s.stateManager); err != nil {
|
2024-03-12 19:06:16 +01:00
|
|
|
l.Errorf("Failed to apply nameserver deactivation on the host: %v", err)
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
2024-03-12 19:06:16 +01:00
|
|
|
|
2024-05-07 12:28:30 +02:00
|
|
|
if runtime.GOOS == "android" && nsGroup.Primary && len(s.hostsDNSHolder.get()) > 0 {
|
|
|
|
s.addHostRootZone()
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:06:16 +01:00
|
|
|
s.updateNSState(nsGroup, err, false)
|
|
|
|
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
reactivate = func() {
|
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
|
|
|
|
for domain, i := range removeIndex {
|
2023-12-18 11:46:58 +01:00
|
|
|
if i == -1 || i >= len(s.currentConfig.Domains) || s.currentConfig.Domains[i].Domain != domain {
|
2023-06-12 14:43:55 +02:00
|
|
|
continue
|
|
|
|
}
|
2023-12-18 11:46:58 +01:00
|
|
|
s.currentConfig.Domains[i].Disabled = false
|
2023-07-14 21:56:22 +02:00
|
|
|
s.service.RegisterMux(domain, handler)
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
l := log.WithField("nameservers", nsGroup.NameServers)
|
2024-01-23 17:23:12 +01:00
|
|
|
l.Debug("reactivate temporary disabled nameserver group")
|
2023-06-12 14:43:55 +02:00
|
|
|
|
|
|
|
if nsGroup.Primary {
|
2023-12-18 11:46:58 +01:00
|
|
|
s.currentConfig.RouteAll = true
|
2024-05-13 18:11:08 +02:00
|
|
|
s.service.RegisterMux(nbdns.RootZone, handler)
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
2024-10-24 10:53:46 +02:00
|
|
|
if err := s.hostManager.applyDNSConfig(s.currentConfig, s.stateManager); err != nil {
|
2024-01-23 17:23:12 +01:00
|
|
|
l.WithError(err).Error("reactivate temporary disabled nameserver group, DNS update apply")
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
2024-03-12 19:06:16 +01:00
|
|
|
|
|
|
|
s.updateNSState(nsGroup, nil, true)
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-14 21:56:22 +02:00
|
|
|
func (s *DefaultServer) addHostRootZone() {
|
2024-03-12 19:06:16 +01:00
|
|
|
handler, err := newUpstreamResolver(
|
|
|
|
s.ctx,
|
|
|
|
s.wgInterface.Name(),
|
|
|
|
s.wgInterface.Address().IP,
|
|
|
|
s.wgInterface.Address().Network,
|
|
|
|
s.statusRecorder,
|
2024-05-07 12:28:30 +02:00
|
|
|
s.hostsDNSHolder,
|
2024-03-12 19:06:16 +01:00
|
|
|
)
|
2023-12-18 11:46:58 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to create a new upstream resolver, error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-12-01 14:26:42 +01:00
|
|
|
|
2024-05-07 12:28:30 +02:00
|
|
|
handler.upstreamServers = make([]string, 0)
|
|
|
|
for k := range s.hostsDNSHolder.get() {
|
|
|
|
handler.upstreamServers = append(handler.upstreamServers, k)
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
2024-03-12 19:06:16 +01:00
|
|
|
handler.deactivate = func(error) {}
|
2023-07-14 21:56:22 +02:00
|
|
|
handler.reactivate = func() {}
|
|
|
|
s.service.RegisterMux(nbdns.RootZone, handler)
|
2023-06-12 14:43:55 +02:00
|
|
|
}
|
2024-03-12 19:06:16 +01:00
|
|
|
|
|
|
|
func (s *DefaultServer) updateNSGroupStates(groups []*nbdns.NameServerGroup) {
|
|
|
|
var states []peer.NSGroupState
|
|
|
|
|
|
|
|
for _, group := range groups {
|
|
|
|
var servers []string
|
|
|
|
for _, ns := range group.NameServers {
|
|
|
|
servers = append(servers, fmt.Sprintf("%s:%d", ns.IP, ns.Port))
|
|
|
|
}
|
|
|
|
|
|
|
|
state := peer.NSGroupState{
|
|
|
|
ID: generateGroupKey(group),
|
|
|
|
Servers: servers,
|
|
|
|
Domains: group.Domains,
|
|
|
|
// The probe will determine the state, default enabled
|
|
|
|
Enabled: true,
|
|
|
|
Error: nil,
|
|
|
|
}
|
|
|
|
states = append(states, state)
|
|
|
|
}
|
|
|
|
s.statusRecorder.UpdateDNSStates(states)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultServer) updateNSState(nsGroup *nbdns.NameServerGroup, err error, enabled bool) {
|
|
|
|
states := s.statusRecorder.GetDNSStates()
|
|
|
|
id := generateGroupKey(nsGroup)
|
|
|
|
for i, state := range states {
|
|
|
|
if state.ID == id {
|
|
|
|
states[i].Enabled = enabled
|
|
|
|
states[i].Error = err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.statusRecorder.UpdateDNSStates(states)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateGroupKey(nsGroup *nbdns.NameServerGroup) string {
|
|
|
|
var servers []string
|
|
|
|
for _, ns := range nsGroup.NameServers {
|
|
|
|
servers = append(servers, fmt.Sprintf("%s:%d", ns.IP, ns.Port))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s_%s_%s", nsGroup.ID, nsGroup.Name, strings.Join(servers, ","))
|
|
|
|
}
|