Skip to content

Apply host DNS settings on peer state change #2291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion client/internal/dns/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ func dnsConfigToHostDNSConfig(dnsConfig nbdns.Config, ip string, port int) HostD
if len(nsConfig.NameServers) == 0 {
continue
}
if nsConfig.Primary {
if nsConfig.Primary && nsConfig.Enabled {
config.RouteAll = true
}

for _, domain := range nsConfig.Domains {
config.Domains = append(config.Domains, DomainConfig{
Domain: strings.TrimSuffix(domain, "."),
Disabled: !nsConfig.Enabled,
MatchOnly: !nsConfig.SearchDomainsEnabled,
})
}
Expand Down
54 changes: 37 additions & 17 deletions client/internal/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/netip"
"runtime"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -297,6 +298,11 @@ func (s *DefaultServer) applyConfiguration(update nbdns.Config) error {
s.service.Stop()
}

// update each NameServerGroup config in nbdns.Config base on peerCount and IP.IsPrivate()
if runtime.GOOS != "android" && runtime.GOOS != "ios" {
s.toggleNameServerGroupsOnStatus(update.NameServerGroups)
}

localMuxUpdates, localRecords, err := s.buildLocalHandlerUpdate(update.CustomZones)
if err != nil {
return fmt.Errorf("not applying dns update, error: %v", err)
Expand Down Expand Up @@ -410,7 +416,7 @@ func (s *DefaultServer) buildUpstreamHandlerUpdate(nameServerGroups []*nbdns.Nam
// contains this upstream settings (temporal deactivation not removed it)
handler.deactivate, handler.reactivate = s.upstreamCallbacks(nsGroup, handler)

if nsGroup.Primary {
if nsGroup.Primary && nsGroup.Enabled {
muxUpdates = append(muxUpdates, muxUpdate{
domain: nbdns.RootZone,
handler: handler,
Expand All @@ -428,6 +434,10 @@ func (s *DefaultServer) buildUpstreamHandlerUpdate(nameServerGroups []*nbdns.Nam
handler.stop()
return nil, fmt.Errorf("received a nameserver group with an empty domain element")
}
if !nsGroup.Enabled {
handler.disabled.Store(true)
continue
}
muxUpdates = append(muxUpdates, muxUpdate{
domain: domain,
handler: handler,
Expand Down Expand Up @@ -491,6 +501,21 @@ func (s *DefaultServer) updateLocalResolver(update map[string]nbdns.SimpleRecord
s.localResolver.registeredMap = updatedMap
}

func (s *DefaultServer) toggleNameServerGroupsOnStatus(nameServerGroups []*nbdns.NameServerGroup) {
peerCount := s.statusRecorder.GetConnectedPeersCount()
for _, nsGroup := range nameServerGroups {
var hasPublicNameServer bool
for _, s := range nsGroup.NameServers {
if !s.IP.IsPrivate() {
hasPublicNameServer = true
}
}
isEnable := hasPublicNameServer || (peerCount >= 1)
log.Infof("Name servers: %#v are enabled %t", nsGroup.Name, isEnable)
nsGroup.Enabled = isEnable
}
}

func getNSHostPort(ns nbdns.NameServer) string {
return fmt.Sprintf("%s:%d", ns.IP.String(), ns.Port)
}
Expand All @@ -502,29 +527,22 @@ func (s *DefaultServer) upstreamCallbacks(
nsGroup *nbdns.NameServerGroup,
handler dns.Handler,
) (deactivate func(error), reactivate func()) {
var removeIndex map[string]int
deactivate = func(err error) {
s.mux.Lock()
defer s.mux.Unlock()

l := log.WithField("nameservers", nsGroup.NameServers)
l.Info("Temporarily deactivating nameservers group due to timeout")

removeIndex = make(map[string]int)
for _, domain := range nsGroup.Domains {
removeIndex[domain] = -1
}
if nsGroup.Primary {
removeIndex[nbdns.RootZone] = -1
s.currentConfig.RouteAll = false
s.service.DeregisterMux(nbdns.RootZone)
}

for i, item := range s.currentConfig.Domains {
if _, found := removeIndex[item.Domain]; found {
if slices.Contains(nsGroup.Domains, item.Domain) {
s.currentConfig.Domains[i].Disabled = true
s.service.DeregisterMux(item.Domain)
removeIndex[item.Domain] = i
}
}

Expand All @@ -543,18 +561,16 @@ func (s *DefaultServer) upstreamCallbacks(
}

s.updateNSState(nsGroup, err, false)

}
reactivate = func() {
s.mux.Lock()
defer s.mux.Unlock()

for domain, i := range removeIndex {
if i == -1 || i >= len(s.currentConfig.Domains) || s.currentConfig.Domains[i].Domain != domain {
continue
for i, item := range s.currentConfig.Domains {
if slices.Contains(nsGroup.Domains, item.Domain) {
s.currentConfig.Domains[i].Disabled = false
s.service.RegisterMux(item.Domain, handler)
}
s.currentConfig.Domains[i].Disabled = false
s.service.RegisterMux(domain, handler)
}

l := log.WithField("nameservers", nsGroup.NameServers)
Expand Down Expand Up @@ -601,6 +617,10 @@ func (s *DefaultServer) updateNSGroupStates(groups []*nbdns.NameServerGroup) {

for _, group := range groups {
var servers []string
var nsError error
if !group.Enabled {
nsError = fmt.Errorf("no peers connected")
}
for _, ns := range group.NameServers {
servers = append(servers, fmt.Sprintf("%s:%d", ns.IP, ns.Port))
}
Expand All @@ -610,8 +630,8 @@ func (s *DefaultServer) updateNSGroupStates(groups []*nbdns.NameServerGroup) {
Servers: servers,
Domains: group.Domains,
// The probe will determine the state, default enabled
Enabled: true,
Error: nil,
Enabled: group.Enabled,
Error: nsError,
}
states = append(states, state)
}
Expand Down
23 changes: 22 additions & 1 deletion client/internal/dns/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/netip"
"os"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -129,10 +130,12 @@ func TestUpdateDNSServer(t *testing.T) {
},
NameServerGroups: []*nbdns.NameServerGroup{
{
Enabled: true,
Domains: []string{"netbird.io"},
NameServers: nameServers,
},
{
Enabled: true,
NameServers: nameServers,
Primary: true,
},
Expand All @@ -157,6 +160,7 @@ func TestUpdateDNSServer(t *testing.T) {
},
NameServerGroups: []*nbdns.NameServerGroup{
{
Enabled: true,
Domains: []string{"netbird.io"},
NameServers: nameServers,
},
Expand Down Expand Up @@ -292,7 +296,22 @@ func TestUpdateDNSServer(t *testing.T) {
t.Log(err)
}
}()
dnsServer, err := NewDefaultServer(context.Background(), wgIface, "", &peer.Status{}, nil)
statusRecorder := peer.NewRecorder("https://mgm")
key := "abc"
err = statusRecorder.AddPeer(key, "abc.netbird")
if err != nil {
t.Fatal(err)
}
err = statusRecorder.UpdatePeerState(peer.State{
PubKey: key,
Mux: new(sync.RWMutex),
ConnStatus: peer.StatusConnected,
ConnStatusUpdate: time.Now(),
})
if err != nil {
t.Fatal(err)
}
dnsServer, err := NewDefaultServer(context.Background(), wgIface, "", statusRecorder, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -448,10 +467,12 @@ func TestDNSFakeResolverHandleUpdates(t *testing.T) {
{
Domains: []string{"netbird.io"},
NameServers: nameServers,
Enabled: true,
},
{
NameServers: nameServers,
Primary: true,
Enabled: true,
},
},
}
Expand Down
Loading
Loading